Drop 'linux/irq.h' wrapper
[lttng-modules.git] / src / lttng-statedump-impl.c
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * lttng-statedump.c
4 *
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
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/irq.h>
26 #include <linux/irqnr.h>
27 #include <linux/netdevice.h>
28 #include <linux/inetdevice.h>
29 #include <linux/mm.h>
30 #include <linux/swap.h>
31 #include <linux/wait.h>
32 #include <linux/mutex.h>
33 #include <linux/device.h>
34
35 #include <linux/blkdev.h>
36
37 #include <lttng/events.h>
38 #include <lttng/tracer.h>
39 #include <wrapper/cpu.h>
40 #include <wrapper/irqdesc.h>
41 #include <wrapper/fdtable.h>
42 #include <wrapper/namespace.h>
43 #include <wrapper/tracepoint.h>
44 #include <wrapper/blkdev.h>
45 #include <wrapper/fdtable.h>
46 #include <wrapper/sched.h>
47
48 /* Define the tracepoints, but do not build the probes */
49 #define CREATE_TRACE_POINTS
50 #define TRACE_INCLUDE_PATH instrumentation/events
51 #define TRACE_INCLUDE_FILE lttng-statedump
52 #define LTTNG_INSTRUMENTATION
53 #include <instrumentation/events/lttng-statedump.h>
54
55 LTTNG_DEFINE_TRACE(lttng_statedump_block_device,
56 TP_PROTO(struct lttng_kernel_session *session,
57 dev_t dev, const char *diskname),
58 TP_ARGS(session, dev, diskname));
59
60 LTTNG_DEFINE_TRACE(lttng_statedump_end,
61 TP_PROTO(struct lttng_kernel_session *session),
62 TP_ARGS(session));
63
64 LTTNG_DEFINE_TRACE(lttng_statedump_interrupt,
65 TP_PROTO(struct lttng_kernel_session *session,
66 unsigned int irq, const char *chip_name,
67 struct irqaction *action),
68 TP_ARGS(session, irq, chip_name, action));
69
70 LTTNG_DEFINE_TRACE(lttng_statedump_file_descriptor,
71 TP_PROTO(struct lttng_kernel_session *session,
72 struct files_struct *files,
73 int fd, const char *filename,
74 unsigned int flags, fmode_t fmode),
75 TP_ARGS(session, files, fd, filename, flags, fmode));
76
77 LTTNG_DEFINE_TRACE(lttng_statedump_start,
78 TP_PROTO(struct lttng_kernel_session *session),
79 TP_ARGS(session));
80
81 LTTNG_DEFINE_TRACE(lttng_statedump_process_state,
82 TP_PROTO(struct lttng_kernel_session *session,
83 struct task_struct *p,
84 int type, int mode, int submode, int status,
85 struct files_struct *files),
86 TP_ARGS(session, p, type, mode, submode, status, files));
87
88 LTTNG_DEFINE_TRACE(lttng_statedump_process_pid_ns,
89 TP_PROTO(struct lttng_kernel_session *session,
90 struct task_struct *p,
91 struct pid_namespace *pid_ns),
92 TP_ARGS(session, p, pid_ns));
93
94 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0))
95 LTTNG_DEFINE_TRACE(lttng_statedump_process_cgroup_ns,
96 TP_PROTO(struct lttng_kernel_session *session,
97 struct task_struct *p,
98 struct cgroup_namespace *cgroup_ns),
99 TP_ARGS(session, p, cgroup_ns));
100 #endif
101
102 LTTNG_DEFINE_TRACE(lttng_statedump_process_ipc_ns,
103 TP_PROTO(struct lttng_kernel_session *session,
104 struct task_struct *p,
105 struct ipc_namespace *ipc_ns),
106 TP_ARGS(session, p, ipc_ns));
107
108 #ifndef LTTNG_MNT_NS_MISSING_HEADER
109 LTTNG_DEFINE_TRACE(lttng_statedump_process_mnt_ns,
110 TP_PROTO(struct lttng_kernel_session *session,
111 struct task_struct *p,
112 struct mnt_namespace *mnt_ns),
113 TP_ARGS(session, p, mnt_ns));
114 #endif
115
116 LTTNG_DEFINE_TRACE(lttng_statedump_process_net_ns,
117 TP_PROTO(struct lttng_kernel_session *session,
118 struct task_struct *p,
119 struct net *net_ns),
120 TP_ARGS(session, p, net_ns));
121
122 LTTNG_DEFINE_TRACE(lttng_statedump_process_user_ns,
123 TP_PROTO(struct lttng_kernel_session *session,
124 struct task_struct *p,
125 struct user_namespace *user_ns),
126 TP_ARGS(session, p, user_ns));
127
128 LTTNG_DEFINE_TRACE(lttng_statedump_process_uts_ns,
129 TP_PROTO(struct lttng_kernel_session *session,
130 struct task_struct *p,
131 struct uts_namespace *uts_ns),
132 TP_ARGS(session, p, uts_ns));
133
134 LTTNG_DEFINE_TRACE(lttng_statedump_process_time_ns,
135 TP_PROTO(struct lttng_kernel_session *session,
136 struct task_struct *p,
137 struct time_namespace *time_ns),
138 TP_ARGS(session, p, time_ns));
139
140 LTTNG_DEFINE_TRACE(lttng_statedump_network_interface,
141 TP_PROTO(struct lttng_kernel_session *session,
142 struct net_device *dev, struct in_ifaddr *ifa),
143 TP_ARGS(session, dev, ifa));
144
145 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
146 LTTNG_DEFINE_TRACE(lttng_statedump_cpu_topology,
147 TP_PROTO(struct lttng_kernel_session *session, struct cpuinfo_x86 *c),
148 TP_ARGS(session, c));
149 #endif
150
151 struct lttng_fd_ctx {
152 char *page;
153 struct lttng_kernel_session *session;
154 struct files_struct *files;
155 };
156
157 /*
158 * Protected by the trace lock.
159 */
160 static struct delayed_work cpu_work[NR_CPUS];
161 static DECLARE_WAIT_QUEUE_HEAD(statedump_wq);
162 static atomic_t kernel_threads_to_run;
163
164 enum lttng_thread_type {
165 LTTNG_USER_THREAD = 0,
166 LTTNG_KERNEL_THREAD = 1,
167 };
168
169 enum lttng_execution_mode {
170 LTTNG_USER_MODE = 0,
171 LTTNG_SYSCALL = 1,
172 LTTNG_TRAP = 2,
173 LTTNG_IRQ = 3,
174 LTTNG_SOFTIRQ = 4,
175 LTTNG_MODE_UNKNOWN = 5,
176 };
177
178 enum lttng_execution_submode {
179 LTTNG_NONE = 0,
180 LTTNG_UNKNOWN = 1,
181 };
182
183 enum lttng_process_status {
184 LTTNG_UNNAMED = 0,
185 LTTNG_WAIT_FORK = 1,
186 LTTNG_WAIT_CPU = 2,
187 LTTNG_EXIT = 3,
188 LTTNG_ZOMBIE = 4,
189 LTTNG_WAIT = 5,
190 LTTNG_RUN = 6,
191 LTTNG_DEAD = 7,
192 };
193
194
195 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(6,0,0))
196
197 #define LTTNG_PART_STRUCT_TYPE struct block_device
198
199 static
200 int lttng_get_part_name(struct gendisk *disk, struct block_device *part, char *name_buf)
201 {
202 int ret;
203
204 ret = snprintf(name_buf, BDEVNAME_SIZE, "%pg", part);
205 if (ret < 0 || ret >= BDEVNAME_SIZE)
206 return -ENOSYS;
207
208 return 0;
209 }
210
211 static
212 dev_t lttng_get_part_devt(struct block_device *part)
213 {
214 return part->bd_dev;
215 }
216
217 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,11,0))
218
219 #define LTTNG_PART_STRUCT_TYPE struct block_device
220
221 static
222 int lttng_get_part_name(struct gendisk *disk, struct block_device *part, char *name_buf)
223 {
224 const char *p;
225
226 p = bdevname(part, name_buf);
227 if (!p)
228 return -ENOSYS;
229
230 return 0;
231 }
232
233 static
234 dev_t lttng_get_part_devt(struct block_device *part)
235 {
236 return part->bd_dev;
237 }
238
239 #else
240
241 #define LTTNG_PART_STRUCT_TYPE struct hd_struct
242
243 static
244 int lttng_get_part_name(struct gendisk *disk, struct hd_struct *part, char *name_buf)
245 {
246 const char *p;
247 struct block_device bdev;
248
249 /*
250 * Create a partial 'struct blockdevice' to use
251 * 'bdevname()' which is a simple wrapper over
252 * 'disk_name()' but has the honor to be EXPORT_SYMBOL.
253 */
254 bdev.bd_disk = disk;
255 bdev.bd_part = part;
256
257 p = bdevname(&bdev, name_buf);
258 if (!p)
259 return -ENOSYS;
260
261 return 0;
262 }
263
264 static
265 dev_t lttng_get_part_devt(struct hd_struct *part)
266 {
267 return part_devt(part);
268 }
269 #endif
270
271 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,12,0))
272 static
273 int lttng_statedump_each_block_device(struct lttng_kernel_session *session, struct gendisk *disk)
274 {
275 struct block_device *part;
276 unsigned long idx;
277 int ret = 0;
278
279 /* Include partition 0 */
280 idx = 0;
281
282 rcu_read_lock();
283 xa_for_each(&disk->part_tbl, idx, part) {
284 char name_buf[BDEVNAME_SIZE];
285
286 /* Exclude non-partitions bdev and empty partitions. */
287 if (bdev_is_partition(part) && !bdev_nr_sectors(part))
288 continue;
289
290 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
291 ret = -ENOSYS;
292 goto end;
293 }
294 trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
295 name_buf);
296 }
297 end:
298 rcu_read_unlock();
299 return ret;
300 }
301 #else
302 static
303 int lttng_statedump_each_block_device(struct lttng_kernel_session *session, struct gendisk *disk)
304 {
305 struct disk_part_iter piter;
306 LTTNG_PART_STRUCT_TYPE *part;
307
308 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
309
310 while ((part = disk_part_iter_next(&piter))) {
311 char name_buf[BDEVNAME_SIZE];
312
313 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
314 disk_part_iter_exit(&piter);
315 return -ENOSYS;
316 }
317 trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
318 name_buf);
319 }
320 disk_part_iter_exit(&piter);
321
322 return 0;
323 }
324 #endif
325
326 static
327 int lttng_enumerate_block_devices(struct lttng_kernel_session *session)
328 {
329 struct class *ptr_block_class;
330 struct device_type *ptr_disk_type;
331 struct class_dev_iter iter;
332 struct device *dev;
333 int ret = 0;
334
335 ptr_block_class = wrapper_get_block_class();
336 if (!ptr_block_class) {
337 ret = -ENOSYS;
338 goto end;
339 }
340 ptr_disk_type = wrapper_get_disk_type();
341 if (!ptr_disk_type) {
342 ret = -ENOSYS;
343 goto end;
344 }
345 class_dev_iter_init(&iter, ptr_block_class, NULL, ptr_disk_type);
346 while ((dev = class_dev_iter_next(&iter))) {
347 struct gendisk *disk = dev_to_disk(dev);
348
349 /*
350 * Don't show empty devices or things that have been
351 * suppressed
352 */
353 if (get_capacity(disk) == 0 ||
354 (disk->flags & LTTNG_GENHD_FL_HIDDEN))
355 continue;
356
357 ret = lttng_statedump_each_block_device(session, disk);
358 }
359 class_dev_iter_exit(&iter);
360 end:
361 return ret;
362 }
363
364 #ifdef CONFIG_INET
365
366 static
367 void lttng_enumerate_device(struct lttng_kernel_session *session,
368 struct net_device *dev)
369 {
370 struct in_device *in_dev;
371 struct in_ifaddr *ifa;
372
373 if (dev->flags & IFF_UP) {
374 in_dev = in_dev_get(dev);
375 if (in_dev) {
376 for (ifa = in_dev->ifa_list; ifa != NULL;
377 ifa = ifa->ifa_next) {
378 trace_lttng_statedump_network_interface(
379 session, dev, ifa);
380 }
381 in_dev_put(in_dev);
382 }
383 } else {
384 trace_lttng_statedump_network_interface(
385 session, dev, NULL);
386 }
387 }
388
389 static
390 int lttng_enumerate_network_ip_interface(struct lttng_kernel_session *session)
391 {
392 struct net_device *dev;
393
394 read_lock(&dev_base_lock);
395 for_each_netdev(&init_net, dev)
396 lttng_enumerate_device(session, dev);
397 read_unlock(&dev_base_lock);
398
399 return 0;
400 }
401 #else /* CONFIG_INET */
402 static inline
403 int lttng_enumerate_network_ip_interface(struct lttng_kernel_session *session)
404 {
405 return 0;
406 }
407 #endif /* CONFIG_INET */
408
409 static
410 int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
411 {
412 const struct lttng_fd_ctx *ctx = p;
413 const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
414 unsigned int flags = file->f_flags;
415 struct fdtable *fdt;
416
417 /*
418 * We don't expose kernel internal flags, only userspace-visible
419 * flags.
420 */
421 flags &= ~FMODE_NONOTIFY;
422 fdt = files_fdtable(ctx->files);
423 /*
424 * We need to check here again whether fd is within the fdt
425 * max_fds range, because we might be seeing a different
426 * files_fdtable() than iterate_fd(), assuming only RCU is
427 * protecting the read. In reality, iterate_fd() holds
428 * file_lock, which should ensure the fdt does not change while
429 * the lock is taken, but we are not aware whether this is
430 * guaranteed or not, so play safe.
431 */
432 if (fd < fdt->max_fds && lttng_close_on_exec(fd, fdt))
433 flags |= O_CLOEXEC;
434 if (IS_ERR(s)) {
435 struct dentry *dentry = file->f_path.dentry;
436
437 /* Make sure we give at least some info */
438 spin_lock(&dentry->d_lock);
439 trace_lttng_statedump_file_descriptor(ctx->session,
440 ctx->files, fd, dentry->d_name.name, flags,
441 file->f_mode);
442 spin_unlock(&dentry->d_lock);
443 goto end;
444 }
445 trace_lttng_statedump_file_descriptor(ctx->session,
446 ctx->files, fd, s, flags, file->f_mode);
447 end:
448 return 0;
449 }
450
451 /* Called with task lock held. */
452 static
453 void lttng_enumerate_files(struct lttng_kernel_session *session,
454 struct files_struct *files,
455 char *tmp)
456 {
457 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .files = files, };
458
459 lttng_iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
460 }
461
462 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
463 static
464 int lttng_enumerate_cpu_topology(struct lttng_kernel_session *session)
465 {
466 int cpu;
467 const cpumask_t *cpumask = cpu_possible_mask;
468
469 for (cpu = cpumask_first(cpumask); cpu < nr_cpu_ids;
470 cpu = cpumask_next(cpu, cpumask)) {
471 trace_lttng_statedump_cpu_topology(session, &cpu_data(cpu));
472 }
473
474 return 0;
475 }
476 #else
477 static
478 int lttng_enumerate_cpu_topology(struct lttng_kernel_session *session)
479 {
480 return 0;
481 }
482 #endif
483
484 #if 0
485 /*
486 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
487 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
488 * iteration, but it is not exported to modules.
489 */
490 static
491 void lttng_enumerate_task_vm_maps(struct lttng_kernel_session *session,
492 struct task_struct *p)
493 {
494 struct mm_struct *mm;
495 struct vm_area_struct *map;
496 unsigned long ino;
497
498 /* get_task_mm does a task_lock... */
499 mm = get_task_mm(p);
500 if (!mm)
501 return;
502
503 map = mm->mmap;
504 if (map) {
505 down_read(&mm->mmap_sem);
506 while (map) {
507 if (map->vm_file)
508 ino = map->vm_file->f_path.dentry->d_inode->i_ino;
509 else
510 ino = 0;
511 trace_lttng_statedump_vm_map(session, p, map, ino);
512 map = map->vm_next;
513 }
514 up_read(&mm->mmap_sem);
515 }
516 mmput(mm);
517 }
518
519 static
520 int lttng_enumerate_vm_maps(struct lttng_kernel_session *session)
521 {
522 struct task_struct *p;
523
524 rcu_read_lock();
525 for_each_process(p)
526 lttng_enumerate_task_vm_maps(session, p);
527 rcu_read_unlock();
528 return 0;
529 }
530 #endif
531
532 static
533 int lttng_list_interrupts(struct lttng_kernel_session *session)
534 {
535 unsigned int irq;
536 unsigned long flags = 0;
537 struct irq_desc *desc;
538
539 #define irq_to_desc wrapper_irq_to_desc
540 /* needs irq_desc */
541 for_each_irq_desc(irq, desc) {
542 struct irqaction *action;
543 const char *irq_chip_name =
544 irq_desc_get_chip(desc)->name ? : "unnamed_irq_chip";
545
546 local_irq_save(flags);
547 raw_spin_lock(&desc->lock);
548 for (action = desc->action; action; action = action->next) {
549 trace_lttng_statedump_interrupt(session,
550 irq, irq_chip_name, action);
551 }
552 raw_spin_unlock(&desc->lock);
553 local_irq_restore(flags);
554 }
555 return 0;
556 #undef irq_to_desc
557 }
558
559 /*
560 * Statedump the task's namespaces using the proc filesystem inode number as
561 * the unique identifier. The user and pid ns are nested and will be dumped
562 * recursively.
563 *
564 * Called with task lock held.
565 */
566 static
567 void lttng_statedump_process_ns(struct lttng_kernel_session *session,
568 struct task_struct *p,
569 enum lttng_thread_type type,
570 enum lttng_execution_mode mode,
571 enum lttng_execution_submode submode,
572 enum lttng_process_status status)
573 {
574 struct nsproxy *proxy;
575 struct pid_namespace *pid_ns;
576 struct user_namespace *user_ns;
577
578 /*
579 * The pid and user namespaces are special, they are nested and
580 * accessed with specific functions instead of the nsproxy struct
581 * like the other namespaces.
582 */
583 pid_ns = task_active_pid_ns(p);
584 do {
585 trace_lttng_statedump_process_pid_ns(session, p, pid_ns);
586 pid_ns = pid_ns ? pid_ns->parent : NULL;
587 } while (pid_ns);
588
589
590 user_ns = task_cred_xxx(p, user_ns);
591 do {
592 trace_lttng_statedump_process_user_ns(session, p, user_ns);
593 /*
594 * trace_lttng_statedump_process_user_ns() internally
595 * checks whether user_ns is NULL. While this does not
596 * appear to be a possible return value for
597 * task_cred_xxx(), err on the safe side and check
598 * for NULL here as well to be consistent with the
599 * paranoid behavior of
600 * trace_lttng_statedump_process_user_ns().
601 */
602 user_ns = user_ns ? user_ns->lttng_user_ns_parent : NULL;
603 } while (user_ns);
604
605 /*
606 * Back and forth on locking strategy within Linux upstream for nsproxy.
607 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
608 * "namespaces: Use task_lock and not rcu to protect nsproxy"
609 * for details.
610 */
611 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) || \
612 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
613 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
614 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
615 proxy = p->nsproxy;
616 #else
617 rcu_read_lock();
618 proxy = task_nsproxy(p);
619 #endif
620 if (proxy) {
621 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0))
622 trace_lttng_statedump_process_cgroup_ns(session, p, proxy->cgroup_ns);
623 #endif
624 trace_lttng_statedump_process_ipc_ns(session, p, proxy->ipc_ns);
625 #ifndef LTTNG_MNT_NS_MISSING_HEADER
626 trace_lttng_statedump_process_mnt_ns(session, p, proxy->mnt_ns);
627 #endif
628 trace_lttng_statedump_process_net_ns(session, p, proxy->net_ns);
629 trace_lttng_statedump_process_uts_ns(session, p, proxy->uts_ns);
630 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0) || \
631 LTTNG_RHEL_KERNEL_RANGE(4,18,0,305,0,0, 4,19,0,0,0,0))
632 trace_lttng_statedump_process_time_ns(session, p, proxy->time_ns);
633 #endif
634 }
635 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) || \
636 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
637 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
638 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
639 /* (nothing) */
640 #else
641 rcu_read_unlock();
642 #endif
643 }
644
645 static
646 int lttng_enumerate_process_states(struct lttng_kernel_session *session)
647 {
648 struct task_struct *g, *p;
649 char *tmp;
650
651 tmp = (char *) __get_free_page(GFP_KERNEL);
652 if (!tmp)
653 return -ENOMEM;
654
655 rcu_read_lock();
656 for_each_process(g) {
657 struct files_struct *prev_files = NULL;
658
659 p = g;
660 do {
661 enum lttng_execution_mode mode =
662 LTTNG_MODE_UNKNOWN;
663 enum lttng_execution_submode submode =
664 LTTNG_UNKNOWN;
665 enum lttng_process_status status;
666 enum lttng_thread_type type;
667 struct files_struct *files;
668
669 task_lock(p);
670 if (p->exit_state == EXIT_ZOMBIE)
671 status = LTTNG_ZOMBIE;
672 else if (p->exit_state == EXIT_DEAD)
673 status = LTTNG_DEAD;
674 else if (lttng_task_is_running(p)) {
675 /* Is this a forked child that has not run yet? */
676 if (list_empty(&p->rt.run_list))
677 status = LTTNG_WAIT_FORK;
678 else
679 /*
680 * All tasks are considered as wait_cpu;
681 * the viewer will sort out if the task
682 * was really running at this time.
683 */
684 status = LTTNG_WAIT_CPU;
685 } else if (lttng_get_task_state(p) &
686 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
687 /* Task is waiting for something to complete */
688 status = LTTNG_WAIT;
689 } else
690 status = LTTNG_UNNAMED;
691 submode = LTTNG_NONE;
692
693 /*
694 * Verification of t->mm is to filter out kernel
695 * threads; Viewer will further filter out if a
696 * user-space thread was in syscall mode or not.
697 */
698 if (p->mm)
699 type = LTTNG_USER_THREAD;
700 else
701 type = LTTNG_KERNEL_THREAD;
702 files = p->files;
703
704 trace_lttng_statedump_process_state(session,
705 p, type, mode, submode, status, files);
706 lttng_statedump_process_ns(session,
707 p, type, mode, submode, status);
708 /*
709 * As an optimisation for the common case, do not
710 * repeat information for the same files_struct in
711 * two consecutive threads. This is the common case
712 * for threads sharing the same fd table. RCU guarantees
713 * that the same files_struct pointer is not re-used
714 * throughout processes/threads iteration.
715 */
716 if (files && files != prev_files) {
717 lttng_enumerate_files(session, files, tmp);
718 prev_files = files;
719 }
720 task_unlock(p);
721 } while_each_thread(g, p);
722 }
723 rcu_read_unlock();
724
725 free_page((unsigned long) tmp);
726
727 return 0;
728 }
729
730 static
731 void lttng_statedump_work_func(struct work_struct *work)
732 {
733 if (atomic_dec_and_test(&kernel_threads_to_run))
734 /* If we are the last thread, wake up do_lttng_statedump */
735 wake_up(&statedump_wq);
736 }
737
738 static
739 int do_lttng_statedump(struct lttng_kernel_session *session)
740 {
741 int cpu, ret;
742
743 trace_lttng_statedump_start(session);
744 ret = lttng_enumerate_process_states(session);
745 if (ret)
746 return ret;
747 /*
748 * FIXME
749 * ret = lttng_enumerate_vm_maps(session);
750 * if (ret)
751 * return ret;
752 */
753 ret = lttng_list_interrupts(session);
754 if (ret)
755 return ret;
756 ret = lttng_enumerate_network_ip_interface(session);
757 if (ret)
758 return ret;
759 ret = lttng_enumerate_block_devices(session);
760 switch (ret) {
761 case 0:
762 break;
763 case -ENOSYS:
764 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
765 break;
766 default:
767 return ret;
768 }
769 ret = lttng_enumerate_cpu_topology(session);
770 if (ret)
771 return ret;
772
773 /* TODO lttng_dump_idt_table(session); */
774 /* TODO lttng_dump_softirq_vec(session); */
775 /* TODO lttng_list_modules(session); */
776 /* TODO lttng_dump_swap_files(session); */
777
778 /*
779 * Fire off a work queue on each CPU. Their sole purpose in life
780 * is to guarantee that each CPU has been in a state where is was in
781 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
782 */
783 lttng_cpus_read_lock();
784 atomic_set(&kernel_threads_to_run, num_online_cpus());
785 for_each_online_cpu(cpu) {
786 INIT_DELAYED_WORK(&cpu_work[cpu], lttng_statedump_work_func);
787 schedule_delayed_work_on(cpu, &cpu_work[cpu], 0);
788 }
789 /* Wait for all threads to run */
790 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
791 lttng_cpus_read_unlock();
792 /* Our work is done */
793 trace_lttng_statedump_end(session);
794 return 0;
795 }
796
797 /*
798 * Called with session mutex held.
799 */
800 int lttng_statedump_start(struct lttng_kernel_session *session)
801 {
802 return do_lttng_statedump(session);
803 }
804 EXPORT_SYMBOL_GPL(lttng_statedump_start);
805
806 static
807 int __init lttng_statedump_init(void)
808 {
809 /*
810 * Allow module to load even if the fixup cannot be done. This
811 * will allow seemless transition when the underlying issue fix
812 * is merged into the Linux kernel, and when tracepoint.c
813 * "tracepoint_module_notify" is turned into a static function.
814 */
815 (void) wrapper_lttng_fixup_sig(THIS_MODULE);
816 return 0;
817 }
818
819 module_init(lttng_statedump_init);
820
821 static
822 void __exit lttng_statedump_exit(void)
823 {
824 }
825
826 module_exit(lttng_statedump_exit);
827
828 MODULE_LICENSE("GPL and additional rights");
829 MODULE_AUTHOR("Jean-Hugues Deschenes");
830 MODULE_DESCRIPTION("LTTng statedump provider");
831 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
832 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
833 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
834 LTTNG_MODULES_EXTRAVERSION);
This page took 0.044986 seconds and 5 git commands to generate.