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