trace_kprobe.c 36 KB

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