2 * Linux Trace Toolkit Next Generation Kernel State Dump
4 * Copyright 2005 Jean-Hugues Deschenes <jean-hugues.deschenes@polymtl.ca>
5 * Copyright 2006-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Eric Clement: Add listing of network IP interface
9 * 2006, 2007 Mathieu Desnoyers Fix kernel threads
12 * Dual LGPL v2.1/GPL v2 license.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/netlink.h>
18 #include <linux/inet.h>
20 #include <linux/kthread.h>
21 #include <linux/proc_fs.h>
22 #include <linux/file.h>
23 #include <linux/interrupt.h>
24 #include <linux/irqnr.h>
25 #include <linux/cpu.h>
26 #include <linux/netdevice.h>
27 #include <linux/inetdevice.h>
28 #include <linux/sched.h>
30 #include <linux/fdtable.h>
31 #include <linux/swap.h>
32 #include <linux/wait.h>
33 #include <linux/mutex.h>
35 #include "lttng-events.h"
36 #include "wrapper/irqdesc.h"
38 #ifdef CONFIG_GENERIC_HARDIRQS
39 #include <linux/irq.h>
42 /* Define the tracepoints, but do not build the probes */
43 #define CREATE_TRACE_POINTS
44 #define TRACE_INCLUDE_PATH ../instrumentation/events/lttng-module
45 #define TRACE_INCLUDE_FILE lttng-statedump
46 #include "instrumentation/events/lttng-module/lttng-statedump.h"
49 * Protected by the trace lock.
51 static struct delayed_work cpu_work
[NR_CPUS
];
52 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq
);
53 static atomic_t kernel_threads_to_run
;
55 enum lttng_thread_type
{
56 LTTNG_USER_THREAD
= 0,
57 LTTNG_KERNEL_THREAD
= 1,
60 enum lttng_execution_mode
{
66 LTTNG_MODE_UNKNOWN
= 5,
69 enum lttng_execution_submode
{
74 enum lttng_process_status
{
87 void lttng_enumerate_device(struct lttng_session
*session
,
88 struct net_device
*dev
)
90 struct in_device
*in_dev
;
91 struct in_ifaddr
*ifa
;
93 if (dev
->flags
& IFF_UP
) {
94 in_dev
= in_dev_get(dev
);
96 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
97 ifa
= ifa
->ifa_next
) {
98 trace_lttng_statedump_network_interface(
104 trace_lttng_statedump_network_interface(
110 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
112 struct net_device
*dev
;
114 read_lock(&dev_base_lock
);
115 for_each_netdev(&init_net
, dev
)
116 lttng_enumerate_device(session
, dev
);
117 read_unlock(&dev_base_lock
);
121 #else /* CONFIG_INET */
123 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
127 #endif /* CONFIG_INET */
131 void lttng_enumerate_task_fd(struct lttng_session
*session
,
132 struct task_struct
*p
, char *tmp
)
137 const unsigned char *path
;
142 spin_lock(&p
->files
->file_lock
);
143 fdt
= files_fdtable(p
->files
);
144 for (i
= 0; i
< fdt
->max_fds
; i
++) {
145 filp
= fcheck_files(p
->files
, i
);
148 path
= d_path(&filp
->f_path
, tmp
, PAGE_SIZE
);
149 /* Make sure we give at least some info */
150 trace_lttng_statedump_file_descriptor(session
, p
, i
,
152 filp
->f_dentry
->d_name
.name
:
155 spin_unlock(&p
->files
->file_lock
);
161 int lttng_enumerate_file_descriptors(struct lttng_session
*session
)
163 struct task_struct
*p
;
164 char *tmp
= (char *) __get_free_page(GFP_KERNEL
);
166 /* Enumerate active file descriptors */
169 lttng_enumerate_task_fd(session
, p
, tmp
);
171 free_page((unsigned long) tmp
);
176 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
177 struct task_struct
*p
)
179 struct mm_struct
*mm
;
180 struct vm_area_struct
*map
;
183 /* get_task_mm does a task_lock... */
190 down_read(&mm
->mmap_sem
);
193 ino
= map
->vm_file
->f_dentry
->d_inode
->i_ino
;
196 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
199 up_read(&mm
->mmap_sem
);
205 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
207 struct task_struct
*p
;
211 lttng_enumerate_task_vm_maps(session
, p
);
216 #ifdef CONFIG_GENERIC_HARDIRQS
218 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39))
219 #define irq_desc_get_chip(desc) get_irq_desc_chip(desc)
223 void lttng_list_interrupts(struct lttng_session
*session
)
226 unsigned long flags
= 0;
227 struct irq_desc
*desc
;
229 #define irq_to_desc wrapper_irq_to_desc
231 for_each_irq_desc(irq
, desc
) {
232 struct irqaction
*action
;
233 const char *irq_chip_name
=
234 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
236 local_irq_save(flags
);
237 raw_spin_lock(&desc
->lock
);
238 for (action
= desc
->action
; action
; action
= action
->next
) {
239 trace_lttng_statedump_interrupt(session
,
240 irq
, irq_chip_name
, action
);
242 raw_spin_unlock(&desc
->lock
);
243 local_irq_restore(flags
);
249 void list_interrupts(struct lttng_session
*session
)
255 int lttng_enumerate_process_states(struct lttng_session
*session
)
257 struct task_struct
*g
, *p
;
260 for_each_process(g
) {
263 enum lttng_execution_mode mode
=
265 enum lttng_execution_submode submode
=
267 enum lttng_process_status status
;
268 enum lttng_thread_type type
;
271 if (p
->exit_state
== EXIT_ZOMBIE
)
272 status
= LTTNG_ZOMBIE
;
273 else if (p
->exit_state
== EXIT_DEAD
)
275 else if (p
->state
== TASK_RUNNING
) {
276 /* Is this a forked child that has not run yet? */
277 if (list_empty(&p
->rt
.run_list
))
278 status
= LTTNG_WAIT_FORK
;
281 * All tasks are considered as wait_cpu;
282 * the viewer will sort out if the task
283 * was really running at this time.
285 status
= LTTNG_WAIT_CPU
;
286 } else if (p
->state
&
287 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
288 /* Task is waiting for something to complete */
291 status
= LTTNG_UNNAMED
;
292 submode
= LTTNG_NONE
;
295 * Verification of t->mm is to filter out kernel
296 * threads; Viewer will further filter out if a
297 * user-space thread was in syscall mode or not.
300 type
= LTTNG_USER_THREAD
;
302 type
= LTTNG_KERNEL_THREAD
;
303 trace_lttng_statedump_process_state(session
,
304 p
, type
, mode
, submode
, status
);
306 } while_each_thread(g
, p
);
314 void lttng_statedump_work_func(struct work_struct
*work
)
316 if (atomic_dec_and_test(&kernel_threads_to_run
))
317 /* If we are the last thread, wake up do_lttng_statedump */
318 wake_up(&statedump_wq
);
322 int do_lttng_statedump(struct lttng_session
*session
)
326 printk(KERN_DEBUG
"LTT state dump thread start\n");
327 trace_lttng_statedump_start(session
);
328 lttng_enumerate_process_states(session
);
329 lttng_enumerate_file_descriptors(session
);
330 lttng_enumerate_vm_maps(session
);
331 lttng_list_interrupts(session
);
332 lttng_enumerate_network_ip_interface(session
);
334 /* TODO lttng_dump_idt_table(session); */
335 /* TODO lttng_dump_softirq_vec(session); */
336 /* TODO lttng_list_modules(session); */
337 /* TODO lttng_dump_swap_files(session); */
340 * Fire off a work queue on each CPU. Their sole purpose in life
341 * is to guarantee that each CPU has been in a state where is was in
342 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
345 atomic_set(&kernel_threads_to_run
, num_online_cpus());
346 for_each_online_cpu(cpu
) {
347 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
348 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
350 /* Wait for all threads to run */
351 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) != 0));
353 /* Our work is done */
354 printk(KERN_DEBUG
"LTT state dump end\n");
355 trace_lttng_statedump_end(session
);
360 * Called with session mutex held.
362 int lttng_statedump_start(struct lttng_session
*session
)
364 printk(KERN_DEBUG
"LTTng: state dump begin\n");
365 return do_lttng_statedump(session
);
367 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
369 MODULE_LICENSE("GPL and additional rights");
370 MODULE_AUTHOR("Jean-Hugues Deschenes");
371 MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Statedump");