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"
68 DEFINE_TRACE(lttng_statedump_end
);
69 DEFINE_TRACE(lttng_statedump_interrupt
);
70 DEFINE_TRACE(lttng_statedump_file_descriptor
);
71 DEFINE_TRACE(lttng_statedump_start
);
72 DEFINE_TRACE(lttng_statedump_process_state
);
73 DEFINE_TRACE(lttng_statedump_network_interface
);
77 struct lttng_session
*session
;
78 struct task_struct
*p
;
82 * Protected by the trace lock.
84 static struct delayed_work cpu_work
[NR_CPUS
];
85 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq
);
86 static atomic_t kernel_threads_to_run
;
88 enum lttng_thread_type
{
89 LTTNG_USER_THREAD
= 0,
90 LTTNG_KERNEL_THREAD
= 1,
93 enum lttng_execution_mode
{
99 LTTNG_MODE_UNKNOWN
= 5,
102 enum lttng_execution_submode
{
107 enum lttng_process_status
{
120 void lttng_enumerate_device(struct lttng_session
*session
,
121 struct net_device
*dev
)
123 struct in_device
*in_dev
;
124 struct in_ifaddr
*ifa
;
126 if (dev
->flags
& IFF_UP
) {
127 in_dev
= in_dev_get(dev
);
129 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
130 ifa
= ifa
->ifa_next
) {
131 trace_lttng_statedump_network_interface(
137 trace_lttng_statedump_network_interface(
143 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
145 struct net_device
*dev
;
147 read_lock(&dev_base_lock
);
148 for_each_netdev(&init_net
, dev
)
149 lttng_enumerate_device(session
, dev
);
150 read_unlock(&dev_base_lock
);
154 #else /* CONFIG_INET */
156 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
160 #endif /* CONFIG_INET */
163 int lttng_dump_one_fd(const void *p
, struct file
*file
, unsigned int fd
)
165 const struct lttng_fd_ctx
*ctx
= p
;
166 const char *s
= d_path(&file
->f_path
, ctx
->page
, PAGE_SIZE
);
169 struct dentry
*dentry
= file
->f_path
.dentry
;
171 /* Make sure we give at least some info */
172 spin_lock(&dentry
->d_lock
);
173 trace_lttng_statedump_file_descriptor(ctx
->session
, ctx
->p
, fd
,
174 dentry
->d_name
.name
);
175 spin_unlock(&dentry
->d_lock
);
178 trace_lttng_statedump_file_descriptor(ctx
->session
, ctx
->p
, fd
, s
);
184 void lttng_enumerate_task_fd(struct lttng_session
*session
,
185 struct task_struct
*p
, char *tmp
)
187 struct lttng_fd_ctx ctx
= { .page
= tmp
, .session
= session
, .p
= p
};
190 lttng_iterate_fd(p
->files
, 0, lttng_dump_one_fd
, &ctx
);
195 int lttng_enumerate_file_descriptors(struct lttng_session
*session
)
197 struct task_struct
*p
;
198 char *tmp
= (char *) __get_free_page(GFP_KERNEL
);
200 /* Enumerate active file descriptors */
203 lttng_enumerate_task_fd(session
, p
, tmp
);
205 free_page((unsigned long) tmp
);
211 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
212 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
213 * iteration, but it is not exported to modules.
216 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
217 struct task_struct
*p
)
219 struct mm_struct
*mm
;
220 struct vm_area_struct
*map
;
223 /* get_task_mm does a task_lock... */
230 down_read(&mm
->mmap_sem
);
233 ino
= map
->vm_file
->f_dentry
->d_inode
->i_ino
;
236 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
239 up_read(&mm
->mmap_sem
);
245 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
247 struct task_struct
*p
;
251 lttng_enumerate_task_vm_maps(session
, p
);
257 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
259 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39))
260 #define irq_desc_get_chip(desc) get_irq_desc_chip(desc)
264 void lttng_list_interrupts(struct lttng_session
*session
)
267 unsigned long flags
= 0;
268 struct irq_desc
*desc
;
270 #define irq_to_desc wrapper_irq_to_desc
272 for_each_irq_desc(irq
, desc
) {
273 struct irqaction
*action
;
274 const char *irq_chip_name
=
275 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
277 local_irq_save(flags
);
278 wrapper_desc_spin_lock(&desc
->lock
);
279 for (action
= desc
->action
; action
; action
= action
->next
) {
280 trace_lttng_statedump_interrupt(session
,
281 irq
, irq_chip_name
, action
);
283 wrapper_desc_spin_unlock(&desc
->lock
);
284 local_irq_restore(flags
);
290 void lttng_list_interrupts(struct lttng_session
*session
)
296 void lttng_statedump_process_ns(struct lttng_session
*session
,
297 struct task_struct
*p
,
298 enum lttng_thread_type type
,
299 enum lttng_execution_mode mode
,
300 enum lttng_execution_submode submode
,
301 enum lttng_process_status status
)
303 struct nsproxy
*proxy
;
304 struct pid_namespace
*pid_ns
;
307 proxy
= task_nsproxy(p
);
309 pid_ns
= lttng_get_proxy_pid_ns(proxy
);
311 trace_lttng_statedump_process_state(session
,
312 p
, type
, mode
, submode
, status
, pid_ns
);
313 pid_ns
= pid_ns
->parent
;
316 trace_lttng_statedump_process_state(session
,
317 p
, type
, mode
, submode
, status
, NULL
);
323 int lttng_enumerate_process_states(struct lttng_session
*session
)
325 struct task_struct
*g
, *p
;
328 for_each_process(g
) {
331 enum lttng_execution_mode mode
=
333 enum lttng_execution_submode submode
=
335 enum lttng_process_status status
;
336 enum lttng_thread_type type
;
339 if (p
->exit_state
== EXIT_ZOMBIE
)
340 status
= LTTNG_ZOMBIE
;
341 else if (p
->exit_state
== EXIT_DEAD
)
343 else if (p
->state
== TASK_RUNNING
) {
344 /* Is this a forked child that has not run yet? */
345 if (list_empty(&p
->rt
.run_list
))
346 status
= LTTNG_WAIT_FORK
;
349 * All tasks are considered as wait_cpu;
350 * the viewer will sort out if the task
351 * was really running at this time.
353 status
= LTTNG_WAIT_CPU
;
354 } else if (p
->state
&
355 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
356 /* Task is waiting for something to complete */
359 status
= LTTNG_UNNAMED
;
360 submode
= LTTNG_NONE
;
363 * Verification of t->mm is to filter out kernel
364 * threads; Viewer will further filter out if a
365 * user-space thread was in syscall mode or not.
368 type
= LTTNG_USER_THREAD
;
370 type
= LTTNG_KERNEL_THREAD
;
371 lttng_statedump_process_ns(session
,
372 p
, type
, mode
, submode
, status
);
374 } while_each_thread(g
, p
);
382 void lttng_statedump_work_func(struct work_struct
*work
)
384 if (atomic_dec_and_test(&kernel_threads_to_run
))
385 /* If we are the last thread, wake up do_lttng_statedump */
386 wake_up(&statedump_wq
);
390 int do_lttng_statedump(struct lttng_session
*session
)
394 trace_lttng_statedump_start(session
);
395 lttng_enumerate_process_states(session
);
396 lttng_enumerate_file_descriptors(session
);
397 /* FIXME lttng_enumerate_vm_maps(session); */
398 lttng_list_interrupts(session
);
399 lttng_enumerate_network_ip_interface(session
);
401 /* TODO lttng_dump_idt_table(session); */
402 /* TODO lttng_dump_softirq_vec(session); */
403 /* TODO lttng_list_modules(session); */
404 /* TODO lttng_dump_swap_files(session); */
407 * Fire off a work queue on each CPU. Their sole purpose in life
408 * is to guarantee that each CPU has been in a state where is was in
409 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
412 atomic_set(&kernel_threads_to_run
, num_online_cpus());
413 for_each_online_cpu(cpu
) {
414 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
415 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
417 /* Wait for all threads to run */
418 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) == 0));
420 /* Our work is done */
421 trace_lttng_statedump_end(session
);
426 * Called with session mutex held.
428 int lttng_statedump_start(struct lttng_session
*session
)
430 return do_lttng_statedump(session
);
432 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
435 int __init
lttng_statedump_init(void)
438 * Allow module to load even if the fixup cannot be done. This
439 * will allow seemless transition when the underlying issue fix
440 * is merged into the Linux kernel, and when tracepoint.c
441 * "tracepoint_module_notify" is turned into a static function.
443 (void) wrapper_lttng_fixup_sig(THIS_MODULE
);
447 module_init(lttng_statedump_init
);
450 void __exit
lttng_statedump_exit(void)
454 module_exit(lttng_statedump_exit
);
456 MODULE_LICENSE("GPL and additional rights");
457 MODULE_AUTHOR("Jean-Hugues Deschenes");
458 MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Statedump");
459 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
460 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
461 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
462 LTTNG_MODULES_EXTRAVERSION
);