trace_kprobe.c 41 KB

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