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