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