1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
5 * Linux Trace Toolkit Next Generation Kernel State Dump
7 * Copyright 2005 Jean-Hugues Deschenes <jean-hugues.deschenes@polymtl.ca>
8 * Copyright 2006-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * Eric Clement: Add listing of network IP interface
12 * 2006, 2007 Mathieu Desnoyers Fix kernel threads
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/netlink.h>
19 #include <linux/inet.h>
21 #include <linux/kthread.h>
22 #include <linux/proc_fs.h>
23 #include <linux/file.h>
24 #include <linux/interrupt.h>
25 #include <linux/irqnr.h>
26 #include <linux/cpu.h>
27 #include <linux/netdevice.h>
28 #include <linux/inetdevice.h>
29 #include <linux/sched.h>
31 #include <linux/fdtable.h>
32 #include <linux/swap.h>
33 #include <linux/wait.h>
34 #include <linux/mutex.h>
35 #include <linux/device.h>
37 #include <lttng-events.h>
38 #include <lttng-tracer.h>
39 #include <wrapper/irqdesc.h>
40 #include <wrapper/spinlock.h>
41 #include <wrapper/fdtable.h>
42 #include <wrapper/irq.h>
43 #include <wrapper/tracepoint.h>
44 #include <wrapper/genhd.h>
45 #include <wrapper/file.h>
46 #include <wrapper/time.h>
48 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
49 #include <linux/irq.h>
52 /* Define the tracepoints, but do not build the probes */
53 #define CREATE_TRACE_POINTS
54 #define TRACE_INCLUDE_PATH instrumentation/events/lttng-module
55 #define TRACE_INCLUDE_FILE lttng-statedump
56 #define LTTNG_INSTRUMENTATION
57 #include <instrumentation/events/lttng-module/lttng-statedump.h>
59 DEFINE_TRACE(lttng_statedump_block_device
);
60 DEFINE_TRACE(lttng_statedump_end
);
61 DEFINE_TRACE(lttng_statedump_interrupt
);
62 DEFINE_TRACE(lttng_statedump_file_descriptor
);
63 DEFINE_TRACE(lttng_statedump_start
);
64 DEFINE_TRACE(lttng_statedump_process_state
);
65 DEFINE_TRACE(lttng_statedump_network_interface
);
66 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
67 DEFINE_TRACE(lttng_statedump_cpu_topology
);
72 struct lttng_session
*session
;
73 struct task_struct
*p
;
74 struct files_struct
*files
;
78 * Protected by the trace lock.
80 static struct delayed_work cpu_work
[NR_CPUS
];
81 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq
);
82 static atomic_t kernel_threads_to_run
;
84 enum lttng_thread_type
{
85 LTTNG_USER_THREAD
= 0,
86 LTTNG_KERNEL_THREAD
= 1,
89 enum lttng_execution_mode
{
95 LTTNG_MODE_UNKNOWN
= 5,
98 enum lttng_execution_submode
{
103 enum lttng_process_status
{
115 int lttng_enumerate_block_devices(struct lttng_session
*session
)
117 struct class *ptr_block_class
;
118 struct device_type
*ptr_disk_type
;
119 struct class_dev_iter iter
;
122 ptr_block_class
= wrapper_get_block_class();
123 if (!ptr_block_class
)
125 ptr_disk_type
= wrapper_get_disk_type();
126 if (!ptr_disk_type
) {
129 class_dev_iter_init(&iter
, ptr_block_class
, NULL
, ptr_disk_type
);
130 while ((dev
= class_dev_iter_next(&iter
))) {
131 struct disk_part_iter piter
;
132 struct gendisk
*disk
= dev_to_disk(dev
);
133 struct hd_struct
*part
;
136 * Don't show empty devices or things that have been
139 if (get_capacity(disk
) == 0 ||
140 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
143 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
144 while ((part
= disk_part_iter_next(&piter
))) {
145 char name_buf
[BDEVNAME_SIZE
];
148 p
= wrapper_disk_name(disk
, part
->partno
, name_buf
);
150 disk_part_iter_exit(&piter
);
151 class_dev_iter_exit(&iter
);
154 trace_lttng_statedump_block_device(session
,
155 part_devt(part
), name_buf
);
157 disk_part_iter_exit(&piter
);
159 class_dev_iter_exit(&iter
);
166 void lttng_enumerate_device(struct lttng_session
*session
,
167 struct net_device
*dev
)
169 struct in_device
*in_dev
;
170 struct in_ifaddr
*ifa
;
172 if (dev
->flags
& IFF_UP
) {
173 in_dev
= in_dev_get(dev
);
175 for (ifa
= in_dev
->ifa_list
; ifa
!= NULL
;
176 ifa
= ifa
->ifa_next
) {
177 trace_lttng_statedump_network_interface(
183 trace_lttng_statedump_network_interface(
189 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
191 struct net_device
*dev
;
193 read_lock(&dev_base_lock
);
194 for_each_netdev(&init_net
, dev
)
195 lttng_enumerate_device(session
, dev
);
196 read_unlock(&dev_base_lock
);
200 #else /* CONFIG_INET */
202 int lttng_enumerate_network_ip_interface(struct lttng_session
*session
)
206 #endif /* CONFIG_INET */
209 int lttng_dump_one_fd(const void *p
, struct file
*file
, unsigned int fd
)
211 const struct lttng_fd_ctx
*ctx
= p
;
212 const char *s
= d_path(&file
->f_path
, ctx
->page
, PAGE_SIZE
);
213 unsigned int flags
= file
->f_flags
;
217 * We don't expose kernel internal flags, only userspace-visible
220 flags
&= ~FMODE_NONOTIFY
;
221 fdt
= files_fdtable(ctx
->files
);
223 * We need to check here again whether fd is within the fdt
224 * max_fds range, because we might be seeing a different
225 * files_fdtable() than iterate_fd(), assuming only RCU is
226 * protecting the read. In reality, iterate_fd() holds
227 * file_lock, which should ensure the fdt does not change while
228 * the lock is taken, but we are not aware whether this is
229 * guaranteed or not, so play safe.
231 if (fd
< fdt
->max_fds
&& lttng_close_on_exec(fd
, fdt
))
234 struct dentry
*dentry
= file
->f_path
.dentry
;
236 /* Make sure we give at least some info */
237 spin_lock(&dentry
->d_lock
);
238 trace_lttng_statedump_file_descriptor(ctx
->session
, ctx
->p
, fd
,
239 dentry
->d_name
.name
, flags
, file
->f_mode
);
240 spin_unlock(&dentry
->d_lock
);
243 trace_lttng_statedump_file_descriptor(ctx
->session
, ctx
->p
, fd
, s
,
244 flags
, file
->f_mode
);
250 void lttng_enumerate_task_fd(struct lttng_session
*session
,
251 struct task_struct
*p
, char *tmp
)
253 struct lttng_fd_ctx ctx
= { .page
= tmp
, .session
= session
, .p
= p
};
254 struct files_struct
*files
;
261 lttng_iterate_fd(files
, 0, lttng_dump_one_fd
, &ctx
);
267 int lttng_enumerate_file_descriptors(struct lttng_session
*session
)
269 struct task_struct
*p
;
272 tmp
= (char *) __get_free_page(GFP_KERNEL
);
276 /* Enumerate active file descriptors */
279 lttng_enumerate_task_fd(session
, p
, tmp
);
281 free_page((unsigned long) tmp
);
285 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
287 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
290 const cpumask_t
*cpumask
= cpu_possible_mask
;
292 for (cpu
= cpumask_first(cpumask
); cpu
< nr_cpu_ids
;
293 cpu
= cpumask_next(cpu
, cpumask
)) {
294 trace_lttng_statedump_cpu_topology(session
, &cpu_data(cpu
));
301 int lttng_enumerate_cpu_topology(struct lttng_session
*session
)
309 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
310 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
311 * iteration, but it is not exported to modules.
314 void lttng_enumerate_task_vm_maps(struct lttng_session
*session
,
315 struct task_struct
*p
)
317 struct mm_struct
*mm
;
318 struct vm_area_struct
*map
;
321 /* get_task_mm does a task_lock... */
328 down_read(&mm
->mmap_sem
);
331 ino
= map
->vm_file
->lttng_f_dentry
->d_inode
->i_ino
;
334 trace_lttng_statedump_vm_map(session
, p
, map
, ino
);
337 up_read(&mm
->mmap_sem
);
343 int lttng_enumerate_vm_maps(struct lttng_session
*session
)
345 struct task_struct
*p
;
349 lttng_enumerate_task_vm_maps(session
, p
);
355 #ifdef CONFIG_LTTNG_HAS_LIST_IRQ
357 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39))
358 #define irq_desc_get_chip(desc) get_irq_desc_chip(desc)
362 int lttng_list_interrupts(struct lttng_session
*session
)
365 unsigned long flags
= 0;
366 struct irq_desc
*desc
;
368 #define irq_to_desc wrapper_irq_to_desc
370 for_each_irq_desc(irq
, desc
) {
371 struct irqaction
*action
;
372 const char *irq_chip_name
=
373 irq_desc_get_chip(desc
)->name
? : "unnamed_irq_chip";
375 local_irq_save(flags
);
376 wrapper_desc_spin_lock(&desc
->lock
);
377 for (action
= desc
->action
; action
; action
= action
->next
) {
378 trace_lttng_statedump_interrupt(session
,
379 irq
, irq_chip_name
, action
);
381 wrapper_desc_spin_unlock(&desc
->lock
);
382 local_irq_restore(flags
);
389 int lttng_list_interrupts(struct lttng_session
*session
)
396 * Called with task lock held.
399 void lttng_statedump_process_ns(struct lttng_session
*session
,
400 struct task_struct
*p
,
401 enum lttng_thread_type type
,
402 enum lttng_execution_mode mode
,
403 enum lttng_execution_submode submode
,
404 enum lttng_process_status status
)
406 struct pid_namespace
*pid_ns
;
408 pid_ns
= task_active_pid_ns(p
);
410 trace_lttng_statedump_process_state(session
,
411 p
, type
, mode
, submode
, status
, pid_ns
);
412 pid_ns
= pid_ns
->parent
;
417 int lttng_enumerate_process_states(struct lttng_session
*session
)
419 struct task_struct
*g
, *p
;
422 for_each_process(g
) {
425 enum lttng_execution_mode mode
=
427 enum lttng_execution_submode submode
=
429 enum lttng_process_status status
;
430 enum lttng_thread_type type
;
433 if (p
->exit_state
== EXIT_ZOMBIE
)
434 status
= LTTNG_ZOMBIE
;
435 else if (p
->exit_state
== EXIT_DEAD
)
437 else if (p
->state
== TASK_RUNNING
) {
438 /* Is this a forked child that has not run yet? */
439 if (list_empty(&p
->rt
.run_list
))
440 status
= LTTNG_WAIT_FORK
;
443 * All tasks are considered as wait_cpu;
444 * the viewer will sort out if the task
445 * was really running at this time.
447 status
= LTTNG_WAIT_CPU
;
448 } else if (p
->state
&
449 (TASK_INTERRUPTIBLE
| TASK_UNINTERRUPTIBLE
)) {
450 /* Task is waiting for something to complete */
453 status
= LTTNG_UNNAMED
;
454 submode
= LTTNG_NONE
;
457 * Verification of t->mm is to filter out kernel
458 * threads; Viewer will further filter out if a
459 * user-space thread was in syscall mode or not.
462 type
= LTTNG_USER_THREAD
;
464 type
= LTTNG_KERNEL_THREAD
;
465 lttng_statedump_process_ns(session
,
466 p
, type
, mode
, submode
, status
);
468 } while_each_thread(g
, p
);
476 void lttng_statedump_work_func(struct work_struct
*work
)
478 if (atomic_dec_and_test(&kernel_threads_to_run
))
479 /* If we are the last thread, wake up do_lttng_statedump */
480 wake_up(&statedump_wq
);
484 int do_lttng_statedump(struct lttng_session
*session
)
488 trace_lttng_statedump_start(session
);
489 ret
= lttng_enumerate_process_states(session
);
492 ret
= lttng_enumerate_file_descriptors(session
);
497 * ret = lttng_enumerate_vm_maps(session);
501 ret
= lttng_list_interrupts(session
);
504 ret
= lttng_enumerate_network_ip_interface(session
);
507 ret
= lttng_enumerate_block_devices(session
);
512 printk(KERN_WARNING
"LTTng: block device enumeration is not supported by kernel\n");
517 ret
= lttng_enumerate_cpu_topology(session
);
521 /* TODO lttng_dump_idt_table(session); */
522 /* TODO lttng_dump_softirq_vec(session); */
523 /* TODO lttng_list_modules(session); */
524 /* TODO lttng_dump_swap_files(session); */
527 * Fire off a work queue on each CPU. Their sole purpose in life
528 * is to guarantee that each CPU has been in a state where is was in
529 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
532 atomic_set(&kernel_threads_to_run
, num_online_cpus());
533 for_each_online_cpu(cpu
) {
534 INIT_DELAYED_WORK(&cpu_work
[cpu
], lttng_statedump_work_func
);
535 schedule_delayed_work_on(cpu
, &cpu_work
[cpu
], 0);
537 /* Wait for all threads to run */
538 __wait_event(statedump_wq
, (atomic_read(&kernel_threads_to_run
) == 0));
540 /* Our work is done */
541 trace_lttng_statedump_end(session
);
546 * Called with session mutex held.
548 int lttng_statedump_start(struct lttng_session
*session
)
550 return do_lttng_statedump(session
);
552 EXPORT_SYMBOL_GPL(lttng_statedump_start
);
555 int __init
lttng_statedump_init(void)
558 * Allow module to load even if the fixup cannot be done. This
559 * will allow seemless transition when the underlying issue fix
560 * is merged into the Linux kernel, and when tracepoint.c
561 * "tracepoint_module_notify" is turned into a static function.
563 (void) wrapper_lttng_fixup_sig(THIS_MODULE
);
567 module_init(lttng_statedump_init
);
570 void __exit
lttng_statedump_exit(void)
574 module_exit(lttng_statedump_exit
);
576 MODULE_LICENSE("GPL and additional rights");
577 MODULE_AUTHOR("Jean-Hugues Deschenes");
578 MODULE_DESCRIPTION("LTTng statedump provider");
579 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
580 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
581 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
582 LTTNG_MODULES_EXTRAVERSION
);