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