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 void lttng_list_interrupts(struct lttng_session
*session
)
221 unsigned long flags
= 0;
222 struct irq_desc
*desc
;
224 #define irq_to_desc wrapper_irq_to_desc
226 for_each_irq_desc(irq
, desc
) {
227 struct irqaction
*action
;
228 const char *irq_chip_name
=
229 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
231 local_irq_save(flags
);
232 raw_spin_lock(&desc
->lock
);
233 for (action
= desc
->action
; action
; action
= action
->next
) {
234 trace_lttng_statedump_interrupt(session
,
235 irq
, irq_chip_name
, action
);
237 raw_spin_unlock(&desc
->lock
);
238 local_irq_restore(flags
);
244 void list_interrupts(struct lttng_session
*session
)
250 int lttng_enumerate_process_states(struct lttng_session
*session
)
252 struct task_struct
*g
, *p
;
255 for_each_process(g
) {
258 enum lttng_execution_mode mode
=
260 enum lttng_execution_submode submode
=
262 enum lttng_process_status status
;
263 enum lttng_thread_type type
;
266 if (p
->exit_state
== EXIT_ZOMBIE
)
267 status
= LTTNG_ZOMBIE
;
268 else if (p
->exit_state
== EXIT_DEAD
)
270 else if (p
->state
== TASK_RUNNING
) {
271 /* Is this a forked child that has not run yet? */
272 if (list_empty(&p
->rt
.run_list
))
273 status
= LTTNG_WAIT_FORK
;
276 * All tasks are considered as wait_cpu;
277 * the viewer will sort out if the task
278 * was really running at this time.
280 status
= LTTNG_WAIT_CPU
;
281 } else if (p
->state
&
282 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
283 /* Task is waiting for something to complete */
286 status
= LTTNG_UNNAMED
;
287 submode
= LTTNG_NONE
;
290 * Verification of t->mm is to filter out kernel
291 * threads; Viewer will further filter out if a
292 * user-space thread was in syscall mode or not.
295 type
= LTTNG_USER_THREAD
;
297 type
= LTTNG_KERNEL_THREAD
;
298 trace_lttng_statedump_process_state(session
,
299 p
, type
, mode
, submode
, status
);
301 } while_each_thread(g
, p
);
309 void lttng_statedump_work_func(struct work_struct
*work
)
311 if (atomic_dec_and_test(&kernel_threads_to_run
))
312 /* If we are the last thread, wake up do_lttng_statedump */
313 wake_up(&statedump_wq
);
317 int do_lttng_statedump(struct lttng_session
*session
)
321 printk(KERN_DEBUG
"LTT state dump thread start\n");
322 trace_lttng_statedump_start(session
);
323 lttng_enumerate_process_states(session
);
324 lttng_enumerate_file_descriptors(session
);
325 lttng_enumerate_vm_maps(session
);
326 lttng_list_interrupts(session
);
327 lttng_enumerate_network_ip_interface(session
);
329 /* TODO lttng_dump_idt_table(session); */
330 /* TODO lttng_dump_softirq_vec(session); */
331 /* TODO lttng_list_modules(session); */
332 /* TODO lttng_dump_swap_files(session); */
335 * Fire off a work queue on each CPU. Their sole purpose in life
336 * is to guarantee that each CPU has been in a state where is was in
337 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
340 atomic_set(&kernel_threads_to_run
, num_online_cpus());
341 for_each_online_cpu(cpu
) {
342 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
343 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
345 /* Wait for all threads to run */
346 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) != 0));
348 /* Our work is done */
349 printk(KERN_DEBUG
"LTT state dump end\n");
350 trace_lttng_statedump_end(session
);
355 * Called with session mutex held.
357 int lttng_statedump_start(struct lttng_session
*session
)
359 printk(KERN_DEBUG
"LTTng: state dump begin\n");
360 return do_lttng_statedump(session
);
362 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
364 MODULE_LICENSE("GPL and additional rights");
365 MODULE_AUTHOR("Jean-Hugues Deschenes");
366 MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Statedump");