trace_syscalls.c 19 KB

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