trace_kdb.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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, cpu);
  45. ring_buffer_read_start(iter.buffer_iter[cpu]);
  46. tracing_iter_reset(&iter, cpu);
  47. }
  48. } else {
  49. iter.cpu_file = cpu_file;
  50. iter.buffer_iter[cpu_file] =
  51. ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu_file);
  52. ring_buffer_read_start(iter.buffer_iter[cpu_file]);
  53. tracing_iter_reset(&iter, cpu_file);
  54. }
  55. while (trace_find_next_entry_inc(&iter)) {
  56. if (!cnt)
  57. kdb_printf("---------------------------------\n");
  58. cnt++;
  59. if (!skip_lines) {
  60. print_trace_line(&iter);
  61. trace_printk_seq(&iter.seq);
  62. } else {
  63. skip_lines--;
  64. }
  65. if (KDB_FLAG(CMD_INTERRUPT))
  66. goto out;
  67. }
  68. if (!cnt)
  69. kdb_printf(" (ftrace buffer empty)\n");
  70. else
  71. kdb_printf("---------------------------------\n");
  72. out:
  73. tr->trace_flags = old_userobj;
  74. for_each_tracing_cpu(cpu) {
  75. atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
  76. }
  77. for_each_tracing_cpu(cpu) {
  78. if (iter.buffer_iter[cpu]) {
  79. ring_buffer_read_finish(iter.buffer_iter[cpu]);
  80. iter.buffer_iter[cpu] = NULL;
  81. }
  82. }
  83. }
  84. /*
  85. * kdb_ftdump - Dump the ftrace log buffer
  86. */
  87. static int kdb_ftdump(int argc, const char **argv)
  88. {
  89. int skip_lines = 0;
  90. long cpu_file;
  91. char *cp;
  92. if (argc > 2)
  93. return KDB_ARGCOUNT;
  94. if (argc) {
  95. skip_lines = simple_strtol(argv[1], &cp, 0);
  96. if (*cp)
  97. skip_lines = 0;
  98. }
  99. if (argc == 2) {
  100. cpu_file = simple_strtol(argv[2], &cp, 0);
  101. if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 ||
  102. !cpu_online(cpu_file))
  103. return KDB_BADINT;
  104. } else {
  105. cpu_file = RING_BUFFER_ALL_CPUS;
  106. }
  107. kdb_trap_printk++;
  108. ftrace_dump_buf(skip_lines, cpu_file);
  109. kdb_trap_printk--;
  110. return 0;
  111. }
  112. static __init int kdb_ftrace_register(void)
  113. {
  114. kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
  115. "Dump ftrace log", 0, KDB_ENABLE_ALWAYS_SAFE);
  116. return 0;
  117. }
  118. late_initcall(kdb_ftrace_register);