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