trace_kprobe.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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 "trace_probe.h"
  22. #define KPROBE_EVENT_SYSTEM "kprobes"
  23. /**
  24. * Kprobe event core functions
  25. */
  26. struct trace_kprobe {
  27. struct list_head list;
  28. struct kretprobe rp; /* Use rp.kp for kprobe use */
  29. unsigned long nhit;
  30. const char *symbol; /* symbol name */
  31. struct trace_probe tp;
  32. };
  33. #define SIZEOF_TRACE_KPROBE(n) \
  34. (offsetof(struct trace_kprobe, tp.args) + \
  35. (sizeof(struct probe_arg) * (n)))
  36. static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
  37. {
  38. return tk->rp.handler != NULL;
  39. }
  40. static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
  41. {
  42. return tk->symbol ? tk->symbol : "unknown";
  43. }
  44. static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
  45. {
  46. return tk->rp.kp.offset;
  47. }
  48. static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
  49. {
  50. return !!(kprobe_gone(&tk->rp.kp));
  51. }
  52. static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
  53. struct module *mod)
  54. {
  55. int len = strlen(mod->name);
  56. const char *name = trace_kprobe_symbol(tk);
  57. return strncmp(mod->name, name, len) == 0 && name[len] == ':';
  58. }
  59. static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
  60. {
  61. return !!strchr(trace_kprobe_symbol(tk), ':');
  62. }
  63. static int register_kprobe_event(struct trace_kprobe *tk);
  64. static int unregister_kprobe_event(struct trace_kprobe *tk);
  65. static DEFINE_MUTEX(probe_lock);
  66. static LIST_HEAD(probe_list);
  67. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  68. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  69. struct pt_regs *regs);
  70. /* Memory fetching by symbol */
  71. struct symbol_cache {
  72. char *symbol;
  73. long offset;
  74. unsigned long addr;
  75. };
  76. unsigned long update_symbol_cache(struct symbol_cache *sc)
  77. {
  78. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  79. if (sc->addr)
  80. sc->addr += sc->offset;
  81. return sc->addr;
  82. }
  83. void free_symbol_cache(struct symbol_cache *sc)
  84. {
  85. kfree(sc->symbol);
  86. kfree(sc);
  87. }
  88. struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  89. {
  90. struct symbol_cache *sc;
  91. if (!sym || strlen(sym) == 0)
  92. return NULL;
  93. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  94. if (!sc)
  95. return NULL;
  96. sc->symbol = kstrdup(sym, GFP_KERNEL);
  97. if (!sc->symbol) {
  98. kfree(sc);
  99. return NULL;
  100. }
  101. sc->offset = offset;
  102. update_symbol_cache(sc);
  103. return sc;
  104. }
  105. /*
  106. * Kprobes-specific fetch functions
  107. */
  108. #define DEFINE_FETCH_stack(type) \
  109. static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
  110. void *offset, void *dest) \
  111. { \
  112. *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
  113. (unsigned int)((unsigned long)offset)); \
  114. } \
  115. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
  116. DEFINE_BASIC_FETCH_FUNCS(stack)
  117. /* No string on the stack entry */
  118. #define fetch_stack_string NULL
  119. #define fetch_stack_string_size NULL
  120. #define DEFINE_FETCH_memory(type) \
  121. static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
  122. void *addr, void *dest) \
  123. { \
  124. type retval; \
  125. if (probe_kernel_address(addr, retval)) \
  126. *(type *)dest = 0; \
  127. else \
  128. *(type *)dest = retval; \
  129. } \
  130. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
  131. DEFINE_BASIC_FETCH_FUNCS(memory)
  132. /*
  133. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  134. * length and relative data location.
  135. */
  136. static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
  137. void *addr, void *dest)
  138. {
  139. int maxlen = get_rloc_len(*(u32 *)dest);
  140. u8 *dst = get_rloc_data(dest);
  141. long ret;
  142. if (!maxlen)
  143. return;
  144. /*
  145. * Try to get string again, since the string can be changed while
  146. * probing.
  147. */
  148. ret = strncpy_from_unsafe(dst, addr, maxlen);
  149. if (ret < 0) { /* Failed to fetch string */
  150. dst[0] = '\0';
  151. *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
  152. } else {
  153. *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
  154. }
  155. }
  156. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
  157. /* Return the length of string -- including null terminal byte */
  158. static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
  159. void *addr, void *dest)
  160. {
  161. mm_segment_t old_fs;
  162. int ret, len = 0;
  163. u8 c;
  164. old_fs = get_fs();
  165. set_fs(KERNEL_DS);
  166. pagefault_disable();
  167. do {
  168. ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
  169. len++;
  170. } while (c && ret == 0 && len < MAX_STRING_SIZE);
  171. pagefault_enable();
  172. set_fs(old_fs);
  173. if (ret < 0) /* Failed to check the length */
  174. *(u32 *)dest = 0;
  175. else
  176. *(u32 *)dest = len;
  177. }
  178. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
  179. #define DEFINE_FETCH_symbol(type) \
  180. void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
  181. { \
  182. struct symbol_cache *sc = data; \
  183. if (sc->addr) \
  184. fetch_memory_##type(regs, (void *)sc->addr, dest); \
  185. else \
  186. *(type *)dest = 0; \
  187. } \
  188. NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
  189. DEFINE_BASIC_FETCH_FUNCS(symbol)
  190. DEFINE_FETCH_symbol(string)
  191. DEFINE_FETCH_symbol(string_size)
  192. /* kprobes don't support file_offset fetch methods */
  193. #define fetch_file_offset_u8 NULL
  194. #define fetch_file_offset_u16 NULL
  195. #define fetch_file_offset_u32 NULL
  196. #define fetch_file_offset_u64 NULL
  197. #define fetch_file_offset_string NULL
  198. #define fetch_file_offset_string_size NULL
  199. /* Fetch type information table */
  200. static const struct fetch_type kprobes_fetch_type_table[] = {
  201. /* Special types */
  202. [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
  203. sizeof(u32), 1, "__data_loc char[]"),
  204. [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
  205. string_size, sizeof(u32), 0, "u32"),
  206. /* Basic types */
  207. ASSIGN_FETCH_TYPE(u8, u8, 0),
  208. ASSIGN_FETCH_TYPE(u16, u16, 0),
  209. ASSIGN_FETCH_TYPE(u32, u32, 0),
  210. ASSIGN_FETCH_TYPE(u64, u64, 0),
  211. ASSIGN_FETCH_TYPE(s8, u8, 1),
  212. ASSIGN_FETCH_TYPE(s16, u16, 1),
  213. ASSIGN_FETCH_TYPE(s32, u32, 1),
  214. ASSIGN_FETCH_TYPE(s64, u64, 1),
  215. ASSIGN_FETCH_TYPE_END
  216. };
  217. /*
  218. * Allocate new trace_probe and initialize it (including kprobes).
  219. */
  220. static struct trace_kprobe *alloc_trace_kprobe(const char *group,
  221. const char *event,
  222. void *addr,
  223. const char *symbol,
  224. unsigned long offs,
  225. int nargs, bool is_return)
  226. {
  227. struct trace_kprobe *tk;
  228. int ret = -ENOMEM;
  229. tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
  230. if (!tk)
  231. return ERR_PTR(ret);
  232. if (symbol) {
  233. tk->symbol = kstrdup(symbol, GFP_KERNEL);
  234. if (!tk->symbol)
  235. goto error;
  236. tk->rp.kp.symbol_name = tk->symbol;
  237. tk->rp.kp.offset = offs;
  238. } else
  239. tk->rp.kp.addr = addr;
  240. if (is_return)
  241. tk->rp.handler = kretprobe_dispatcher;
  242. else
  243. tk->rp.kp.pre_handler = kprobe_dispatcher;
  244. if (!event || !is_good_name(event)) {
  245. ret = -EINVAL;
  246. goto error;
  247. }
  248. tk->tp.call.class = &tk->tp.class;
  249. tk->tp.call.name = kstrdup(event, GFP_KERNEL);
  250. if (!tk->tp.call.name)
  251. goto error;
  252. if (!group || !is_good_name(group)) {
  253. ret = -EINVAL;
  254. goto error;
  255. }
  256. tk->tp.class.system = kstrdup(group, GFP_KERNEL);
  257. if (!tk->tp.class.system)
  258. goto error;
  259. INIT_LIST_HEAD(&tk->list);
  260. INIT_LIST_HEAD(&tk->tp.files);
  261. return tk;
  262. error:
  263. kfree(tk->tp.call.name);
  264. kfree(tk->symbol);
  265. kfree(tk);
  266. return ERR_PTR(ret);
  267. }
  268. static void free_trace_kprobe(struct trace_kprobe *tk)
  269. {
  270. int i;
  271. for (i = 0; i < tk->tp.nr_args; i++)
  272. traceprobe_free_probe_arg(&tk->tp.args[i]);
  273. kfree(tk->tp.call.class->system);
  274. kfree(tk->tp.call.name);
  275. kfree(tk->symbol);
  276. kfree(tk);
  277. }
  278. static struct trace_kprobe *find_trace_kprobe(const char *event,
  279. const char *group)
  280. {
  281. struct trace_kprobe *tk;
  282. list_for_each_entry(tk, &probe_list, list)
  283. if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
  284. strcmp(tk->tp.call.class->system, group) == 0)
  285. return tk;
  286. return NULL;
  287. }
  288. /*
  289. * Enable trace_probe
  290. * if the file is NULL, enable "perf" handler, or enable "trace" handler.
  291. */
  292. static int
  293. enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
  294. {
  295. int ret = 0;
  296. if (file) {
  297. struct event_file_link *link;
  298. link = kmalloc(sizeof(*link), GFP_KERNEL);
  299. if (!link) {
  300. ret = -ENOMEM;
  301. goto out;
  302. }
  303. link->file = file;
  304. list_add_tail_rcu(&link->list, &tk->tp.files);
  305. tk->tp.flags |= TP_FLAG_TRACE;
  306. } else
  307. tk->tp.flags |= TP_FLAG_PROFILE;
  308. if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
  309. if (trace_kprobe_is_return(tk))
  310. ret = enable_kretprobe(&tk->rp);
  311. else
  312. ret = enable_kprobe(&tk->rp.kp);
  313. }
  314. out:
  315. return ret;
  316. }
  317. /*
  318. * Disable trace_probe
  319. * if the file is NULL, disable "perf" handler, or disable "trace" handler.
  320. */
  321. static int
  322. disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
  323. {
  324. struct event_file_link *link = NULL;
  325. int wait = 0;
  326. int ret = 0;
  327. if (file) {
  328. link = find_event_file_link(&tk->tp, file);
  329. if (!link) {
  330. ret = -EINVAL;
  331. goto out;
  332. }
  333. list_del_rcu(&link->list);
  334. wait = 1;
  335. if (!list_empty(&tk->tp.files))
  336. goto out;
  337. tk->tp.flags &= ~TP_FLAG_TRACE;
  338. } else
  339. tk->tp.flags &= ~TP_FLAG_PROFILE;
  340. if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
  341. if (trace_kprobe_is_return(tk))
  342. disable_kretprobe(&tk->rp);
  343. else
  344. disable_kprobe(&tk->rp.kp);
  345. wait = 1;
  346. }
  347. out:
  348. if (wait) {
  349. /*
  350. * Synchronize with kprobe_trace_func/kretprobe_trace_func
  351. * to ensure disabled (all running handlers are finished).
  352. * This is not only for kfree(), but also the caller,
  353. * trace_remove_event_call() supposes it for releasing
  354. * event_call related objects, which will be accessed in
  355. * the kprobe_trace_func/kretprobe_trace_func.
  356. */
  357. synchronize_sched();
  358. kfree(link); /* Ignored if link == NULL */
  359. }
  360. return ret;
  361. }
  362. /* Internal register function - just handle k*probes and flags */
  363. static int __register_trace_kprobe(struct trace_kprobe *tk)
  364. {
  365. int i, ret;
  366. if (trace_probe_is_registered(&tk->tp))
  367. return -EINVAL;
  368. for (i = 0; i < tk->tp.nr_args; i++)
  369. traceprobe_update_arg(&tk->tp.args[i]);
  370. /* Set/clear disabled flag according to tp->flag */
  371. if (trace_probe_is_enabled(&tk->tp))
  372. tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
  373. else
  374. tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  375. if (trace_kprobe_is_return(tk))
  376. ret = register_kretprobe(&tk->rp);
  377. else
  378. ret = register_kprobe(&tk->rp.kp);
  379. if (ret == 0)
  380. tk->tp.flags |= TP_FLAG_REGISTERED;
  381. else {
  382. pr_warning("Could not insert probe at %s+%lu: %d\n",
  383. trace_kprobe_symbol(tk), trace_kprobe_offset(tk), ret);
  384. if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
  385. pr_warning("This probe might be able to register after"
  386. "target module is loaded. Continue.\n");
  387. ret = 0;
  388. } else if (ret == -EILSEQ) {
  389. pr_warning("Probing address(0x%p) is not an "
  390. "instruction boundary.\n",
  391. tk->rp.kp.addr);
  392. ret = -EINVAL;
  393. }
  394. }
  395. return ret;
  396. }
  397. /* Internal unregister function - just handle k*probes and flags */
  398. static void __unregister_trace_kprobe(struct trace_kprobe *tk)
  399. {
  400. if (trace_probe_is_registered(&tk->tp)) {
  401. if (trace_kprobe_is_return(tk))
  402. unregister_kretprobe(&tk->rp);
  403. else
  404. unregister_kprobe(&tk->rp.kp);
  405. tk->tp.flags &= ~TP_FLAG_REGISTERED;
  406. /* Cleanup kprobe for reuse */
  407. if (tk->rp.kp.symbol_name)
  408. tk->rp.kp.addr = NULL;
  409. }
  410. }
  411. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  412. static int unregister_trace_kprobe(struct trace_kprobe *tk)
  413. {
  414. /* Enabled event can not be unregistered */
  415. if (trace_probe_is_enabled(&tk->tp))
  416. return -EBUSY;
  417. /* Will fail if probe is being used by ftrace or perf */
  418. if (unregister_kprobe_event(tk))
  419. return -EBUSY;
  420. __unregister_trace_kprobe(tk);
  421. list_del(&tk->list);
  422. return 0;
  423. }
  424. /* Register a trace_probe and probe_event */
  425. static int register_trace_kprobe(struct trace_kprobe *tk)
  426. {
  427. struct trace_kprobe *old_tk;
  428. int ret;
  429. mutex_lock(&probe_lock);
  430. /* Delete old (same name) event if exist */
  431. old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
  432. tk->tp.call.class->system);
  433. if (old_tk) {
  434. ret = unregister_trace_kprobe(old_tk);
  435. if (ret < 0)
  436. goto end;
  437. free_trace_kprobe(old_tk);
  438. }
  439. /* Register new event */
  440. ret = register_kprobe_event(tk);
  441. if (ret) {
  442. pr_warning("Failed to register probe event(%d)\n", ret);
  443. goto end;
  444. }
  445. /* Register k*probe */
  446. ret = __register_trace_kprobe(tk);
  447. if (ret < 0)
  448. unregister_kprobe_event(tk);
  449. else
  450. list_add_tail(&tk->list, &probe_list);
  451. end:
  452. mutex_unlock(&probe_lock);
  453. return ret;
  454. }
  455. /* Module notifier call back, checking event on the module */
  456. static int trace_kprobe_module_callback(struct notifier_block *nb,
  457. unsigned long val, void *data)
  458. {
  459. struct module *mod = data;
  460. struct trace_kprobe *tk;
  461. int ret;
  462. if (val != MODULE_STATE_COMING)
  463. return NOTIFY_DONE;
  464. /* Update probes on coming module */
  465. mutex_lock(&probe_lock);
  466. list_for_each_entry(tk, &probe_list, list) {
  467. if (trace_kprobe_within_module(tk, mod)) {
  468. /* Don't need to check busy - this should have gone. */
  469. __unregister_trace_kprobe(tk);
  470. ret = __register_trace_kprobe(tk);
  471. if (ret)
  472. pr_warning("Failed to re-register probe %s on"
  473. "%s: %d\n",
  474. trace_event_name(&tk->tp.call),
  475. mod->name, ret);
  476. }
  477. }
  478. mutex_unlock(&probe_lock);
  479. return NOTIFY_DONE;
  480. }
  481. static struct notifier_block trace_kprobe_module_nb = {
  482. .notifier_call = trace_kprobe_module_callback,
  483. .priority = 1 /* Invoked after kprobe module callback */
  484. };
  485. static int create_trace_kprobe(int argc, char **argv)
  486. {
  487. /*
  488. * Argument syntax:
  489. * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  490. * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
  491. * Fetch args:
  492. * $retval : fetch return value
  493. * $stack : fetch stack address
  494. * $stackN : fetch Nth of stack (N:0-)
  495. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  496. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  497. * %REG : fetch register REG
  498. * Dereferencing memory fetch:
  499. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  500. * Alias name of args:
  501. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  502. * Type of args:
  503. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  504. */
  505. struct trace_kprobe *tk;
  506. int i, ret = 0;
  507. bool is_return = false, is_delete = false;
  508. char *symbol = NULL, *event = NULL, *group = NULL;
  509. char *arg;
  510. unsigned long offset = 0;
  511. void *addr = NULL;
  512. char buf[MAX_EVENT_NAME_LEN];
  513. /* argc must be >= 1 */
  514. if (argv[0][0] == 'p')
  515. is_return = false;
  516. else if (argv[0][0] == 'r')
  517. is_return = true;
  518. else if (argv[0][0] == '-')
  519. is_delete = true;
  520. else {
  521. pr_info("Probe definition must be started with 'p', 'r' or"
  522. " '-'.\n");
  523. return -EINVAL;
  524. }
  525. if (argv[0][1] == ':') {
  526. event = &argv[0][2];
  527. if (strchr(event, '/')) {
  528. group = event;
  529. event = strchr(group, '/') + 1;
  530. event[-1] = '\0';
  531. if (strlen(group) == 0) {
  532. pr_info("Group name is not specified\n");
  533. return -EINVAL;
  534. }
  535. }
  536. if (strlen(event) == 0) {
  537. pr_info("Event name is not specified\n");
  538. return -EINVAL;
  539. }
  540. }
  541. if (!group)
  542. group = KPROBE_EVENT_SYSTEM;
  543. if (is_delete) {
  544. if (!event) {
  545. pr_info("Delete command needs an event name.\n");
  546. return -EINVAL;
  547. }
  548. mutex_lock(&probe_lock);
  549. tk = find_trace_kprobe(event, group);
  550. if (!tk) {
  551. mutex_unlock(&probe_lock);
  552. pr_info("Event %s/%s doesn't exist.\n", group, event);
  553. return -ENOENT;
  554. }
  555. /* delete an event */
  556. ret = unregister_trace_kprobe(tk);
  557. if (ret == 0)
  558. free_trace_kprobe(tk);
  559. mutex_unlock(&probe_lock);
  560. return ret;
  561. }
  562. if (argc < 2) {
  563. pr_info("Probe point is not specified.\n");
  564. return -EINVAL;
  565. }
  566. if (isdigit(argv[1][0])) {
  567. if (is_return) {
  568. pr_info("Return probe point must be a symbol.\n");
  569. return -EINVAL;
  570. }
  571. /* an address specified */
  572. ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr);
  573. if (ret) {
  574. pr_info("Failed to parse address.\n");
  575. return ret;
  576. }
  577. } else {
  578. /* a symbol specified */
  579. symbol = argv[1];
  580. /* TODO: support .init module functions */
  581. ret = traceprobe_split_symbol_offset(symbol, &offset);
  582. if (ret) {
  583. pr_info("Failed to parse symbol.\n");
  584. return ret;
  585. }
  586. if (offset && is_return) {
  587. pr_info("Return probe must be used without offset.\n");
  588. return -EINVAL;
  589. }
  590. }
  591. argc -= 2; argv += 2;
  592. /* setup a probe */
  593. if (!event) {
  594. /* Make a new event name */
  595. if (symbol)
  596. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  597. is_return ? 'r' : 'p', symbol, offset);
  598. else
  599. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  600. is_return ? 'r' : 'p', addr);
  601. event = buf;
  602. }
  603. tk = alloc_trace_kprobe(group, event, addr, symbol, offset, argc,
  604. is_return);
  605. if (IS_ERR(tk)) {
  606. pr_info("Failed to allocate trace_probe.(%d)\n",
  607. (int)PTR_ERR(tk));
  608. return PTR_ERR(tk);
  609. }
  610. /* parse arguments */
  611. ret = 0;
  612. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  613. struct probe_arg *parg = &tk->tp.args[i];
  614. /* Increment count for freeing args in error case */
  615. tk->tp.nr_args++;
  616. /* Parse argument name */
  617. arg = strchr(argv[i], '=');
  618. if (arg) {
  619. *arg++ = '\0';
  620. parg->name = kstrdup(argv[i], GFP_KERNEL);
  621. } else {
  622. arg = argv[i];
  623. /* If argument name is omitted, set "argN" */
  624. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  625. parg->name = kstrdup(buf, GFP_KERNEL);
  626. }
  627. if (!parg->name) {
  628. pr_info("Failed to allocate argument[%d] name.\n", i);
  629. ret = -ENOMEM;
  630. goto error;
  631. }
  632. if (!is_good_name(parg->name)) {
  633. pr_info("Invalid argument[%d] name: %s\n",
  634. i, parg->name);
  635. ret = -EINVAL;
  636. goto error;
  637. }
  638. if (traceprobe_conflict_field_name(parg->name,
  639. tk->tp.args, i)) {
  640. pr_info("Argument[%d] name '%s' conflicts with "
  641. "another field.\n", i, argv[i]);
  642. ret = -EINVAL;
  643. goto error;
  644. }
  645. /* Parse fetch argument */
  646. ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
  647. is_return, true,
  648. kprobes_fetch_type_table);
  649. if (ret) {
  650. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  651. goto error;
  652. }
  653. }
  654. ret = register_trace_kprobe(tk);
  655. if (ret)
  656. goto error;
  657. return 0;
  658. error:
  659. free_trace_kprobe(tk);
  660. return ret;
  661. }
  662. static int release_all_trace_kprobes(void)
  663. {
  664. struct trace_kprobe *tk;
  665. int ret = 0;
  666. mutex_lock(&probe_lock);
  667. /* Ensure no probe is in use. */
  668. list_for_each_entry(tk, &probe_list, list)
  669. if (trace_probe_is_enabled(&tk->tp)) {
  670. ret = -EBUSY;
  671. goto end;
  672. }
  673. /* TODO: Use batch unregistration */
  674. while (!list_empty(&probe_list)) {
  675. tk = list_entry(probe_list.next, struct trace_kprobe, list);
  676. ret = unregister_trace_kprobe(tk);
  677. if (ret)
  678. goto end;
  679. free_trace_kprobe(tk);
  680. }
  681. end:
  682. mutex_unlock(&probe_lock);
  683. return ret;
  684. }
  685. /* Probes listing interfaces */
  686. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  687. {
  688. mutex_lock(&probe_lock);
  689. return seq_list_start(&probe_list, *pos);
  690. }
  691. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  692. {
  693. return seq_list_next(v, &probe_list, pos);
  694. }
  695. static void probes_seq_stop(struct seq_file *m, void *v)
  696. {
  697. mutex_unlock(&probe_lock);
  698. }
  699. static int probes_seq_show(struct seq_file *m, void *v)
  700. {
  701. struct trace_kprobe *tk = v;
  702. int i;
  703. seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
  704. seq_printf(m, ":%s/%s", tk->tp.call.class->system,
  705. trace_event_name(&tk->tp.call));
  706. if (!tk->symbol)
  707. seq_printf(m, " 0x%p", tk->rp.kp.addr);
  708. else if (tk->rp.kp.offset)
  709. seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
  710. tk->rp.kp.offset);
  711. else
  712. seq_printf(m, " %s", trace_kprobe_symbol(tk));
  713. for (i = 0; i < tk->tp.nr_args; i++)
  714. seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
  715. seq_putc(m, '\n');
  716. return 0;
  717. }
  718. static const struct seq_operations probes_seq_op = {
  719. .start = probes_seq_start,
  720. .next = probes_seq_next,
  721. .stop = probes_seq_stop,
  722. .show = probes_seq_show
  723. };
  724. static int probes_open(struct inode *inode, struct file *file)
  725. {
  726. int ret;
  727. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  728. ret = release_all_trace_kprobes();
  729. if (ret < 0)
  730. return ret;
  731. }
  732. return seq_open(file, &probes_seq_op);
  733. }
  734. static ssize_t probes_write(struct file *file, const char __user *buffer,
  735. size_t count, loff_t *ppos)
  736. {
  737. return traceprobe_probes_write(file, buffer, count, ppos,
  738. create_trace_kprobe);
  739. }
  740. static const struct file_operations kprobe_events_ops = {
  741. .owner = THIS_MODULE,
  742. .open = probes_open,
  743. .read = seq_read,
  744. .llseek = seq_lseek,
  745. .release = seq_release,
  746. .write = probes_write,
  747. };
  748. /* Probes profiling interfaces */
  749. static int probes_profile_seq_show(struct seq_file *m, void *v)
  750. {
  751. struct trace_kprobe *tk = v;
  752. seq_printf(m, " %-44s %15lu %15lu\n",
  753. trace_event_name(&tk->tp.call), tk->nhit,
  754. tk->rp.kp.nmissed);
  755. return 0;
  756. }
  757. static const struct seq_operations profile_seq_op = {
  758. .start = probes_seq_start,
  759. .next = probes_seq_next,
  760. .stop = probes_seq_stop,
  761. .show = probes_profile_seq_show
  762. };
  763. static int profile_open(struct inode *inode, struct file *file)
  764. {
  765. return seq_open(file, &profile_seq_op);
  766. }
  767. static const struct file_operations kprobe_profile_ops = {
  768. .owner = THIS_MODULE,
  769. .open = profile_open,
  770. .read = seq_read,
  771. .llseek = seq_lseek,
  772. .release = seq_release,
  773. };
  774. /* Kprobe handler */
  775. static nokprobe_inline void
  776. __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
  777. struct trace_event_file *trace_file)
  778. {
  779. struct kprobe_trace_entry_head *entry;
  780. struct ring_buffer_event *event;
  781. struct ring_buffer *buffer;
  782. int size, dsize, pc;
  783. unsigned long irq_flags;
  784. struct trace_event_call *call = &tk->tp.call;
  785. WARN_ON(call != trace_file->event_call);
  786. if (trace_trigger_soft_disabled(trace_file))
  787. return;
  788. local_save_flags(irq_flags);
  789. pc = preempt_count();
  790. dsize = __get_data_size(&tk->tp, regs);
  791. size = sizeof(*entry) + tk->tp.size + dsize;
  792. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  793. call->event.type,
  794. size, irq_flags, pc);
  795. if (!event)
  796. return;
  797. entry = ring_buffer_event_data(event);
  798. entry->ip = (unsigned long)tk->rp.kp.addr;
  799. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  800. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  801. entry, irq_flags, pc, regs);
  802. }
  803. static void
  804. kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
  805. {
  806. struct event_file_link *link;
  807. list_for_each_entry_rcu(link, &tk->tp.files, list)
  808. __kprobe_trace_func(tk, regs, link->file);
  809. }
  810. NOKPROBE_SYMBOL(kprobe_trace_func);
  811. /* Kretprobe handler */
  812. static nokprobe_inline void
  813. __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  814. struct pt_regs *regs,
  815. struct trace_event_file *trace_file)
  816. {
  817. struct kretprobe_trace_entry_head *entry;
  818. struct ring_buffer_event *event;
  819. struct ring_buffer *buffer;
  820. int size, pc, dsize;
  821. unsigned long irq_flags;
  822. struct trace_event_call *call = &tk->tp.call;
  823. WARN_ON(call != trace_file->event_call);
  824. if (trace_trigger_soft_disabled(trace_file))
  825. return;
  826. local_save_flags(irq_flags);
  827. pc = preempt_count();
  828. dsize = __get_data_size(&tk->tp, regs);
  829. size = sizeof(*entry) + tk->tp.size + dsize;
  830. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  831. call->event.type,
  832. size, irq_flags, pc);
  833. if (!event)
  834. return;
  835. entry = ring_buffer_event_data(event);
  836. entry->func = (unsigned long)tk->rp.kp.addr;
  837. entry->ret_ip = (unsigned long)ri->ret_addr;
  838. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  839. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  840. entry, irq_flags, pc, regs);
  841. }
  842. static void
  843. kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  844. struct pt_regs *regs)
  845. {
  846. struct event_file_link *link;
  847. list_for_each_entry_rcu(link, &tk->tp.files, list)
  848. __kretprobe_trace_func(tk, ri, regs, link->file);
  849. }
  850. NOKPROBE_SYMBOL(kretprobe_trace_func);
  851. /* Event entry printers */
  852. static enum print_line_t
  853. print_kprobe_event(struct trace_iterator *iter, int flags,
  854. struct trace_event *event)
  855. {
  856. struct kprobe_trace_entry_head *field;
  857. struct trace_seq *s = &iter->seq;
  858. struct trace_probe *tp;
  859. u8 *data;
  860. int i;
  861. field = (struct kprobe_trace_entry_head *)iter->ent;
  862. tp = container_of(event, struct trace_probe, call.event);
  863. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  864. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  865. goto out;
  866. trace_seq_putc(s, ')');
  867. data = (u8 *)&field[1];
  868. for (i = 0; i < tp->nr_args; i++)
  869. if (!tp->args[i].type->print(s, tp->args[i].name,
  870. data + tp->args[i].offset, field))
  871. goto out;
  872. trace_seq_putc(s, '\n');
  873. out:
  874. return trace_handle_return(s);
  875. }
  876. static enum print_line_t
  877. print_kretprobe_event(struct trace_iterator *iter, int flags,
  878. struct trace_event *event)
  879. {
  880. struct kretprobe_trace_entry_head *field;
  881. struct trace_seq *s = &iter->seq;
  882. struct trace_probe *tp;
  883. u8 *data;
  884. int i;
  885. field = (struct kretprobe_trace_entry_head *)iter->ent;
  886. tp = container_of(event, struct trace_probe, call.event);
  887. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  888. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  889. goto out;
  890. trace_seq_puts(s, " <- ");
  891. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  892. goto out;
  893. trace_seq_putc(s, ')');
  894. data = (u8 *)&field[1];
  895. for (i = 0; i < tp->nr_args; i++)
  896. if (!tp->args[i].type->print(s, tp->args[i].name,
  897. data + tp->args[i].offset, field))
  898. goto out;
  899. trace_seq_putc(s, '\n');
  900. out:
  901. return trace_handle_return(s);
  902. }
  903. static int kprobe_event_define_fields(struct trace_event_call *event_call)
  904. {
  905. int ret, i;
  906. struct kprobe_trace_entry_head field;
  907. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  908. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  909. /* Set argument names as fields */
  910. for (i = 0; i < tk->tp.nr_args; i++) {
  911. struct probe_arg *parg = &tk->tp.args[i];
  912. ret = trace_define_field(event_call, parg->type->fmttype,
  913. parg->name,
  914. sizeof(field) + parg->offset,
  915. parg->type->size,
  916. parg->type->is_signed,
  917. FILTER_OTHER);
  918. if (ret)
  919. return ret;
  920. }
  921. return 0;
  922. }
  923. static int kretprobe_event_define_fields(struct trace_event_call *event_call)
  924. {
  925. int ret, i;
  926. struct kretprobe_trace_entry_head field;
  927. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  928. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  929. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  930. /* Set argument names as fields */
  931. for (i = 0; i < tk->tp.nr_args; i++) {
  932. struct probe_arg *parg = &tk->tp.args[i];
  933. ret = trace_define_field(event_call, parg->type->fmttype,
  934. parg->name,
  935. sizeof(field) + parg->offset,
  936. parg->type->size,
  937. parg->type->is_signed,
  938. FILTER_OTHER);
  939. if (ret)
  940. return ret;
  941. }
  942. return 0;
  943. }
  944. #ifdef CONFIG_PERF_EVENTS
  945. /* Kprobe profile handler */
  946. static void
  947. kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
  948. {
  949. struct trace_event_call *call = &tk->tp.call;
  950. struct bpf_prog *prog = call->prog;
  951. struct kprobe_trace_entry_head *entry;
  952. struct hlist_head *head;
  953. int size, __size, dsize;
  954. int rctx;
  955. if (prog && !trace_call_bpf(prog, regs))
  956. return;
  957. head = this_cpu_ptr(call->perf_events);
  958. if (hlist_empty(head))
  959. return;
  960. dsize = __get_data_size(&tk->tp, regs);
  961. __size = sizeof(*entry) + tk->tp.size + dsize;
  962. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  963. size -= sizeof(u32);
  964. entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
  965. if (!entry)
  966. return;
  967. entry->ip = (unsigned long)tk->rp.kp.addr;
  968. memset(&entry[1], 0, dsize);
  969. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  970. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  971. }
  972. NOKPROBE_SYMBOL(kprobe_perf_func);
  973. /* Kretprobe profile handler */
  974. static void
  975. kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  976. struct pt_regs *regs)
  977. {
  978. struct trace_event_call *call = &tk->tp.call;
  979. struct bpf_prog *prog = call->prog;
  980. struct kretprobe_trace_entry_head *entry;
  981. struct hlist_head *head;
  982. int size, __size, dsize;
  983. int rctx;
  984. if (prog && !trace_call_bpf(prog, regs))
  985. return;
  986. head = this_cpu_ptr(call->perf_events);
  987. if (hlist_empty(head))
  988. return;
  989. dsize = __get_data_size(&tk->tp, regs);
  990. __size = sizeof(*entry) + tk->tp.size + dsize;
  991. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  992. size -= sizeof(u32);
  993. entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
  994. if (!entry)
  995. return;
  996. entry->func = (unsigned long)tk->rp.kp.addr;
  997. entry->ret_ip = (unsigned long)ri->ret_addr;
  998. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  999. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  1000. }
  1001. NOKPROBE_SYMBOL(kretprobe_perf_func);
  1002. #endif /* CONFIG_PERF_EVENTS */
  1003. /*
  1004. * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
  1005. *
  1006. * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
  1007. * lockless, but we can't race with this __init function.
  1008. */
  1009. static int kprobe_register(struct trace_event_call *event,
  1010. enum trace_reg type, void *data)
  1011. {
  1012. struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
  1013. struct trace_event_file *file = data;
  1014. switch (type) {
  1015. case TRACE_REG_REGISTER:
  1016. return enable_trace_kprobe(tk, file);
  1017. case TRACE_REG_UNREGISTER:
  1018. return disable_trace_kprobe(tk, file);
  1019. #ifdef CONFIG_PERF_EVENTS
  1020. case TRACE_REG_PERF_REGISTER:
  1021. return enable_trace_kprobe(tk, NULL);
  1022. case TRACE_REG_PERF_UNREGISTER:
  1023. return disable_trace_kprobe(tk, NULL);
  1024. case TRACE_REG_PERF_OPEN:
  1025. case TRACE_REG_PERF_CLOSE:
  1026. case TRACE_REG_PERF_ADD:
  1027. case TRACE_REG_PERF_DEL:
  1028. return 0;
  1029. #endif
  1030. }
  1031. return 0;
  1032. }
  1033. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1034. {
  1035. struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
  1036. tk->nhit++;
  1037. if (tk->tp.flags & TP_FLAG_TRACE)
  1038. kprobe_trace_func(tk, regs);
  1039. #ifdef CONFIG_PERF_EVENTS
  1040. if (tk->tp.flags & TP_FLAG_PROFILE)
  1041. kprobe_perf_func(tk, regs);
  1042. #endif
  1043. return 0; /* We don't tweek kernel, so just return 0 */
  1044. }
  1045. NOKPROBE_SYMBOL(kprobe_dispatcher);
  1046. static int
  1047. kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1048. {
  1049. struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
  1050. tk->nhit++;
  1051. if (tk->tp.flags & TP_FLAG_TRACE)
  1052. kretprobe_trace_func(tk, ri, regs);
  1053. #ifdef CONFIG_PERF_EVENTS
  1054. if (tk->tp.flags & TP_FLAG_PROFILE)
  1055. kretprobe_perf_func(tk, ri, regs);
  1056. #endif
  1057. return 0; /* We don't tweek kernel, so just return 0 */
  1058. }
  1059. NOKPROBE_SYMBOL(kretprobe_dispatcher);
  1060. static struct trace_event_functions kretprobe_funcs = {
  1061. .trace = print_kretprobe_event
  1062. };
  1063. static struct trace_event_functions kprobe_funcs = {
  1064. .trace = print_kprobe_event
  1065. };
  1066. static int register_kprobe_event(struct trace_kprobe *tk)
  1067. {
  1068. struct trace_event_call *call = &tk->tp.call;
  1069. int ret;
  1070. /* Initialize trace_event_call */
  1071. INIT_LIST_HEAD(&call->class->fields);
  1072. if (trace_kprobe_is_return(tk)) {
  1073. call->event.funcs = &kretprobe_funcs;
  1074. call->class->define_fields = kretprobe_event_define_fields;
  1075. } else {
  1076. call->event.funcs = &kprobe_funcs;
  1077. call->class->define_fields = kprobe_event_define_fields;
  1078. }
  1079. if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
  1080. return -ENOMEM;
  1081. ret = register_trace_event(&call->event);
  1082. if (!ret) {
  1083. kfree(call->print_fmt);
  1084. return -ENODEV;
  1085. }
  1086. call->flags = TRACE_EVENT_FL_KPROBE;
  1087. call->class->reg = kprobe_register;
  1088. call->data = tk;
  1089. ret = trace_add_event_call(call);
  1090. if (ret) {
  1091. pr_info("Failed to register kprobe event: %s\n",
  1092. trace_event_name(call));
  1093. kfree(call->print_fmt);
  1094. unregister_trace_event(&call->event);
  1095. }
  1096. return ret;
  1097. }
  1098. static int unregister_kprobe_event(struct trace_kprobe *tk)
  1099. {
  1100. int ret;
  1101. /* tp->event is unregistered in trace_remove_event_call() */
  1102. ret = trace_remove_event_call(&tk->tp.call);
  1103. if (!ret)
  1104. kfree(tk->tp.call.print_fmt);
  1105. return ret;
  1106. }
  1107. /* Make a tracefs interface for controlling probe points */
  1108. static __init int init_kprobe_trace(void)
  1109. {
  1110. struct dentry *d_tracer;
  1111. struct dentry *entry;
  1112. if (register_module_notifier(&trace_kprobe_module_nb))
  1113. return -EINVAL;
  1114. d_tracer = tracing_init_dentry();
  1115. if (IS_ERR(d_tracer))
  1116. return 0;
  1117. entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
  1118. NULL, &kprobe_events_ops);
  1119. /* Event list interface */
  1120. if (!entry)
  1121. pr_warning("Could not create tracefs "
  1122. "'kprobe_events' entry\n");
  1123. /* Profile interface */
  1124. entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
  1125. NULL, &kprobe_profile_ops);
  1126. if (!entry)
  1127. pr_warning("Could not create tracefs "
  1128. "'kprobe_profile' entry\n");
  1129. return 0;
  1130. }
  1131. fs_initcall(init_kprobe_trace);
  1132. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1133. /*
  1134. * The "__used" keeps gcc from removing the function symbol
  1135. * from the kallsyms table.
  1136. */
  1137. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1138. int a4, int a5, int a6)
  1139. {
  1140. return a1 + a2 + a3 + a4 + a5 + a6;
  1141. }
  1142. static struct trace_event_file *
  1143. find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
  1144. {
  1145. struct trace_event_file *file;
  1146. list_for_each_entry(file, &tr->events, list)
  1147. if (file->event_call == &tk->tp.call)
  1148. return file;
  1149. return NULL;
  1150. }
  1151. /*
  1152. * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
  1153. * stage, we can do this lockless.
  1154. */
  1155. static __init int kprobe_trace_self_tests_init(void)
  1156. {
  1157. int ret, warn = 0;
  1158. int (*target)(int, int, int, int, int, int);
  1159. struct trace_kprobe *tk;
  1160. struct trace_event_file *file;
  1161. if (tracing_is_disabled())
  1162. return -ENODEV;
  1163. target = kprobe_trace_selftest_target;
  1164. pr_info("Testing kprobe tracing: ");
  1165. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1166. "$stack $stack0 +0($stack)",
  1167. create_trace_kprobe);
  1168. if (WARN_ON_ONCE(ret)) {
  1169. pr_warn("error on probing function entry.\n");
  1170. warn++;
  1171. } else {
  1172. /* Enable trace point */
  1173. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1174. if (WARN_ON_ONCE(tk == NULL)) {
  1175. pr_warn("error on getting new probe.\n");
  1176. warn++;
  1177. } else {
  1178. file = find_trace_probe_file(tk, top_trace_array());
  1179. if (WARN_ON_ONCE(file == NULL)) {
  1180. pr_warn("error on getting probe file.\n");
  1181. warn++;
  1182. } else
  1183. enable_trace_kprobe(tk, file);
  1184. }
  1185. }
  1186. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1187. "$retval", create_trace_kprobe);
  1188. if (WARN_ON_ONCE(ret)) {
  1189. pr_warn("error on probing function return.\n");
  1190. warn++;
  1191. } else {
  1192. /* Enable trace point */
  1193. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1194. if (WARN_ON_ONCE(tk == NULL)) {
  1195. pr_warn("error on getting 2nd new probe.\n");
  1196. warn++;
  1197. } else {
  1198. file = find_trace_probe_file(tk, top_trace_array());
  1199. if (WARN_ON_ONCE(file == NULL)) {
  1200. pr_warn("error on getting probe file.\n");
  1201. warn++;
  1202. } else
  1203. enable_trace_kprobe(tk, file);
  1204. }
  1205. }
  1206. if (warn)
  1207. goto end;
  1208. ret = target(1, 2, 3, 4, 5, 6);
  1209. /* Disable trace points before removing it */
  1210. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1211. if (WARN_ON_ONCE(tk == NULL)) {
  1212. pr_warn("error on getting test probe.\n");
  1213. warn++;
  1214. } else {
  1215. file = find_trace_probe_file(tk, top_trace_array());
  1216. if (WARN_ON_ONCE(file == NULL)) {
  1217. pr_warn("error on getting probe file.\n");
  1218. warn++;
  1219. } else
  1220. disable_trace_kprobe(tk, file);
  1221. }
  1222. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1223. if (WARN_ON_ONCE(tk == NULL)) {
  1224. pr_warn("error on getting 2nd test probe.\n");
  1225. warn++;
  1226. } else {
  1227. file = find_trace_probe_file(tk, top_trace_array());
  1228. if (WARN_ON_ONCE(file == NULL)) {
  1229. pr_warn("error on getting probe file.\n");
  1230. warn++;
  1231. } else
  1232. disable_trace_kprobe(tk, file);
  1233. }
  1234. ret = traceprobe_command("-:testprobe", create_trace_kprobe);
  1235. if (WARN_ON_ONCE(ret)) {
  1236. pr_warn("error on deleting a probe.\n");
  1237. warn++;
  1238. }
  1239. ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
  1240. if (WARN_ON_ONCE(ret)) {
  1241. pr_warn("error on deleting a probe.\n");
  1242. warn++;
  1243. }
  1244. end:
  1245. release_all_trace_kprobes();
  1246. if (warn)
  1247. pr_cont("NG: Some tests are failed. Please check them.\n");
  1248. else
  1249. pr_cont("OK\n");
  1250. return 0;
  1251. }
  1252. late_initcall(kprobe_trace_self_tests_init);
  1253. #endif