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