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