trace_kprobe.c 35 KB

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