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