Version 2.4.4
[lttng-modules.git] / lttng-statedump-impl.c
CommitLineData
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"
31c77635 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
361c023a
MD
68struct lttng_fd_ctx {
69 char *page;
70 struct lttng_session *session;
71 struct task_struct *p;
72};
73
c337ddc2
MD
74/*
75 * Protected by the trace lock.
76 */
77static struct delayed_work cpu_work[NR_CPUS];
78static DECLARE_WAIT_QUEUE_HEAD(statedump_wq);
79static atomic_t kernel_threads_to_run;
80
81enum lttng_thread_type {
82 LTTNG_USER_THREAD = 0,
83 LTTNG_KERNEL_THREAD = 1,
84};
85
86enum lttng_execution_mode {
87 LTTNG_USER_MODE = 0,
88 LTTNG_SYSCALL = 1,
89 LTTNG_TRAP = 2,
90 LTTNG_IRQ = 3,
91 LTTNG_SOFTIRQ = 4,
92 LTTNG_MODE_UNKNOWN = 5,
93};
94
95enum lttng_execution_submode {
96 LTTNG_NONE = 0,
97 LTTNG_UNKNOWN = 1,
98};
99
100enum lttng_process_status {
101 LTTNG_UNNAMED = 0,
102 LTTNG_WAIT_FORK = 1,
103 LTTNG_WAIT_CPU = 2,
104 LTTNG_EXIT = 3,
105 LTTNG_ZOMBIE = 4,
106 LTTNG_WAIT = 5,
107 LTTNG_RUN = 6,
108 LTTNG_DEAD = 7,
109};
110
111#ifdef CONFIG_INET
112static
113void lttng_enumerate_device(struct lttng_session *session,
114 struct net_device *dev)
115{
116 struct in_device *in_dev;
117 struct in_ifaddr *ifa;
118
119 if (dev->flags & IFF_UP) {
120 in_dev = in_dev_get(dev);
121 if (in_dev) {
122 for (ifa = in_dev->ifa_list; ifa != NULL;
123 ifa = ifa->ifa_next) {
124 trace_lttng_statedump_network_interface(
125 session, dev, ifa);
126 }
127 in_dev_put(in_dev);
128 }
129 } else {
130 trace_lttng_statedump_network_interface(
131 session, dev, NULL);
132 }
133}
134
135static
136int lttng_enumerate_network_ip_interface(struct lttng_session *session)
137{
138 struct net_device *dev;
139
140 read_lock(&dev_base_lock);
141 for_each_netdev(&init_net, dev)
142 lttng_enumerate_device(session, dev);
143 read_unlock(&dev_base_lock);
144
145 return 0;
146}
147#else /* CONFIG_INET */
148static inline
149int lttng_enumerate_network_ip_interface(struct lttng_session *session)
150{
151 return 0;
152}
153#endif /* CONFIG_INET */
154
361c023a
MD
155static
156int lttng_dump_one_fd(const void *p, struct file *file, unsigned int fd)
157{
158 const struct lttng_fd_ctx *ctx = p;
159 const char *s = d_path(&file->f_path, ctx->page, PAGE_SIZE);
160
161 if (IS_ERR(s)) {
162 struct dentry *dentry = file->f_path.dentry;
163
164 /* Make sure we give at least some info */
165 spin_lock(&dentry->d_lock);
166 trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd,
167 dentry->d_name.name);
168 spin_unlock(&dentry->d_lock);
169 goto end;
170 }
171 trace_lttng_statedump_file_descriptor(ctx->session, ctx->p, fd, s);
172end:
173 return 0;
174}
c337ddc2
MD
175
176static
177void lttng_enumerate_task_fd(struct lttng_session *session,
178 struct task_struct *p, char *tmp)
179{
361c023a 180 struct lttng_fd_ctx ctx = { .page = tmp, .session = session, .p = p };
c337ddc2
MD
181
182 task_lock(p);
361c023a 183 lttng_iterate_fd(p->files, 0, lttng_dump_one_fd, &ctx);
c337ddc2
MD
184 task_unlock(p);
185}
186
187static
188int lttng_enumerate_file_descriptors(struct lttng_session *session)
189{
190 struct task_struct *p;
2f471d07
MD
191 char *tmp;
192
193 tmp = (char *) __get_free_page(GFP_KERNEL);
194 if (!tmp)
195 return -ENOMEM;
c337ddc2
MD
196
197 /* Enumerate active file descriptors */
198 rcu_read_lock();
199 for_each_process(p)
200 lttng_enumerate_task_fd(session, p, tmp);
201 rcu_read_unlock();
202 free_page((unsigned long) tmp);
203 return 0;
204}
205
0658bdda
MD
206#if 0
207/*
208 * FIXME: we cannot take a mmap_sem while in a RCU read-side critical section
209 * (scheduling in atomic). Normally, the tasklist lock protects this kind of
210 * iteration, but it is not exported to modules.
211 */
c337ddc2
MD
212static
213void lttng_enumerate_task_vm_maps(struct lttng_session *session,
214 struct task_struct *p)
215{
216 struct mm_struct *mm;
217 struct vm_area_struct *map;
218 unsigned long ino;
219
220 /* get_task_mm does a task_lock... */
221 mm = get_task_mm(p);
222 if (!mm)
223 return;
224
225 map = mm->mmap;
226 if (map) {
227 down_read(&mm->mmap_sem);
228 while (map) {
229 if (map->vm_file)
230 ino = map->vm_file->f_dentry->d_inode->i_ino;
231 else
232 ino = 0;
233 trace_lttng_statedump_vm_map(session, p, map, ino);
234 map = map->vm_next;
235 }
236 up_read(&mm->mmap_sem);
237 }
238 mmput(mm);
239}
240
241static
242int lttng_enumerate_vm_maps(struct lttng_session *session)
243{
244 struct task_struct *p;
245
246 rcu_read_lock();
247 for_each_process(p)
248 lttng_enumerate_task_vm_maps(session, p);
249 rcu_read_unlock();
250 return 0;
251}
0658bdda 252#endif
c337ddc2 253
29784493 254#ifdef CONFIG_LTTNG_HAS_LIST_IRQ
47faec4b
JN
255
256#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39))
257#define irq_desc_get_chip(desc) get_irq_desc_chip(desc)
258#endif
259
c337ddc2 260static
2f471d07 261int lttng_list_interrupts(struct lttng_session *session)
c337ddc2
MD
262{
263 unsigned int irq;
264 unsigned long flags = 0;
265 struct irq_desc *desc;
266
267#define irq_to_desc wrapper_irq_to_desc
268 /* needs irq_desc */
269 for_each_irq_desc(irq, desc) {
270 struct irqaction *action;
271 const char *irq_chip_name =
272 irq_desc_get_chip(desc)->name ? : "unnamed_irq_chip";
273
274 local_irq_save(flags);
3a523f5b 275 wrapper_desc_spin_lock(&desc->lock);
c337ddc2
MD
276 for (action = desc->action; action; action = action->next) {
277 trace_lttng_statedump_interrupt(session,
278 irq, irq_chip_name, action);
279 }
3a523f5b 280 wrapper_desc_spin_unlock(&desc->lock);
c337ddc2
MD
281 local_irq_restore(flags);
282 }
2f471d07 283 return 0;
c337ddc2
MD
284#undef irq_to_desc
285}
286#else
287static inline
2f471d07 288int lttng_list_interrupts(struct lttng_session *session)
c337ddc2 289{
2f471d07 290 return 0;
c337ddc2
MD
291}
292#endif
293
73e8ba37
JD
294static
295void lttng_statedump_process_ns(struct lttng_session *session,
296 struct task_struct *p,
297 enum lttng_thread_type type,
298 enum lttng_execution_mode mode,
299 enum lttng_execution_submode submode,
300 enum lttng_process_status status)
301{
302 struct nsproxy *proxy;
303 struct pid_namespace *pid_ns;
304
305 rcu_read_lock();
306 proxy = task_nsproxy(p);
307 if (proxy) {
3247f8bd 308 pid_ns = lttng_get_proxy_pid_ns(proxy);
73e8ba37
JD
309 do {
310 trace_lttng_statedump_process_state(session,
311 p, type, mode, submode, status, pid_ns);
af73f727 312 pid_ns = pid_ns->parent;
73e8ba37
JD
313 } while (pid_ns);
314 } else {
315 trace_lttng_statedump_process_state(session,
316 p, type, mode, submode, status, NULL);
317 }
318 rcu_read_unlock();
319}
320
c337ddc2
MD
321static
322int lttng_enumerate_process_states(struct lttng_session *session)
323{
324 struct task_struct *g, *p;
325
326 rcu_read_lock();
327 for_each_process(g) {
328 p = g;
329 do {
330 enum lttng_execution_mode mode =
331 LTTNG_MODE_UNKNOWN;
332 enum lttng_execution_submode submode =
333 LTTNG_UNKNOWN;
334 enum lttng_process_status status;
335 enum lttng_thread_type type;
336
337 task_lock(p);
338 if (p->exit_state == EXIT_ZOMBIE)
339 status = LTTNG_ZOMBIE;
340 else if (p->exit_state == EXIT_DEAD)
341 status = LTTNG_DEAD;
342 else if (p->state == TASK_RUNNING) {
343 /* Is this a forked child that has not run yet? */
344 if (list_empty(&p->rt.run_list))
345 status = LTTNG_WAIT_FORK;
346 else
347 /*
348 * All tasks are considered as wait_cpu;
349 * the viewer will sort out if the task
350 * was really running at this time.
351 */
352 status = LTTNG_WAIT_CPU;
353 } else if (p->state &
354 (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)) {
355 /* Task is waiting for something to complete */
356 status = LTTNG_WAIT;
357 } else
358 status = LTTNG_UNNAMED;
359 submode = LTTNG_NONE;
360
361 /*
362 * Verification of t->mm is to filter out kernel
363 * threads; Viewer will further filter out if a
364 * user-space thread was in syscall mode or not.
365 */
366 if (p->mm)
367 type = LTTNG_USER_THREAD;
368 else
369 type = LTTNG_KERNEL_THREAD;
73e8ba37 370 lttng_statedump_process_ns(session,
c337ddc2
MD
371 p, type, mode, submode, status);
372 task_unlock(p);
373 } while_each_thread(g, p);
374 }
375 rcu_read_unlock();
376
377 return 0;
378}
379
380static
381void lttng_statedump_work_func(struct work_struct *work)
382{
383 if (atomic_dec_and_test(&kernel_threads_to_run))
384 /* If we are the last thread, wake up do_lttng_statedump */
385 wake_up(&statedump_wq);
386}
387
388static
389int do_lttng_statedump(struct lttng_session *session)
390{
2f471d07 391 int cpu, ret;
c337ddc2 392
c337ddc2 393 trace_lttng_statedump_start(session);
2f471d07
MD
394 ret = lttng_enumerate_process_states(session);
395 if (ret)
396 return ret;
397 ret = lttng_enumerate_file_descriptors(session);
398 if (ret)
399 return ret;
400 /*
401 * FIXME
402 * ret = lttng_enumerate_vm_maps(session);
403 * if (ret)
404 * return ret;
405 */
406 ret = lttng_list_interrupts(session);
407 if (ret)
408 return ret;
409 ret = lttng_enumerate_network_ip_interface(session);
410 if (ret)
411 return ret;
c337ddc2
MD
412
413 /* TODO lttng_dump_idt_table(session); */
414 /* TODO lttng_dump_softirq_vec(session); */
415 /* TODO lttng_list_modules(session); */
416 /* TODO lttng_dump_swap_files(session); */
417
418 /*
419 * Fire off a work queue on each CPU. Their sole purpose in life
420 * is to guarantee that each CPU has been in a state where is was in
421 * syscall mode (i.e. not in a trap, an IRQ or a soft IRQ).
422 */
423 get_online_cpus();
424 atomic_set(&kernel_threads_to_run, num_online_cpus());
425 for_each_online_cpu(cpu) {
426 INIT_DELAYED_WORK(&cpu_work[cpu], lttng_statedump_work_func);
427 schedule_delayed_work_on(cpu, &cpu_work[cpu], 0);
428 }
429 /* Wait for all threads to run */
7a7128e0 430 __wait_event(statedump_wq, (atomic_read(&kernel_threads_to_run) == 0));
c337ddc2
MD
431 put_online_cpus();
432 /* Our work is done */
c337ddc2
MD
433 trace_lttng_statedump_end(session);
434 return 0;
435}
436
437/*
438 * Called with session mutex held.
439 */
440int lttng_statedump_start(struct lttng_session *session)
441{
c337ddc2
MD
442 return do_lttng_statedump(session);
443}
444EXPORT_SYMBOL_GPL(lttng_statedump_start);
445
dd8d5afb
MD
446static
447int __init lttng_statedump_init(void)
448{
d16aa9c9
MD
449 /*
450 * Allow module to load even if the fixup cannot be done. This
451 * will allow seemless transition when the underlying issue fix
452 * is merged into the Linux kernel, and when tracepoint.c
453 * "tracepoint_module_notify" is turned into a static function.
454 */
455 (void) wrapper_lttng_fixup_sig(THIS_MODULE);
456 return 0;
dd8d5afb
MD
457}
458
459module_init(lttng_statedump_init);
460
461277e7
MD
461static
462void __exit lttng_statedump_exit(void)
463{
464}
465
466module_exit(lttng_statedump_exit);
467
c337ddc2
MD
468MODULE_LICENSE("GPL and additional rights");
469MODULE_AUTHOR("Jean-Hugues Deschenes");
470MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Statedump");
31c77635
MD
471MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
472 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
473 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
474 LTTNG_MODULES_EXTRAVERSION);
This page took 0.042641 seconds and 4 git commands to generate.