4 * Linux Trace Toolkit Next Generation Kernel State Dump
6 * Copyright 2005 Jean-Hugues Deschenes <jean-hugues.deschenes@polymtl.ca>
7 * Copyright 2006-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Eric Clement: Add listing of network IP interface
25 * 2006, 2007 Mathieu Desnoyers Fix kernel threads
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/netlink.h>
32 #include <linux/inet.h>
34 #include <linux/kthread.h>
35 #include <linux/proc_fs.h>
36 #include <linux/file.h>
37 #include <linux/interrupt.h>
38 #include <linux/irqnr.h>
39 #include <linux/cpu.h>
40 #include <linux/netdevice.h>
41 #include <linux/inetdevice.h>
42 #include <linux/sched.h>
44 #include <linux/fdtable.h>
45 #include <linux/swap.h>
46 #include <linux/wait.h>
47 #include <linux/mutex.h>
49 #include "lttng-events.h"
50 #include "lttng-tracer.h"
51 #include "wrapper/irqdesc.h"
52 #include "wrapper/spinlock.h"
53 #include "wrapper/fdtable.h"
54 #include "wrapper/nsproxy.h"
55 #include "wrapper/irq.h"
56 #include "wrapper/tracepoint.h"
58 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
59 #include <linux/irq.h>
62 /* Define the tracepoints, but do not build the probes */
63 #define CREATE_TRACE_POINTS
64 #define TRACE_INCLUDE_PATH ../instrumentation/events/lttng-module
65 #define TRACE_INCLUDE_FILE lttng-statedump
66 #include "instrumentation/events/lttng-module/lttng-statedump.h"
70 struct lttng_session
*session
;
71 struct task_struct
*p
;
75 * Protected by the trace lock.
77 static struct delayed_work cpu_work
[NR_CPUS
];
78 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq
);
79 static atomic_t kernel_threads_to_run
;
81 enum lttng_thread_type
{
82 LTTNG_USER_THREAD
= 0,
83 LTTNG_KERNEL_THREAD
= 1,
86 enum lttng_execution_mode
{
92 LTTNG_MODE_UNKNOWN
= 5,
95 enum lttng_execution_submode
{
100 enum lttng_process_status
{
113 void lttng_enumerate_device(struct lttng_session
*session
,
114 struct net_device
*dev
)
116 struct in_device
*in_dev
;
117 struct in_ifaddr
*ifa
;
119 if (dev
->flags
& IFF_UP
) {
120 in_dev
= in_dev_get(dev
);
122 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
123 ifa
= ifa
->ifa_next
) {
124 trace_lttng_statedump_network_interface(
130 trace_lttng_statedump_network_interface(
136 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
138 struct net_device
*dev
;
140 read_lock(&dev_base_lock
);
141 for_each_netdev(&init_net
, dev
)
142 lttng_enumerate_device(session
, dev
);
143 read_unlock(&dev_base_lock
);
147 #else /* CONFIG_INET */
149 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
153 #endif /* CONFIG_INET */
156 int lttng_dump_one_fd(const void *p
, struct file
*file
, unsigned int fd
)
158 const struct lttng_fd_ctx
*ctx
= p
;
159 const char *s
= d_path(&file
->f_path
, ctx
->page
, PAGE_SIZE
);
162 struct dentry
*dentry
= file
->f_path
.dentry
;
164 /* Make sure we give at least some info */
165 spin_lock(&dentry
->d_lock
);
166 trace_lttng_statedump_file_descriptor(ctx
->session
, ctx
->p
, fd
,
167 dentry
->d_name
.name
);
168 spin_unlock(&dentry
->d_lock
);
171 trace_lttng_statedump_file_descriptor(ctx
->session
, ctx
->p
, fd
, s
);
177 void lttng_enumerate_task_fd(struct lttng_session
*session
,
178 struct task_struct
*p
, char *tmp
)
180 struct lttng_fd_ctx ctx
= { .page
= tmp
, .session
= session
, .p
= p
};
183 lttng_iterate_fd(p
->files
, 0, lttng_dump_one_fd
, &ctx
);
188 int lttng_enumerate_file_descriptors(struct lttng_session
*session
)
190 struct task_struct
*p
;
193 tmp
= (char *) __get_free_page(GFP_KERNEL
);
197 /* Enumerate active file descriptors */
200 lttng_enumerate_task_fd(session
, p
, tmp
);
202 free_page((unsigned long) tmp
);
208 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
209 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
210 * iteration, but it is not exported to modules.
213 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
214 struct task_struct
*p
)
216 struct mm_struct
*mm
;
217 struct vm_area_struct
*map
;
220 /* get_task_mm does a task_lock... */
227 down_read(&mm
->mmap_sem
);
230 ino
= map
->vm_file
->f_dentry
->d_inode
->i_ino
;
233 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
236 up_read(&mm
->mmap_sem
);
242 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
244 struct task_struct
*p
;
248 lttng_enumerate_task_vm_maps(session
, p
);
254 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
256 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39))
257 #define irq_desc_get_chip(desc) get_irq_desc_chip(desc)
261 int lttng_list_interrupts(struct lttng_session
*session
)
264 unsigned long flags
= 0;
265 struct irq_desc
*desc
;
267 #define irq_to_desc wrapper_irq_to_desc
269 for_each_irq_desc(irq
, desc
) {
270 struct irqaction
*action
;
271 const char *irq_chip_name
=
272 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
274 local_irq_save(flags
);
275 wrapper_desc_spin_lock(&desc
->lock
);
276 for (action
= desc
->action
; action
; action
= action
->next
) {
277 trace_lttng_statedump_interrupt(session
,
278 irq
, irq_chip_name
, action
);
280 wrapper_desc_spin_unlock(&desc
->lock
);
281 local_irq_restore(flags
);
288 int lttng_list_interrupts(struct lttng_session
*session
)
295 void lttng_statedump_process_ns(struct lttng_session
*session
,
296 struct task_struct
*p
,
297 enum lttng_thread_type type
,
298 enum lttng_execution_mode mode
,
299 enum lttng_execution_submode submode
,
300 enum lttng_process_status status
)
302 struct nsproxy
*proxy
;
303 struct pid_namespace
*pid_ns
;
306 proxy
= task_nsproxy(p
);
308 pid_ns
= lttng_get_proxy_pid_ns(proxy
);
310 trace_lttng_statedump_process_state(session
,
311 p
, type
, mode
, submode
, status
, pid_ns
);
312 pid_ns
= pid_ns
->parent
;
315 trace_lttng_statedump_process_state(session
,
316 p
, type
, mode
, submode
, status
, NULL
);
322 int lttng_enumerate_process_states(struct lttng_session
*session
)
324 struct task_struct
*g
, *p
;
327 for_each_process(g
) {
330 enum lttng_execution_mode mode
=
332 enum lttng_execution_submode submode
=
334 enum lttng_process_status status
;
335 enum lttng_thread_type type
;
338 if (p
->exit_state
== EXIT_ZOMBIE
)
339 status
= LTTNG_ZOMBIE
;
340 else if (p
->exit_state
== EXIT_DEAD
)
342 else if (p
->state
== TASK_RUNNING
) {
343 /* Is this a forked child that has not run yet? */
344 if (list_empty(&p
->rt
.run_list
))
345 status
= LTTNG_WAIT_FORK
;
348 * All tasks are considered as wait_cpu;
349 * the viewer will sort out if the task
350 * was really running at this time.
352 status
= LTTNG_WAIT_CPU
;
353 } else if (p
->state
&
354 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
355 /* Task is waiting for something to complete */
358 status
= LTTNG_UNNAMED
;
359 submode
= LTTNG_NONE
;
362 * Verification of t->mm is to filter out kernel
363 * threads; Viewer will further filter out if a
364 * user-space thread was in syscall mode or not.
367 type
= LTTNG_USER_THREAD
;
369 type
= LTTNG_KERNEL_THREAD
;
370 lttng_statedump_process_ns(session
,
371 p
, type
, mode
, submode
, status
);
373 } while_each_thread(g
, p
);
381 void lttng_statedump_work_func(struct work_struct
*work
)
383 if (atomic_dec_and_test(&kernel_threads_to_run
))
384 /* If we are the last thread, wake up do_lttng_statedump */
385 wake_up(&statedump_wq
);
389 int do_lttng_statedump(struct lttng_session
*session
)
393 trace_lttng_statedump_start(session
);
394 ret
= lttng_enumerate_process_states(session
);
397 ret
= lttng_enumerate_file_descriptors(session
);
402 * ret = lttng_enumerate_vm_maps(session);
406 ret
= lttng_list_interrupts(session
);
409 ret
= lttng_enumerate_network_ip_interface(session
);
413 /* TODO lttng_dump_idt_table(session); */
414 /* TODO lttng_dump_softirq_vec(session); */
415 /* TODO lttng_list_modules(session); */
416 /* TODO lttng_dump_swap_files(session); */
419 * Fire off a work queue on each CPU. Their sole purpose in life
420 * is to guarantee that each CPU has been in a state where is was in
421 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
424 atomic_set(&kernel_threads_to_run
, num_online_cpus());
425 for_each_online_cpu(cpu
) {
426 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
427 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
429 /* Wait for all threads to run */
430 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) == 0));
432 /* Our work is done */
433 trace_lttng_statedump_end(session
);
438 * Called with session mutex held.
440 int lttng_statedump_start(struct lttng_session
*session
)
442 return do_lttng_statedump(session
);
444 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
447 int __init
lttng_statedump_init(void)
450 * Allow module to load even if the fixup cannot be done. This
451 * will allow seemless transition when the underlying issue fix
452 * is merged into the Linux kernel, and when tracepoint.c
453 * "tracepoint_module_notify" is turned into a static function.
455 (void) wrapper_lttng_fixup_sig(THIS_MODULE
);
459 module_init(lttng_statedump_init
);
462 void __exit
lttng_statedump_exit(void)
466 module_exit(lttng_statedump_exit
);
468 MODULE_LICENSE("GPL and additional rights");
469 MODULE_AUTHOR("Jean-Hugues Deschenes");
470 MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Statedump");
471 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
472 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
473 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
474 LTTNG_MODULES_EXTRAVERSION
);