trace_syscalls.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include <trace/syscall.h>
  2. #include <linux/kernel.h>
  3. #include <asm/syscall.h>
  4. #include "trace_output.h"
  5. #include "trace.h"
  6. /* Keep a counter of the syscall tracing users */
  7. static int refcount;
  8. /* Prevent from races on thread flags toggling */
  9. static DEFINE_MUTEX(syscall_trace_lock);
  10. /* Option to display the parameters types */
  11. enum {
  12. TRACE_SYSCALLS_OPT_TYPES = 0x1,
  13. };
  14. static struct tracer_opt syscalls_opts[] = {
  15. { TRACER_OPT(syscall_arg_type, TRACE_SYSCALLS_OPT_TYPES) },
  16. { }
  17. };
  18. static struct tracer_flags syscalls_flags = {
  19. .val = 0, /* By default: no parameters types */
  20. .opts = syscalls_opts
  21. };
  22. enum print_line_t
  23. print_syscall_enter(struct trace_iterator *iter, int flags)
  24. {
  25. struct trace_seq *s = &iter->seq;
  26. struct trace_entry *ent = iter->ent;
  27. struct syscall_trace_enter *trace;
  28. struct syscall_metadata *entry;
  29. int i, ret, syscall;
  30. trace_assign_type(trace, ent);
  31. syscall = trace->nr;
  32. entry = syscall_nr_to_meta(syscall);
  33. if (!entry)
  34. goto end;
  35. ret = trace_seq_printf(s, "%s(", entry->name);
  36. if (!ret)
  37. return TRACE_TYPE_PARTIAL_LINE;
  38. for (i = 0; i < entry->nb_args; i++) {
  39. /* parameter types */
  40. if (syscalls_flags.val & TRACE_SYSCALLS_OPT_TYPES) {
  41. ret = trace_seq_printf(s, "%s ", entry->types[i]);
  42. if (!ret)
  43. return TRACE_TYPE_PARTIAL_LINE;
  44. }
  45. /* parameter values */
  46. ret = trace_seq_printf(s, "%s: %lx%s ", entry->args[i],
  47. trace->args[i],
  48. i == entry->nb_args - 1 ? ")" : ",");
  49. if (!ret)
  50. return TRACE_TYPE_PARTIAL_LINE;
  51. }
  52. end:
  53. trace_seq_printf(s, "\n");
  54. return TRACE_TYPE_HANDLED;
  55. }
  56. enum print_line_t
  57. print_syscall_exit(struct trace_iterator *iter, int flags)
  58. {
  59. struct trace_seq *s = &iter->seq;
  60. struct trace_entry *ent = iter->ent;
  61. struct syscall_trace_exit *trace;
  62. int syscall;
  63. struct syscall_metadata *entry;
  64. int ret;
  65. trace_assign_type(trace, ent);
  66. syscall = trace->nr;
  67. entry = syscall_nr_to_meta(syscall);
  68. if (!entry) {
  69. trace_seq_printf(s, "\n");
  70. return TRACE_TYPE_HANDLED;
  71. }
  72. ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
  73. trace->ret);
  74. if (!ret)
  75. return TRACE_TYPE_PARTIAL_LINE;
  76. return TRACE_TYPE_HANDLED;
  77. }
  78. void start_ftrace_syscalls(void)
  79. {
  80. unsigned long flags;
  81. struct task_struct *g, *t;
  82. mutex_lock(&syscall_trace_lock);
  83. /* Don't enable the flag on the tasks twice */
  84. if (++refcount != 1)
  85. goto unlock;
  86. arch_init_ftrace_syscalls();
  87. read_lock_irqsave(&tasklist_lock, flags);
  88. do_each_thread(g, t) {
  89. set_tsk_thread_flag(t, TIF_SYSCALL_FTRACE);
  90. } while_each_thread(g, t);
  91. read_unlock_irqrestore(&tasklist_lock, flags);
  92. unlock:
  93. mutex_unlock(&syscall_trace_lock);
  94. }
  95. void stop_ftrace_syscalls(void)
  96. {
  97. unsigned long flags;
  98. struct task_struct *g, *t;
  99. mutex_lock(&syscall_trace_lock);
  100. /* There are perhaps still some users */
  101. if (--refcount)
  102. goto unlock;
  103. read_lock_irqsave(&tasklist_lock, flags);
  104. do_each_thread(g, t) {
  105. clear_tsk_thread_flag(t, TIF_SYSCALL_FTRACE);
  106. } while_each_thread(g, t);
  107. read_unlock_irqrestore(&tasklist_lock, flags);
  108. unlock:
  109. mutex_unlock(&syscall_trace_lock);
  110. }
  111. void ftrace_syscall_enter(struct pt_regs *regs)
  112. {
  113. struct syscall_trace_enter *entry;
  114. struct syscall_metadata *sys_data;
  115. struct ring_buffer_event *event;
  116. int size;
  117. int syscall_nr;
  118. syscall_nr = syscall_get_nr(current, regs);
  119. sys_data = syscall_nr_to_meta(syscall_nr);
  120. if (!sys_data)
  121. return;
  122. size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
  123. event = trace_current_buffer_lock_reserve(TRACE_SYSCALL_ENTER, size,
  124. 0, 0);
  125. if (!event)
  126. return;
  127. entry = ring_buffer_event_data(event);
  128. entry->nr = syscall_nr;
  129. syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
  130. trace_current_buffer_unlock_commit(event, 0, 0);
  131. trace_wake_up();
  132. }
  133. void ftrace_syscall_exit(struct pt_regs *regs)
  134. {
  135. struct syscall_trace_exit *entry;
  136. struct syscall_metadata *sys_data;
  137. struct ring_buffer_event *event;
  138. int syscall_nr;
  139. syscall_nr = syscall_get_nr(current, regs);
  140. sys_data = syscall_nr_to_meta(syscall_nr);
  141. if (!sys_data)
  142. return;
  143. event = trace_current_buffer_lock_reserve(TRACE_SYSCALL_EXIT,
  144. sizeof(*entry), 0, 0);
  145. if (!event)
  146. return;
  147. entry = ring_buffer_event_data(event);
  148. entry->nr = syscall_nr;
  149. entry->ret = syscall_get_return_value(current, regs);
  150. trace_current_buffer_unlock_commit(event, 0, 0);
  151. trace_wake_up();
  152. }
  153. static int init_syscall_tracer(struct trace_array *tr)
  154. {
  155. start_ftrace_syscalls();
  156. return 0;
  157. }
  158. static void reset_syscall_tracer(struct trace_array *tr)
  159. {
  160. stop_ftrace_syscalls();
  161. tracing_reset_online_cpus(tr);
  162. }
  163. static struct trace_event syscall_enter_event = {
  164. .type = TRACE_SYSCALL_ENTER,
  165. .trace = print_syscall_enter,
  166. };
  167. static struct trace_event syscall_exit_event = {
  168. .type = TRACE_SYSCALL_EXIT,
  169. .trace = print_syscall_exit,
  170. };
  171. static struct tracer syscall_tracer __read_mostly = {
  172. .name = "syscall",
  173. .init = init_syscall_tracer,
  174. .reset = reset_syscall_tracer,
  175. .flags = &syscalls_flags,
  176. };
  177. __init int register_ftrace_syscalls(void)
  178. {
  179. int ret;
  180. ret = register_ftrace_event(&syscall_enter_event);
  181. if (!ret) {
  182. printk(KERN_WARNING "event %d failed to register\n",
  183. syscall_enter_event.type);
  184. WARN_ON_ONCE(1);
  185. }
  186. ret = register_ftrace_event(&syscall_exit_event);
  187. if (!ret) {
  188. printk(KERN_WARNING "event %d failed to register\n",
  189. syscall_exit_event.type);
  190. WARN_ON_ONCE(1);
  191. }
  192. return register_tracer(&syscall_tracer);
  193. }
  194. device_initcall(register_ftrace_syscalls);