fix: block: add a disk_uevent helper (v5.12)
[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>
26#include <linux/cpu.h>
27#include <linux/netdevice.h>
28#include <linux/inetdevice.h>
29#include <linux/sched.h>
30#include <linux/mm.h>
c337ddc2
MD
31#include <linux/swap.h>
32#include <linux/wait.h>
33#include <linux/mutex.h>
f0dbdefb 34#include <linux/device.h>
c337ddc2 35
241ae9a8
MD
36#include <lttng-events.h>
37#include <lttng-tracer.h>
38#include <wrapper/irqdesc.h>
241ae9a8 39#include <wrapper/fdtable.h>
1965e6b4 40#include <wrapper/namespace.h>
241ae9a8
MD
41#include <wrapper/irq.h>
42#include <wrapper/tracepoint.h>
43#include <wrapper/genhd.h>
44#include <wrapper/file.h>
518dba2d 45#include <wrapper/fdtable.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
f0dbdefb
HD
246static
247int lttng_enumerate_block_devices(struct lttng_session *session)
248{
249 struct class *ptr_block_class;
250 struct device_type *ptr_disk_type;
251 struct class_dev_iter iter;
252 struct device *dev;
d8933959 253 int ret = 0;
f0dbdefb
HD
254
255 ptr_block_class = wrapper_get_block_class();
d8933959
MJ
256 if (!ptr_block_class) {
257 ret = -ENOSYS;
258 goto end;
259 }
f0dbdefb
HD
260 ptr_disk_type = wrapper_get_disk_type();
261 if (!ptr_disk_type) {
d8933959
MJ
262 ret = -ENOSYS;
263 goto end;
f0dbdefb
HD
264 }
265 class_dev_iter_init(&iter, ptr_block_class, NULL, ptr_disk_type);
266 while ((dev = class_dev_iter_next(&iter))) {
267 struct disk_part_iter piter;
268 struct gendisk *disk = dev_to_disk(dev);
c2d5363f 269 LTTNG_PART_STRUCT_TYPE *part;
f0dbdefb 270
5a91f3df
MD
271 /*
272 * Don't show empty devices or things that have been
273 * suppressed
274 */
275 if (get_capacity(disk) == 0 ||
276 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
277 continue;
278
d8933959
MJ
279 /*
280 * The original 'disk_part_iter_init' returns void, but our
281 * wrapper can fail to lookup the original symbol.
282 */
283 if (wrapper_disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0) < 0) {
284 ret = -ENOSYS;
285 goto iter_exit;
286 }
287
288 while ((part = wrapper_disk_part_iter_next(&piter))) {
f0dbdefb 289 char name_buf[BDEVNAME_SIZE];
c2d5363f
MJ
290
291 if (lttng_get_part_name(disk, part, name_buf) == -ENOSYS) {
d8933959
MJ
292 wrapper_disk_part_iter_exit(&piter);
293 ret = -ENOSYS;
294 goto iter_exit;
f0dbdefb
HD
295 }
296 trace_lttng_statedump_block_device(session,
c2d5363f 297 lttng_get_part_devt(part), name_buf);
f0dbdefb 298 }
d8933959 299 wrapper_disk_part_iter_exit(&piter);
f0dbdefb 300 }
d8933959 301iter_exit:
f0dbdefb 302 class_dev_iter_exit(&iter);
d8933959
MJ
303end:
304 return ret;
f0dbdefb
HD
305}
306
c337ddc2 307#ifdef CONFIG_INET
f0dbdefb 308
c337ddc2
MD
309static
310void lttng_enumerate_device(struct lttng_session *session,
311 struct net_device *dev)
312{
313 struct in_device *in_dev;
314 struct in_ifaddr *ifa;
315
316 if (dev->flags & IFF_UP) {
317 in_dev = in_dev_get(dev);
318 if (in_dev) {
319 for (ifa = in_dev->ifa_list; ifa != NULL;
320 ifa = ifa->ifa_next) {
321 trace_lttng_statedump_network_interface(
322 session, dev, ifa);
323 }
324 in_dev_put(in_dev);
325 }
326 } else {
327 trace_lttng_statedump_network_interface(
328 session, dev, NULL);
329 }
330}
331
332static
333int lttng_enumerate_network_ip_interface(struct lttng_session *session)
334{
335 struct net_device *dev;
336
337 read_lock(&dev_base_lock);
338 for_each_netdev(&init_net, dev)
339 lttng_enumerate_device(session, dev);
340 read_unlock(&dev_base_lock);
341
342 return 0;
343}
344#else /* CONFIG_INET */
345static inline
346int lttng_enumerate_network_ip_interface(struct lttng_session *session)
347{
348 return 0;
349}
350#endif /* CONFIG_INET */
351
361c023a
MD
352static
353int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
354{
355 const struct lttng_fd_ctx *ctx = p;
356 const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
29021503 357 unsigned int flags = file->f_flags;
d561ecfb 358 struct fdtable *fdt;
361c023a 359
29021503
MD
360 /*
361 * We don't expose kernel internal flags, only userspace-visible
362 * flags.
363 */
364 flags &= ~FMODE_NONOTIFY;
d561ecfb
MD
365 fdt = files_fdtable(ctx->files);
366 /*
367 * We need to check here again whether fd is within the fdt
368 * max_fds range, because we might be seeing a different
369 * files_fdtable() than iterate_fd(), assuming only RCU is
370 * protecting the read. In reality, iterate_fd() holds
371 * file_lock, which should ensure the fdt does not change while
372 * the lock is taken, but we are not aware whether this is
373 * guaranteed or not, so play safe.
374 */
aa29f2d3 375 if (fd < fdt->max_fds && lttng_close_on_exec(fd, fdt))
29021503 376 flags |= O_CLOEXEC;
361c023a
MD
377 if (IS_ERR(s)) {
378 struct dentry *dentry = file->f_path.dentry;
379
380 /* Make sure we give at least some info */
381 spin_lock(&dentry->d_lock);
df493bfd
MD
382 trace_lttng_statedump_file_descriptor(ctx->session,
383 ctx->files, fd, dentry->d_name.name, flags,
384 file->f_mode);
361c023a
MD
385 spin_unlock(&dentry->d_lock);
386 goto end;
387 }
df493bfd
MD
388 trace_lttng_statedump_file_descriptor(ctx->session,
389 ctx->files, fd, s, flags, file->f_mode);
361c023a
MD
390end:
391 return 0;
392}
c337ddc2 393
df493bfd 394/* Called with task lock held. */
c337ddc2 395static
df493bfd
MD
396void lttng_enumerate_files(struct lttng_session *session,
397 struct files_struct *files,
398 char *tmp)
c337ddc2 399{
df493bfd 400 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .files = files, };
c337ddc2 401
d561ecfb 402 lttng_iterate_fd(files, 0, lttng_dump_one_fd, &ctx);
c337ddc2
MD
403}
404
d0b55e4c 405#ifdef LTTNG_HAVE_STATEDUMP_CPU_TOPOLOGY
502e4132
JD
406static
407int lttng_enumerate_cpu_topology(struct lttng_session *session)
408{
409 int cpu;
410 const cpumask_t *cpumask = cpu_possible_mask;
411
412 for (cpu = cpumask_first(cpumask); cpu < nr_cpu_ids;
413 cpu = cpumask_next(cpu, cpumask)) {
414 trace_lttng_statedump_cpu_topology(session, &cpu_data(cpu));
415 }
416
417 return 0;
418}
419#else
420static
421int lttng_enumerate_cpu_topology(struct lttng_session *session)
422{
423 return 0;
424}
425#endif
426
0658bdda
MD
427#if 0
428/*
429 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
430 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
431 * iteration, but it is not exported to modules.
432 */
c337ddc2
MD
433static
434void lttng_enumerate_task_vm_maps(struct lttng_session *session,
435 struct task_struct *p)
436{
437 struct mm_struct *mm;
438 struct vm_area_struct *map;
439 unsigned long ino;
440
441 /* get_task_mm does a task_lock... */
442 mm = get_task_mm(p);
443 if (!mm)
444 return;
445
446 map = mm->mmap;
447 if (map) {
448 down_read(&mm->mmap_sem);
449 while (map) {
450 if (map->vm_file)
b06ed645 451 ino = map->vm_file->lttng_f_dentry->d_inode->i_ino;
c337ddc2
MD
452 else
453 ino = 0;
454 trace_lttng_statedump_vm_map(session, p, map, ino);
455 map = map->vm_next;
456 }
457 up_read(&mm->mmap_sem);
458 }
459 mmput(mm);
460}
461
462static
463int lttng_enumerate_vm_maps(struct lttng_session *session)
464{
465 struct task_struct *p;
466
467 rcu_read_lock();
468 for_each_process(p)
469 lttng_enumerate_task_vm_maps(session, p);
470 rcu_read_unlock();
471 return 0;
472}
0658bdda 473#endif
c337ddc2 474
29784493 475#ifdef CONFIG_LTTNG_HAS_LIST_IRQ
47faec4b 476
c337ddc2 477static
cfcee1c7 478int lttng_list_interrupts(struct lttng_session *session)
c337ddc2
MD
479{
480 unsigned int irq;
481 unsigned long flags = 0;
482 struct irq_desc *desc;
483
484#define irq_to_desc wrapper_irq_to_desc
485 /* needs irq_desc */
486 for_each_irq_desc(irq, desc) {
487 struct irqaction *action;
488 const char *irq_chip_name =
489 irq_desc_get_chip(desc)->name ? : "unnamed_irq_chip";
490
491 local_irq_save(flags);
fc94c945 492 raw_spin_lock(&desc->lock);
c337ddc2
MD
493 for (action = desc->action; action; action = action->next) {
494 trace_lttng_statedump_interrupt(session,
495 irq, irq_chip_name, action);
496 }
fc94c945 497 raw_spin_unlock(&desc->lock);
c337ddc2
MD
498 local_irq_restore(flags);
499 }
cfcee1c7 500 return 0;
c337ddc2
MD
501#undef irq_to_desc
502}
503#else
504static inline
cfcee1c7 505int lttng_list_interrupts(struct lttng_session *session)
c337ddc2 506{
cfcee1c7 507 return 0;
c337ddc2
MD
508}
509#endif
510
4ba1f53c 511/*
1965e6b4
MJ
512 * Statedump the task's namespaces using the proc filesystem inode number as
513 * the unique identifier. The user and pid ns are nested and will be dumped
514 * recursively.
515 *
4ba1f53c
MD
516 * Called with task lock held.
517 */
73e8ba37
JD
518static
519void lttng_statedump_process_ns(struct lttng_session *session,
520 struct task_struct *p,
521 enum lttng_thread_type type,
522 enum lttng_execution_mode mode,
523 enum lttng_execution_submode submode,
524 enum lttng_process_status status)
525{
1965e6b4 526 struct nsproxy *proxy;
73e8ba37 527 struct pid_namespace *pid_ns;
1965e6b4 528 struct user_namespace *user_ns;
73e8ba37 529
1965e6b4
MJ
530 /*
531 * The pid and user namespaces are special, they are nested and
532 * accessed with specific functions instead of the nsproxy struct
533 * like the other namespaces.
534 */
887bcdac
MJ
535 pid_ns = task_active_pid_ns(p);
536 do {
1965e6b4 537 trace_lttng_statedump_process_pid_ns(session, p, pid_ns);
51831abd 538 pid_ns = pid_ns ? pid_ns->parent : NULL;
887bcdac 539 } while (pid_ns);
1965e6b4
MJ
540
541
542 user_ns = task_cred_xxx(p, user_ns);
543 do {
544 trace_lttng_statedump_process_user_ns(session, p, user_ns);
acdd4850
MD
545 /*
546 * trace_lttng_statedump_process_user_ns() internally
547 * checks whether user_ns is NULL. While this does not
548 * appear to be a possible return value for
549 * task_cred_xxx(), err on the safe side and check
550 * for NULL here as well to be consistent with the
551 * paranoid behavior of
552 * trace_lttng_statedump_process_user_ns().
553 */
554 user_ns = user_ns ? user_ns->lttng_user_ns_parent : NULL;
1965e6b4
MJ
555 } while (user_ns);
556
557 /*
558 * Back and forth on locking strategy within Linux upstream for nsproxy.
559 * See Linux upstream commit 728dba3a39c66b3d8ac889ddbe38b5b1c264aec3
560 * "namespaces: Use task_lock and not rcu to protect nsproxy"
561 * for details.
562 */
2d042821 563#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) || \
1965e6b4
MJ
564 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
565 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
566 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
567 proxy = p->nsproxy;
568#else
569 rcu_read_lock();
570 proxy = task_nsproxy(p);
571#endif
572 if (proxy) {
2d042821 573#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,6,0))
1965e6b4
MJ
574 trace_lttng_statedump_process_cgroup_ns(session, p, proxy->cgroup_ns);
575#endif
576 trace_lttng_statedump_process_ipc_ns(session, p, proxy->ipc_ns);
577#ifndef LTTNG_MNT_NS_MISSING_HEADER
578 trace_lttng_statedump_process_mnt_ns(session, p, proxy->mnt_ns);
579#endif
580 trace_lttng_statedump_process_net_ns(session, p, proxy->net_ns);
581 trace_lttng_statedump_process_uts_ns(session, p, proxy->uts_ns);
582 }
2d042821 583#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) || \
1965e6b4
MJ
584 LTTNG_UBUNTU_KERNEL_RANGE(3,13,11,36, 3,14,0,0) || \
585 LTTNG_UBUNTU_KERNEL_RANGE(3,16,1,11, 3,17,0,0) || \
586 LTTNG_RHEL_KERNEL_RANGE(3,10,0,229,13,0, 3,11,0,0,0,0))
587 /* (nothing) */
588#else
589 rcu_read_unlock();
590#endif
73e8ba37
JD
591}
592
c337ddc2
MD
593static
594int lttng_enumerate_process_states(struct lttng_session *session)
595{
596 struct task_struct *g, *p;
df493bfd
MD
597 char *tmp;
598
599 tmp = (char *) __get_free_page(GFP_KERNEL);
600 if (!tmp)
601 return -ENOMEM;
c337ddc2
MD
602
603 rcu_read_lock();
604 for_each_process(g) {
df493bfd
MD
605 struct files_struct *prev_files = NULL;
606
c337ddc2
MD
607 p = g;
608 do {
609 enum lttng_execution_mode mode =
610 LTTNG_MODE_UNKNOWN;
611 enum lttng_execution_submode submode =
612 LTTNG_UNKNOWN;
613 enum lttng_process_status status;
614 enum lttng_thread_type type;
df493bfd 615 struct files_struct *files;
c337ddc2
MD
616
617 task_lock(p);
618 if (p->exit_state == EXIT_ZOMBIE)
619 status = LTTNG_ZOMBIE;
620 else if (p->exit_state == EXIT_DEAD)
621 status = LTTNG_DEAD;
622 else if (p->state == TASK_RUNNING) {
623 /* Is this a forked child that has not run yet? */
624 if (list_empty(&p->rt.run_list))
625 status = LTTNG_WAIT_FORK;
626 else
627 /*
628 * All tasks are considered as wait_cpu;
629 * the viewer will sort out if the task
630 * was really running at this time.
631 */
632 status = LTTNG_WAIT_CPU;
633 } else if (p->state &
634 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
635 /* Task is waiting for something to complete */
636 status = LTTNG_WAIT;
637 } else
638 status = LTTNG_UNNAMED;
639 submode = LTTNG_NONE;
640
641 /*
642 * Verification of t->mm is to filter out kernel
643 * threads; Viewer will further filter out if a
644 * user-space thread was in syscall mode or not.
645 */
646 if (p->mm)
647 type = LTTNG_USER_THREAD;
648 else
649 type = LTTNG_KERNEL_THREAD;
df493bfd 650 files = p->files;
d2a927ac
MJ
651
652 trace_lttng_statedump_process_state(session,
df493bfd 653 p, type, mode, submode, status, files);
73e8ba37 654 lttng_statedump_process_ns(session,
c337ddc2 655 p, type, mode, submode, status);
df493bfd
MD
656 /*
657 * As an optimisation for the common case, do not
658 * repeat information for the same files_struct in
659 * two consecutive threads. This is the common case
660 * for threads sharing the same fd table. RCU guarantees
661 * that the same files_struct pointer is not re-used
662 * throughout processes/threads iteration.
663 */
664 if (files && files != prev_files) {
665 lttng_enumerate_files(session, files, tmp);
666 prev_files = files;
667 }
c337ddc2
MD
668 task_unlock(p);
669 } while_each_thread(g, p);
670 }
671 rcu_read_unlock();
672
df493bfd
MD
673 free_page((unsigned long) tmp);
674
c337ddc2
MD
675 return 0;
676}
677
678static
679void lttng_statedump_work_func(struct work_struct *work)
680{
681 if (atomic_dec_and_test(&kernel_threads_to_run))
682 /* If we are the last thread, wake up do_lttng_statedump */
683 wake_up(&statedump_wq);
684}
685
686static
687int do_lttng_statedump(struct lttng_session *session)
688{
cfcee1c7 689 int cpu, ret;
c337ddc2 690
c337ddc2 691 trace_lttng_statedump_start(session);
cfcee1c7 692 ret = lttng_enumerate_process_states(session);
cfcee1c7
MD
693 if (ret)
694 return ret;
695 /*
696 * FIXME
697 * ret = lttng_enumerate_vm_maps(session);
698 * if (ret)
699 * return ret;
700 */
701 ret = lttng_list_interrupts(session);
702 if (ret)
703 return ret;
704 ret = lttng_enumerate_network_ip_interface(session);
705 if (ret)
706 return ret;
707 ret = lttng_enumerate_block_devices(session);
708 switch (ret) {
84c7055e
MD
709 case 0:
710 break;
cfcee1c7
MD
711 case -ENOSYS:
712 printk(KERN_WARNING "LTTng: block device enumeration is not supported by kernel\n");
713 break;
714 default:
715 return ret;
716 }
502e4132
JD
717 ret = lttng_enumerate_cpu_topology(session);
718 if (ret)
719 return ret;
c337ddc2
MD
720
721 /* TODO lttng_dump_idt_table(session); */
722 /* TODO lttng_dump_softirq_vec(session); */
723 /* TODO lttng_list_modules(session); */
724 /* TODO lttng_dump_swap_files(session); */
725
726 /*
727 * Fire off a work queue on each CPU. Their sole purpose in life
728 * is to guarantee that each CPU has been in a state where is was in
729 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
730 */
731 get_online_cpus();
732 atomic_set(&kernel_threads_to_run, num_online_cpus());
733 for_each_online_cpu(cpu) {
734 INIT_DELAYED_WORK(&cpu_work[cpu], lttng_statedump_work_func);
735 schedule_delayed_work_on(cpu, &cpu_work[cpu], 0);
736 }
737 /* Wait for all threads to run */
7a7128e0 738 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
c337ddc2
MD
739 put_online_cpus();
740 /* Our work is done */
c337ddc2
MD
741 trace_lttng_statedump_end(session);
742 return 0;
743}
744
745/*
746 * Called with session mutex held.
747 */
748int lttng_statedump_start(struct lttng_session *session)
749{
c337ddc2
MD
750 return do_lttng_statedump(session);
751}
752EXPORT_SYMBOL_GPL(lttng_statedump_start);
753
dd8d5afb
MD
754static
755int __init lttng_statedump_init(void)
756{
d16aa9c9
MD
757 /*
758 * Allow module to load even if the fixup cannot be done. This
759 * will allow seemless transition when the underlying issue fix
760 * is merged into the Linux kernel, and when tracepoint.c
761 * "tracepoint_module_notify" is turned into a static function.
762 */
763 (void) wrapper_lttng_fixup_sig(THIS_MODULE);
764 return 0;
dd8d5afb
MD
765}
766
767module_init(lttng_statedump_init);
768
461277e7
MD
769static
770void __exit lttng_statedump_exit(void)
771{
772}
773
774module_exit(lttng_statedump_exit);
775
c337ddc2
MD
776MODULE_LICENSE("GPL and additional rights");
777MODULE_AUTHOR("Jean-Hugues Deschenes");
1c124020 778MODULE_DESCRIPTION("LTTng statedump provider");
13ab8b0a
MD
779MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
780 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
781 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
782 LTTNG_MODULES_EXTRAVERSION);
This page took 0.076801 seconds and 4 git commands to generate.