trace_kprobe.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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. 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(ftrace_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 ftrace_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 ftrace_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(ftrace_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. ftrace_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. if (ret) {
  659. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  660. goto error;
  661. }
  662. }
  663. ret = register_trace_kprobe(tk);
  664. if (ret)
  665. goto error;
  666. return 0;
  667. error:
  668. free_trace_kprobe(tk);
  669. return ret;
  670. }
  671. static int release_all_trace_kprobes(void)
  672. {
  673. struct trace_kprobe *tk;
  674. int ret = 0;
  675. mutex_lock(&probe_lock);
  676. /* Ensure no probe is in use. */
  677. list_for_each_entry(tk, &probe_list, list)
  678. if (trace_probe_is_enabled(&tk->tp)) {
  679. ret = -EBUSY;
  680. goto end;
  681. }
  682. /* TODO: Use batch unregistration */
  683. while (!list_empty(&probe_list)) {
  684. tk = list_entry(probe_list.next, struct trace_kprobe, list);
  685. ret = unregister_trace_kprobe(tk);
  686. if (ret)
  687. goto end;
  688. free_trace_kprobe(tk);
  689. }
  690. end:
  691. mutex_unlock(&probe_lock);
  692. return ret;
  693. }
  694. /* Probes listing interfaces */
  695. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  696. {
  697. mutex_lock(&probe_lock);
  698. return seq_list_start(&probe_list, *pos);
  699. }
  700. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  701. {
  702. return seq_list_next(v, &probe_list, pos);
  703. }
  704. static void probes_seq_stop(struct seq_file *m, void *v)
  705. {
  706. mutex_unlock(&probe_lock);
  707. }
  708. static int probes_seq_show(struct seq_file *m, void *v)
  709. {
  710. struct trace_kprobe *tk = v;
  711. int i;
  712. seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
  713. seq_printf(m, ":%s/%s", tk->tp.call.class->system,
  714. ftrace_event_name(&tk->tp.call));
  715. if (!tk->symbol)
  716. seq_printf(m, " 0x%p", tk->rp.kp.addr);
  717. else if (tk->rp.kp.offset)
  718. seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
  719. tk->rp.kp.offset);
  720. else
  721. seq_printf(m, " %s", trace_kprobe_symbol(tk));
  722. for (i = 0; i < tk->tp.nr_args; i++)
  723. seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
  724. seq_putc(m, '\n');
  725. return 0;
  726. }
  727. static const struct seq_operations probes_seq_op = {
  728. .start = probes_seq_start,
  729. .next = probes_seq_next,
  730. .stop = probes_seq_stop,
  731. .show = probes_seq_show
  732. };
  733. static int probes_open(struct inode *inode, struct file *file)
  734. {
  735. int ret;
  736. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  737. ret = release_all_trace_kprobes();
  738. if (ret < 0)
  739. return ret;
  740. }
  741. return seq_open(file, &probes_seq_op);
  742. }
  743. static ssize_t probes_write(struct file *file, const char __user *buffer,
  744. size_t count, loff_t *ppos)
  745. {
  746. return traceprobe_probes_write(file, buffer, count, ppos,
  747. create_trace_kprobe);
  748. }
  749. static const struct file_operations kprobe_events_ops = {
  750. .owner = THIS_MODULE,
  751. .open = probes_open,
  752. .read = seq_read,
  753. .llseek = seq_lseek,
  754. .release = seq_release,
  755. .write = probes_write,
  756. };
  757. /* Probes profiling interfaces */
  758. static int probes_profile_seq_show(struct seq_file *m, void *v)
  759. {
  760. struct trace_kprobe *tk = v;
  761. seq_printf(m, " %-44s %15lu %15lu\n",
  762. ftrace_event_name(&tk->tp.call), tk->nhit,
  763. tk->rp.kp.nmissed);
  764. return 0;
  765. }
  766. static const struct seq_operations profile_seq_op = {
  767. .start = probes_seq_start,
  768. .next = probes_seq_next,
  769. .stop = probes_seq_stop,
  770. .show = probes_profile_seq_show
  771. };
  772. static int profile_open(struct inode *inode, struct file *file)
  773. {
  774. return seq_open(file, &profile_seq_op);
  775. }
  776. static const struct file_operations kprobe_profile_ops = {
  777. .owner = THIS_MODULE,
  778. .open = profile_open,
  779. .read = seq_read,
  780. .llseek = seq_lseek,
  781. .release = seq_release,
  782. };
  783. /* Kprobe handler */
  784. static nokprobe_inline void
  785. __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
  786. struct ftrace_event_file *ftrace_file)
  787. {
  788. struct kprobe_trace_entry_head *entry;
  789. struct ring_buffer_event *event;
  790. struct ring_buffer *buffer;
  791. int size, dsize, pc;
  792. unsigned long irq_flags;
  793. struct ftrace_event_call *call = &tk->tp.call;
  794. WARN_ON(call != ftrace_file->event_call);
  795. if (ftrace_trigger_soft_disabled(ftrace_file))
  796. return;
  797. local_save_flags(irq_flags);
  798. pc = preempt_count();
  799. dsize = __get_data_size(&tk->tp, regs);
  800. size = sizeof(*entry) + tk->tp.size + dsize;
  801. event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
  802. call->event.type,
  803. size, irq_flags, pc);
  804. if (!event)
  805. return;
  806. entry = ring_buffer_event_data(event);
  807. entry->ip = (unsigned long)tk->rp.kp.addr;
  808. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  809. event_trigger_unlock_commit_regs(ftrace_file, buffer, event,
  810. entry, irq_flags, pc, regs);
  811. }
  812. static void
  813. kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
  814. {
  815. struct event_file_link *link;
  816. list_for_each_entry_rcu(link, &tk->tp.files, list)
  817. __kprobe_trace_func(tk, regs, link->file);
  818. }
  819. NOKPROBE_SYMBOL(kprobe_trace_func);
  820. /* Kretprobe handler */
  821. static nokprobe_inline void
  822. __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  823. struct pt_regs *regs,
  824. struct ftrace_event_file *ftrace_file)
  825. {
  826. struct kretprobe_trace_entry_head *entry;
  827. struct ring_buffer_event *event;
  828. struct ring_buffer *buffer;
  829. int size, pc, dsize;
  830. unsigned long irq_flags;
  831. struct ftrace_event_call *call = &tk->tp.call;
  832. WARN_ON(call != ftrace_file->event_call);
  833. if (ftrace_trigger_soft_disabled(ftrace_file))
  834. return;
  835. local_save_flags(irq_flags);
  836. pc = preempt_count();
  837. dsize = __get_data_size(&tk->tp, regs);
  838. size = sizeof(*entry) + tk->tp.size + dsize;
  839. event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
  840. call->event.type,
  841. size, irq_flags, pc);
  842. if (!event)
  843. return;
  844. entry = ring_buffer_event_data(event);
  845. entry->func = (unsigned long)tk->rp.kp.addr;
  846. entry->ret_ip = (unsigned long)ri->ret_addr;
  847. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  848. event_trigger_unlock_commit_regs(ftrace_file, buffer, event,
  849. entry, irq_flags, pc, regs);
  850. }
  851. static void
  852. kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  853. struct pt_regs *regs)
  854. {
  855. struct event_file_link *link;
  856. list_for_each_entry_rcu(link, &tk->tp.files, list)
  857. __kretprobe_trace_func(tk, ri, regs, link->file);
  858. }
  859. NOKPROBE_SYMBOL(kretprobe_trace_func);
  860. /* Event entry printers */
  861. static enum print_line_t
  862. print_kprobe_event(struct trace_iterator *iter, int flags,
  863. struct trace_event *event)
  864. {
  865. struct kprobe_trace_entry_head *field;
  866. struct trace_seq *s = &iter->seq;
  867. struct trace_probe *tp;
  868. u8 *data;
  869. int i;
  870. field = (struct kprobe_trace_entry_head *)iter->ent;
  871. tp = container_of(event, struct trace_probe, call.event);
  872. trace_seq_printf(s, "%s: (", ftrace_event_name(&tp->call));
  873. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  874. goto out;
  875. trace_seq_putc(s, ')');
  876. data = (u8 *)&field[1];
  877. for (i = 0; i < tp->nr_args; i++)
  878. if (!tp->args[i].type->print(s, tp->args[i].name,
  879. data + tp->args[i].offset, field))
  880. goto out;
  881. trace_seq_putc(s, '\n');
  882. out:
  883. return trace_handle_return(s);
  884. }
  885. static enum print_line_t
  886. print_kretprobe_event(struct trace_iterator *iter, int flags,
  887. struct trace_event *event)
  888. {
  889. struct kretprobe_trace_entry_head *field;
  890. struct trace_seq *s = &iter->seq;
  891. struct trace_probe *tp;
  892. u8 *data;
  893. int i;
  894. field = (struct kretprobe_trace_entry_head *)iter->ent;
  895. tp = container_of(event, struct trace_probe, call.event);
  896. trace_seq_printf(s, "%s: (", ftrace_event_name(&tp->call));
  897. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  898. goto out;
  899. trace_seq_puts(s, " <- ");
  900. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  901. goto out;
  902. trace_seq_putc(s, ')');
  903. data = (u8 *)&field[1];
  904. for (i = 0; i < tp->nr_args; i++)
  905. if (!tp->args[i].type->print(s, tp->args[i].name,
  906. data + tp->args[i].offset, field))
  907. goto out;
  908. trace_seq_putc(s, '\n');
  909. out:
  910. return trace_handle_return(s);
  911. }
  912. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  913. {
  914. int ret, i;
  915. struct kprobe_trace_entry_head field;
  916. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  917. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  918. /* Set argument names as fields */
  919. for (i = 0; i < tk->tp.nr_args; i++) {
  920. struct probe_arg *parg = &tk->tp.args[i];
  921. ret = trace_define_field(event_call, parg->type->fmttype,
  922. parg->name,
  923. sizeof(field) + parg->offset,
  924. parg->type->size,
  925. parg->type->is_signed,
  926. FILTER_OTHER);
  927. if (ret)
  928. return ret;
  929. }
  930. return 0;
  931. }
  932. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  933. {
  934. int ret, i;
  935. struct kretprobe_trace_entry_head field;
  936. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  937. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  938. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  939. /* Set argument names as fields */
  940. for (i = 0; i < tk->tp.nr_args; i++) {
  941. struct probe_arg *parg = &tk->tp.args[i];
  942. ret = trace_define_field(event_call, parg->type->fmttype,
  943. parg->name,
  944. sizeof(field) + parg->offset,
  945. parg->type->size,
  946. parg->type->is_signed,
  947. FILTER_OTHER);
  948. if (ret)
  949. return ret;
  950. }
  951. return 0;
  952. }
  953. #ifdef CONFIG_PERF_EVENTS
  954. /* Kprobe profile handler */
  955. static void
  956. kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
  957. {
  958. struct ftrace_event_call *call = &tk->tp.call;
  959. struct kprobe_trace_entry_head *entry;
  960. struct hlist_head *head;
  961. int size, __size, dsize;
  962. int rctx;
  963. head = this_cpu_ptr(call->perf_events);
  964. if (hlist_empty(head))
  965. return;
  966. dsize = __get_data_size(&tk->tp, regs);
  967. __size = sizeof(*entry) + tk->tp.size + dsize;
  968. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  969. size -= sizeof(u32);
  970. entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
  971. if (!entry)
  972. return;
  973. entry->ip = (unsigned long)tk->rp.kp.addr;
  974. memset(&entry[1], 0, dsize);
  975. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  976. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  977. }
  978. NOKPROBE_SYMBOL(kprobe_perf_func);
  979. /* Kretprobe profile handler */
  980. static void
  981. kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  982. struct pt_regs *regs)
  983. {
  984. struct ftrace_event_call *call = &tk->tp.call;
  985. struct kretprobe_trace_entry_head *entry;
  986. struct hlist_head *head;
  987. int size, __size, dsize;
  988. int rctx;
  989. head = this_cpu_ptr(call->perf_events);
  990. if (hlist_empty(head))
  991. return;
  992. dsize = __get_data_size(&tk->tp, regs);
  993. __size = sizeof(*entry) + tk->tp.size + dsize;
  994. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  995. size -= sizeof(u32);
  996. entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
  997. if (!entry)
  998. return;
  999. entry->func = (unsigned long)tk->rp.kp.addr;
  1000. entry->ret_ip = (unsigned long)ri->ret_addr;
  1001. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  1002. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  1003. }
  1004. NOKPROBE_SYMBOL(kretprobe_perf_func);
  1005. #endif /* CONFIG_PERF_EVENTS */
  1006. /*
  1007. * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
  1008. *
  1009. * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
  1010. * lockless, but we can't race with this __init function.
  1011. */
  1012. static int kprobe_register(struct ftrace_event_call *event,
  1013. enum trace_reg type, void *data)
  1014. {
  1015. struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
  1016. struct ftrace_event_file *file = data;
  1017. switch (type) {
  1018. case TRACE_REG_REGISTER:
  1019. return enable_trace_kprobe(tk, file);
  1020. case TRACE_REG_UNREGISTER:
  1021. return disable_trace_kprobe(tk, file);
  1022. #ifdef CONFIG_PERF_EVENTS
  1023. case TRACE_REG_PERF_REGISTER:
  1024. return enable_trace_kprobe(tk, NULL);
  1025. case TRACE_REG_PERF_UNREGISTER:
  1026. return disable_trace_kprobe(tk, NULL);
  1027. case TRACE_REG_PERF_OPEN:
  1028. case TRACE_REG_PERF_CLOSE:
  1029. case TRACE_REG_PERF_ADD:
  1030. case TRACE_REG_PERF_DEL:
  1031. return 0;
  1032. #endif
  1033. }
  1034. return 0;
  1035. }
  1036. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1037. {
  1038. struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
  1039. tk->nhit++;
  1040. if (tk->tp.flags & TP_FLAG_TRACE)
  1041. kprobe_trace_func(tk, regs);
  1042. #ifdef CONFIG_PERF_EVENTS
  1043. if (tk->tp.flags & TP_FLAG_PROFILE)
  1044. kprobe_perf_func(tk, regs);
  1045. #endif
  1046. return 0; /* We don't tweek kernel, so just return 0 */
  1047. }
  1048. NOKPROBE_SYMBOL(kprobe_dispatcher);
  1049. static int
  1050. kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1051. {
  1052. struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
  1053. tk->nhit++;
  1054. if (tk->tp.flags & TP_FLAG_TRACE)
  1055. kretprobe_trace_func(tk, ri, regs);
  1056. #ifdef CONFIG_PERF_EVENTS
  1057. if (tk->tp.flags & TP_FLAG_PROFILE)
  1058. kretprobe_perf_func(tk, ri, regs);
  1059. #endif
  1060. return 0; /* We don't tweek kernel, so just return 0 */
  1061. }
  1062. NOKPROBE_SYMBOL(kretprobe_dispatcher);
  1063. static struct trace_event_functions kretprobe_funcs = {
  1064. .trace = print_kretprobe_event
  1065. };
  1066. static struct trace_event_functions kprobe_funcs = {
  1067. .trace = print_kprobe_event
  1068. };
  1069. static int register_kprobe_event(struct trace_kprobe *tk)
  1070. {
  1071. struct ftrace_event_call *call = &tk->tp.call;
  1072. int ret;
  1073. /* Initialize ftrace_event_call */
  1074. INIT_LIST_HEAD(&call->class->fields);
  1075. if (trace_kprobe_is_return(tk)) {
  1076. call->event.funcs = &kretprobe_funcs;
  1077. call->class->define_fields = kretprobe_event_define_fields;
  1078. } else {
  1079. call->event.funcs = &kprobe_funcs;
  1080. call->class->define_fields = kprobe_event_define_fields;
  1081. }
  1082. if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
  1083. return -ENOMEM;
  1084. ret = register_ftrace_event(&call->event);
  1085. if (!ret) {
  1086. kfree(call->print_fmt);
  1087. return -ENODEV;
  1088. }
  1089. call->flags = 0;
  1090. call->class->reg = kprobe_register;
  1091. call->data = tk;
  1092. ret = trace_add_event_call(call);
  1093. if (ret) {
  1094. pr_info("Failed to register kprobe event: %s\n",
  1095. ftrace_event_name(call));
  1096. kfree(call->print_fmt);
  1097. unregister_ftrace_event(&call->event);
  1098. }
  1099. return ret;
  1100. }
  1101. static int unregister_kprobe_event(struct trace_kprobe *tk)
  1102. {
  1103. int ret;
  1104. /* tp->event is unregistered in trace_remove_event_call() */
  1105. ret = trace_remove_event_call(&tk->tp.call);
  1106. if (!ret)
  1107. kfree(tk->tp.call.print_fmt);
  1108. return ret;
  1109. }
  1110. /* Make a debugfs interface for controlling probe points */
  1111. static __init int init_kprobe_trace(void)
  1112. {
  1113. struct dentry *d_tracer;
  1114. struct dentry *entry;
  1115. if (register_module_notifier(&trace_kprobe_module_nb))
  1116. return -EINVAL;
  1117. d_tracer = tracing_init_dentry();
  1118. if (IS_ERR(d_tracer))
  1119. return 0;
  1120. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1121. NULL, &kprobe_events_ops);
  1122. /* Event list interface */
  1123. if (!entry)
  1124. pr_warning("Could not create debugfs "
  1125. "'kprobe_events' entry\n");
  1126. /* Profile interface */
  1127. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1128. NULL, &kprobe_profile_ops);
  1129. if (!entry)
  1130. pr_warning("Could not create debugfs "
  1131. "'kprobe_profile' entry\n");
  1132. return 0;
  1133. }
  1134. fs_initcall(init_kprobe_trace);
  1135. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1136. /*
  1137. * The "__used" keeps gcc from removing the function symbol
  1138. * from the kallsyms table.
  1139. */
  1140. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1141. int a4, int a5, int a6)
  1142. {
  1143. return a1 + a2 + a3 + a4 + a5 + a6;
  1144. }
  1145. static struct ftrace_event_file *
  1146. find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
  1147. {
  1148. struct ftrace_event_file *file;
  1149. list_for_each_entry(file, &tr->events, list)
  1150. if (file->event_call == &tk->tp.call)
  1151. return file;
  1152. return NULL;
  1153. }
  1154. /*
  1155. * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
  1156. * stage, we can do this lockless.
  1157. */
  1158. static __init int kprobe_trace_self_tests_init(void)
  1159. {
  1160. int ret, warn = 0;
  1161. int (*target)(int, int, int, int, int, int);
  1162. struct trace_kprobe *tk;
  1163. struct ftrace_event_file *file;
  1164. if (tracing_is_disabled())
  1165. return -ENODEV;
  1166. target = kprobe_trace_selftest_target;
  1167. pr_info("Testing kprobe tracing: ");
  1168. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1169. "$stack $stack0 +0($stack)",
  1170. create_trace_kprobe);
  1171. if (WARN_ON_ONCE(ret)) {
  1172. pr_warn("error on probing function entry.\n");
  1173. warn++;
  1174. } else {
  1175. /* Enable trace point */
  1176. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1177. if (WARN_ON_ONCE(tk == NULL)) {
  1178. pr_warn("error on getting new probe.\n");
  1179. warn++;
  1180. } else {
  1181. file = find_trace_probe_file(tk, top_trace_array());
  1182. if (WARN_ON_ONCE(file == NULL)) {
  1183. pr_warn("error on getting probe file.\n");
  1184. warn++;
  1185. } else
  1186. enable_trace_kprobe(tk, file);
  1187. }
  1188. }
  1189. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1190. "$retval", create_trace_kprobe);
  1191. if (WARN_ON_ONCE(ret)) {
  1192. pr_warn("error on probing function return.\n");
  1193. warn++;
  1194. } else {
  1195. /* Enable trace point */
  1196. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1197. if (WARN_ON_ONCE(tk == NULL)) {
  1198. pr_warn("error on getting 2nd new probe.\n");
  1199. warn++;
  1200. } else {
  1201. file = find_trace_probe_file(tk, top_trace_array());
  1202. if (WARN_ON_ONCE(file == NULL)) {
  1203. pr_warn("error on getting probe file.\n");
  1204. warn++;
  1205. } else
  1206. enable_trace_kprobe(tk, file);
  1207. }
  1208. }
  1209. if (warn)
  1210. goto end;
  1211. ret = target(1, 2, 3, 4, 5, 6);
  1212. /* Disable trace points before removing it */
  1213. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1214. if (WARN_ON_ONCE(tk == NULL)) {
  1215. pr_warn("error on getting test probe.\n");
  1216. warn++;
  1217. } else {
  1218. file = find_trace_probe_file(tk, top_trace_array());
  1219. if (WARN_ON_ONCE(file == NULL)) {
  1220. pr_warn("error on getting probe file.\n");
  1221. warn++;
  1222. } else
  1223. disable_trace_kprobe(tk, file);
  1224. }
  1225. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1226. if (WARN_ON_ONCE(tk == NULL)) {
  1227. pr_warn("error on getting 2nd test probe.\n");
  1228. warn++;
  1229. } else {
  1230. file = find_trace_probe_file(tk, top_trace_array());
  1231. if (WARN_ON_ONCE(file == NULL)) {
  1232. pr_warn("error on getting probe file.\n");
  1233. warn++;
  1234. } else
  1235. disable_trace_kprobe(tk, file);
  1236. }
  1237. ret = traceprobe_command("-:testprobe", create_trace_kprobe);
  1238. if (WARN_ON_ONCE(ret)) {
  1239. pr_warn("error on deleting a probe.\n");
  1240. warn++;
  1241. }
  1242. ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
  1243. if (WARN_ON_ONCE(ret)) {
  1244. pr_warn("error on deleting a probe.\n");
  1245. warn++;
  1246. }
  1247. end:
  1248. release_all_trace_kprobes();
  1249. if (warn)
  1250. pr_cont("NG: Some tests are failed. Please check them.\n");
  1251. else
  1252. pr_cont("OK\n");
  1253. return 0;
  1254. }
  1255. late_initcall(kprobe_trace_self_tests_init);
  1256. #endif