trace_kprobe.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  498. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  499. * %REG : fetch register REG
  500. * Dereferencing memory fetch:
  501. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  502. * Alias name of args:
  503. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  504. * Type of args:
  505. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  506. */
  507. struct trace_kprobe *tk;
  508. int i, ret = 0;
  509. bool is_return = false, is_delete = false;
  510. char *symbol = NULL, *event = NULL, *group = NULL;
  511. char *arg;
  512. unsigned long offset = 0;
  513. void *addr = NULL;
  514. char buf[MAX_EVENT_NAME_LEN];
  515. /* argc must be >= 1 */
  516. if (argv[0][0] == 'p')
  517. is_return = false;
  518. else if (argv[0][0] == 'r')
  519. is_return = true;
  520. else if (argv[0][0] == '-')
  521. is_delete = true;
  522. else {
  523. pr_info("Probe definition must be started with 'p', 'r' or"
  524. " '-'.\n");
  525. return -EINVAL;
  526. }
  527. if (argv[0][1] == ':') {
  528. event = &argv[0][2];
  529. if (strchr(event, '/')) {
  530. group = event;
  531. event = strchr(group, '/') + 1;
  532. event[-1] = '\0';
  533. if (strlen(group) == 0) {
  534. pr_info("Group name is not specified\n");
  535. return -EINVAL;
  536. }
  537. }
  538. if (strlen(event) == 0) {
  539. pr_info("Event name is not specified\n");
  540. return -EINVAL;
  541. }
  542. }
  543. if (!group)
  544. group = KPROBE_EVENT_SYSTEM;
  545. if (is_delete) {
  546. if (!event) {
  547. pr_info("Delete command needs an event name.\n");
  548. return -EINVAL;
  549. }
  550. mutex_lock(&probe_lock);
  551. tk = find_trace_kprobe(event, group);
  552. if (!tk) {
  553. mutex_unlock(&probe_lock);
  554. pr_info("Event %s/%s doesn't exist.\n", group, event);
  555. return -ENOENT;
  556. }
  557. /* delete an event */
  558. ret = unregister_trace_kprobe(tk);
  559. if (ret == 0)
  560. free_trace_kprobe(tk);
  561. mutex_unlock(&probe_lock);
  562. return ret;
  563. }
  564. if (argc < 2) {
  565. pr_info("Probe point is not specified.\n");
  566. return -EINVAL;
  567. }
  568. if (isdigit(argv[1][0])) {
  569. if (is_return) {
  570. pr_info("Return probe point must be a symbol.\n");
  571. return -EINVAL;
  572. }
  573. /* an address specified */
  574. ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr);
  575. if (ret) {
  576. pr_info("Failed to parse address.\n");
  577. return ret;
  578. }
  579. } else {
  580. /* a symbol specified */
  581. symbol = argv[1];
  582. /* TODO: support .init module functions */
  583. ret = traceprobe_split_symbol_offset(symbol, &offset);
  584. if (ret) {
  585. pr_info("Failed to parse symbol.\n");
  586. return ret;
  587. }
  588. if (offset && is_return) {
  589. pr_info("Return probe must be used without offset.\n");
  590. return -EINVAL;
  591. }
  592. }
  593. argc -= 2; argv += 2;
  594. /* setup a probe */
  595. if (!event) {
  596. /* Make a new event name */
  597. if (symbol)
  598. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  599. is_return ? 'r' : 'p', symbol, offset);
  600. else
  601. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  602. is_return ? 'r' : 'p', addr);
  603. event = buf;
  604. }
  605. tk = alloc_trace_kprobe(group, event, addr, symbol, offset, argc,
  606. is_return);
  607. if (IS_ERR(tk)) {
  608. pr_info("Failed to allocate trace_probe.(%d)\n",
  609. (int)PTR_ERR(tk));
  610. return PTR_ERR(tk);
  611. }
  612. /* parse arguments */
  613. ret = 0;
  614. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  615. struct probe_arg *parg = &tk->tp.args[i];
  616. /* Increment count for freeing args in error case */
  617. tk->tp.nr_args++;
  618. /* Parse argument name */
  619. arg = strchr(argv[i], '=');
  620. if (arg) {
  621. *arg++ = '\0';
  622. parg->name = kstrdup(argv[i], GFP_KERNEL);
  623. } else {
  624. arg = argv[i];
  625. /* If argument name is omitted, set "argN" */
  626. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  627. parg->name = kstrdup(buf, GFP_KERNEL);
  628. }
  629. if (!parg->name) {
  630. pr_info("Failed to allocate argument[%d] name.\n", i);
  631. ret = -ENOMEM;
  632. goto error;
  633. }
  634. if (!is_good_name(parg->name)) {
  635. pr_info("Invalid argument[%d] name: %s\n",
  636. i, parg->name);
  637. ret = -EINVAL;
  638. goto error;
  639. }
  640. if (traceprobe_conflict_field_name(parg->name,
  641. tk->tp.args, i)) {
  642. pr_info("Argument[%d] name '%s' conflicts with "
  643. "another field.\n", i, argv[i]);
  644. ret = -EINVAL;
  645. goto error;
  646. }
  647. /* Parse fetch argument */
  648. ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
  649. is_return, true,
  650. kprobes_fetch_type_table);
  651. if (ret) {
  652. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  653. goto error;
  654. }
  655. }
  656. ret = register_trace_kprobe(tk);
  657. if (ret)
  658. goto error;
  659. return 0;
  660. error:
  661. free_trace_kprobe(tk);
  662. return ret;
  663. }
  664. static int release_all_trace_kprobes(void)
  665. {
  666. struct trace_kprobe *tk;
  667. int ret = 0;
  668. mutex_lock(&probe_lock);
  669. /* Ensure no probe is in use. */
  670. list_for_each_entry(tk, &probe_list, list)
  671. if (trace_probe_is_enabled(&tk->tp)) {
  672. ret = -EBUSY;
  673. goto end;
  674. }
  675. /* TODO: Use batch unregistration */
  676. while (!list_empty(&probe_list)) {
  677. tk = list_entry(probe_list.next, struct trace_kprobe, list);
  678. ret = unregister_trace_kprobe(tk);
  679. if (ret)
  680. goto end;
  681. free_trace_kprobe(tk);
  682. }
  683. end:
  684. mutex_unlock(&probe_lock);
  685. return ret;
  686. }
  687. /* Probes listing interfaces */
  688. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  689. {
  690. mutex_lock(&probe_lock);
  691. return seq_list_start(&probe_list, *pos);
  692. }
  693. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  694. {
  695. return seq_list_next(v, &probe_list, pos);
  696. }
  697. static void probes_seq_stop(struct seq_file *m, void *v)
  698. {
  699. mutex_unlock(&probe_lock);
  700. }
  701. static int probes_seq_show(struct seq_file *m, void *v)
  702. {
  703. struct trace_kprobe *tk = v;
  704. int i;
  705. seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
  706. seq_printf(m, ":%s/%s", tk->tp.call.class->system,
  707. trace_event_name(&tk->tp.call));
  708. if (!tk->symbol)
  709. seq_printf(m, " 0x%p", tk->rp.kp.addr);
  710. else if (tk->rp.kp.offset)
  711. seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
  712. tk->rp.kp.offset);
  713. else
  714. seq_printf(m, " %s", trace_kprobe_symbol(tk));
  715. for (i = 0; i < tk->tp.nr_args; i++)
  716. seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
  717. seq_putc(m, '\n');
  718. return 0;
  719. }
  720. static const struct seq_operations probes_seq_op = {
  721. .start = probes_seq_start,
  722. .next = probes_seq_next,
  723. .stop = probes_seq_stop,
  724. .show = probes_seq_show
  725. };
  726. static int probes_open(struct inode *inode, struct file *file)
  727. {
  728. int ret;
  729. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  730. ret = release_all_trace_kprobes();
  731. if (ret < 0)
  732. return ret;
  733. }
  734. return seq_open(file, &probes_seq_op);
  735. }
  736. static ssize_t probes_write(struct file *file, const char __user *buffer,
  737. size_t count, loff_t *ppos)
  738. {
  739. return traceprobe_probes_write(file, buffer, count, ppos,
  740. create_trace_kprobe);
  741. }
  742. static const struct file_operations kprobe_events_ops = {
  743. .owner = THIS_MODULE,
  744. .open = probes_open,
  745. .read = seq_read,
  746. .llseek = seq_lseek,
  747. .release = seq_release,
  748. .write = probes_write,
  749. };
  750. /* Probes profiling interfaces */
  751. static int probes_profile_seq_show(struct seq_file *m, void *v)
  752. {
  753. struct trace_kprobe *tk = v;
  754. unsigned long nhit = 0;
  755. int cpu;
  756. for_each_possible_cpu(cpu)
  757. nhit += *per_cpu_ptr(tk->nhit, cpu);
  758. seq_printf(m, " %-44s %15lu %15lu\n",
  759. trace_event_name(&tk->tp.call), nhit,
  760. tk->rp.kp.nmissed);
  761. return 0;
  762. }
  763. static const struct seq_operations profile_seq_op = {
  764. .start = probes_seq_start,
  765. .next = probes_seq_next,
  766. .stop = probes_seq_stop,
  767. .show = probes_profile_seq_show
  768. };
  769. static int profile_open(struct inode *inode, struct file *file)
  770. {
  771. return seq_open(file, &profile_seq_op);
  772. }
  773. static const struct file_operations kprobe_profile_ops = {
  774. .owner = THIS_MODULE,
  775. .open = profile_open,
  776. .read = seq_read,
  777. .llseek = seq_lseek,
  778. .release = seq_release,
  779. };
  780. /* Kprobe handler */
  781. static nokprobe_inline void
  782. __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
  783. struct trace_event_file *trace_file)
  784. {
  785. struct kprobe_trace_entry_head *entry;
  786. struct ring_buffer_event *event;
  787. struct ring_buffer *buffer;
  788. int size, dsize, pc;
  789. unsigned long irq_flags;
  790. struct trace_event_call *call = &tk->tp.call;
  791. WARN_ON(call != trace_file->event_call);
  792. if (trace_trigger_soft_disabled(trace_file))
  793. return;
  794. local_save_flags(irq_flags);
  795. pc = preempt_count();
  796. dsize = __get_data_size(&tk->tp, regs);
  797. size = sizeof(*entry) + tk->tp.size + dsize;
  798. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  799. call->event.type,
  800. size, irq_flags, pc);
  801. if (!event)
  802. return;
  803. entry = ring_buffer_event_data(event);
  804. entry->ip = (unsigned long)tk->rp.kp.addr;
  805. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  806. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  807. entry, irq_flags, pc, regs);
  808. }
  809. static void
  810. kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
  811. {
  812. struct event_file_link *link;
  813. list_for_each_entry_rcu(link, &tk->tp.files, list)
  814. __kprobe_trace_func(tk, regs, link->file);
  815. }
  816. NOKPROBE_SYMBOL(kprobe_trace_func);
  817. /* Kretprobe handler */
  818. static nokprobe_inline void
  819. __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  820. struct pt_regs *regs,
  821. struct trace_event_file *trace_file)
  822. {
  823. struct kretprobe_trace_entry_head *entry;
  824. struct ring_buffer_event *event;
  825. struct ring_buffer *buffer;
  826. int size, pc, dsize;
  827. unsigned long irq_flags;
  828. struct trace_event_call *call = &tk->tp.call;
  829. WARN_ON(call != trace_file->event_call);
  830. if (trace_trigger_soft_disabled(trace_file))
  831. return;
  832. local_save_flags(irq_flags);
  833. pc = preempt_count();
  834. dsize = __get_data_size(&tk->tp, regs);
  835. size = sizeof(*entry) + tk->tp.size + dsize;
  836. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  837. call->event.type,
  838. size, irq_flags, pc);
  839. if (!event)
  840. return;
  841. entry = ring_buffer_event_data(event);
  842. entry->func = (unsigned long)tk->rp.kp.addr;
  843. entry->ret_ip = (unsigned long)ri->ret_addr;
  844. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  845. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  846. entry, irq_flags, pc, regs);
  847. }
  848. static void
  849. kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  850. struct pt_regs *regs)
  851. {
  852. struct event_file_link *link;
  853. list_for_each_entry_rcu(link, &tk->tp.files, list)
  854. __kretprobe_trace_func(tk, ri, regs, link->file);
  855. }
  856. NOKPROBE_SYMBOL(kretprobe_trace_func);
  857. /* Event entry printers */
  858. static enum print_line_t
  859. print_kprobe_event(struct trace_iterator *iter, int flags,
  860. struct trace_event *event)
  861. {
  862. struct kprobe_trace_entry_head *field;
  863. struct trace_seq *s = &iter->seq;
  864. struct trace_probe *tp;
  865. u8 *data;
  866. int i;
  867. field = (struct kprobe_trace_entry_head *)iter->ent;
  868. tp = container_of(event, struct trace_probe, call.event);
  869. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  870. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  871. goto out;
  872. trace_seq_putc(s, ')');
  873. data = (u8 *)&field[1];
  874. for (i = 0; i < tp->nr_args; i++)
  875. if (!tp->args[i].type->print(s, tp->args[i].name,
  876. data + tp->args[i].offset, field))
  877. goto out;
  878. trace_seq_putc(s, '\n');
  879. out:
  880. return trace_handle_return(s);
  881. }
  882. static enum print_line_t
  883. print_kretprobe_event(struct trace_iterator *iter, int flags,
  884. struct trace_event *event)
  885. {
  886. struct kretprobe_trace_entry_head *field;
  887. struct trace_seq *s = &iter->seq;
  888. struct trace_probe *tp;
  889. u8 *data;
  890. int i;
  891. field = (struct kretprobe_trace_entry_head *)iter->ent;
  892. tp = container_of(event, struct trace_probe, call.event);
  893. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  894. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  895. goto out;
  896. trace_seq_puts(s, " <- ");
  897. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  898. goto out;
  899. trace_seq_putc(s, ')');
  900. data = (u8 *)&field[1];
  901. for (i = 0; i < tp->nr_args; i++)
  902. if (!tp->args[i].type->print(s, tp->args[i].name,
  903. data + tp->args[i].offset, field))
  904. goto out;
  905. trace_seq_putc(s, '\n');
  906. out:
  907. return trace_handle_return(s);
  908. }
  909. static int kprobe_event_define_fields(struct trace_event_call *event_call)
  910. {
  911. int ret, i;
  912. struct kprobe_trace_entry_head field;
  913. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  914. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  915. /* Set argument names as fields */
  916. for (i = 0; i < tk->tp.nr_args; i++) {
  917. struct probe_arg *parg = &tk->tp.args[i];
  918. ret = trace_define_field(event_call, parg->type->fmttype,
  919. parg->name,
  920. sizeof(field) + parg->offset,
  921. parg->type->size,
  922. parg->type->is_signed,
  923. FILTER_OTHER);
  924. if (ret)
  925. return ret;
  926. }
  927. return 0;
  928. }
  929. static int kretprobe_event_define_fields(struct trace_event_call *event_call)
  930. {
  931. int ret, i;
  932. struct kretprobe_trace_entry_head field;
  933. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  934. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  935. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  936. /* Set argument names as fields */
  937. for (i = 0; i < tk->tp.nr_args; i++) {
  938. struct probe_arg *parg = &tk->tp.args[i];
  939. ret = trace_define_field(event_call, parg->type->fmttype,
  940. parg->name,
  941. sizeof(field) + parg->offset,
  942. parg->type->size,
  943. parg->type->is_signed,
  944. FILTER_OTHER);
  945. if (ret)
  946. return ret;
  947. }
  948. return 0;
  949. }
  950. #ifdef CONFIG_PERF_EVENTS
  951. /* Kprobe profile handler */
  952. static void
  953. kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
  954. {
  955. struct trace_event_call *call = &tk->tp.call;
  956. struct bpf_prog *prog = call->prog;
  957. struct kprobe_trace_entry_head *entry;
  958. struct hlist_head *head;
  959. int size, __size, dsize;
  960. int rctx;
  961. if (prog && !trace_call_bpf(prog, regs))
  962. return;
  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 trace_event_call *call = &tk->tp.call;
  985. struct bpf_prog *prog = call->prog;
  986. struct kretprobe_trace_entry_head *entry;
  987. struct hlist_head *head;
  988. int size, __size, dsize;
  989. int rctx;
  990. if (prog && !trace_call_bpf(prog, regs))
  991. return;
  992. head = this_cpu_ptr(call->perf_events);
  993. if (hlist_empty(head))
  994. return;
  995. dsize = __get_data_size(&tk->tp, regs);
  996. __size = sizeof(*entry) + tk->tp.size + dsize;
  997. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  998. size -= sizeof(u32);
  999. entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
  1000. if (!entry)
  1001. return;
  1002. entry->func = (unsigned long)tk->rp.kp.addr;
  1003. entry->ret_ip = (unsigned long)ri->ret_addr;
  1004. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  1005. perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
  1006. }
  1007. NOKPROBE_SYMBOL(kretprobe_perf_func);
  1008. #endif /* CONFIG_PERF_EVENTS */
  1009. /*
  1010. * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
  1011. *
  1012. * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
  1013. * lockless, but we can't race with this __init function.
  1014. */
  1015. static int kprobe_register(struct trace_event_call *event,
  1016. enum trace_reg type, void *data)
  1017. {
  1018. struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
  1019. struct trace_event_file *file = data;
  1020. switch (type) {
  1021. case TRACE_REG_REGISTER:
  1022. return enable_trace_kprobe(tk, file);
  1023. case TRACE_REG_UNREGISTER:
  1024. return disable_trace_kprobe(tk, file);
  1025. #ifdef CONFIG_PERF_EVENTS
  1026. case TRACE_REG_PERF_REGISTER:
  1027. return enable_trace_kprobe(tk, NULL);
  1028. case TRACE_REG_PERF_UNREGISTER:
  1029. return disable_trace_kprobe(tk, NULL);
  1030. case TRACE_REG_PERF_OPEN:
  1031. case TRACE_REG_PERF_CLOSE:
  1032. case TRACE_REG_PERF_ADD:
  1033. case TRACE_REG_PERF_DEL:
  1034. return 0;
  1035. #endif
  1036. }
  1037. return 0;
  1038. }
  1039. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1040. {
  1041. struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
  1042. raw_cpu_inc(*tk->nhit);
  1043. if (tk->tp.flags & TP_FLAG_TRACE)
  1044. kprobe_trace_func(tk, regs);
  1045. #ifdef CONFIG_PERF_EVENTS
  1046. if (tk->tp.flags & TP_FLAG_PROFILE)
  1047. kprobe_perf_func(tk, regs);
  1048. #endif
  1049. return 0; /* We don't tweek kernel, so just return 0 */
  1050. }
  1051. NOKPROBE_SYMBOL(kprobe_dispatcher);
  1052. static int
  1053. kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1054. {
  1055. struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
  1056. raw_cpu_inc(*tk->nhit);
  1057. if (tk->tp.flags & TP_FLAG_TRACE)
  1058. kretprobe_trace_func(tk, ri, regs);
  1059. #ifdef CONFIG_PERF_EVENTS
  1060. if (tk->tp.flags & TP_FLAG_PROFILE)
  1061. kretprobe_perf_func(tk, ri, regs);
  1062. #endif
  1063. return 0; /* We don't tweek kernel, so just return 0 */
  1064. }
  1065. NOKPROBE_SYMBOL(kretprobe_dispatcher);
  1066. static struct trace_event_functions kretprobe_funcs = {
  1067. .trace = print_kretprobe_event
  1068. };
  1069. static struct trace_event_functions kprobe_funcs = {
  1070. .trace = print_kprobe_event
  1071. };
  1072. static int register_kprobe_event(struct trace_kprobe *tk)
  1073. {
  1074. struct trace_event_call *call = &tk->tp.call;
  1075. int ret;
  1076. /* Initialize trace_event_call */
  1077. INIT_LIST_HEAD(&call->class->fields);
  1078. if (trace_kprobe_is_return(tk)) {
  1079. call->event.funcs = &kretprobe_funcs;
  1080. call->class->define_fields = kretprobe_event_define_fields;
  1081. } else {
  1082. call->event.funcs = &kprobe_funcs;
  1083. call->class->define_fields = kprobe_event_define_fields;
  1084. }
  1085. if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
  1086. return -ENOMEM;
  1087. ret = register_trace_event(&call->event);
  1088. if (!ret) {
  1089. kfree(call->print_fmt);
  1090. return -ENODEV;
  1091. }
  1092. call->flags = TRACE_EVENT_FL_KPROBE;
  1093. call->class->reg = kprobe_register;
  1094. call->data = tk;
  1095. ret = trace_add_event_call(call);
  1096. if (ret) {
  1097. pr_info("Failed to register kprobe event: %s\n",
  1098. trace_event_name(call));
  1099. kfree(call->print_fmt);
  1100. unregister_trace_event(&call->event);
  1101. }
  1102. return ret;
  1103. }
  1104. static int unregister_kprobe_event(struct trace_kprobe *tk)
  1105. {
  1106. int ret;
  1107. /* tp->event is unregistered in trace_remove_event_call() */
  1108. ret = trace_remove_event_call(&tk->tp.call);
  1109. if (!ret)
  1110. kfree(tk->tp.call.print_fmt);
  1111. return ret;
  1112. }
  1113. /* Make a tracefs interface for controlling probe points */
  1114. static __init int init_kprobe_trace(void)
  1115. {
  1116. struct dentry *d_tracer;
  1117. struct dentry *entry;
  1118. if (register_module_notifier(&trace_kprobe_module_nb))
  1119. return -EINVAL;
  1120. d_tracer = tracing_init_dentry();
  1121. if (IS_ERR(d_tracer))
  1122. return 0;
  1123. entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
  1124. NULL, &kprobe_events_ops);
  1125. /* Event list interface */
  1126. if (!entry)
  1127. pr_warn("Could not create tracefs 'kprobe_events' entry\n");
  1128. /* Profile interface */
  1129. entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
  1130. NULL, &kprobe_profile_ops);
  1131. if (!entry)
  1132. pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
  1133. return 0;
  1134. }
  1135. fs_initcall(init_kprobe_trace);
  1136. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1137. /*
  1138. * The "__used" keeps gcc from removing the function symbol
  1139. * from the kallsyms table.
  1140. */
  1141. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1142. int a4, int a5, int a6)
  1143. {
  1144. return a1 + a2 + a3 + a4 + a5 + a6;
  1145. }
  1146. static struct trace_event_file *
  1147. find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
  1148. {
  1149. struct trace_event_file *file;
  1150. list_for_each_entry(file, &tr->events, list)
  1151. if (file->event_call == &tk->tp.call)
  1152. return file;
  1153. return NULL;
  1154. }
  1155. /*
  1156. * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
  1157. * stage, we can do this lockless.
  1158. */
  1159. static __init int kprobe_trace_self_tests_init(void)
  1160. {
  1161. int ret, warn = 0;
  1162. int (*target)(int, int, int, int, int, int);
  1163. struct trace_kprobe *tk;
  1164. struct trace_event_file *file;
  1165. if (tracing_is_disabled())
  1166. return -ENODEV;
  1167. target = kprobe_trace_selftest_target;
  1168. pr_info("Testing kprobe tracing: ");
  1169. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1170. "$stack $stack0 +0($stack)",
  1171. create_trace_kprobe);
  1172. if (WARN_ON_ONCE(ret)) {
  1173. pr_warn("error on probing function entry.\n");
  1174. warn++;
  1175. } else {
  1176. /* Enable trace point */
  1177. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1178. if (WARN_ON_ONCE(tk == NULL)) {
  1179. pr_warn("error on getting new probe.\n");
  1180. warn++;
  1181. } else {
  1182. file = find_trace_probe_file(tk, top_trace_array());
  1183. if (WARN_ON_ONCE(file == NULL)) {
  1184. pr_warn("error on getting probe file.\n");
  1185. warn++;
  1186. } else
  1187. enable_trace_kprobe(tk, file);
  1188. }
  1189. }
  1190. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1191. "$retval", create_trace_kprobe);
  1192. if (WARN_ON_ONCE(ret)) {
  1193. pr_warn("error on probing function return.\n");
  1194. warn++;
  1195. } else {
  1196. /* Enable trace point */
  1197. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1198. if (WARN_ON_ONCE(tk == NULL)) {
  1199. pr_warn("error on getting 2nd new probe.\n");
  1200. warn++;
  1201. } else {
  1202. file = find_trace_probe_file(tk, top_trace_array());
  1203. if (WARN_ON_ONCE(file == NULL)) {
  1204. pr_warn("error on getting probe file.\n");
  1205. warn++;
  1206. } else
  1207. enable_trace_kprobe(tk, file);
  1208. }
  1209. }
  1210. if (warn)
  1211. goto end;
  1212. ret = target(1, 2, 3, 4, 5, 6);
  1213. /* Disable trace points before removing it */
  1214. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1215. if (WARN_ON_ONCE(tk == NULL)) {
  1216. pr_warn("error on getting test probe.\n");
  1217. warn++;
  1218. } else {
  1219. file = find_trace_probe_file(tk, top_trace_array());
  1220. if (WARN_ON_ONCE(file == NULL)) {
  1221. pr_warn("error on getting probe file.\n");
  1222. warn++;
  1223. } else
  1224. disable_trace_kprobe(tk, file);
  1225. }
  1226. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1227. if (WARN_ON_ONCE(tk == NULL)) {
  1228. pr_warn("error on getting 2nd test probe.\n");
  1229. warn++;
  1230. } else {
  1231. file = find_trace_probe_file(tk, top_trace_array());
  1232. if (WARN_ON_ONCE(file == NULL)) {
  1233. pr_warn("error on getting probe file.\n");
  1234. warn++;
  1235. } else
  1236. disable_trace_kprobe(tk, file);
  1237. }
  1238. ret = traceprobe_command("-:testprobe", create_trace_kprobe);
  1239. if (WARN_ON_ONCE(ret)) {
  1240. pr_warn("error on deleting a probe.\n");
  1241. warn++;
  1242. }
  1243. ret = traceprobe_command("-:testprobe2", create_trace_kprobe);
  1244. if (WARN_ON_ONCE(ret)) {
  1245. pr_warn("error on deleting a probe.\n");
  1246. warn++;
  1247. }
  1248. end:
  1249. release_all_trace_kprobes();
  1250. if (warn)
  1251. pr_cont("NG: Some tests are failed. Please check them.\n");
  1252. else
  1253. pr_cont("OK\n");
  1254. return 0;
  1255. }
  1256. late_initcall(kprobe_trace_self_tests_init);
  1257. #endif