trace_kprobe.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /*
  2. * Kprobes-based tracing events
  3. *
  4. * Created by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/smp.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/ctype.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/perf_event.h>
  31. #include "trace.h"
  32. #include "trace_output.h"
  33. #define MAX_TRACE_ARGS 128
  34. #define MAX_ARGSTR_LEN 63
  35. #define MAX_EVENT_NAME_LEN 64
  36. #define KPROBE_EVENT_SYSTEM "kprobes"
  37. /* Reserved field names */
  38. #define FIELD_STRING_IP "__probe_ip"
  39. #define FIELD_STRING_NARGS "__probe_nargs"
  40. #define FIELD_STRING_RETIP "__probe_ret_ip"
  41. #define FIELD_STRING_FUNC "__probe_func"
  42. const char *reserved_field_names[] = {
  43. "common_type",
  44. "common_flags",
  45. "common_preempt_count",
  46. "common_pid",
  47. "common_tgid",
  48. "common_lock_depth",
  49. FIELD_STRING_IP,
  50. FIELD_STRING_NARGS,
  51. FIELD_STRING_RETIP,
  52. FIELD_STRING_FUNC,
  53. };
  54. struct fetch_func {
  55. unsigned long (*func)(struct pt_regs *, void *);
  56. void *data;
  57. };
  58. static __kprobes unsigned long call_fetch(struct fetch_func *f,
  59. struct pt_regs *regs)
  60. {
  61. return f->func(regs, f->data);
  62. }
  63. /* fetch handlers */
  64. static __kprobes unsigned long fetch_register(struct pt_regs *regs,
  65. void *offset)
  66. {
  67. return regs_get_register(regs, (unsigned int)((unsigned long)offset));
  68. }
  69. static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
  70. void *num)
  71. {
  72. return regs_get_kernel_stack_nth(regs,
  73. (unsigned int)((unsigned long)num));
  74. }
  75. static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
  76. {
  77. unsigned long retval;
  78. if (probe_kernel_address(addr, retval))
  79. return 0;
  80. return retval;
  81. }
  82. static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
  83. {
  84. return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
  85. }
  86. static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
  87. void *dummy)
  88. {
  89. return regs_return_value(regs);
  90. }
  91. static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
  92. void *dummy)
  93. {
  94. return kernel_stack_pointer(regs);
  95. }
  96. /* Memory fetching by symbol */
  97. struct symbol_cache {
  98. char *symbol;
  99. long offset;
  100. unsigned long addr;
  101. };
  102. static unsigned long update_symbol_cache(struct symbol_cache *sc)
  103. {
  104. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  105. if (sc->addr)
  106. sc->addr += sc->offset;
  107. return sc->addr;
  108. }
  109. static void free_symbol_cache(struct symbol_cache *sc)
  110. {
  111. kfree(sc->symbol);
  112. kfree(sc);
  113. }
  114. static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  115. {
  116. struct symbol_cache *sc;
  117. if (!sym || strlen(sym) == 0)
  118. return NULL;
  119. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  120. if (!sc)
  121. return NULL;
  122. sc->symbol = kstrdup(sym, GFP_KERNEL);
  123. if (!sc->symbol) {
  124. kfree(sc);
  125. return NULL;
  126. }
  127. sc->offset = offset;
  128. update_symbol_cache(sc);
  129. return sc;
  130. }
  131. static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
  132. {
  133. struct symbol_cache *sc = data;
  134. if (sc->addr)
  135. return fetch_memory(regs, (void *)sc->addr);
  136. else
  137. return 0;
  138. }
  139. /* Special indirect memory access interface */
  140. struct indirect_fetch_data {
  141. struct fetch_func orig;
  142. long offset;
  143. };
  144. static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
  145. {
  146. struct indirect_fetch_data *ind = data;
  147. unsigned long addr;
  148. addr = call_fetch(&ind->orig, regs);
  149. if (addr) {
  150. addr += ind->offset;
  151. return fetch_memory(regs, (void *)addr);
  152. } else
  153. return 0;
  154. }
  155. static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
  156. {
  157. if (data->orig.func == fetch_indirect)
  158. free_indirect_fetch_data(data->orig.data);
  159. else if (data->orig.func == fetch_symbol)
  160. free_symbol_cache(data->orig.data);
  161. kfree(data);
  162. }
  163. /**
  164. * Kprobe event core functions
  165. */
  166. struct probe_arg {
  167. struct fetch_func fetch;
  168. const char *name;
  169. };
  170. /* Flags for trace_probe */
  171. #define TP_FLAG_TRACE 1
  172. #define TP_FLAG_PROFILE 2
  173. struct trace_probe {
  174. struct list_head list;
  175. struct kretprobe rp; /* Use rp.kp for kprobe use */
  176. unsigned long nhit;
  177. unsigned int flags; /* For TP_FLAG_* */
  178. const char *symbol; /* symbol name */
  179. struct ftrace_event_call call;
  180. struct trace_event event;
  181. unsigned int nr_args;
  182. struct probe_arg args[];
  183. };
  184. #define SIZEOF_TRACE_PROBE(n) \
  185. (offsetof(struct trace_probe, args) + \
  186. (sizeof(struct probe_arg) * (n)))
  187. static __kprobes int probe_is_return(struct trace_probe *tp)
  188. {
  189. return tp->rp.handler != NULL;
  190. }
  191. static __kprobes const char *probe_symbol(struct trace_probe *tp)
  192. {
  193. return tp->symbol ? tp->symbol : "unknown";
  194. }
  195. static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
  196. {
  197. int ret = -EINVAL;
  198. if (ff->func == fetch_argument)
  199. ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data);
  200. else if (ff->func == fetch_register) {
  201. const char *name;
  202. name = regs_query_register_name((unsigned int)((long)ff->data));
  203. ret = snprintf(buf, n, "%%%s", name);
  204. } else if (ff->func == fetch_stack)
  205. ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
  206. else if (ff->func == fetch_memory)
  207. ret = snprintf(buf, n, "@0x%p", ff->data);
  208. else if (ff->func == fetch_symbol) {
  209. struct symbol_cache *sc = ff->data;
  210. ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
  211. } else if (ff->func == fetch_retvalue)
  212. ret = snprintf(buf, n, "$retval");
  213. else if (ff->func == fetch_stack_address)
  214. ret = snprintf(buf, n, "$stack");
  215. else if (ff->func == fetch_indirect) {
  216. struct indirect_fetch_data *id = ff->data;
  217. size_t l = 0;
  218. ret = snprintf(buf, n, "%+ld(", id->offset);
  219. if (ret >= n)
  220. goto end;
  221. l += ret;
  222. ret = probe_arg_string(buf + l, n - l, &id->orig);
  223. if (ret < 0)
  224. goto end;
  225. l += ret;
  226. ret = snprintf(buf + l, n - l, ")");
  227. ret += l;
  228. }
  229. end:
  230. if (ret >= n)
  231. return -ENOSPC;
  232. return ret;
  233. }
  234. static int register_probe_event(struct trace_probe *tp);
  235. static void unregister_probe_event(struct trace_probe *tp);
  236. static DEFINE_MUTEX(probe_lock);
  237. static LIST_HEAD(probe_list);
  238. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  239. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  240. struct pt_regs *regs);
  241. /*
  242. * Allocate new trace_probe and initialize it (including kprobes).
  243. */
  244. static struct trace_probe *alloc_trace_probe(const char *group,
  245. const char *event,
  246. void *addr,
  247. const char *symbol,
  248. unsigned long offs,
  249. int nargs, int is_return)
  250. {
  251. struct trace_probe *tp;
  252. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  253. if (!tp)
  254. return ERR_PTR(-ENOMEM);
  255. if (symbol) {
  256. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  257. if (!tp->symbol)
  258. goto error;
  259. tp->rp.kp.symbol_name = tp->symbol;
  260. tp->rp.kp.offset = offs;
  261. } else
  262. tp->rp.kp.addr = addr;
  263. if (is_return)
  264. tp->rp.handler = kretprobe_dispatcher;
  265. else
  266. tp->rp.kp.pre_handler = kprobe_dispatcher;
  267. if (!event)
  268. goto error;
  269. tp->call.name = kstrdup(event, GFP_KERNEL);
  270. if (!tp->call.name)
  271. goto error;
  272. if (!group)
  273. goto error;
  274. tp->call.system = kstrdup(group, GFP_KERNEL);
  275. if (!tp->call.system)
  276. goto error;
  277. INIT_LIST_HEAD(&tp->list);
  278. return tp;
  279. error:
  280. kfree(tp->call.name);
  281. kfree(tp->symbol);
  282. kfree(tp);
  283. return ERR_PTR(-ENOMEM);
  284. }
  285. static void free_probe_arg(struct probe_arg *arg)
  286. {
  287. if (arg->fetch.func == fetch_symbol)
  288. free_symbol_cache(arg->fetch.data);
  289. else if (arg->fetch.func == fetch_indirect)
  290. free_indirect_fetch_data(arg->fetch.data);
  291. kfree(arg->name);
  292. }
  293. static void free_trace_probe(struct trace_probe *tp)
  294. {
  295. int i;
  296. for (i = 0; i < tp->nr_args; i++)
  297. free_probe_arg(&tp->args[i]);
  298. kfree(tp->call.system);
  299. kfree(tp->call.name);
  300. kfree(tp->symbol);
  301. kfree(tp);
  302. }
  303. static struct trace_probe *find_probe_event(const char *event,
  304. const char *group)
  305. {
  306. struct trace_probe *tp;
  307. list_for_each_entry(tp, &probe_list, list)
  308. if (strcmp(tp->call.name, event) == 0 &&
  309. strcmp(tp->call.system, group) == 0)
  310. return tp;
  311. return NULL;
  312. }
  313. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  314. static void unregister_trace_probe(struct trace_probe *tp)
  315. {
  316. if (probe_is_return(tp))
  317. unregister_kretprobe(&tp->rp);
  318. else
  319. unregister_kprobe(&tp->rp.kp);
  320. list_del(&tp->list);
  321. unregister_probe_event(tp);
  322. }
  323. /* Register a trace_probe and probe_event */
  324. static int register_trace_probe(struct trace_probe *tp)
  325. {
  326. struct trace_probe *old_tp;
  327. int ret;
  328. mutex_lock(&probe_lock);
  329. /* register as an event */
  330. old_tp = find_probe_event(tp->call.name, tp->call.system);
  331. if (old_tp) {
  332. /* delete old event */
  333. unregister_trace_probe(old_tp);
  334. free_trace_probe(old_tp);
  335. }
  336. ret = register_probe_event(tp);
  337. if (ret) {
  338. pr_warning("Faild to register probe event(%d)\n", ret);
  339. goto end;
  340. }
  341. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  342. if (probe_is_return(tp))
  343. ret = register_kretprobe(&tp->rp);
  344. else
  345. ret = register_kprobe(&tp->rp.kp);
  346. if (ret) {
  347. pr_warning("Could not insert probe(%d)\n", ret);
  348. if (ret == -EILSEQ) {
  349. pr_warning("Probing address(0x%p) is not an "
  350. "instruction boundary.\n",
  351. tp->rp.kp.addr);
  352. ret = -EINVAL;
  353. }
  354. unregister_probe_event(tp);
  355. } else
  356. list_add_tail(&tp->list, &probe_list);
  357. end:
  358. mutex_unlock(&probe_lock);
  359. return ret;
  360. }
  361. /* Split symbol and offset. */
  362. static int split_symbol_offset(char *symbol, unsigned long *offset)
  363. {
  364. char *tmp;
  365. int ret;
  366. if (!offset)
  367. return -EINVAL;
  368. tmp = strchr(symbol, '+');
  369. if (tmp) {
  370. /* skip sign because strict_strtol doesn't accept '+' */
  371. ret = strict_strtoul(tmp + 1, 0, offset);
  372. if (ret)
  373. return ret;
  374. *tmp = '\0';
  375. } else
  376. *offset = 0;
  377. return 0;
  378. }
  379. #define PARAM_MAX_ARGS 16
  380. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  381. static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
  382. {
  383. int ret = 0;
  384. unsigned long param;
  385. if (strcmp(arg, "retval") == 0) {
  386. if (is_return) {
  387. ff->func = fetch_retvalue;
  388. ff->data = NULL;
  389. } else
  390. ret = -EINVAL;
  391. } else if (strncmp(arg, "stack", 5) == 0) {
  392. if (arg[5] == '\0') {
  393. ff->func = fetch_stack_address;
  394. ff->data = NULL;
  395. } else if (isdigit(arg[5])) {
  396. ret = strict_strtoul(arg + 5, 10, &param);
  397. if (ret || param > PARAM_MAX_STACK)
  398. ret = -EINVAL;
  399. else {
  400. ff->func = fetch_stack;
  401. ff->data = (void *)param;
  402. }
  403. } else
  404. ret = -EINVAL;
  405. } else if (strncmp(arg, "arg", 3) == 0 && isdigit(arg[3])) {
  406. ret = strict_strtoul(arg + 3, 10, &param);
  407. if (ret || param > PARAM_MAX_ARGS)
  408. ret = -EINVAL;
  409. else {
  410. ff->func = fetch_argument;
  411. ff->data = (void *)param;
  412. }
  413. } else
  414. ret = -EINVAL;
  415. return ret;
  416. }
  417. static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
  418. {
  419. int ret = 0;
  420. unsigned long param;
  421. long offset;
  422. char *tmp;
  423. switch (arg[0]) {
  424. case '$':
  425. ret = parse_probe_vars(arg + 1, ff, is_return);
  426. break;
  427. case '%': /* named register */
  428. ret = regs_query_register_offset(arg + 1);
  429. if (ret >= 0) {
  430. ff->func = fetch_register;
  431. ff->data = (void *)(unsigned long)ret;
  432. ret = 0;
  433. }
  434. break;
  435. case '@': /* memory or symbol */
  436. if (isdigit(arg[1])) {
  437. ret = strict_strtoul(arg + 1, 0, &param);
  438. if (ret)
  439. break;
  440. ff->func = fetch_memory;
  441. ff->data = (void *)param;
  442. } else {
  443. ret = split_symbol_offset(arg + 1, &offset);
  444. if (ret)
  445. break;
  446. ff->data = alloc_symbol_cache(arg + 1, offset);
  447. if (ff->data)
  448. ff->func = fetch_symbol;
  449. else
  450. ret = -EINVAL;
  451. }
  452. break;
  453. case '+': /* indirect memory */
  454. case '-':
  455. tmp = strchr(arg, '(');
  456. if (!tmp) {
  457. ret = -EINVAL;
  458. break;
  459. }
  460. *tmp = '\0';
  461. ret = strict_strtol(arg + 1, 0, &offset);
  462. if (ret)
  463. break;
  464. if (arg[0] == '-')
  465. offset = -offset;
  466. arg = tmp + 1;
  467. tmp = strrchr(arg, ')');
  468. if (tmp) {
  469. struct indirect_fetch_data *id;
  470. *tmp = '\0';
  471. id = kzalloc(sizeof(struct indirect_fetch_data),
  472. GFP_KERNEL);
  473. if (!id)
  474. return -ENOMEM;
  475. id->offset = offset;
  476. ret = parse_probe_arg(arg, &id->orig, is_return);
  477. if (ret)
  478. kfree(id);
  479. else {
  480. ff->func = fetch_indirect;
  481. ff->data = (void *)id;
  482. }
  483. } else
  484. ret = -EINVAL;
  485. break;
  486. default:
  487. /* TODO: support custom handler */
  488. ret = -EINVAL;
  489. }
  490. return ret;
  491. }
  492. /* Return 1 if name is reserved or already used by another argument */
  493. static int conflict_field_name(const char *name,
  494. struct probe_arg *args, int narg)
  495. {
  496. int i;
  497. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  498. if (strcmp(reserved_field_names[i], name) == 0)
  499. return 1;
  500. for (i = 0; i < narg; i++)
  501. if (strcmp(args[i].name, name) == 0)
  502. return 1;
  503. return 0;
  504. }
  505. static int create_trace_probe(int argc, char **argv)
  506. {
  507. /*
  508. * Argument syntax:
  509. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  510. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  511. * Fetch args:
  512. * $argN : fetch Nth of function argument. (N:0-)
  513. * $retval : fetch return value
  514. * $stack : fetch stack address
  515. * $stackN : fetch Nth of stack (N:0-)
  516. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  517. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  518. * %REG : fetch register REG
  519. * Indirect memory fetch:
  520. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  521. * Alias name of args:
  522. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  523. */
  524. struct trace_probe *tp;
  525. int i, ret = 0;
  526. int is_return = 0;
  527. char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
  528. unsigned long offset = 0;
  529. void *addr = NULL;
  530. char buf[MAX_EVENT_NAME_LEN];
  531. if (argc < 2) {
  532. pr_info("Probe point is not specified.\n");
  533. return -EINVAL;
  534. }
  535. if (argv[0][0] == 'p')
  536. is_return = 0;
  537. else if (argv[0][0] == 'r')
  538. is_return = 1;
  539. else {
  540. pr_info("Probe definition must be started with 'p' or 'r'.\n");
  541. return -EINVAL;
  542. }
  543. if (argv[0][1] == ':') {
  544. event = &argv[0][2];
  545. if (strchr(event, '/')) {
  546. group = event;
  547. event = strchr(group, '/') + 1;
  548. event[-1] = '\0';
  549. if (strlen(group) == 0) {
  550. pr_info("Group name is not specifiled\n");
  551. return -EINVAL;
  552. }
  553. }
  554. if (strlen(event) == 0) {
  555. pr_info("Event name is not specifiled\n");
  556. return -EINVAL;
  557. }
  558. }
  559. if (isdigit(argv[1][0])) {
  560. if (is_return) {
  561. pr_info("Return probe point must be a symbol.\n");
  562. return -EINVAL;
  563. }
  564. /* an address specified */
  565. ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
  566. if (ret) {
  567. pr_info("Failed to parse address.\n");
  568. return ret;
  569. }
  570. } else {
  571. /* a symbol specified */
  572. symbol = argv[1];
  573. /* TODO: support .init module functions */
  574. ret = split_symbol_offset(symbol, &offset);
  575. if (ret) {
  576. pr_info("Failed to parse symbol.\n");
  577. return ret;
  578. }
  579. if (offset && is_return) {
  580. pr_info("Return probe must be used without offset.\n");
  581. return -EINVAL;
  582. }
  583. }
  584. argc -= 2; argv += 2;
  585. /* setup a probe */
  586. if (!group)
  587. group = KPROBE_EVENT_SYSTEM;
  588. if (!event) {
  589. /* Make a new event name */
  590. if (symbol)
  591. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
  592. is_return ? 'r' : 'p', symbol, offset);
  593. else
  594. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
  595. is_return ? 'r' : 'p', addr);
  596. event = buf;
  597. }
  598. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  599. is_return);
  600. if (IS_ERR(tp)) {
  601. pr_info("Failed to allocate trace_probe.(%d)\n",
  602. (int)PTR_ERR(tp));
  603. return PTR_ERR(tp);
  604. }
  605. /* parse arguments */
  606. ret = 0;
  607. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  608. /* Parse argument name */
  609. arg = strchr(argv[i], '=');
  610. if (arg)
  611. *arg++ = '\0';
  612. else
  613. arg = argv[i];
  614. if (conflict_field_name(argv[i], tp->args, i)) {
  615. pr_info("Argument%d name '%s' conflicts with "
  616. "another field.\n", i, argv[i]);
  617. ret = -EINVAL;
  618. goto error;
  619. }
  620. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  621. /* Parse fetch argument */
  622. if (strlen(arg) > MAX_ARGSTR_LEN) {
  623. pr_info("Argument%d(%s) is too long.\n", i, arg);
  624. ret = -ENOSPC;
  625. goto error;
  626. }
  627. ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
  628. if (ret) {
  629. pr_info("Parse error at argument%d. (%d)\n", i, ret);
  630. goto error;
  631. }
  632. }
  633. tp->nr_args = i;
  634. ret = register_trace_probe(tp);
  635. if (ret)
  636. goto error;
  637. return 0;
  638. error:
  639. free_trace_probe(tp);
  640. return ret;
  641. }
  642. static void cleanup_all_probes(void)
  643. {
  644. struct trace_probe *tp;
  645. mutex_lock(&probe_lock);
  646. /* TODO: Use batch unregistration */
  647. while (!list_empty(&probe_list)) {
  648. tp = list_entry(probe_list.next, struct trace_probe, list);
  649. unregister_trace_probe(tp);
  650. free_trace_probe(tp);
  651. }
  652. mutex_unlock(&probe_lock);
  653. }
  654. /* Probes listing interfaces */
  655. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  656. {
  657. mutex_lock(&probe_lock);
  658. return seq_list_start(&probe_list, *pos);
  659. }
  660. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  661. {
  662. return seq_list_next(v, &probe_list, pos);
  663. }
  664. static void probes_seq_stop(struct seq_file *m, void *v)
  665. {
  666. mutex_unlock(&probe_lock);
  667. }
  668. static int probes_seq_show(struct seq_file *m, void *v)
  669. {
  670. struct trace_probe *tp = v;
  671. int i, ret;
  672. char buf[MAX_ARGSTR_LEN + 1];
  673. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  674. seq_printf(m, ":%s", tp->call.name);
  675. if (tp->symbol)
  676. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  677. else
  678. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  679. for (i = 0; i < tp->nr_args; i++) {
  680. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
  681. if (ret < 0) {
  682. pr_warning("Argument%d decoding error(%d).\n", i, ret);
  683. return ret;
  684. }
  685. seq_printf(m, " %s=%s", tp->args[i].name, buf);
  686. }
  687. seq_printf(m, "\n");
  688. return 0;
  689. }
  690. static const struct seq_operations probes_seq_op = {
  691. .start = probes_seq_start,
  692. .next = probes_seq_next,
  693. .stop = probes_seq_stop,
  694. .show = probes_seq_show
  695. };
  696. static int probes_open(struct inode *inode, struct file *file)
  697. {
  698. if ((file->f_mode & FMODE_WRITE) &&
  699. (file->f_flags & O_TRUNC))
  700. cleanup_all_probes();
  701. return seq_open(file, &probes_seq_op);
  702. }
  703. static int command_trace_probe(const char *buf)
  704. {
  705. char **argv;
  706. int argc = 0, ret = 0;
  707. argv = argv_split(GFP_KERNEL, buf, &argc);
  708. if (!argv)
  709. return -ENOMEM;
  710. if (argc)
  711. ret = create_trace_probe(argc, argv);
  712. argv_free(argv);
  713. return ret;
  714. }
  715. #define WRITE_BUFSIZE 128
  716. static ssize_t probes_write(struct file *file, const char __user *buffer,
  717. size_t count, loff_t *ppos)
  718. {
  719. char *kbuf, *tmp;
  720. int ret;
  721. size_t done;
  722. size_t size;
  723. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  724. if (!kbuf)
  725. return -ENOMEM;
  726. ret = done = 0;
  727. while (done < count) {
  728. size = count - done;
  729. if (size >= WRITE_BUFSIZE)
  730. size = WRITE_BUFSIZE - 1;
  731. if (copy_from_user(kbuf, buffer + done, size)) {
  732. ret = -EFAULT;
  733. goto out;
  734. }
  735. kbuf[size] = '\0';
  736. tmp = strchr(kbuf, '\n');
  737. if (tmp) {
  738. *tmp = '\0';
  739. size = tmp - kbuf + 1;
  740. } else if (done + size < count) {
  741. pr_warning("Line length is too long: "
  742. "Should be less than %d.", WRITE_BUFSIZE);
  743. ret = -EINVAL;
  744. goto out;
  745. }
  746. done += size;
  747. /* Remove comments */
  748. tmp = strchr(kbuf, '#');
  749. if (tmp)
  750. *tmp = '\0';
  751. ret = command_trace_probe(kbuf);
  752. if (ret)
  753. goto out;
  754. }
  755. ret = done;
  756. out:
  757. kfree(kbuf);
  758. return ret;
  759. }
  760. static const struct file_operations kprobe_events_ops = {
  761. .owner = THIS_MODULE,
  762. .open = probes_open,
  763. .read = seq_read,
  764. .llseek = seq_lseek,
  765. .release = seq_release,
  766. .write = probes_write,
  767. };
  768. /* Probes profiling interfaces */
  769. static int probes_profile_seq_show(struct seq_file *m, void *v)
  770. {
  771. struct trace_probe *tp = v;
  772. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  773. tp->rp.kp.nmissed);
  774. return 0;
  775. }
  776. static const struct seq_operations profile_seq_op = {
  777. .start = probes_seq_start,
  778. .next = probes_seq_next,
  779. .stop = probes_seq_stop,
  780. .show = probes_profile_seq_show
  781. };
  782. static int profile_open(struct inode *inode, struct file *file)
  783. {
  784. return seq_open(file, &profile_seq_op);
  785. }
  786. static const struct file_operations kprobe_profile_ops = {
  787. .owner = THIS_MODULE,
  788. .open = profile_open,
  789. .read = seq_read,
  790. .llseek = seq_lseek,
  791. .release = seq_release,
  792. };
  793. /* Kprobe handler */
  794. static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  795. {
  796. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  797. struct kprobe_trace_entry *entry;
  798. struct ring_buffer_event *event;
  799. struct ring_buffer *buffer;
  800. int size, i, pc;
  801. unsigned long irq_flags;
  802. struct ftrace_event_call *call = &tp->call;
  803. tp->nhit++;
  804. local_save_flags(irq_flags);
  805. pc = preempt_count();
  806. size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  807. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  808. irq_flags, pc);
  809. if (!event)
  810. return 0;
  811. entry = ring_buffer_event_data(event);
  812. entry->nargs = tp->nr_args;
  813. entry->ip = (unsigned long)kp->addr;
  814. for (i = 0; i < tp->nr_args; i++)
  815. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  816. if (!filter_current_check_discard(buffer, call, entry, event))
  817. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  818. return 0;
  819. }
  820. /* Kretprobe handler */
  821. static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
  822. struct pt_regs *regs)
  823. {
  824. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  825. struct kretprobe_trace_entry *entry;
  826. struct ring_buffer_event *event;
  827. struct ring_buffer *buffer;
  828. int size, i, pc;
  829. unsigned long irq_flags;
  830. struct ftrace_event_call *call = &tp->call;
  831. local_save_flags(irq_flags);
  832. pc = preempt_count();
  833. size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  834. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  835. irq_flags, pc);
  836. if (!event)
  837. return 0;
  838. entry = ring_buffer_event_data(event);
  839. entry->nargs = tp->nr_args;
  840. entry->func = (unsigned long)tp->rp.kp.addr;
  841. entry->ret_ip = (unsigned long)ri->ret_addr;
  842. for (i = 0; i < tp->nr_args; i++)
  843. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  844. if (!filter_current_check_discard(buffer, call, entry, event))
  845. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  846. return 0;
  847. }
  848. /* Event entry printers */
  849. enum print_line_t
  850. print_kprobe_event(struct trace_iterator *iter, int flags)
  851. {
  852. struct kprobe_trace_entry *field;
  853. struct trace_seq *s = &iter->seq;
  854. struct trace_event *event;
  855. struct trace_probe *tp;
  856. int i;
  857. field = (struct kprobe_trace_entry *)iter->ent;
  858. event = ftrace_find_event(field->ent.type);
  859. tp = container_of(event, struct trace_probe, event);
  860. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  861. goto partial;
  862. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  863. goto partial;
  864. if (!trace_seq_puts(s, ")"))
  865. goto partial;
  866. for (i = 0; i < field->nargs; i++)
  867. if (!trace_seq_printf(s, " %s=%lx",
  868. tp->args[i].name, field->args[i]))
  869. goto partial;
  870. if (!trace_seq_puts(s, "\n"))
  871. goto partial;
  872. return TRACE_TYPE_HANDLED;
  873. partial:
  874. return TRACE_TYPE_PARTIAL_LINE;
  875. }
  876. enum print_line_t
  877. print_kretprobe_event(struct trace_iterator *iter, int flags)
  878. {
  879. struct kretprobe_trace_entry *field;
  880. struct trace_seq *s = &iter->seq;
  881. struct trace_event *event;
  882. struct trace_probe *tp;
  883. int i;
  884. field = (struct kretprobe_trace_entry *)iter->ent;
  885. event = ftrace_find_event(field->ent.type);
  886. tp = container_of(event, struct trace_probe, event);
  887. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  888. goto partial;
  889. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  890. goto partial;
  891. if (!trace_seq_puts(s, " <- "))
  892. goto partial;
  893. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  894. goto partial;
  895. if (!trace_seq_puts(s, ")"))
  896. goto partial;
  897. for (i = 0; i < field->nargs; i++)
  898. if (!trace_seq_printf(s, " %s=%lx",
  899. tp->args[i].name, field->args[i]))
  900. goto partial;
  901. if (!trace_seq_puts(s, "\n"))
  902. goto partial;
  903. return TRACE_TYPE_HANDLED;
  904. partial:
  905. return TRACE_TYPE_PARTIAL_LINE;
  906. }
  907. static int probe_event_enable(struct ftrace_event_call *call)
  908. {
  909. struct trace_probe *tp = (struct trace_probe *)call->data;
  910. tp->flags |= TP_FLAG_TRACE;
  911. if (probe_is_return(tp))
  912. return enable_kretprobe(&tp->rp);
  913. else
  914. return enable_kprobe(&tp->rp.kp);
  915. }
  916. static void probe_event_disable(struct ftrace_event_call *call)
  917. {
  918. struct trace_probe *tp = (struct trace_probe *)call->data;
  919. tp->flags &= ~TP_FLAG_TRACE;
  920. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  921. if (probe_is_return(tp))
  922. disable_kretprobe(&tp->rp);
  923. else
  924. disable_kprobe(&tp->rp.kp);
  925. }
  926. }
  927. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  928. {
  929. INIT_LIST_HEAD(&event_call->fields);
  930. return 0;
  931. }
  932. #undef DEFINE_FIELD
  933. #define DEFINE_FIELD(type, item, name, is_signed) \
  934. do { \
  935. ret = trace_define_field(event_call, #type, name, \
  936. offsetof(typeof(field), item), \
  937. sizeof(field.item), is_signed, \
  938. FILTER_OTHER); \
  939. if (ret) \
  940. return ret; \
  941. } while (0)
  942. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  943. {
  944. int ret, i;
  945. struct kprobe_trace_entry field;
  946. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  947. ret = trace_define_common_fields(event_call);
  948. if (!ret)
  949. return ret;
  950. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  951. DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
  952. /* Set argument names as fields */
  953. for (i = 0; i < tp->nr_args; i++)
  954. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  955. return 0;
  956. }
  957. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  958. {
  959. int ret, i;
  960. struct kretprobe_trace_entry field;
  961. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  962. ret = trace_define_common_fields(event_call);
  963. if (!ret)
  964. return ret;
  965. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  966. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  967. DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
  968. /* Set argument names as fields */
  969. for (i = 0; i < tp->nr_args; i++)
  970. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  971. return 0;
  972. }
  973. static int __probe_event_show_format(struct trace_seq *s,
  974. struct trace_probe *tp, const char *fmt,
  975. const char *arg)
  976. {
  977. int i;
  978. /* Show format */
  979. if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
  980. return 0;
  981. for (i = 0; i < tp->nr_args; i++)
  982. if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
  983. return 0;
  984. if (!trace_seq_printf(s, "\", %s", arg))
  985. return 0;
  986. for (i = 0; i < tp->nr_args; i++)
  987. if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
  988. return 0;
  989. return trace_seq_puts(s, "\n");
  990. }
  991. #undef SHOW_FIELD
  992. #define SHOW_FIELD(type, item, name) \
  993. do { \
  994. ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
  995. "offset:%u;\tsize:%u;\n", name, \
  996. (unsigned int)offsetof(typeof(field), item),\
  997. (unsigned int)sizeof(type)); \
  998. if (!ret) \
  999. return 0; \
  1000. } while (0)
  1001. static int kprobe_event_show_format(struct ftrace_event_call *call,
  1002. struct trace_seq *s)
  1003. {
  1004. struct kprobe_trace_entry field __attribute__((unused));
  1005. int ret, i;
  1006. struct trace_probe *tp = (struct trace_probe *)call->data;
  1007. SHOW_FIELD(unsigned long, ip, FIELD_STRING_IP);
  1008. SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
  1009. /* Show fields */
  1010. for (i = 0; i < tp->nr_args; i++)
  1011. SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
  1012. trace_seq_puts(s, "\n");
  1013. return __probe_event_show_format(s, tp, "(%lx)",
  1014. "REC->" FIELD_STRING_IP);
  1015. }
  1016. static int kretprobe_event_show_format(struct ftrace_event_call *call,
  1017. struct trace_seq *s)
  1018. {
  1019. struct kretprobe_trace_entry field __attribute__((unused));
  1020. int ret, i;
  1021. struct trace_probe *tp = (struct trace_probe *)call->data;
  1022. SHOW_FIELD(unsigned long, func, FIELD_STRING_FUNC);
  1023. SHOW_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP);
  1024. SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
  1025. /* Show fields */
  1026. for (i = 0; i < tp->nr_args; i++)
  1027. SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
  1028. trace_seq_puts(s, "\n");
  1029. return __probe_event_show_format(s, tp, "(%lx <- %lx)",
  1030. "REC->" FIELD_STRING_FUNC
  1031. ", REC->" FIELD_STRING_RETIP);
  1032. }
  1033. #ifdef CONFIG_EVENT_PROFILE
  1034. /* Kprobe profile handler */
  1035. static __kprobes int kprobe_profile_func(struct kprobe *kp,
  1036. struct pt_regs *regs)
  1037. {
  1038. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1039. struct ftrace_event_call *call = &tp->call;
  1040. struct kprobe_trace_entry *entry;
  1041. struct perf_trace_buf *trace_buf;
  1042. struct trace_entry *ent;
  1043. int size, __size, i, pc, __cpu;
  1044. unsigned long irq_flags;
  1045. char *raw_data;
  1046. pc = preempt_count();
  1047. __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  1048. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1049. size -= sizeof(u32);
  1050. if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
  1051. "profile buffer not large enough"))
  1052. return 0;
  1053. /*
  1054. * Protect the non nmi buffer
  1055. * This also protects the rcu read side
  1056. */
  1057. local_irq_save(irq_flags);
  1058. __cpu = smp_processor_id();
  1059. if (in_nmi())
  1060. trace_buf = rcu_dereference(perf_trace_buf_nmi);
  1061. else
  1062. trace_buf = rcu_dereference(perf_trace_buf);
  1063. if (!trace_buf)
  1064. goto end;
  1065. trace_buf = per_cpu_ptr(trace_buf, __cpu);
  1066. if (trace_buf->recursion++)
  1067. goto end_recursion;
  1068. /*
  1069. * Make recursion update visible before entering perf_tp_event
  1070. * so that we protect from perf recursions.
  1071. */
  1072. barrier();
  1073. raw_data = trace_buf->buf;
  1074. /* Zero dead bytes from alignment to avoid buffer leak to userspace */
  1075. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1076. entry = (struct kprobe_trace_entry *)raw_data;
  1077. ent = &entry->ent;
  1078. tracing_generic_entry_update(ent, irq_flags, pc);
  1079. ent->type = call->id;
  1080. entry->nargs = tp->nr_args;
  1081. entry->ip = (unsigned long)kp->addr;
  1082. for (i = 0; i < tp->nr_args; i++)
  1083. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1084. perf_tp_event(call->id, entry->ip, 1, entry, size);
  1085. end_recursion:
  1086. trace_buf->recursion--;
  1087. end:
  1088. local_irq_restore(irq_flags);
  1089. return 0;
  1090. }
  1091. /* Kretprobe profile handler */
  1092. static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
  1093. struct pt_regs *regs)
  1094. {
  1095. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1096. struct ftrace_event_call *call = &tp->call;
  1097. struct kretprobe_trace_entry *entry;
  1098. struct perf_trace_buf *trace_buf;
  1099. struct trace_entry *ent;
  1100. int size, __size, i, pc, __cpu;
  1101. unsigned long irq_flags;
  1102. char *raw_data;
  1103. pc = preempt_count();
  1104. __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  1105. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1106. size -= sizeof(u32);
  1107. if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
  1108. "profile buffer not large enough"))
  1109. return 0;
  1110. /*
  1111. * Protect the non nmi buffer
  1112. * This also protects the rcu read side
  1113. */
  1114. local_irq_save(irq_flags);
  1115. __cpu = smp_processor_id();
  1116. if (in_nmi())
  1117. trace_buf = rcu_dereference(perf_trace_buf_nmi);
  1118. else
  1119. trace_buf = rcu_dereference(perf_trace_buf);
  1120. if (!trace_buf)
  1121. goto end;
  1122. trace_buf = per_cpu_ptr(trace_buf, __cpu);
  1123. if (trace_buf->recursion++)
  1124. goto end_recursion;
  1125. /*
  1126. * Make recursion update visible before entering perf_tp_event
  1127. * so that we protect from perf recursions.
  1128. */
  1129. barrier();
  1130. raw_data = trace_buf->buf;
  1131. /* Zero dead bytes from alignment to avoid buffer leak to userspace */
  1132. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1133. entry = (struct kretprobe_trace_entry *)raw_data;
  1134. ent = &entry->ent;
  1135. tracing_generic_entry_update(ent, irq_flags, pc);
  1136. ent->type = call->id;
  1137. entry->nargs = tp->nr_args;
  1138. entry->func = (unsigned long)tp->rp.kp.addr;
  1139. entry->ret_ip = (unsigned long)ri->ret_addr;
  1140. for (i = 0; i < tp->nr_args; i++)
  1141. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1142. perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
  1143. end_recursion:
  1144. trace_buf->recursion--;
  1145. end:
  1146. local_irq_restore(irq_flags);
  1147. return 0;
  1148. }
  1149. static int probe_profile_enable(struct ftrace_event_call *call)
  1150. {
  1151. struct trace_probe *tp = (struct trace_probe *)call->data;
  1152. tp->flags |= TP_FLAG_PROFILE;
  1153. if (probe_is_return(tp))
  1154. return enable_kretprobe(&tp->rp);
  1155. else
  1156. return enable_kprobe(&tp->rp.kp);
  1157. }
  1158. static void probe_profile_disable(struct ftrace_event_call *call)
  1159. {
  1160. struct trace_probe *tp = (struct trace_probe *)call->data;
  1161. tp->flags &= ~TP_FLAG_PROFILE;
  1162. if (!(tp->flags & TP_FLAG_TRACE)) {
  1163. if (probe_is_return(tp))
  1164. disable_kretprobe(&tp->rp);
  1165. else
  1166. disable_kprobe(&tp->rp.kp);
  1167. }
  1168. }
  1169. #endif /* CONFIG_EVENT_PROFILE */
  1170. static __kprobes
  1171. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1172. {
  1173. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1174. if (tp->flags & TP_FLAG_TRACE)
  1175. kprobe_trace_func(kp, regs);
  1176. #ifdef CONFIG_EVENT_PROFILE
  1177. if (tp->flags & TP_FLAG_PROFILE)
  1178. kprobe_profile_func(kp, regs);
  1179. #endif /* CONFIG_EVENT_PROFILE */
  1180. return 0; /* We don't tweek kernel, so just return 0 */
  1181. }
  1182. static __kprobes
  1183. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1184. {
  1185. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1186. if (tp->flags & TP_FLAG_TRACE)
  1187. kretprobe_trace_func(ri, regs);
  1188. #ifdef CONFIG_EVENT_PROFILE
  1189. if (tp->flags & TP_FLAG_PROFILE)
  1190. kretprobe_profile_func(ri, regs);
  1191. #endif /* CONFIG_EVENT_PROFILE */
  1192. return 0; /* We don't tweek kernel, so just return 0 */
  1193. }
  1194. static int register_probe_event(struct trace_probe *tp)
  1195. {
  1196. struct ftrace_event_call *call = &tp->call;
  1197. int ret;
  1198. /* Initialize ftrace_event_call */
  1199. if (probe_is_return(tp)) {
  1200. tp->event.trace = print_kretprobe_event;
  1201. call->raw_init = probe_event_raw_init;
  1202. call->show_format = kretprobe_event_show_format;
  1203. call->define_fields = kretprobe_event_define_fields;
  1204. } else {
  1205. tp->event.trace = print_kprobe_event;
  1206. call->raw_init = probe_event_raw_init;
  1207. call->show_format = kprobe_event_show_format;
  1208. call->define_fields = kprobe_event_define_fields;
  1209. }
  1210. call->event = &tp->event;
  1211. call->id = register_ftrace_event(&tp->event);
  1212. if (!call->id)
  1213. return -ENODEV;
  1214. call->enabled = 0;
  1215. call->regfunc = probe_event_enable;
  1216. call->unregfunc = probe_event_disable;
  1217. #ifdef CONFIG_EVENT_PROFILE
  1218. atomic_set(&call->profile_count, -1);
  1219. call->profile_enable = probe_profile_enable;
  1220. call->profile_disable = probe_profile_disable;
  1221. #endif
  1222. call->data = tp;
  1223. ret = trace_add_event_call(call);
  1224. if (ret) {
  1225. pr_info("Failed to register kprobe event: %s\n", call->name);
  1226. unregister_ftrace_event(&tp->event);
  1227. }
  1228. return ret;
  1229. }
  1230. static void unregister_probe_event(struct trace_probe *tp)
  1231. {
  1232. /* tp->event is unregistered in trace_remove_event_call() */
  1233. trace_remove_event_call(&tp->call);
  1234. }
  1235. /* Make a debugfs interface for controling probe points */
  1236. static __init int init_kprobe_trace(void)
  1237. {
  1238. struct dentry *d_tracer;
  1239. struct dentry *entry;
  1240. d_tracer = tracing_init_dentry();
  1241. if (!d_tracer)
  1242. return 0;
  1243. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1244. NULL, &kprobe_events_ops);
  1245. /* Event list interface */
  1246. if (!entry)
  1247. pr_warning("Could not create debugfs "
  1248. "'kprobe_events' entry\n");
  1249. /* Profile interface */
  1250. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1251. NULL, &kprobe_profile_ops);
  1252. if (!entry)
  1253. pr_warning("Could not create debugfs "
  1254. "'kprobe_profile' entry\n");
  1255. return 0;
  1256. }
  1257. fs_initcall(init_kprobe_trace);
  1258. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1259. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1260. int a4, int a5, int a6)
  1261. {
  1262. return a1 + a2 + a3 + a4 + a5 + a6;
  1263. }
  1264. static __init int kprobe_trace_self_tests_init(void)
  1265. {
  1266. int ret;
  1267. int (*target)(int, int, int, int, int, int);
  1268. target = kprobe_trace_selftest_target;
  1269. pr_info("Testing kprobe tracing: ");
  1270. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1271. "$arg1 $arg2 $arg3 $arg4 $stack $stack0");
  1272. if (WARN_ON_ONCE(ret))
  1273. pr_warning("error enabling function entry\n");
  1274. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1275. "$retval");
  1276. if (WARN_ON_ONCE(ret))
  1277. pr_warning("error enabling function return\n");
  1278. ret = target(1, 2, 3, 4, 5, 6);
  1279. cleanup_all_probes();
  1280. pr_cont("OK\n");
  1281. return 0;
  1282. }
  1283. late_initcall(kprobe_trace_self_tests_init);
  1284. #endif