trace_kprobe.c 36 KB

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