Fix: signal: Remove SEND_SIG_FORCED (v4.20)
[lttng-modules.git] / lttng-statedump-impl.c
CommitLineData
9f36eaed
MJ
1/* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
2 *
886d51a3
MD
3 * lttng-statedump.c
4 *
c337ddc2
MD
5 * Linux Trace Toolkit Next Generation Kernel State Dump
6 *
7 * Copyright 2005 Jean-Hugues Deschenes <jean-hugues.deschenes@polymtl.ca>
8 * Copyright 2006-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Changes:
11 * Eric Clement: Add listing of network IP interface
12 * 2006, 2007 Mathieu Desnoyers Fix kernel threads
13 * Various updates
c337ddc2
MD
14 */
15
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/netlink.h>
19#include <linux/inet.h>
20#include <linux/ip.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>
30#include <linux/mm.h>
31#include <linux/fdtable.h>
32#include <linux/swap.h>
33#include <linux/wait.h>
34#include <linux/mutex.h>
f0dbdefb 35#include <linux/device.h>
c337ddc2 36
241ae9a8
MD
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>
241ae9a8
MD
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>
c337ddc2 47
29784493 48#ifdef CONFIG_LTTNG_HAS_LIST_IRQ
c337ddc2
MD
49#include <linux/irq.h>
50#endif
51
52/* Define the tracepoints, but do not build the probes */
53#define CREATE_TRACE_POINTS
241ae9a8 54#define TRACE_INCLUDE_PATH instrumentation/events/lttng-module
c337ddc2 55#define TRACE_INCLUDE_FILE lttng-statedump
3bc29f0a 56#define LTTNG_INSTRUMENTATION
241ae9a8 57#include <instrumentation/events/lttng-module/lttng-statedump.h>
c337ddc2 58
f0dbdefb 59DEFINE_TRACE(lttng_statedump_block_device);
20591cf7
MD
60DEFINE_TRACE(lttng_statedump_end);
61DEFINE_TRACE(lttng_statedump_interrupt);
62DEFINE_TRACE(lttng_statedump_file_descriptor);
63DEFINE_TRACE(lttng_statedump_start);
64DEFINE_TRACE(lttng_statedump_process_state);
65DEFINE_TRACE(lttng_statedump_network_interface);
d0b55e4c 66#ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
502e4132
JD
67DEFINE_TRACE(lttng_statedump_cpu_topology);
68#endif
20591cf7 69
361c023a
MD
70struct lttng_fd_ctx {
71 char *page;
72 struct lttng_session *session;
73 struct task_struct *p;
d561ecfb 74 struct files_struct *files;
361c023a
MD
75};
76
c337ddc2
MD
77/*
78 * Protected by the trace lock.
79 */
80static struct delayed_work cpu_work[NR_CPUS];
81static DECLARE_WAIT_QUEUE_HEAD(statedump_wq);
82static atomic_t kernel_threads_to_run;
83
84enum lttng_thread_type {
85 LTTNG_USER_THREAD = 0,
86 LTTNG_KERNEL_THREAD = 1,
87};
88
89enum lttng_execution_mode {
90 LTTNG_USER_MODE = 0,
91 LTTNG_SYSCALL = 1,
92 LTTNG_TRAP = 2,
93 LTTNG_IRQ = 3,
94 LTTNG_SOFTIRQ = 4,
95 LTTNG_MODE_UNKNOWN = 5,
96};
97
98enum lttng_execution_submode {
99 LTTNG_NONE = 0,
100 LTTNG_UNKNOWN = 1,
101};
102
103enum lttng_process_status {
104 LTTNG_UNNAMED = 0,
105 LTTNG_WAIT_FORK = 1,
106 LTTNG_WAIT_CPU = 2,
107 LTTNG_EXIT = 3,
108 LTTNG_ZOMBIE = 4,
109 LTTNG_WAIT = 5,
110 LTTNG_RUN = 6,
111 LTTNG_DEAD = 7,
112};
113
f0dbdefb
HD
114static
115int lttng_enumerate_block_devices(struct lttng_session *session)
116{
117 struct class *ptr_block_class;
118 struct device_type *ptr_disk_type;
119 struct class_dev_iter iter;
120 struct device *dev;
121
122 ptr_block_class = wrapper_get_block_class();
123 if (!ptr_block_class)
124 return -ENOSYS;
125 ptr_disk_type = wrapper_get_disk_type();
126 if (!ptr_disk_type) {
127 return -ENOSYS;
128 }
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;
134
5a91f3df
MD
135 /*
136 * Don't show empty devices or things that have been
137 * suppressed
138 */
139 if (get_capacity(disk) == 0 ||
140 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
141 continue;
142
f0dbdefb
HD
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];
146 char *p;
147
148 p = wrapper_disk_name(disk, part->partno, name_buf);
149 if (!p) {
150 disk_part_iter_exit(&piter);
151 class_dev_iter_exit(&iter);
152 return -ENOSYS;
153 }
154 trace_lttng_statedump_block_device(session,
155 part_devt(part), name_buf);
156 }
157 disk_part_iter_exit(&piter);
158 }
159 class_dev_iter_exit(&iter);
160 return 0;
161}
162
c337ddc2 163#ifdef CONFIG_INET
f0dbdefb 164
c337ddc2
MD
165static
166void lttng_enumerate_device(struct lttng_session *session,
167 struct net_device *dev)
168{
169 struct in_device *in_dev;
170 struct in_ifaddr *ifa;
171
172 if (dev->flags & IFF_UP) {
173 in_dev = in_dev_get(dev);
174 if (in_dev) {
175 for (ifa = in_dev->ifa_list; ifa != NULL;
176 ifa = ifa->ifa_next) {
177 trace_lttng_statedump_network_interface(
178 session, dev, ifa);
179 }
180 in_dev_put(in_dev);
181 }
182 } else {
183 trace_lttng_statedump_network_interface(
184 session, dev, NULL);
185 }
186}
187
188static
189int lttng_enumerate_network_ip_interface(struct lttng_session *session)
190{
191 struct net_device *dev;
192
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);
197
198 return 0;
199}
200#else /* CONFIG_INET */
201static inline
202int lttng_enumerate_network_ip_interface(struct lttng_session *session)
203{
204 return 0;
205}
206#endif /* CONFIG_INET */
207
361c023a
MD
208static
209int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
210{
211 const struct lttng_fd_ctx *ctx = p;
212 const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
29021503 213 unsigned int flags = file->f_flags;
d561ecfb 214 struct fdtable *fdt;
361c023a 215
29021503
MD
216 /*
217 * We don't expose kernel internal flags, only userspace-visible
218 * flags.
219 */
220 flags &= ~FMODE_NONOTIFY;
d561ecfb
MD
221 fdt = files_fdtable(ctx->files);
222 /*
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.
230 */
aa29f2d3 231 if (fd < fdt->max_fds && lttng_close_on_exec(fd, fdt))
29021503 232 flags |= O_CLOEXEC;
361c023a
MD
233 if (IS_ERR(s)) {
234 struct dentry *dentry = file->f_path.dentry;
235
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,
29021503 239 dentry->d_name.name, flags, file->f_mode);
361c023a
MD
240 spin_unlock(&dentry->d_lock);
241 goto end;
242 }
29021503
MD
243 trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd, s,
244 flags, file->f_mode);
361c023a
MD
245end:
246 return 0;
247}
c337ddc2
MD
248
249static
250void lttng_enumerate_task_fd(struct lttng_session *session,
251 struct task_struct *p, char *tmp)
252{
361c023a 253 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .p = p };
d561ecfb 254 struct files_struct *files;
c337ddc2
MD
255
256 task_lock(p);
d561ecfb
MD
257 files = p->files;
258 if (!files)
259 goto end;
260 ctx.files = files;
261 lttng_iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
262end:
c337ddc2
MD
263 task_unlock(p);
264}
265
266static
267int lttng_enumerate_file_descriptors(struct lttng_session *session)
268{
269 struct task_struct *p;
cfcee1c7
MD
270 char *tmp;
271
272 tmp = (char *) __get_free_page(GFP_KERNEL);
273 if (!tmp)
274 return -ENOMEM;
c337ddc2
MD
275
276 /* Enumerate active file descriptors */
277 rcu_read_lock();
278 for_each_process(p)
279 lttng_enumerate_task_fd(session, p, tmp);
280 rcu_read_unlock();
281 free_page((unsigned long) tmp);
282 return 0;
283}
284
d0b55e4c 285#ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
502e4132
JD
286static
287int lttng_enumerate_cpu_topology(struct lttng_session *session)
288{
289 int cpu;
290 const cpumask_t *cpumask = cpu_possible_mask;
291
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));
295 }
296
297 return 0;
298}
299#else
300static
301int lttng_enumerate_cpu_topology(struct lttng_session *session)
302{
303 return 0;
304}
305#endif
306
0658bdda
MD
307#if 0
308/*
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.
312 */
c337ddc2
MD
313static
314void lttng_enumerate_task_vm_maps(struct lttng_session *session,
315 struct task_struct *p)
316{
317 struct mm_struct *mm;
318 struct vm_area_struct *map;
319 unsigned long ino;
320
321 /* get_task_mm does a task_lock... */
322 mm = get_task_mm(p);
323 if (!mm)
324 return;
325
326 map = mm->mmap;
327 if (map) {
328 down_read(&mm->mmap_sem);
329 while (map) {
330 if (map->vm_file)
b06ed645 331 ino = map->vm_file->lttng_f_dentry->d_inode->i_ino;
c337ddc2
MD
332 else
333 ino = 0;
334 trace_lttng_statedump_vm_map(session, p, map, ino);
335 map = map->vm_next;
336 }
337 up_read(&mm->mmap_sem);
338 }
339 mmput(mm);
340}
341
342static
343int lttng_enumerate_vm_maps(struct lttng_session *session)
344{
345 struct task_struct *p;
346
347 rcu_read_lock();
348 for_each_process(p)
349 lttng_enumerate_task_vm_maps(session, p);
350 rcu_read_unlock();
351 return 0;
352}
0658bdda 353#endif
c337ddc2 354
29784493 355#ifdef CONFIG_LTTNG_HAS_LIST_IRQ
47faec4b
JN
356
357#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39))
358#define irq_desc_get_chip(desc) get_irq_desc_chip(desc)
359#endif
360
c337ddc2 361static
cfcee1c7 362int lttng_list_interrupts(struct lttng_session *session)
c337ddc2
MD
363{
364 unsigned int irq;
365 unsigned long flags = 0;
366 struct irq_desc *desc;
367
368#define irq_to_desc wrapper_irq_to_desc
369 /* needs irq_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";
374
375 local_irq_save(flags);
3a523f5b 376 wrapper_desc_spin_lock(&desc->lock);
c337ddc2
MD
377 for (action = desc->action; action; action = action->next) {
378 trace_lttng_statedump_interrupt(session,
379 irq, irq_chip_name, action);
380 }
3a523f5b 381 wrapper_desc_spin_unlock(&desc->lock);
c337ddc2
MD
382 local_irq_restore(flags);
383 }
cfcee1c7 384 return 0;
c337ddc2
MD
385#undef irq_to_desc
386}
387#else
388static inline
cfcee1c7 389int lttng_list_interrupts(struct lttng_session *session)
c337ddc2 390{
cfcee1c7 391 return 0;
c337ddc2
MD
392}
393#endif
394
4ba1f53c
MD
395/*
396 * Called with task lock held.
397 */
73e8ba37
JD
398static
399void 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)
405{
73e8ba37
JD
406 struct pid_namespace *pid_ns;
407
887bcdac
MJ
408 pid_ns = task_active_pid_ns(p);
409 do {
73e8ba37 410 trace_lttng_statedump_process_state(session,
887bcdac
MJ
411 p, type, mode, submode, status, pid_ns);
412 pid_ns = pid_ns->parent;
413 } while (pid_ns);
73e8ba37
JD
414}
415
c337ddc2
MD
416static
417int lttng_enumerate_process_states(struct lttng_session *session)
418{
419 struct task_struct *g, *p;
420
421 rcu_read_lock();
422 for_each_process(g) {
423 p = g;
424 do {
425 enum lttng_execution_mode mode =
426 LTTNG_MODE_UNKNOWN;
427 enum lttng_execution_submode submode =
428 LTTNG_UNKNOWN;
429 enum lttng_process_status status;
430 enum lttng_thread_type type;
431
432 task_lock(p);
433 if (p->exit_state == EXIT_ZOMBIE)
434 status = LTTNG_ZOMBIE;
435 else if (p->exit_state == EXIT_DEAD)
436 status = LTTNG_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;
441 else
442 /*
443 * All tasks are considered as wait_cpu;
444 * the viewer will sort out if the task
445 * was really running at this time.
446 */
447 status = LTTNG_WAIT_CPU;
448 } else if (p->state &
449 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
450 /* Task is waiting for something to complete */
451 status = LTTNG_WAIT;
452 } else
453 status = LTTNG_UNNAMED;
454 submode = LTTNG_NONE;
455
456 /*
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.
460 */
461 if (p->mm)
462 type = LTTNG_USER_THREAD;
463 else
464 type = LTTNG_KERNEL_THREAD;
73e8ba37 465 lttng_statedump_process_ns(session,
c337ddc2
MD
466 p, type, mode, submode, status);
467 task_unlock(p);
468 } while_each_thread(g, p);
469 }
470 rcu_read_unlock();
471
472 return 0;
473}
474
475static
476void lttng_statedump_work_func(struct work_struct *work)
477{
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);
481}
482
483static
484int do_lttng_statedump(struct lttng_session *session)
485{
cfcee1c7 486 int cpu, ret;
c337ddc2 487
c337ddc2 488 trace_lttng_statedump_start(session);
cfcee1c7
MD
489 ret = lttng_enumerate_process_states(session);
490 if (ret)
491 return ret;
492 ret = lttng_enumerate_file_descriptors(session);
493 if (ret)
494 return ret;
495 /*
496 * FIXME
497 * ret = lttng_enumerate_vm_maps(session);
498 * if (ret)
499 * return ret;
500 */
501 ret = lttng_list_interrupts(session);
502 if (ret)
503 return ret;
504 ret = lttng_enumerate_network_ip_interface(session);
505 if (ret)
506 return ret;
507 ret = lttng_enumerate_block_devices(session);
508 switch (ret) {
84c7055e
MD
509 case 0:
510 break;
cfcee1c7
MD
511 case -ENOSYS:
512 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
513 break;
514 default:
515 return ret;
516 }
502e4132
JD
517 ret = lttng_enumerate_cpu_topology(session);
518 if (ret)
519 return ret;
c337ddc2
MD
520
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); */
525
526 /*
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).
530 */
531 get_online_cpus();
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);
536 }
537 /* Wait for all threads to run */
7a7128e0 538 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
c337ddc2
MD
539 put_online_cpus();
540 /* Our work is done */
c337ddc2
MD
541 trace_lttng_statedump_end(session);
542 return 0;
543}
544
545/*
546 * Called with session mutex held.
547 */
548int lttng_statedump_start(struct lttng_session *session)
549{
c337ddc2
MD
550 return do_lttng_statedump(session);
551}
552EXPORT_SYMBOL_GPL(lttng_statedump_start);
553
dd8d5afb
MD
554static
555int __init lttng_statedump_init(void)
556{
d16aa9c9
MD
557 /*
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.
562 */
563 (void) wrapper_lttng_fixup_sig(THIS_MODULE);
564 return 0;
dd8d5afb
MD
565}
566
567module_init(lttng_statedump_init);
568
461277e7
MD
569static
570void __exit lttng_statedump_exit(void)
571{
572}
573
574module_exit(lttng_statedump_exit);
575
c337ddc2
MD
576MODULE_LICENSE("GPL and additional rights");
577MODULE_AUTHOR("Jean-Hugues Deschenes");
1c124020 578MODULE_DESCRIPTION("LTTng statedump provider");
13ab8b0a
MD
579MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
580 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
581 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
582 LTTNG_MODULES_EXTRAVERSION);
This page took 0.056418 seconds and 4 git commands to generate.