trace_syscalls.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. read_lock_irqsave(&tasklist_lock, flags);
  87. do_each_thread(g, t) {
  88. set_tsk_thread_flag(t, TIF_SYSCALL_FTRACE);
  89. } while_each_thread(g, t);
  90. read_unlock_irqrestore(&tasklist_lock, flags);
  91. unlock:
  92. mutex_unlock(&syscall_trace_lock);
  93. }
  94. void stop_ftrace_syscalls(void)
  95. {
  96. unsigned long flags;
  97. struct task_struct *g, *t;
  98. mutex_lock(&syscall_trace_lock);
  99. /* There are perhaps still some users */
  100. if (--refcount)
  101. goto unlock;
  102. read_lock_irqsave(&tasklist_lock, flags);
  103. do_each_thread(g, t) {
  104. clear_tsk_thread_flag(t, TIF_SYSCALL_FTRACE);
  105. } while_each_thread(g, t);
  106. read_unlock_irqrestore(&tasklist_lock, flags);
  107. unlock:
  108. mutex_unlock(&syscall_trace_lock);
  109. }
  110. void ftrace_syscall_enter(struct pt_regs *regs)
  111. {
  112. struct syscall_trace_enter *entry;
  113. struct syscall_metadata *sys_data;
  114. struct ring_buffer_event *event;
  115. int size;
  116. int syscall_nr;
  117. syscall_nr = syscall_get_nr(current, regs);
  118. sys_data = syscall_nr_to_meta(syscall_nr);
  119. if (!sys_data)
  120. return;
  121. size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
  122. event = trace_current_buffer_lock_reserve(TRACE_SYSCALL_ENTER, size,
  123. 0, 0);
  124. if (!event)
  125. return;
  126. entry = ring_buffer_event_data(event);
  127. entry->nr = syscall_nr;
  128. syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
  129. trace_current_buffer_unlock_commit(event, 0, 0);
  130. trace_wake_up();
  131. }
  132. void ftrace_syscall_exit(struct pt_regs *regs)
  133. {
  134. struct syscall_trace_exit *entry;
  135. struct syscall_metadata *sys_data;
  136. struct ring_buffer_event *event;
  137. int syscall_nr;
  138. syscall_nr = syscall_get_nr(current, regs);
  139. sys_data = syscall_nr_to_meta(syscall_nr);
  140. if (!sys_data)
  141. return;
  142. event = trace_current_buffer_lock_reserve(TRACE_SYSCALL_EXIT,
  143. sizeof(*entry), 0, 0);
  144. if (!event)
  145. return;
  146. entry = ring_buffer_event_data(event);
  147. entry->nr = syscall_nr;
  148. entry->ret = syscall_get_return_value(current, regs);
  149. trace_current_buffer_unlock_commit(event, 0, 0);
  150. trace_wake_up();
  151. }
  152. static int init_syscall_tracer(struct trace_array *tr)
  153. {
  154. start_ftrace_syscalls();
  155. return 0;
  156. }
  157. static void reset_syscall_tracer(struct trace_array *tr)
  158. {
  159. stop_ftrace_syscalls();
  160. tracing_reset_online_cpus(tr);
  161. }
  162. static struct trace_event syscall_enter_event = {
  163. .type = TRACE_SYSCALL_ENTER,
  164. .trace = print_syscall_enter,
  165. };
  166. static struct trace_event syscall_exit_event = {
  167. .type = TRACE_SYSCALL_EXIT,
  168. .trace = print_syscall_exit,
  169. };
  170. static struct tracer syscall_tracer __read_mostly = {
  171. .name = "syscall",
  172. .init = init_syscall_tracer,
  173. .reset = reset_syscall_tracer,
  174. .flags = &syscalls_flags,
  175. };
  176. __init int register_ftrace_syscalls(void)
  177. {
  178. int ret;
  179. ret = register_ftrace_event(&syscall_enter_event);
  180. if (!ret) {
  181. printk(KERN_WARNING "event %d failed to register\n",
  182. syscall_enter_event.type);
  183. WARN_ON_ONCE(1);
  184. }
  185. ret = register_ftrace_event(&syscall_exit_event);
  186. if (!ret) {
  187. printk(KERN_WARNING "event %d failed to register\n",
  188. syscall_exit_event.type);
  189. WARN_ON_ONCE(1);
  190. }
  191. return register_tracer(&syscall_tracer);
  192. }
  193. device_initcall(register_ftrace_syscalls);