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