page alloc wrapper: Fix get_pfnblock_flags_mask prototype
[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 <lttng/events-internal.h>
40 #include <wrapper/cpu.h>
41 #include <wrapper/irqdesc.h>
42 #include <wrapper/fdtable.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 LTTNG_RHEL_KERNEL_RANGE(5,14,0,163,0,0, 5,15,0,0,0,0))
197
198 #define LTTNG_PART_STRUCT_TYPE struct block_device
199
200 static
201 int lttng_get_part_name(struct gendisk *disk, struct block_device *part, char *name_buf)
202 {
203 int ret;
204
205 ret = snprintf(name_buf, BDEVNAME_SIZE, "%pg", part);
206 if (ret < 0 || ret >= BDEVNAME_SIZE)
207 return -ENOSYS;
208
209 return 0;
210 }
211
212 static
213 dev_t lttng_get_part_devt(struct block_device *part)
214 {
215 return part->bd_dev;
216 }
217
218 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,11,0))
219
220 #define LTTNG_PART_STRUCT_TYPE struct block_device
221
222 static
223 int lttng_get_part_name(struct gendisk *disk, struct block_device *part, char *name_buf)
224 {
225 const char *p;
226
227 p = bdevname(part, name_buf);
228 if (!p)
229 return -ENOSYS;
230
231 return 0;
232 }
233
234 static
235 dev_t lttng_get_part_devt(struct block_device *part)
236 {
237 return part->bd_dev;
238 }
239
240 #else
241
242 #define LTTNG_PART_STRUCT_TYPE struct hd_struct
243
244 static
245 int lttng_get_part_name(struct gendisk *disk, struct hd_struct *part, char *name_buf)
246 {
247 const char *p;
248 struct block_device bdev;
249
250 /*
251 * Create a partial 'struct blockdevice' to use
252 * 'bdevname()' which is a simple wrapper over
253 * 'disk_name()' but has the honor to be EXPORT_SYMBOL.
254 */
255 bdev.bd_disk = disk;
256 bdev.bd_part = part;
257
258 p = bdevname(&bdev, name_buf);
259 if (!p)
260 return -ENOSYS;
261
262 return 0;
263 }
264
265 static
266 dev_t lttng_get_part_devt(struct hd_struct *part)
267 {
268 return part_devt(part);
269 }
270 #endif
271
272 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,12,0))
273 static
274 int lttng_statedump_each_block_device(struct lttng_kernel_session *session, struct gendisk *disk)
275 {
276 struct block_device *part;
277 unsigned long idx;
278 int ret = 0;
279
280 /* Include partition 0 */
281 idx = 0;
282
283 rcu_read_lock();
284 xa_for_each(&disk->part_tbl, idx, part) {
285 char name_buf[BDEVNAME_SIZE];
286
287 /* Exclude non-partitions bdev and empty partitions. */
288 if (bdev_is_partition(part) && !bdev_nr_sectors(part))
289 continue;
290
291 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
292 ret = -ENOSYS;
293 goto end;
294 }
295 trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
296 name_buf);
297 }
298 end:
299 rcu_read_unlock();
300 return ret;
301 }
302 #else
303 static
304 int lttng_statedump_each_block_device(struct lttng_kernel_session *session, struct gendisk *disk)
305 {
306 struct disk_part_iter piter;
307 LTTNG_PART_STRUCT_TYPE *part;
308
309 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
310
311 while ((part = disk_part_iter_next(&piter))) {
312 char name_buf[BDEVNAME_SIZE];
313
314 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
315 disk_part_iter_exit(&piter);
316 return -ENOSYS;
317 }
318 trace_lttng_statedump_block_device(session, lttng_get_part_devt(part),
319 name_buf);
320 }
321 disk_part_iter_exit(&piter);
322
323 return 0;
324 }
325 #endif
326
327 static
328 int lttng_enumerate_block_devices(struct lttng_kernel_session *session)
329 {
330 struct class *ptr_block_class;
331 struct device_type *ptr_disk_type;
332 struct class_dev_iter iter;
333 struct device *dev;
334 int ret = 0;
335
336 ptr_block_class = wrapper_get_block_class();
337 if (!ptr_block_class) {
338 ret = -ENOSYS;
339 goto end;
340 }
341 ptr_disk_type = wrapper_get_disk_type();
342 if (!ptr_disk_type) {
343 ret = -ENOSYS;
344 goto end;
345 }
346 class_dev_iter_init(&iter, ptr_block_class, NULL, ptr_disk_type);
347 while ((dev = class_dev_iter_next(&iter))) {
348 struct gendisk *disk = dev_to_disk(dev);
349
350 /*
351 * Don't show empty devices or things that have been
352 * suppressed
353 */
354 if (get_capacity(disk) == 0 ||
355 (disk->flags & LTTNG_GENHD_FL_HIDDEN))
356 continue;
357
358 ret = lttng_statedump_each_block_device(session, disk);
359 }
360 class_dev_iter_exit(&iter);
361 end:
362 return ret;
363 }
364
365 #ifdef CONFIG_INET
366
367 static
368 void lttng_enumerate_device(struct lttng_kernel_session *session,
369 struct net_device *dev)
370 {
371 struct in_device *in_dev;
372 struct in_ifaddr *ifa;
373
374 if (dev->flags & IFF_UP) {
375 in_dev = in_dev_get(dev);
376 if (in_dev) {
377 for (ifa = in_dev->ifa_list; ifa != NULL;
378 ifa = ifa->ifa_next) {
379 trace_lttng_statedump_network_interface(
380 session, dev, ifa);
381 }
382 in_dev_put(in_dev);
383 }
384 } else {
385 trace_lttng_statedump_network_interface(
386 session, dev, NULL);
387 }
388 }
389
390 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(6,9,0))
391 static
392 int lttng_enumerate_network_ip_interface(struct lttng_kernel_session *session)
393 {
394 struct net_device *dev;
395
396 rtnl_lock();
397 for_each_netdev(&init_net, dev)
398 lttng_enumerate_device(session, dev);
399 rtnl_unlock();
400
401 return 0;
402 }
403 #else
404 static
405 int lttng_enumerate_network_ip_interface(struct lttng_kernel_session *session)
406 {
407 struct net_device *dev;
408
409 read_lock(&dev_base_lock);
410 for_each_netdev(&init_net, dev)
411 lttng_enumerate_device(session, dev);
412 read_unlock(&dev_base_lock);
413
414 return 0;
415 }
416 #endif /* (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(6,9,0)) */
417 #else /* CONFIG_INET */
418 static inline
419 int lttng_enumerate_network_ip_interface(struct lttng_kernel_session *session)
420 {
421 return 0;
422 }
423 #endif /* CONFIG_INET */
424
425 static
426 int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
427 {
428 const struct lttng_fd_ctx *ctx = p;
429 const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
430 unsigned int flags = file->f_flags;
431 struct fdtable *fdt;
432
433 /*
434 * We don't expose kernel internal flags, only userspace-visible
435 * flags.
436 */
437 flags &= ~FMODE_NONOTIFY;
438 fdt = files_fdtable(ctx->files);
439 /*
440 * We need to check here again whether fd is within the fdt
441 * max_fds range, because we might be seeing a different
442 * files_fdtable() than iterate_fd(), assuming only RCU is
443 * protecting the read. In reality, iterate_fd() holds
444 * file_lock, which should ensure the fdt does not change while
445 * the lock is taken, but we are not aware whether this is
446 * guaranteed or not, so play safe.
447 */
448 if (fd < fdt->max_fds && close_on_exec(fd, fdt))
449 flags |= O_CLOEXEC;
450 if (IS_ERR(s)) {
451 struct dentry *dentry = file->f_path.dentry;
452
453 /* Make sure we give at least some info */
454 spin_lock(&dentry->d_lock);
455 trace_lttng_statedump_file_descriptor(ctx->session,
456 ctx->files, fd, dentry->d_name.name, flags,
457 file->f_mode);
458 spin_unlock(&dentry->d_lock);
459 goto end;
460 }
461 trace_lttng_statedump_file_descriptor(ctx->session,
462 ctx->files, fd, s, flags, file->f_mode);
463 end:
464 return 0;
465 }
466
467 /* Called with task lock held. */
468 static
469 void lttng_enumerate_files(struct lttng_kernel_session *session,
470 struct files_struct *files,
471 char *tmp)
472 {
473 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .files = files, };
474
475 iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
476 }
477
478 #ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
479 static
480 int lttng_enumerate_cpu_topology(struct lttng_kernel_session *session)
481 {
482 int cpu;
483 const cpumask_t *cpumask = cpu_possible_mask;
484
485 for (cpu = cpumask_first(cpumask); cpu < nr_cpu_ids;
486 cpu = cpumask_next(cpu, cpumask)) {
487 trace_lttng_statedump_cpu_topology(session, &cpu_data(cpu));
488 }
489
490 return 0;
491 }
492 #else
493 static
494 int lttng_enumerate_cpu_topology(struct lttng_kernel_session *session)
495 {
496 return 0;
497 }
498 #endif
499
500 #if 0
501 /*
502 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
503 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
504 * iteration, but it is not exported to modules.
505 */
506 static
507 void lttng_enumerate_task_vm_maps(struct lttng_kernel_session *session,
508 struct task_struct *p)
509 {
510 struct mm_struct *mm;
511 struct vm_area_struct *map;
512 unsigned long ino;
513
514 /* get_task_mm does a task_lock... */
515 mm = get_task_mm(p);
516 if (!mm)
517 return;
518
519 map = mm->mmap;
520 if (map) {
521 down_read(&mm->mmap_sem);
522 while (map) {
523 if (map->vm_file)
524 ino = map->vm_file->f_path.dentry->d_inode->i_ino;
525 else
526 ino = 0;
527 trace_lttng_statedump_vm_map(session, p, map, ino);
528 map = map->vm_next;
529 }
530 up_read(&mm->mmap_sem);
531 }
532 mmput(mm);
533 }
534
535 static
536 int lttng_enumerate_vm_maps(struct lttng_kernel_session *session)
537 {
538 struct task_struct *p;
539
540 rcu_read_lock();
541 for_each_process(p)
542 lttng_enumerate_task_vm_maps(session, p);
543 rcu_read_unlock();
544 return 0;
545 }
546 #endif
547
548 static
549 int lttng_list_interrupts(struct lttng_kernel_session *session)
550 {
551 unsigned int irq;
552 unsigned long flags = 0;
553 struct irq_desc *desc;
554
555 #define irq_to_desc wrapper_irq_to_desc
556 /* needs irq_desc */
557 for_each_irq_desc(irq, desc) {
558 struct irqaction *action;
559 const char *irq_chip_name =
560 irq_desc_get_chip(desc)->name ? : "unnamed_irq_chip";
561
562 local_irq_save(flags);
563 raw_spin_lock(&desc->lock);
564 for (action = desc->action; action; action = action->next) {
565 trace_lttng_statedump_interrupt(session,
566 irq, irq_chip_name, action);
567 }
568 raw_spin_unlock(&desc->lock);
569 local_irq_restore(flags);
570 }
571 return 0;
572 #undef irq_to_desc
573 }
574
575 /*
576 * Statedump the task's namespaces using the proc filesystem inode number as
577 * the unique identifier. The user and pid ns are nested and will be dumped
578 * recursively.
579 *
580 * Called with task lock held.
581 */
582 static
583 void lttng_statedump_process_ns(struct lttng_kernel_session *session,
584 struct task_struct *p,
585 enum lttng_thread_type type,
586 enum lttng_execution_mode mode,
587 enum lttng_execution_submode submode,
588 enum lttng_process_status status)
589 {
590 struct nsproxy *proxy;
591 struct pid_namespace *pid_ns;
592 struct user_namespace *user_ns;
593
594 /*
595 * The pid and user namespaces are special, they are nested and
596 * accessed with specific functions instead of the nsproxy struct
597 * like the other namespaces.
598 */
599 pid_ns = task_active_pid_ns(p);
600 do {
601 trace_lttng_statedump_process_pid_ns(session, p, pid_ns);
602 pid_ns = pid_ns ? pid_ns->parent : NULL;
603 } while (pid_ns);
604
605
606 user_ns = task_cred_xxx(p, user_ns);
607 do {
608 trace_lttng_statedump_process_user_ns(session, p, user_ns);
609 /*
610 * trace_lttng_statedump_process_user_ns() internally
611 * checks whether user_ns is NULL. While this does not
612 * appear to be a possible return value for
613 * task_cred_xxx(), err on the safe side and check
614 * for NULL here as well to be consistent with the
615 * paranoid behavior of
616 * trace_lttng_statedump_process_user_ns().
617 */
618 user_ns = user_ns ? user_ns->parent : NULL;
619 } while (user_ns);
620
621 proxy = p->nsproxy;
622
623 if (proxy) {
624 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0))
625 trace_lttng_statedump_process_cgroup_ns(session, p, proxy->cgroup_ns);
626 #endif
627 trace_lttng_statedump_process_ipc_ns(session, p, proxy->ipc_ns);
628 #ifndef LTTNG_MNT_NS_MISSING_HEADER
629 trace_lttng_statedump_process_mnt_ns(session, p, proxy->mnt_ns);
630 #endif
631 trace_lttng_statedump_process_net_ns(session, p, proxy->net_ns);
632 trace_lttng_statedump_process_uts_ns(session, p, proxy->uts_ns);
633 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0) || \
634 LTTNG_RHEL_KERNEL_RANGE(4,18,0,305,0,0, 4,19,0,0,0,0))
635 trace_lttng_statedump_process_time_ns(session, p, proxy->time_ns);
636 #endif
637 }
638 }
639
640 static
641 int lttng_enumerate_process_states(struct lttng_kernel_session *session)
642 {
643 struct task_struct *g, *p;
644 char *tmp;
645
646 tmp = (char *) __get_free_page(GFP_KERNEL);
647 if (!tmp)
648 return -ENOMEM;
649
650 rcu_read_lock();
651 for_each_process(g) {
652 struct files_struct *prev_files = NULL;
653
654 p = g;
655 do {
656 enum lttng_execution_mode mode =
657 LTTNG_MODE_UNKNOWN;
658 enum lttng_execution_submode submode =
659 LTTNG_UNKNOWN;
660 enum lttng_process_status status;
661 enum lttng_thread_type type;
662 struct files_struct *files;
663
664 task_lock(p);
665 if (p->exit_state == EXIT_ZOMBIE)
666 status = LTTNG_ZOMBIE;
667 else if (p->exit_state == EXIT_DEAD)
668 status = LTTNG_DEAD;
669 else if (lttng_task_is_running(p)) {
670 /* Is this a forked child that has not run yet? */
671 if (list_empty(&p->rt.run_list))
672 status = LTTNG_WAIT_FORK;
673 else
674 /*
675 * All tasks are considered as wait_cpu;
676 * the viewer will sort out if the task
677 * was really running at this time.
678 */
679 status = LTTNG_WAIT_CPU;
680 } else if (lttng_get_task_state(p) &
681 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
682 /* Task is waiting for something to complete */
683 status = LTTNG_WAIT;
684 } else
685 status = LTTNG_UNNAMED;
686 submode = LTTNG_NONE;
687
688 /*
689 * Verification of t->mm is to filter out kernel
690 * threads; Viewer will further filter out if a
691 * user-space thread was in syscall mode or not.
692 */
693 if (p->mm)
694 type = LTTNG_USER_THREAD;
695 else
696 type = LTTNG_KERNEL_THREAD;
697 files = p->files;
698
699 trace_lttng_statedump_process_state(session,
700 p, type, mode, submode, status, files);
701 lttng_statedump_process_ns(session,
702 p, type, mode, submode, status);
703 /*
704 * As an optimisation for the common case, do not
705 * repeat information for the same files_struct in
706 * two consecutive threads. This is the common case
707 * for threads sharing the same fd table. RCU guarantees
708 * that the same files_struct pointer is not re-used
709 * throughout processes/threads iteration.
710 */
711 if (files && files != prev_files) {
712 lttng_enumerate_files(session, files, tmp);
713 prev_files = files;
714 }
715 task_unlock(p);
716 } while_each_thread(g, p);
717 }
718 rcu_read_unlock();
719
720 free_page((unsigned long) tmp);
721
722 return 0;
723 }
724
725 static
726 void lttng_statedump_work_func(struct work_struct *work)
727 {
728 if (atomic_dec_and_test(&kernel_threads_to_run))
729 /* If we are the last thread, wake up do_lttng_statedump */
730 wake_up(&statedump_wq);
731 }
732
733 static
734 int do_lttng_statedump(struct lttng_kernel_session *session)
735 {
736 int cpu, ret;
737
738 trace_lttng_statedump_start(session);
739 ret = lttng_enumerate_process_states(session);
740 if (ret)
741 return ret;
742 /*
743 * FIXME
744 * ret = lttng_enumerate_vm_maps(session);
745 * if (ret)
746 * return ret;
747 */
748 ret = lttng_list_interrupts(session);
749 if (ret)
750 return ret;
751 ret = lttng_enumerate_network_ip_interface(session);
752 if (ret)
753 return ret;
754 ret = lttng_enumerate_block_devices(session);
755 switch (ret) {
756 case 0:
757 break;
758 case -ENOSYS:
759 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
760 break;
761 default:
762 return ret;
763 }
764 ret = lttng_enumerate_cpu_topology(session);
765 if (ret)
766 return ret;
767
768 /* TODO lttng_dump_idt_table(session); */
769 /* TODO lttng_dump_softirq_vec(session); */
770 /* TODO lttng_list_modules(session); */
771 /* TODO lttng_dump_swap_files(session); */
772
773 /*
774 * Fire off a work queue on each CPU. Their sole purpose in life
775 * is to guarantee that each CPU has been in a state where is was in
776 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
777 */
778 lttng_cpus_read_lock();
779 atomic_set(&kernel_threads_to_run, num_online_cpus());
780 for_each_online_cpu(cpu) {
781 INIT_DELAYED_WORK(&cpu_work[cpu], lttng_statedump_work_func);
782 schedule_delayed_work_on(cpu, &cpu_work[cpu], 0);
783 }
784 /* Wait for all threads to run */
785 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
786 lttng_cpus_read_unlock();
787 /* Our work is done */
788 trace_lttng_statedump_end(session);
789 return 0;
790 }
791
792 /*
793 * Called with session mutex held.
794 */
795 int lttng_statedump_start(struct lttng_kernel_session *session)
796 {
797 return do_lttng_statedump(session);
798 }
799 EXPORT_SYMBOL_GPL(lttng_statedump_start);
800
801 static
802 int __init lttng_statedump_init(void)
803 {
804 return 0;
805 }
806
807 module_init(lttng_statedump_init);
808
809 static
810 void __exit lttng_statedump_exit(void)
811 {
812 }
813
814 module_exit(lttng_statedump_exit);
815
816 MODULE_LICENSE("GPL and additional rights");
817 MODULE_AUTHOR("Jean-Hugues Deschenes");
818 MODULE_DESCRIPTION("LTTng statedump provider");
819 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
820 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
821 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
822 LTTNG_MODULES_EXTRAVERSION);
This page took 0.044953 seconds and 5 git commands to generate.