Commit | Line | Data |
---|---|---|
c337ddc2 | 1 | /* |
886d51a3 MD |
2 | * lttng-statedump.c |
3 | * | |
c337ddc2 MD |
4 | * Linux Trace Toolkit Next Generation Kernel State Dump |
5 | * | |
6 | * Copyright 2005 Jean-Hugues Deschenes <jean-hugues.deschenes@polymtl.ca> | |
7 | * Copyright 2006-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * | |
886d51a3 MD |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; only | |
12 | * version 2.1 of the License. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | * | |
c337ddc2 MD |
23 | * Changes: |
24 | * Eric Clement: Add listing of network IP interface | |
25 | * 2006, 2007 Mathieu Desnoyers Fix kernel threads | |
26 | * Various updates | |
c337ddc2 MD |
27 | */ |
28 | ||
29 | #include <linux/init.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/netlink.h> | |
32 | #include <linux/inet.h> | |
33 | #include <linux/ip.h> | |
34 | #include <linux/kthread.h> | |
35 | #include <linux/proc_fs.h> | |
36 | #include <linux/file.h> | |
37 | #include <linux/interrupt.h> | |
38 | #include <linux/irqnr.h> | |
39 | #include <linux/cpu.h> | |
40 | #include <linux/netdevice.h> | |
41 | #include <linux/inetdevice.h> | |
42 | #include <linux/sched.h> | |
43 | #include <linux/mm.h> | |
44 | #include <linux/fdtable.h> | |
45 | #include <linux/swap.h> | |
46 | #include <linux/wait.h> | |
47 | #include <linux/mutex.h> | |
48 | ||
49 | #include "lttng-events.h" | |
13ab8b0a | 50 | #include "lttng-tracer.h" |
c337ddc2 | 51 | #include "wrapper/irqdesc.h" |
3a523f5b | 52 | #include "wrapper/spinlock.h" |
361c023a | 53 | #include "wrapper/fdtable.h" |
3247f8bd | 54 | #include "wrapper/nsproxy.h" |
29784493 | 55 | #include "wrapper/irq.h" |
dd8d5afb | 56 | #include "wrapper/tracepoint.h" |
c337ddc2 | 57 | |
29784493 | 58 | #ifdef CONFIG_LTTNG_HAS_LIST_IRQ |
c337ddc2 MD |
59 | #include <linux/irq.h> |
60 | #endif | |
61 | ||
62 | /* Define the tracepoints, but do not build the probes */ | |
63 | #define CREATE_TRACE_POINTS | |
64 | #define TRACE_INCLUDE_PATH ../instrumentation/events/lttng-module | |
65 | #define TRACE_INCLUDE_FILE lttng-statedump | |
66 | #include "instrumentation/events/lttng-module/lttng-statedump.h" | |
67 | ||
20591cf7 MD |
68 | DEFINE_TRACE(lttng_statedump_end); |
69 | DEFINE_TRACE(lttng_statedump_interrupt); | |
70 | DEFINE_TRACE(lttng_statedump_file_descriptor); | |
71 | DEFINE_TRACE(lttng_statedump_start); | |
72 | DEFINE_TRACE(lttng_statedump_process_state); | |
73 | DEFINE_TRACE(lttng_statedump_network_interface); | |
74 | ||
361c023a MD |
75 | struct lttng_fd_ctx { |
76 | char *page; | |
77 | struct lttng_session *session; | |
78 | struct task_struct *p; | |
79 | }; | |
80 | ||
c337ddc2 MD |
81 | /* |
82 | * Protected by the trace lock. | |
83 | */ | |
84 | static struct delayed_work cpu_work[NR_CPUS]; | |
85 | static DECLARE_WAIT_QUEUE_HEAD(statedump_wq); | |
86 | static atomic_t kernel_threads_to_run; | |
87 | ||
88 | enum lttng_thread_type { | |
89 | LTTNG_USER_THREAD = 0, | |
90 | LTTNG_KERNEL_THREAD = 1, | |
91 | }; | |
92 | ||
93 | enum lttng_execution_mode { | |
94 | LTTNG_USER_MODE = 0, | |
95 | LTTNG_SYSCALL = 1, | |
96 | LTTNG_TRAP = 2, | |
97 | LTTNG_IRQ = 3, | |
98 | LTTNG_SOFTIRQ = 4, | |
99 | LTTNG_MODE_UNKNOWN = 5, | |
100 | }; | |
101 | ||
102 | enum lttng_execution_submode { | |
103 | LTTNG_NONE = 0, | |
104 | LTTNG_UNKNOWN = 1, | |
105 | }; | |
106 | ||
107 | enum lttng_process_status { | |
108 | LTTNG_UNNAMED = 0, | |
109 | LTTNG_WAIT_FORK = 1, | |
110 | LTTNG_WAIT_CPU = 2, | |
111 | LTTNG_EXIT = 3, | |
112 | LTTNG_ZOMBIE = 4, | |
113 | LTTNG_WAIT = 5, | |
114 | LTTNG_RUN = 6, | |
115 | LTTNG_DEAD = 7, | |
116 | }; | |
117 | ||
118 | #ifdef CONFIG_INET | |
119 | static | |
120 | void lttng_enumerate_device(struct lttng_session *session, | |
121 | struct net_device *dev) | |
122 | { | |
123 | struct in_device *in_dev; | |
124 | struct in_ifaddr *ifa; | |
125 | ||
126 | if (dev->flags & IFF_UP) { | |
127 | in_dev = in_dev_get(dev); | |
128 | if (in_dev) { | |
129 | for (ifa = in_dev->ifa_list; ifa != NULL; | |
130 | ifa = ifa->ifa_next) { | |
131 | trace_lttng_statedump_network_interface( | |
132 | session, dev, ifa); | |
133 | } | |
134 | in_dev_put(in_dev); | |
135 | } | |
136 | } else { | |
137 | trace_lttng_statedump_network_interface( | |
138 | session, dev, NULL); | |
139 | } | |
140 | } | |
141 | ||
142 | static | |
143 | int lttng_enumerate_network_ip_interface(struct lttng_session *session) | |
144 | { | |
145 | struct net_device *dev; | |
146 | ||
147 | read_lock(&dev_base_lock); | |
148 | for_each_netdev(&init_net, dev) | |
149 | lttng_enumerate_device(session, dev); | |
150 | read_unlock(&dev_base_lock); | |
151 | ||
152 | return 0; | |
153 | } | |
154 | #else /* CONFIG_INET */ | |
155 | static inline | |
156 | int lttng_enumerate_network_ip_interface(struct lttng_session *session) | |
157 | { | |
158 | return 0; | |
159 | } | |
160 | #endif /* CONFIG_INET */ | |
161 | ||
361c023a MD |
162 | static |
163 | int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd) | |
164 | { | |
165 | const struct lttng_fd_ctx *ctx = p; | |
166 | const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE); | |
167 | ||
168 | if (IS_ERR(s)) { | |
169 | struct dentry *dentry = file->f_path.dentry; | |
170 | ||
171 | /* Make sure we give at least some info */ | |
172 | spin_lock(&dentry->d_lock); | |
173 | trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd, | |
174 | dentry->d_name.name); | |
175 | spin_unlock(&dentry->d_lock); | |
176 | goto end; | |
177 | } | |
178 | trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd, s); | |
179 | end: | |
180 | return 0; | |
181 | } | |
c337ddc2 MD |
182 | |
183 | static | |
184 | void lttng_enumerate_task_fd(struct lttng_session *session, | |
185 | struct task_struct *p, char *tmp) | |
186 | { | |
361c023a | 187 | struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .p = p }; |
c337ddc2 MD |
188 | |
189 | task_lock(p); | |
361c023a | 190 | lttng_iterate_fd(p->files, 0, lttng_dump_one_fd, &ctx); |
c337ddc2 MD |
191 | task_unlock(p); |
192 | } | |
193 | ||
194 | static | |
195 | int lttng_enumerate_file_descriptors(struct lttng_session *session) | |
196 | { | |
197 | struct task_struct *p; | |
198 | char *tmp = (char *) __get_free_page(GFP_KERNEL); | |
199 | ||
200 | /* Enumerate active file descriptors */ | |
201 | rcu_read_lock(); | |
202 | for_each_process(p) | |
203 | lttng_enumerate_task_fd(session, p, tmp); | |
204 | rcu_read_unlock(); | |
205 | free_page((unsigned long) tmp); | |
206 | return 0; | |
207 | } | |
208 | ||
0658bdda MD |
209 | #if 0 |
210 | /* | |
211 | * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section | |
212 | * (scheduling in atomic). Normally, the tasklist lock protects this kind of | |
213 | * iteration, but it is not exported to modules. | |
214 | */ | |
c337ddc2 MD |
215 | static |
216 | void lttng_enumerate_task_vm_maps(struct lttng_session *session, | |
217 | struct task_struct *p) | |
218 | { | |
219 | struct mm_struct *mm; | |
220 | struct vm_area_struct *map; | |
221 | unsigned long ino; | |
222 | ||
223 | /* get_task_mm does a task_lock... */ | |
224 | mm = get_task_mm(p); | |
225 | if (!mm) | |
226 | return; | |
227 | ||
228 | map = mm->mmap; | |
229 | if (map) { | |
230 | down_read(&mm->mmap_sem); | |
231 | while (map) { | |
232 | if (map->vm_file) | |
233 | ino = map->vm_file->f_dentry->d_inode->i_ino; | |
234 | else | |
235 | ino = 0; | |
236 | trace_lttng_statedump_vm_map(session, p, map, ino); | |
237 | map = map->vm_next; | |
238 | } | |
239 | up_read(&mm->mmap_sem); | |
240 | } | |
241 | mmput(mm); | |
242 | } | |
243 | ||
244 | static | |
245 | int lttng_enumerate_vm_maps(struct lttng_session *session) | |
246 | { | |
247 | struct task_struct *p; | |
248 | ||
249 | rcu_read_lock(); | |
250 | for_each_process(p) | |
251 | lttng_enumerate_task_vm_maps(session, p); | |
252 | rcu_read_unlock(); | |
253 | return 0; | |
254 | } | |
0658bdda | 255 | #endif |
c337ddc2 | 256 | |
29784493 | 257 | #ifdef CONFIG_LTTNG_HAS_LIST_IRQ |
47faec4b JN |
258 | |
259 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)) | |
260 | #define irq_desc_get_chip(desc) get_irq_desc_chip(desc) | |
261 | #endif | |
262 | ||
c337ddc2 MD |
263 | static |
264 | void lttng_list_interrupts(struct lttng_session *session) | |
265 | { | |
266 | unsigned int irq; | |
267 | unsigned long flags = 0; | |
268 | struct irq_desc *desc; | |
269 | ||
270 | #define irq_to_desc wrapper_irq_to_desc | |
271 | /* needs irq_desc */ | |
272 | for_each_irq_desc(irq, desc) { | |
273 | struct irqaction *action; | |
274 | const char *irq_chip_name = | |
275 | irq_desc_get_chip(desc)->name ? : "unnamed_irq_chip"; | |
276 | ||
277 | local_irq_save(flags); | |
3a523f5b | 278 | wrapper_desc_spin_lock(&desc->lock); |
c337ddc2 MD |
279 | for (action = desc->action; action; action = action->next) { |
280 | trace_lttng_statedump_interrupt(session, | |
281 | irq, irq_chip_name, action); | |
282 | } | |
3a523f5b | 283 | wrapper_desc_spin_unlock(&desc->lock); |
c337ddc2 MD |
284 | local_irq_restore(flags); |
285 | } | |
286 | #undef irq_to_desc | |
287 | } | |
288 | #else | |
289 | static inline | |
2e7a0709 | 290 | void lttng_list_interrupts(struct lttng_session *session) |
c337ddc2 MD |
291 | { |
292 | } | |
293 | #endif | |
294 | ||
73e8ba37 JD |
295 | static |
296 | void lttng_statedump_process_ns(struct lttng_session *session, | |
297 | struct task_struct *p, | |
298 | enum lttng_thread_type type, | |
299 | enum lttng_execution_mode mode, | |
300 | enum lttng_execution_submode submode, | |
301 | enum lttng_process_status status) | |
302 | { | |
303 | struct nsproxy *proxy; | |
304 | struct pid_namespace *pid_ns; | |
305 | ||
306 | rcu_read_lock(); | |
307 | proxy = task_nsproxy(p); | |
308 | if (proxy) { | |
3247f8bd | 309 | pid_ns = lttng_get_proxy_pid_ns(proxy); |
73e8ba37 JD |
310 | do { |
311 | trace_lttng_statedump_process_state(session, | |
312 | p, type, mode, submode, status, pid_ns); | |
af73f727 | 313 | pid_ns = pid_ns->parent; |
73e8ba37 JD |
314 | } while (pid_ns); |
315 | } else { | |
316 | trace_lttng_statedump_process_state(session, | |
317 | p, type, mode, submode, status, NULL); | |
318 | } | |
319 | rcu_read_unlock(); | |
320 | } | |
321 | ||
c337ddc2 MD |
322 | static |
323 | int lttng_enumerate_process_states(struct lttng_session *session) | |
324 | { | |
325 | struct task_struct *g, *p; | |
326 | ||
327 | rcu_read_lock(); | |
328 | for_each_process(g) { | |
329 | p = g; | |
330 | do { | |
331 | enum lttng_execution_mode mode = | |
332 | LTTNG_MODE_UNKNOWN; | |
333 | enum lttng_execution_submode submode = | |
334 | LTTNG_UNKNOWN; | |
335 | enum lttng_process_status status; | |
336 | enum lttng_thread_type type; | |
337 | ||
338 | task_lock(p); | |
339 | if (p->exit_state == EXIT_ZOMBIE) | |
340 | status = LTTNG_ZOMBIE; | |
341 | else if (p->exit_state == EXIT_DEAD) | |
342 | status = LTTNG_DEAD; | |
343 | else if (p->state == TASK_RUNNING) { | |
344 | /* Is this a forked child that has not run yet? */ | |
345 | if (list_empty(&p->rt.run_list)) | |
346 | status = LTTNG_WAIT_FORK; | |
347 | else | |
348 | /* | |
349 | * All tasks are considered as wait_cpu; | |
350 | * the viewer will sort out if the task | |
351 | * was really running at this time. | |
352 | */ | |
353 | status = LTTNG_WAIT_CPU; | |
354 | } else if (p->state & | |
355 | (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) { | |
356 | /* Task is waiting for something to complete */ | |
357 | status = LTTNG_WAIT; | |
358 | } else | |
359 | status = LTTNG_UNNAMED; | |
360 | submode = LTTNG_NONE; | |
361 | ||
362 | /* | |
363 | * Verification of t->mm is to filter out kernel | |
364 | * threads; Viewer will further filter out if a | |
365 | * user-space thread was in syscall mode or not. | |
366 | */ | |
367 | if (p->mm) | |
368 | type = LTTNG_USER_THREAD; | |
369 | else | |
370 | type = LTTNG_KERNEL_THREAD; | |
73e8ba37 | 371 | lttng_statedump_process_ns(session, |
c337ddc2 MD |
372 | p, type, mode, submode, status); |
373 | task_unlock(p); | |
374 | } while_each_thread(g, p); | |
375 | } | |
376 | rcu_read_unlock(); | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
381 | static | |
382 | void lttng_statedump_work_func(struct work_struct *work) | |
383 | { | |
384 | if (atomic_dec_and_test(&kernel_threads_to_run)) | |
385 | /* If we are the last thread, wake up do_lttng_statedump */ | |
386 | wake_up(&statedump_wq); | |
387 | } | |
388 | ||
389 | static | |
390 | int do_lttng_statedump(struct lttng_session *session) | |
391 | { | |
392 | int cpu; | |
393 | ||
c337ddc2 MD |
394 | trace_lttng_statedump_start(session); |
395 | lttng_enumerate_process_states(session); | |
396 | lttng_enumerate_file_descriptors(session); | |
0658bdda | 397 | /* FIXME lttng_enumerate_vm_maps(session); */ |
c337ddc2 MD |
398 | lttng_list_interrupts(session); |
399 | lttng_enumerate_network_ip_interface(session); | |
400 | ||
401 | /* TODO lttng_dump_idt_table(session); */ | |
402 | /* TODO lttng_dump_softirq_vec(session); */ | |
403 | /* TODO lttng_list_modules(session); */ | |
404 | /* TODO lttng_dump_swap_files(session); */ | |
405 | ||
406 | /* | |
407 | * Fire off a work queue on each CPU. Their sole purpose in life | |
408 | * is to guarantee that each CPU has been in a state where is was in | |
409 | * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ). | |
410 | */ | |
411 | get_online_cpus(); | |
412 | atomic_set(&kernel_threads_to_run, num_online_cpus()); | |
413 | for_each_online_cpu(cpu) { | |
414 | INIT_DELAYED_WORK(&cpu_work[cpu], lttng_statedump_work_func); | |
415 | schedule_delayed_work_on(cpu, &cpu_work[cpu], 0); | |
416 | } | |
417 | /* Wait for all threads to run */ | |
7a7128e0 | 418 | __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0)); |
c337ddc2 MD |
419 | put_online_cpus(); |
420 | /* Our work is done */ | |
c337ddc2 MD |
421 | trace_lttng_statedump_end(session); |
422 | return 0; | |
423 | } | |
424 | ||
425 | /* | |
426 | * Called with session mutex held. | |
427 | */ | |
428 | int lttng_statedump_start(struct lttng_session *session) | |
429 | { | |
c337ddc2 MD |
430 | return do_lttng_statedump(session); |
431 | } | |
432 | EXPORT_SYMBOL_GPL(lttng_statedump_start); | |
433 | ||
dd8d5afb MD |
434 | static |
435 | int __init lttng_statedump_init(void) | |
436 | { | |
d16aa9c9 MD |
437 | /* |
438 | * Allow module to load even if the fixup cannot be done. This | |
439 | * will allow seemless transition when the underlying issue fix | |
440 | * is merged into the Linux kernel, and when tracepoint.c | |
441 | * "tracepoint_module_notify" is turned into a static function. | |
442 | */ | |
443 | (void) wrapper_lttng_fixup_sig(THIS_MODULE); | |
444 | return 0; | |
dd8d5afb MD |
445 | } |
446 | ||
447 | module_init(lttng_statedump_init); | |
448 | ||
461277e7 MD |
449 | static |
450 | void __exit lttng_statedump_exit(void) | |
451 | { | |
452 | } | |
453 | ||
454 | module_exit(lttng_statedump_exit); | |
455 | ||
c337ddc2 MD |
456 | MODULE_LICENSE("GPL and additional rights"); |
457 | MODULE_AUTHOR("Jean-Hugues Deschenes"); | |
458 | MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Statedump"); | |
13ab8b0a MD |
459 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
460 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." | |
461 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) | |
462 | LTTNG_MODULES_EXTRAVERSION); |