trace_kdb.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * kdb helper for dumping the ftrace buffer
  3. *
  4. * Copyright (C) 2010 Jason Wessel <jason.wessel@windriver.com>
  5. *
  6. * ftrace_dump_buf based on ftrace_dump:
  7. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  8. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  9. *
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kgdb.h>
  13. #include <linux/kdb.h>
  14. #include <linux/ftrace.h>
  15. #include "trace.h"
  16. #include "trace_output.h"
  17. static void ftrace_dump_buf(int skip_lines, long cpu_file)
  18. {
  19. /* use static because iter can be a bit big for the stack */
  20. static struct trace_iterator iter;
  21. static struct ring_buffer_iter *buffer_iter[CONFIG_NR_CPUS];
  22. struct trace_array *tr;
  23. unsigned int old_userobj;
  24. int cnt = 0, cpu;
  25. trace_init_global_iter(&iter);
  26. iter.buffer_iter = buffer_iter;
  27. tr = iter.tr;
  28. for_each_tracing_cpu(cpu) {
  29. atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
  30. }
  31. old_userobj = tr->trace_flags;
  32. /* don't look at user memory in panic mode */
  33. tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  34. kdb_printf("Dumping ftrace buffer:\n");
  35. /* reset all but tr, trace, and overruns */
  36. memset(&iter.seq, 0,
  37. sizeof(struct trace_iterator) -
  38. offsetof(struct trace_iterator, seq));
  39. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  40. iter.pos = -1;
  41. if (cpu_file == RING_BUFFER_ALL_CPUS) {
  42. for_each_tracing_cpu(cpu) {
  43. iter.buffer_iter[cpu] =
  44. ring_buffer_read_prepare(iter.trace_buffer->buffer,
  45. cpu, GFP_ATOMIC);
  46. ring_buffer_read_start(iter.buffer_iter[cpu]);
  47. tracing_iter_reset(&iter, cpu);
  48. }
  49. } else {
  50. iter.cpu_file = cpu_file;
  51. iter.buffer_iter[cpu_file] =
  52. ring_buffer_read_prepare(iter.trace_buffer->buffer,
  53. cpu_file, GFP_ATOMIC);
  54. ring_buffer_read_start(iter.buffer_iter[cpu_file]);
  55. tracing_iter_reset(&iter, cpu_file);
  56. }
  57. while (trace_find_next_entry_inc(&iter)) {
  58. if (!cnt)
  59. kdb_printf("---------------------------------\n");
  60. cnt++;
  61. if (!skip_lines) {
  62. print_trace_line(&iter);
  63. trace_printk_seq(&iter.seq);
  64. } else {
  65. skip_lines--;
  66. }
  67. if (KDB_FLAG(CMD_INTERRUPT))
  68. goto out;
  69. }
  70. if (!cnt)
  71. kdb_printf(" (ftrace buffer empty)\n");
  72. else
  73. kdb_printf("---------------------------------\n");
  74. out:
  75. tr->trace_flags = old_userobj;
  76. for_each_tracing_cpu(cpu) {
  77. atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
  78. }
  79. for_each_tracing_cpu(cpu) {
  80. if (iter.buffer_iter[cpu]) {
  81. ring_buffer_read_finish(iter.buffer_iter[cpu]);
  82. iter.buffer_iter[cpu] = NULL;
  83. }
  84. }
  85. }
  86. /*
  87. * kdb_ftdump - Dump the ftrace log buffer
  88. */
  89. static int kdb_ftdump(int argc, const char **argv)
  90. {
  91. int skip_lines = 0;
  92. long cpu_file;
  93. char *cp;
  94. if (argc > 2)
  95. return KDB_ARGCOUNT;
  96. if (argc) {
  97. skip_lines = simple_strtol(argv[1], &cp, 0);
  98. if (*cp)
  99. skip_lines = 0;
  100. }
  101. if (argc == 2) {
  102. cpu_file = simple_strtol(argv[2], &cp, 0);
  103. if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 ||
  104. !cpu_online(cpu_file))
  105. return KDB_BADINT;
  106. } else {
  107. cpu_file = RING_BUFFER_ALL_CPUS;
  108. }
  109. kdb_trap_printk++;
  110. ftrace_dump_buf(skip_lines, cpu_file);
  111. kdb_trap_printk--;
  112. return 0;
  113. }
  114. static __init int kdb_ftrace_register(void)
  115. {
  116. kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
  117. "Dump ftrace log", 0, KDB_ENABLE_ALWAYS_SAFE);
  118. return 0;
  119. }
  120. late_initcall(kdb_ftrace_register);