trace_syscalls.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. #include <trace/syscall.h>
  2. #include <trace/events/syscalls.h>
  3. #include <linux/syscalls.h>
  4. #include <linux/slab.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */
  7. #include <linux/ftrace.h>
  8. #include <linux/perf_event.h>
  9. #include <asm/syscall.h>
  10. #include "trace_output.h"
  11. #include "trace.h"
  12. static DEFINE_MUTEX(syscall_trace_lock);
  13. static int syscall_enter_register(struct trace_event_call *event,
  14. enum trace_reg type, void *data);
  15. static int syscall_exit_register(struct trace_event_call *event,
  16. enum trace_reg type, void *data);
  17. static struct list_head *
  18. syscall_get_enter_fields(struct trace_event_call *call)
  19. {
  20. struct syscall_metadata *entry = call->data;
  21. return &entry->enter_fields;
  22. }
  23. extern struct syscall_metadata *__start_syscalls_metadata[];
  24. extern struct syscall_metadata *__stop_syscalls_metadata[];
  25. static struct syscall_metadata **syscalls_metadata;
  26. #ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME
  27. static inline bool arch_syscall_match_sym_name(const char *sym, const char *name)
  28. {
  29. /*
  30. * Only compare after the "sys" prefix. Archs that use
  31. * syscall wrappers may have syscalls symbols aliases prefixed
  32. * with ".SyS" or ".sys" instead of "sys", leading to an unwanted
  33. * mismatch.
  34. */
  35. return !strcmp(sym + 3, name + 3);
  36. }
  37. #endif
  38. #ifdef ARCH_TRACE_IGNORE_COMPAT_SYSCALLS
  39. /*
  40. * Some architectures that allow for 32bit applications
  41. * to run on a 64bit kernel, do not map the syscalls for
  42. * the 32bit tasks the same as they do for 64bit tasks.
  43. *
  44. * *cough*x86*cough*
  45. *
  46. * In such a case, instead of reporting the wrong syscalls,
  47. * simply ignore them.
  48. *
  49. * For an arch to ignore the compat syscalls it needs to
  50. * define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS as well as
  51. * define the function arch_trace_is_compat_syscall() to let
  52. * the tracing system know that it should ignore it.
  53. */
  54. static int
  55. trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs)
  56. {
  57. if (unlikely(arch_trace_is_compat_syscall(regs)))
  58. return -1;
  59. return syscall_get_nr(task, regs);
  60. }
  61. #else
  62. static inline int
  63. trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs)
  64. {
  65. return syscall_get_nr(task, regs);
  66. }
  67. #endif /* ARCH_TRACE_IGNORE_COMPAT_SYSCALLS */
  68. static __init struct syscall_metadata *
  69. find_syscall_meta(unsigned long syscall)
  70. {
  71. struct syscall_metadata **start;
  72. struct syscall_metadata **stop;
  73. char str[KSYM_SYMBOL_LEN];
  74. start = __start_syscalls_metadata;
  75. stop = __stop_syscalls_metadata;
  76. kallsyms_lookup(syscall, NULL, NULL, NULL, str);
  77. if (arch_syscall_match_sym_name(str, "sys_ni_syscall"))
  78. return NULL;
  79. for ( ; start < stop; start++) {
  80. if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name))
  81. return *start;
  82. }
  83. return NULL;
  84. }
  85. static struct syscall_metadata *syscall_nr_to_meta(int nr)
  86. {
  87. if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
  88. return NULL;
  89. return syscalls_metadata[nr];
  90. }
  91. static enum print_line_t
  92. print_syscall_enter(struct trace_iterator *iter, int flags,
  93. struct trace_event *event)
  94. {
  95. struct trace_array *tr = iter->tr;
  96. struct trace_seq *s = &iter->seq;
  97. struct trace_entry *ent = iter->ent;
  98. struct syscall_trace_enter *trace;
  99. struct syscall_metadata *entry;
  100. int i, syscall;
  101. trace = (typeof(trace))ent;
  102. syscall = trace->nr;
  103. entry = syscall_nr_to_meta(syscall);
  104. if (!entry)
  105. goto end;
  106. if (entry->enter_event->event.type != ent->type) {
  107. WARN_ON_ONCE(1);
  108. goto end;
  109. }
  110. trace_seq_printf(s, "%s(", entry->name);
  111. for (i = 0; i < entry->nb_args; i++) {
  112. if (trace_seq_has_overflowed(s))
  113. goto end;
  114. /* parameter types */
  115. if (tr->trace_flags & TRACE_ITER_VERBOSE)
  116. trace_seq_printf(s, "%s ", entry->types[i]);
  117. /* parameter values */
  118. trace_seq_printf(s, "%s: %lx%s", entry->args[i],
  119. trace->args[i],
  120. i == entry->nb_args - 1 ? "" : ", ");
  121. }
  122. trace_seq_putc(s, ')');
  123. end:
  124. trace_seq_putc(s, '\n');
  125. return trace_handle_return(s);
  126. }
  127. static enum print_line_t
  128. print_syscall_exit(struct trace_iterator *iter, int flags,
  129. struct trace_event *event)
  130. {
  131. struct trace_seq *s = &iter->seq;
  132. struct trace_entry *ent = iter->ent;
  133. struct syscall_trace_exit *trace;
  134. int syscall;
  135. struct syscall_metadata *entry;
  136. trace = (typeof(trace))ent;
  137. syscall = trace->nr;
  138. entry = syscall_nr_to_meta(syscall);
  139. if (!entry) {
  140. trace_seq_putc(s, '\n');
  141. goto out;
  142. }
  143. if (entry->exit_event->event.type != ent->type) {
  144. WARN_ON_ONCE(1);
  145. return TRACE_TYPE_UNHANDLED;
  146. }
  147. trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
  148. trace->ret);
  149. out:
  150. return trace_handle_return(s);
  151. }
  152. extern char *__bad_type_size(void);
  153. #define SYSCALL_FIELD(type, name) \
  154. sizeof(type) != sizeof(trace.name) ? \
  155. __bad_type_size() : \
  156. #type, #name, offsetof(typeof(trace), name), \
  157. sizeof(trace.name), is_signed_type(type)
  158. static int __init
  159. __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
  160. {
  161. int i;
  162. int pos = 0;
  163. /* When len=0, we just calculate the needed length */
  164. #define LEN_OR_ZERO (len ? len - pos : 0)
  165. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
  166. for (i = 0; i < entry->nb_args; i++) {
  167. pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s",
  168. entry->args[i], sizeof(unsigned long),
  169. i == entry->nb_args - 1 ? "" : ", ");
  170. }
  171. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
  172. for (i = 0; i < entry->nb_args; i++) {
  173. pos += snprintf(buf + pos, LEN_OR_ZERO,
  174. ", ((unsigned long)(REC->%s))", entry->args[i]);
  175. }
  176. #undef LEN_OR_ZERO
  177. /* return the length of print_fmt */
  178. return pos;
  179. }
  180. static int __init set_syscall_print_fmt(struct trace_event_call *call)
  181. {
  182. char *print_fmt;
  183. int len;
  184. struct syscall_metadata *entry = call->data;
  185. if (entry->enter_event != call) {
  186. call->print_fmt = "\"0x%lx\", REC->ret";
  187. return 0;
  188. }
  189. /* First: called with 0 length to calculate the needed length */
  190. len = __set_enter_print_fmt(entry, NULL, 0);
  191. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  192. if (!print_fmt)
  193. return -ENOMEM;
  194. /* Second: actually write the @print_fmt */
  195. __set_enter_print_fmt(entry, print_fmt, len + 1);
  196. call->print_fmt = print_fmt;
  197. return 0;
  198. }
  199. static void __init free_syscall_print_fmt(struct trace_event_call *call)
  200. {
  201. struct syscall_metadata *entry = call->data;
  202. if (entry->enter_event == call)
  203. kfree(call->print_fmt);
  204. }
  205. static int __init syscall_enter_define_fields(struct trace_event_call *call)
  206. {
  207. struct syscall_trace_enter trace;
  208. struct syscall_metadata *meta = call->data;
  209. int ret;
  210. int i;
  211. int offset = offsetof(typeof(trace), args);
  212. ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
  213. if (ret)
  214. return ret;
  215. for (i = 0; i < meta->nb_args; i++) {
  216. ret = trace_define_field(call, meta->types[i],
  217. meta->args[i], offset,
  218. sizeof(unsigned long), 0,
  219. FILTER_OTHER);
  220. offset += sizeof(unsigned long);
  221. }
  222. return ret;
  223. }
  224. static int __init syscall_exit_define_fields(struct trace_event_call *call)
  225. {
  226. struct syscall_trace_exit trace;
  227. int ret;
  228. ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
  229. if (ret)
  230. return ret;
  231. ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
  232. FILTER_OTHER);
  233. return ret;
  234. }
  235. static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)
  236. {
  237. struct trace_array *tr = data;
  238. struct trace_event_file *trace_file;
  239. struct syscall_trace_enter *entry;
  240. struct syscall_metadata *sys_data;
  241. struct ring_buffer_event *event;
  242. struct ring_buffer *buffer;
  243. unsigned long irq_flags;
  244. int pc;
  245. int syscall_nr;
  246. int size;
  247. syscall_nr = trace_get_syscall_nr(current, regs);
  248. if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
  249. return;
  250. /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */
  251. trace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]);
  252. if (!trace_file)
  253. return;
  254. if (trace_trigger_soft_disabled(trace_file))
  255. return;
  256. sys_data = syscall_nr_to_meta(syscall_nr);
  257. if (!sys_data)
  258. return;
  259. size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
  260. local_save_flags(irq_flags);
  261. pc = preempt_count();
  262. buffer = tr->trace_buffer.buffer;
  263. event = trace_buffer_lock_reserve(buffer,
  264. sys_data->enter_event->event.type, size, irq_flags, pc);
  265. if (!event)
  266. return;
  267. entry = ring_buffer_event_data(event);
  268. entry->nr = syscall_nr;
  269. syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
  270. event_trigger_unlock_commit(trace_file, buffer, event, entry,
  271. irq_flags, pc);
  272. }
  273. static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)
  274. {
  275. struct trace_array *tr = data;
  276. struct trace_event_file *trace_file;
  277. struct syscall_trace_exit *entry;
  278. struct syscall_metadata *sys_data;
  279. struct ring_buffer_event *event;
  280. struct ring_buffer *buffer;
  281. unsigned long irq_flags;
  282. int pc;
  283. int syscall_nr;
  284. syscall_nr = trace_get_syscall_nr(current, regs);
  285. if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
  286. return;
  287. /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */
  288. trace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]);
  289. if (!trace_file)
  290. return;
  291. if (trace_trigger_soft_disabled(trace_file))
  292. return;
  293. sys_data = syscall_nr_to_meta(syscall_nr);
  294. if (!sys_data)
  295. return;
  296. local_save_flags(irq_flags);
  297. pc = preempt_count();
  298. buffer = tr->trace_buffer.buffer;
  299. event = trace_buffer_lock_reserve(buffer,
  300. sys_data->exit_event->event.type, sizeof(*entry),
  301. irq_flags, pc);
  302. if (!event)
  303. return;
  304. entry = ring_buffer_event_data(event);
  305. entry->nr = syscall_nr;
  306. entry->ret = syscall_get_return_value(current, regs);
  307. event_trigger_unlock_commit(trace_file, buffer, event, entry,
  308. irq_flags, pc);
  309. }
  310. static int reg_event_syscall_enter(struct trace_event_file *file,
  311. struct trace_event_call *call)
  312. {
  313. struct trace_array *tr = file->tr;
  314. int ret = 0;
  315. int num;
  316. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  317. if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
  318. return -ENOSYS;
  319. mutex_lock(&syscall_trace_lock);
  320. if (!tr->sys_refcount_enter)
  321. ret = register_trace_sys_enter(ftrace_syscall_enter, tr);
  322. if (!ret) {
  323. rcu_assign_pointer(tr->enter_syscall_files[num], file);
  324. tr->sys_refcount_enter++;
  325. }
  326. mutex_unlock(&syscall_trace_lock);
  327. return ret;
  328. }
  329. static void unreg_event_syscall_enter(struct trace_event_file *file,
  330. struct trace_event_call *call)
  331. {
  332. struct trace_array *tr = file->tr;
  333. int num;
  334. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  335. if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
  336. return;
  337. mutex_lock(&syscall_trace_lock);
  338. tr->sys_refcount_enter--;
  339. RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL);
  340. if (!tr->sys_refcount_enter)
  341. unregister_trace_sys_enter(ftrace_syscall_enter, tr);
  342. mutex_unlock(&syscall_trace_lock);
  343. }
  344. static int reg_event_syscall_exit(struct trace_event_file *file,
  345. struct trace_event_call *call)
  346. {
  347. struct trace_array *tr = file->tr;
  348. int ret = 0;
  349. int num;
  350. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  351. if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
  352. return -ENOSYS;
  353. mutex_lock(&syscall_trace_lock);
  354. if (!tr->sys_refcount_exit)
  355. ret = register_trace_sys_exit(ftrace_syscall_exit, tr);
  356. if (!ret) {
  357. rcu_assign_pointer(tr->exit_syscall_files[num], file);
  358. tr->sys_refcount_exit++;
  359. }
  360. mutex_unlock(&syscall_trace_lock);
  361. return ret;
  362. }
  363. static void unreg_event_syscall_exit(struct trace_event_file *file,
  364. struct trace_event_call *call)
  365. {
  366. struct trace_array *tr = file->tr;
  367. int num;
  368. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  369. if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
  370. return;
  371. mutex_lock(&syscall_trace_lock);
  372. tr->sys_refcount_exit--;
  373. RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL);
  374. if (!tr->sys_refcount_exit)
  375. unregister_trace_sys_exit(ftrace_syscall_exit, tr);
  376. mutex_unlock(&syscall_trace_lock);
  377. }
  378. static int __init init_syscall_trace(struct trace_event_call *call)
  379. {
  380. int id;
  381. int num;
  382. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  383. if (num < 0 || num >= NR_syscalls) {
  384. pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
  385. ((struct syscall_metadata *)call->data)->name);
  386. return -ENOSYS;
  387. }
  388. if (set_syscall_print_fmt(call) < 0)
  389. return -ENOMEM;
  390. id = trace_event_raw_init(call);
  391. if (id < 0) {
  392. free_syscall_print_fmt(call);
  393. return id;
  394. }
  395. return id;
  396. }
  397. struct trace_event_functions enter_syscall_print_funcs = {
  398. .trace = print_syscall_enter,
  399. };
  400. struct trace_event_functions exit_syscall_print_funcs = {
  401. .trace = print_syscall_exit,
  402. };
  403. struct trace_event_class __refdata event_class_syscall_enter = {
  404. .system = "syscalls",
  405. .reg = syscall_enter_register,
  406. .define_fields = syscall_enter_define_fields,
  407. .get_fields = syscall_get_enter_fields,
  408. .raw_init = init_syscall_trace,
  409. };
  410. struct trace_event_class __refdata event_class_syscall_exit = {
  411. .system = "syscalls",
  412. .reg = syscall_exit_register,
  413. .define_fields = syscall_exit_define_fields,
  414. .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields),
  415. .raw_init = init_syscall_trace,
  416. };
  417. unsigned long __init __weak arch_syscall_addr(int nr)
  418. {
  419. return (unsigned long)sys_call_table[nr];
  420. }
  421. void __init init_ftrace_syscalls(void)
  422. {
  423. struct syscall_metadata *meta;
  424. unsigned long addr;
  425. int i;
  426. syscalls_metadata = kcalloc(NR_syscalls, sizeof(*syscalls_metadata),
  427. GFP_KERNEL);
  428. if (!syscalls_metadata) {
  429. WARN_ON(1);
  430. return;
  431. }
  432. for (i = 0; i < NR_syscalls; i++) {
  433. addr = arch_syscall_addr(i);
  434. meta = find_syscall_meta(addr);
  435. if (!meta)
  436. continue;
  437. meta->syscall_nr = i;
  438. syscalls_metadata[i] = meta;
  439. }
  440. }
  441. #ifdef CONFIG_PERF_EVENTS
  442. static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
  443. static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
  444. static int sys_perf_refcount_enter;
  445. static int sys_perf_refcount_exit;
  446. static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
  447. {
  448. struct syscall_metadata *sys_data;
  449. struct syscall_trace_enter *rec;
  450. struct hlist_head *head;
  451. int syscall_nr;
  452. int rctx;
  453. int size;
  454. syscall_nr = trace_get_syscall_nr(current, regs);
  455. if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
  456. return;
  457. if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
  458. return;
  459. sys_data = syscall_nr_to_meta(syscall_nr);
  460. if (!sys_data)
  461. return;
  462. head = this_cpu_ptr(sys_data->enter_event->perf_events);
  463. if (hlist_empty(head))
  464. return;
  465. /* get the size after alignment with the u32 buffer size field */
  466. size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
  467. size = ALIGN(size + sizeof(u32), sizeof(u64));
  468. size -= sizeof(u32);
  469. rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
  470. sys_data->enter_event->event.type, NULL, &rctx);
  471. if (!rec)
  472. return;
  473. rec->nr = syscall_nr;
  474. syscall_get_arguments(current, regs, 0, sys_data->nb_args,
  475. (unsigned long *)&rec->args);
  476. perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL);
  477. }
  478. static int perf_sysenter_enable(struct trace_event_call *call)
  479. {
  480. int ret = 0;
  481. int num;
  482. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  483. mutex_lock(&syscall_trace_lock);
  484. if (!sys_perf_refcount_enter)
  485. ret = register_trace_sys_enter(perf_syscall_enter, NULL);
  486. if (ret) {
  487. pr_info("event trace: Could not activate"
  488. "syscall entry trace point");
  489. } else {
  490. set_bit(num, enabled_perf_enter_syscalls);
  491. sys_perf_refcount_enter++;
  492. }
  493. mutex_unlock(&syscall_trace_lock);
  494. return ret;
  495. }
  496. static void perf_sysenter_disable(struct trace_event_call *call)
  497. {
  498. int num;
  499. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  500. mutex_lock(&syscall_trace_lock);
  501. sys_perf_refcount_enter--;
  502. clear_bit(num, enabled_perf_enter_syscalls);
  503. if (!sys_perf_refcount_enter)
  504. unregister_trace_sys_enter(perf_syscall_enter, NULL);
  505. mutex_unlock(&syscall_trace_lock);
  506. }
  507. static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
  508. {
  509. struct syscall_metadata *sys_data;
  510. struct syscall_trace_exit *rec;
  511. struct hlist_head *head;
  512. int syscall_nr;
  513. int rctx;
  514. int size;
  515. syscall_nr = trace_get_syscall_nr(current, regs);
  516. if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
  517. return;
  518. if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
  519. return;
  520. sys_data = syscall_nr_to_meta(syscall_nr);
  521. if (!sys_data)
  522. return;
  523. head = this_cpu_ptr(sys_data->exit_event->perf_events);
  524. if (hlist_empty(head))
  525. return;
  526. /* We can probably do that at build time */
  527. size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
  528. size -= sizeof(u32);
  529. rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
  530. sys_data->exit_event->event.type, NULL, &rctx);
  531. if (!rec)
  532. return;
  533. rec->nr = syscall_nr;
  534. rec->ret = syscall_get_return_value(current, regs);
  535. perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL);
  536. }
  537. static int perf_sysexit_enable(struct trace_event_call *call)
  538. {
  539. int ret = 0;
  540. int num;
  541. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  542. mutex_lock(&syscall_trace_lock);
  543. if (!sys_perf_refcount_exit)
  544. ret = register_trace_sys_exit(perf_syscall_exit, NULL);
  545. if (ret) {
  546. pr_info("event trace: Could not activate"
  547. "syscall exit trace point");
  548. } else {
  549. set_bit(num, enabled_perf_exit_syscalls);
  550. sys_perf_refcount_exit++;
  551. }
  552. mutex_unlock(&syscall_trace_lock);
  553. return ret;
  554. }
  555. static void perf_sysexit_disable(struct trace_event_call *call)
  556. {
  557. int num;
  558. num = ((struct syscall_metadata *)call->data)->syscall_nr;
  559. mutex_lock(&syscall_trace_lock);
  560. sys_perf_refcount_exit--;
  561. clear_bit(num, enabled_perf_exit_syscalls);
  562. if (!sys_perf_refcount_exit)
  563. unregister_trace_sys_exit(perf_syscall_exit, NULL);
  564. mutex_unlock(&syscall_trace_lock);
  565. }
  566. #endif /* CONFIG_PERF_EVENTS */
  567. static int syscall_enter_register(struct trace_event_call *event,
  568. enum trace_reg type, void *data)
  569. {
  570. struct trace_event_file *file = data;
  571. switch (type) {
  572. case TRACE_REG_REGISTER:
  573. return reg_event_syscall_enter(file, event);
  574. case TRACE_REG_UNREGISTER:
  575. unreg_event_syscall_enter(file, event);
  576. return 0;
  577. #ifdef CONFIG_PERF_EVENTS
  578. case TRACE_REG_PERF_REGISTER:
  579. return perf_sysenter_enable(event);
  580. case TRACE_REG_PERF_UNREGISTER:
  581. perf_sysenter_disable(event);
  582. return 0;
  583. case TRACE_REG_PERF_OPEN:
  584. case TRACE_REG_PERF_CLOSE:
  585. case TRACE_REG_PERF_ADD:
  586. case TRACE_REG_PERF_DEL:
  587. return 0;
  588. #endif
  589. }
  590. return 0;
  591. }
  592. static int syscall_exit_register(struct trace_event_call *event,
  593. enum trace_reg type, void *data)
  594. {
  595. struct trace_event_file *file = data;
  596. switch (type) {
  597. case TRACE_REG_REGISTER:
  598. return reg_event_syscall_exit(file, event);
  599. case TRACE_REG_UNREGISTER:
  600. unreg_event_syscall_exit(file, event);
  601. return 0;
  602. #ifdef CONFIG_PERF_EVENTS
  603. case TRACE_REG_PERF_REGISTER:
  604. return perf_sysexit_enable(event);
  605. case TRACE_REG_PERF_UNREGISTER:
  606. perf_sysexit_disable(event);
  607. return 0;
  608. case TRACE_REG_PERF_OPEN:
  609. case TRACE_REG_PERF_CLOSE:
  610. case TRACE_REG_PERF_ADD:
  611. case TRACE_REG_PERF_DEL:
  612. return 0;
  613. #endif
  614. }
  615. return 0;
  616. }