trace_kprobe.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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. if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
  433. pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
  434. ret = 0;
  435. } else if (ret == -EILSEQ) {
  436. pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
  437. tk->rp.kp.addr);
  438. ret = -EINVAL;
  439. }
  440. }
  441. return ret;
  442. }
  443. /* Internal unregister function - just handle k*probes and flags */
  444. static void __unregister_trace_kprobe(struct trace_kprobe *tk)
  445. {
  446. if (trace_probe_is_registered(&tk->tp)) {
  447. if (trace_kprobe_is_return(tk))
  448. unregister_kretprobe(&tk->rp);
  449. else
  450. unregister_kprobe(&tk->rp.kp);
  451. tk->tp.flags &= ~TP_FLAG_REGISTERED;
  452. /* Cleanup kprobe for reuse */
  453. if (tk->rp.kp.symbol_name)
  454. tk->rp.kp.addr = NULL;
  455. }
  456. }
  457. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  458. static int unregister_trace_kprobe(struct trace_kprobe *tk)
  459. {
  460. /* Enabled event can not be unregistered */
  461. if (trace_probe_is_enabled(&tk->tp))
  462. return -EBUSY;
  463. /* Will fail if probe is being used by ftrace or perf */
  464. if (unregister_kprobe_event(tk))
  465. return -EBUSY;
  466. __unregister_trace_kprobe(tk);
  467. list_del(&tk->list);
  468. return 0;
  469. }
  470. /* Register a trace_probe and probe_event */
  471. static int register_trace_kprobe(struct trace_kprobe *tk)
  472. {
  473. struct trace_kprobe *old_tk;
  474. int ret;
  475. mutex_lock(&probe_lock);
  476. /* Delete old (same name) event if exist */
  477. old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
  478. tk->tp.call.class->system);
  479. if (old_tk) {
  480. ret = unregister_trace_kprobe(old_tk);
  481. if (ret < 0)
  482. goto end;
  483. free_trace_kprobe(old_tk);
  484. }
  485. /* Register new event */
  486. ret = register_kprobe_event(tk);
  487. if (ret) {
  488. pr_warn("Failed to register probe event(%d)\n", ret);
  489. goto end;
  490. }
  491. /* Register k*probe */
  492. ret = __register_trace_kprobe(tk);
  493. if (ret < 0)
  494. unregister_kprobe_event(tk);
  495. else
  496. list_add_tail(&tk->list, &probe_list);
  497. end:
  498. mutex_unlock(&probe_lock);
  499. return ret;
  500. }
  501. /* Module notifier call back, checking event on the module */
  502. static int trace_kprobe_module_callback(struct notifier_block *nb,
  503. unsigned long val, void *data)
  504. {
  505. struct module *mod = data;
  506. struct trace_kprobe *tk;
  507. int ret;
  508. if (val != MODULE_STATE_COMING)
  509. return NOTIFY_DONE;
  510. /* Update probes on coming module */
  511. mutex_lock(&probe_lock);
  512. list_for_each_entry(tk, &probe_list, list) {
  513. if (trace_kprobe_within_module(tk, mod)) {
  514. /* Don't need to check busy - this should have gone. */
  515. __unregister_trace_kprobe(tk);
  516. ret = __register_trace_kprobe(tk);
  517. if (ret)
  518. pr_warn("Failed to re-register probe %s on %s: %d\n",
  519. trace_event_name(&tk->tp.call),
  520. mod->name, ret);
  521. }
  522. }
  523. mutex_unlock(&probe_lock);
  524. return NOTIFY_DONE;
  525. }
  526. static struct notifier_block trace_kprobe_module_nb = {
  527. .notifier_call = trace_kprobe_module_callback,
  528. .priority = 1 /* Invoked after kprobe module callback */
  529. };
  530. /* Convert certain expected symbols into '_' when generating event names */
  531. static inline void sanitize_event_name(char *name)
  532. {
  533. while (*name++ != '\0')
  534. if (*name == ':' || *name == '.')
  535. *name = '_';
  536. }
  537. static int create_trace_kprobe(int argc, char **argv)
  538. {
  539. /*
  540. * Argument syntax:
  541. * - Add kprobe:
  542. * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  543. * - Add kretprobe:
  544. * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
  545. * Fetch args:
  546. * $retval : fetch return value
  547. * $stack : fetch stack address
  548. * $stackN : fetch Nth of stack (N:0-)
  549. * $comm : fetch current task comm
  550. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  551. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  552. * %REG : fetch register REG
  553. * Dereferencing memory fetch:
  554. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  555. * Alias name of args:
  556. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  557. * Type of args:
  558. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  559. */
  560. struct trace_kprobe *tk;
  561. int i, ret = 0;
  562. bool is_return = false, is_delete = false;
  563. char *symbol = NULL, *event = NULL, *group = NULL;
  564. int maxactive = 0;
  565. char *arg;
  566. long offset = 0;
  567. void *addr = NULL;
  568. char buf[MAX_EVENT_NAME_LEN];
  569. /* argc must be >= 1 */
  570. if (argv[0][0] == 'p')
  571. is_return = false;
  572. else if (argv[0][0] == 'r')
  573. is_return = true;
  574. else if (argv[0][0] == '-')
  575. is_delete = true;
  576. else {
  577. pr_info("Probe definition must be started with 'p', 'r' or"
  578. " '-'.\n");
  579. return -EINVAL;
  580. }
  581. event = strchr(&argv[0][1], ':');
  582. if (event) {
  583. event[0] = '\0';
  584. event++;
  585. }
  586. if (is_return && isdigit(argv[0][1])) {
  587. ret = kstrtouint(&argv[0][1], 0, &maxactive);
  588. if (ret) {
  589. pr_info("Failed to parse maxactive.\n");
  590. return ret;
  591. }
  592. /* kretprobes instances are iterated over via a list. The
  593. * maximum should stay reasonable.
  594. */
  595. if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
  596. pr_info("Maxactive is too big (%d > %d).\n",
  597. maxactive, KRETPROBE_MAXACTIVE_MAX);
  598. return -E2BIG;
  599. }
  600. }
  601. if (event) {
  602. if (strchr(event, '/')) {
  603. group = event;
  604. event = strchr(group, '/') + 1;
  605. event[-1] = '\0';
  606. if (strlen(group) == 0) {
  607. pr_info("Group name is not specified\n");
  608. return -EINVAL;
  609. }
  610. }
  611. if (strlen(event) == 0) {
  612. pr_info("Event name is not specified\n");
  613. return -EINVAL;
  614. }
  615. }
  616. if (!group)
  617. group = KPROBE_EVENT_SYSTEM;
  618. if (is_delete) {
  619. if (!event) {
  620. pr_info("Delete command needs an event name.\n");
  621. return -EINVAL;
  622. }
  623. mutex_lock(&probe_lock);
  624. tk = find_trace_kprobe(event, group);
  625. if (!tk) {
  626. mutex_unlock(&probe_lock);
  627. pr_info("Event %s/%s doesn't exist.\n", group, event);
  628. return -ENOENT;
  629. }
  630. /* delete an event */
  631. ret = unregister_trace_kprobe(tk);
  632. if (ret == 0)
  633. free_trace_kprobe(tk);
  634. mutex_unlock(&probe_lock);
  635. return ret;
  636. }
  637. if (argc < 2) {
  638. pr_info("Probe point is not specified.\n");
  639. return -EINVAL;
  640. }
  641. /* try to parse an address. if that fails, try to read the
  642. * input as a symbol. */
  643. if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
  644. /* a symbol specified */
  645. symbol = argv[1];
  646. /* TODO: support .init module functions */
  647. ret = traceprobe_split_symbol_offset(symbol, &offset);
  648. if (ret || offset < 0 || offset > UINT_MAX) {
  649. pr_info("Failed to parse either an address or a symbol.\n");
  650. return ret;
  651. }
  652. if (offset && is_return &&
  653. !kprobe_on_func_entry(NULL, symbol, offset)) {
  654. pr_info("Given offset is not valid for return probe.\n");
  655. return -EINVAL;
  656. }
  657. }
  658. argc -= 2; argv += 2;
  659. /* setup a probe */
  660. if (!event) {
  661. /* Make a new event name */
  662. if (symbol)
  663. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  664. is_return ? 'r' : 'p', symbol, offset);
  665. else
  666. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  667. is_return ? 'r' : 'p', addr);
  668. sanitize_event_name(buf);
  669. event = buf;
  670. }
  671. tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
  672. argc, is_return);
  673. if (IS_ERR(tk)) {
  674. pr_info("Failed to allocate trace_probe.(%d)\n",
  675. (int)PTR_ERR(tk));
  676. return PTR_ERR(tk);
  677. }
  678. /* parse arguments */
  679. ret = 0;
  680. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  681. struct probe_arg *parg = &tk->tp.args[i];
  682. /* Increment count for freeing args in error case */
  683. tk->tp.nr_args++;
  684. /* Parse argument name */
  685. arg = strchr(argv[i], '=');
  686. if (arg) {
  687. *arg++ = '\0';
  688. parg->name = kstrdup(argv[i], GFP_KERNEL);
  689. } else {
  690. arg = argv[i];
  691. /* If argument name is omitted, set "argN" */
  692. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  693. parg->name = kstrdup(buf, GFP_KERNEL);
  694. }
  695. if (!parg->name) {
  696. pr_info("Failed to allocate argument[%d] name.\n", i);
  697. ret = -ENOMEM;
  698. goto error;
  699. }
  700. if (!is_good_name(parg->name)) {
  701. pr_info("Invalid argument[%d] name: %s\n",
  702. i, parg->name);
  703. ret = -EINVAL;
  704. goto error;
  705. }
  706. if (traceprobe_conflict_field_name(parg->name,
  707. tk->tp.args, i)) {
  708. pr_info("Argument[%d] name '%s' conflicts with "
  709. "another field.\n", i, argv[i]);
  710. ret = -EINVAL;
  711. goto error;
  712. }
  713. /* Parse fetch argument */
  714. ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
  715. is_return, true,
  716. kprobes_fetch_type_table);
  717. if (ret) {
  718. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  719. goto error;
  720. }
  721. }
  722. ret = register_trace_kprobe(tk);
  723. if (ret)
  724. goto error;
  725. return 0;
  726. error:
  727. free_trace_kprobe(tk);
  728. return ret;
  729. }
  730. static int release_all_trace_kprobes(void)
  731. {
  732. struct trace_kprobe *tk;
  733. int ret = 0;
  734. mutex_lock(&probe_lock);
  735. /* Ensure no probe is in use. */
  736. list_for_each_entry(tk, &probe_list, list)
  737. if (trace_probe_is_enabled(&tk->tp)) {
  738. ret = -EBUSY;
  739. goto end;
  740. }
  741. /* TODO: Use batch unregistration */
  742. while (!list_empty(&probe_list)) {
  743. tk = list_entry(probe_list.next, struct trace_kprobe, list);
  744. ret = unregister_trace_kprobe(tk);
  745. if (ret)
  746. goto end;
  747. free_trace_kprobe(tk);
  748. }
  749. end:
  750. mutex_unlock(&probe_lock);
  751. return ret;
  752. }
  753. /* Probes listing interfaces */
  754. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  755. {
  756. mutex_lock(&probe_lock);
  757. return seq_list_start(&probe_list, *pos);
  758. }
  759. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  760. {
  761. return seq_list_next(v, &probe_list, pos);
  762. }
  763. static void probes_seq_stop(struct seq_file *m, void *v)
  764. {
  765. mutex_unlock(&probe_lock);
  766. }
  767. static int probes_seq_show(struct seq_file *m, void *v)
  768. {
  769. struct trace_kprobe *tk = v;
  770. int i;
  771. seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
  772. seq_printf(m, ":%s/%s", tk->tp.call.class->system,
  773. trace_event_name(&tk->tp.call));
  774. if (!tk->symbol)
  775. seq_printf(m, " 0x%p", tk->rp.kp.addr);
  776. else if (tk->rp.kp.offset)
  777. seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
  778. tk->rp.kp.offset);
  779. else
  780. seq_printf(m, " %s", trace_kprobe_symbol(tk));
  781. for (i = 0; i < tk->tp.nr_args; i++)
  782. seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
  783. seq_putc(m, '\n');
  784. return 0;
  785. }
  786. static const struct seq_operations probes_seq_op = {
  787. .start = probes_seq_start,
  788. .next = probes_seq_next,
  789. .stop = probes_seq_stop,
  790. .show = probes_seq_show
  791. };
  792. static int probes_open(struct inode *inode, struct file *file)
  793. {
  794. int ret;
  795. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  796. ret = release_all_trace_kprobes();
  797. if (ret < 0)
  798. return ret;
  799. }
  800. return seq_open(file, &probes_seq_op);
  801. }
  802. static ssize_t probes_write(struct file *file, const char __user *buffer,
  803. size_t count, loff_t *ppos)
  804. {
  805. return trace_parse_run_command(file, buffer, count, ppos,
  806. create_trace_kprobe);
  807. }
  808. static const struct file_operations kprobe_events_ops = {
  809. .owner = THIS_MODULE,
  810. .open = probes_open,
  811. .read = seq_read,
  812. .llseek = seq_lseek,
  813. .release = seq_release,
  814. .write = probes_write,
  815. };
  816. /* Probes profiling interfaces */
  817. static int probes_profile_seq_show(struct seq_file *m, void *v)
  818. {
  819. struct trace_kprobe *tk = v;
  820. seq_printf(m, " %-44s %15lu %15lu\n",
  821. trace_event_name(&tk->tp.call),
  822. trace_kprobe_nhit(tk),
  823. tk->rp.kp.nmissed);
  824. return 0;
  825. }
  826. static const struct seq_operations profile_seq_op = {
  827. .start = probes_seq_start,
  828. .next = probes_seq_next,
  829. .stop = probes_seq_stop,
  830. .show = probes_profile_seq_show
  831. };
  832. static int profile_open(struct inode *inode, struct file *file)
  833. {
  834. return seq_open(file, &profile_seq_op);
  835. }
  836. static const struct file_operations kprobe_profile_ops = {
  837. .owner = THIS_MODULE,
  838. .open = profile_open,
  839. .read = seq_read,
  840. .llseek = seq_lseek,
  841. .release = seq_release,
  842. };
  843. /* Kprobe handler */
  844. static nokprobe_inline void
  845. __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
  846. struct trace_event_file *trace_file)
  847. {
  848. struct kprobe_trace_entry_head *entry;
  849. struct ring_buffer_event *event;
  850. struct ring_buffer *buffer;
  851. int size, dsize, pc;
  852. unsigned long irq_flags;
  853. struct trace_event_call *call = &tk->tp.call;
  854. WARN_ON(call != trace_file->event_call);
  855. if (trace_trigger_soft_disabled(trace_file))
  856. return;
  857. local_save_flags(irq_flags);
  858. pc = preempt_count();
  859. dsize = __get_data_size(&tk->tp, regs);
  860. size = sizeof(*entry) + tk->tp.size + dsize;
  861. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  862. call->event.type,
  863. size, irq_flags, pc);
  864. if (!event)
  865. return;
  866. entry = ring_buffer_event_data(event);
  867. entry->ip = (unsigned long)tk->rp.kp.addr;
  868. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  869. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  870. entry, irq_flags, pc, regs);
  871. }
  872. static void
  873. kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
  874. {
  875. struct event_file_link *link;
  876. list_for_each_entry_rcu(link, &tk->tp.files, list)
  877. __kprobe_trace_func(tk, regs, link->file);
  878. }
  879. NOKPROBE_SYMBOL(kprobe_trace_func);
  880. /* Kretprobe handler */
  881. static nokprobe_inline void
  882. __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  883. struct pt_regs *regs,
  884. struct trace_event_file *trace_file)
  885. {
  886. struct kretprobe_trace_entry_head *entry;
  887. struct ring_buffer_event *event;
  888. struct ring_buffer *buffer;
  889. int size, pc, dsize;
  890. unsigned long irq_flags;
  891. struct trace_event_call *call = &tk->tp.call;
  892. WARN_ON(call != trace_file->event_call);
  893. if (trace_trigger_soft_disabled(trace_file))
  894. return;
  895. local_save_flags(irq_flags);
  896. pc = preempt_count();
  897. dsize = __get_data_size(&tk->tp, regs);
  898. size = sizeof(*entry) + tk->tp.size + dsize;
  899. event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  900. call->event.type,
  901. size, irq_flags, pc);
  902. if (!event)
  903. return;
  904. entry = ring_buffer_event_data(event);
  905. entry->func = (unsigned long)tk->rp.kp.addr;
  906. entry->ret_ip = (unsigned long)ri->ret_addr;
  907. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  908. event_trigger_unlock_commit_regs(trace_file, buffer, event,
  909. entry, irq_flags, pc, regs);
  910. }
  911. static void
  912. kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  913. struct pt_regs *regs)
  914. {
  915. struct event_file_link *link;
  916. list_for_each_entry_rcu(link, &tk->tp.files, list)
  917. __kretprobe_trace_func(tk, ri, regs, link->file);
  918. }
  919. NOKPROBE_SYMBOL(kretprobe_trace_func);
  920. /* Event entry printers */
  921. static enum print_line_t
  922. print_kprobe_event(struct trace_iterator *iter, int flags,
  923. struct trace_event *event)
  924. {
  925. struct kprobe_trace_entry_head *field;
  926. struct trace_seq *s = &iter->seq;
  927. struct trace_probe *tp;
  928. u8 *data;
  929. int i;
  930. field = (struct kprobe_trace_entry_head *)iter->ent;
  931. tp = container_of(event, struct trace_probe, call.event);
  932. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  933. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  934. goto out;
  935. trace_seq_putc(s, ')');
  936. data = (u8 *)&field[1];
  937. for (i = 0; i < tp->nr_args; i++)
  938. if (!tp->args[i].type->print(s, tp->args[i].name,
  939. data + tp->args[i].offset, field))
  940. goto out;
  941. trace_seq_putc(s, '\n');
  942. out:
  943. return trace_handle_return(s);
  944. }
  945. static enum print_line_t
  946. print_kretprobe_event(struct trace_iterator *iter, int flags,
  947. struct trace_event *event)
  948. {
  949. struct kretprobe_trace_entry_head *field;
  950. struct trace_seq *s = &iter->seq;
  951. struct trace_probe *tp;
  952. u8 *data;
  953. int i;
  954. field = (struct kretprobe_trace_entry_head *)iter->ent;
  955. tp = container_of(event, struct trace_probe, call.event);
  956. trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
  957. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  958. goto out;
  959. trace_seq_puts(s, " <- ");
  960. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  961. goto out;
  962. trace_seq_putc(s, ')');
  963. data = (u8 *)&field[1];
  964. for (i = 0; i < tp->nr_args; i++)
  965. if (!tp->args[i].type->print(s, tp->args[i].name,
  966. data + tp->args[i].offset, field))
  967. goto out;
  968. trace_seq_putc(s, '\n');
  969. out:
  970. return trace_handle_return(s);
  971. }
  972. static int kprobe_event_define_fields(struct trace_event_call *event_call)
  973. {
  974. int ret, i;
  975. struct kprobe_trace_entry_head field;
  976. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  977. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  978. /* Set argument names as fields */
  979. for (i = 0; i < tk->tp.nr_args; i++) {
  980. struct probe_arg *parg = &tk->tp.args[i];
  981. ret = trace_define_field(event_call, parg->type->fmttype,
  982. parg->name,
  983. sizeof(field) + parg->offset,
  984. parg->type->size,
  985. parg->type->is_signed,
  986. FILTER_OTHER);
  987. if (ret)
  988. return ret;
  989. }
  990. return 0;
  991. }
  992. static int kretprobe_event_define_fields(struct trace_event_call *event_call)
  993. {
  994. int ret, i;
  995. struct kretprobe_trace_entry_head field;
  996. struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
  997. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  998. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  999. /* Set argument names as fields */
  1000. for (i = 0; i < tk->tp.nr_args; i++) {
  1001. struct probe_arg *parg = &tk->tp.args[i];
  1002. ret = trace_define_field(event_call, parg->type->fmttype,
  1003. parg->name,
  1004. sizeof(field) + parg->offset,
  1005. parg->type->size,
  1006. parg->type->is_signed,
  1007. FILTER_OTHER);
  1008. if (ret)
  1009. return ret;
  1010. }
  1011. return 0;
  1012. }
  1013. #ifdef CONFIG_PERF_EVENTS
  1014. /* Kprobe profile handler */
  1015. static int
  1016. kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
  1017. {
  1018. struct trace_event_call *call = &tk->tp.call;
  1019. struct kprobe_trace_entry_head *entry;
  1020. struct hlist_head *head;
  1021. int size, __size, dsize;
  1022. int rctx;
  1023. if (bpf_prog_array_valid(call)) {
  1024. unsigned long orig_ip = instruction_pointer(regs);
  1025. int ret;
  1026. ret = trace_call_bpf(call, regs);
  1027. /*
  1028. * We need to check and see if we modified the pc of the
  1029. * pt_regs, and if so clear the kprobe and return 1 so that we
  1030. * don't do the single stepping.
  1031. * The ftrace kprobe handler leaves it up to us to re-enable
  1032. * preemption here before returning if we've modified the ip.
  1033. */
  1034. if (orig_ip != instruction_pointer(regs)) {
  1035. reset_current_kprobe();
  1036. preempt_enable_no_resched();
  1037. return 1;
  1038. }
  1039. if (!ret)
  1040. return 0;
  1041. }
  1042. head = this_cpu_ptr(call->perf_events);
  1043. if (hlist_empty(head))
  1044. return 0;
  1045. dsize = __get_data_size(&tk->tp, regs);
  1046. __size = sizeof(*entry) + tk->tp.size + dsize;
  1047. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1048. size -= sizeof(u32);
  1049. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  1050. if (!entry)
  1051. return 0;
  1052. entry->ip = (unsigned long)tk->rp.kp.addr;
  1053. memset(&entry[1], 0, dsize);
  1054. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  1055. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  1056. head, NULL);
  1057. return 0;
  1058. }
  1059. NOKPROBE_SYMBOL(kprobe_perf_func);
  1060. /* Kretprobe profile handler */
  1061. static void
  1062. kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  1063. struct pt_regs *regs)
  1064. {
  1065. struct trace_event_call *call = &tk->tp.call;
  1066. struct kretprobe_trace_entry_head *entry;
  1067. struct hlist_head *head;
  1068. int size, __size, dsize;
  1069. int rctx;
  1070. if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
  1071. return;
  1072. head = this_cpu_ptr(call->perf_events);
  1073. if (hlist_empty(head))
  1074. return;
  1075. dsize = __get_data_size(&tk->tp, regs);
  1076. __size = sizeof(*entry) + tk->tp.size + dsize;
  1077. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1078. size -= sizeof(u32);
  1079. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  1080. if (!entry)
  1081. return;
  1082. entry->func = (unsigned long)tk->rp.kp.addr;
  1083. entry->ret_ip = (unsigned long)ri->ret_addr;
  1084. store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
  1085. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  1086. head, NULL);
  1087. }
  1088. NOKPROBE_SYMBOL(kretprobe_perf_func);
  1089. int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
  1090. const char **symbol, u64 *probe_offset,
  1091. u64 *probe_addr, bool perf_type_tracepoint)
  1092. {
  1093. const char *pevent = trace_event_name(event->tp_event);
  1094. const char *group = event->tp_event->class->system;
  1095. struct trace_kprobe *tk;
  1096. if (perf_type_tracepoint)
  1097. tk = find_trace_kprobe(pevent, group);
  1098. else
  1099. tk = event->tp_event->data;
  1100. if (!tk)
  1101. return -EINVAL;
  1102. *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
  1103. : BPF_FD_TYPE_KPROBE;
  1104. if (tk->symbol) {
  1105. *symbol = tk->symbol;
  1106. *probe_offset = tk->rp.kp.offset;
  1107. *probe_addr = 0;
  1108. } else {
  1109. *symbol = NULL;
  1110. *probe_offset = 0;
  1111. *probe_addr = (unsigned long)tk->rp.kp.addr;
  1112. }
  1113. return 0;
  1114. }
  1115. #endif /* CONFIG_PERF_EVENTS */
  1116. /*
  1117. * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
  1118. *
  1119. * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
  1120. * lockless, but we can't race with this __init function.
  1121. */
  1122. static int kprobe_register(struct trace_event_call *event,
  1123. enum trace_reg type, void *data)
  1124. {
  1125. struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
  1126. struct trace_event_file *file = data;
  1127. switch (type) {
  1128. case TRACE_REG_REGISTER:
  1129. return enable_trace_kprobe(tk, file);
  1130. case TRACE_REG_UNREGISTER:
  1131. return disable_trace_kprobe(tk, file);
  1132. #ifdef CONFIG_PERF_EVENTS
  1133. case TRACE_REG_PERF_REGISTER:
  1134. return enable_trace_kprobe(tk, NULL);
  1135. case TRACE_REG_PERF_UNREGISTER:
  1136. return disable_trace_kprobe(tk, NULL);
  1137. case TRACE_REG_PERF_OPEN:
  1138. case TRACE_REG_PERF_CLOSE:
  1139. case TRACE_REG_PERF_ADD:
  1140. case TRACE_REG_PERF_DEL:
  1141. return 0;
  1142. #endif
  1143. }
  1144. return 0;
  1145. }
  1146. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1147. {
  1148. struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
  1149. int ret = 0;
  1150. raw_cpu_inc(*tk->nhit);
  1151. if (tk->tp.flags & TP_FLAG_TRACE)
  1152. kprobe_trace_func(tk, regs);
  1153. #ifdef CONFIG_PERF_EVENTS
  1154. if (tk->tp.flags & TP_FLAG_PROFILE)
  1155. ret = kprobe_perf_func(tk, regs);
  1156. #endif
  1157. return ret;
  1158. }
  1159. NOKPROBE_SYMBOL(kprobe_dispatcher);
  1160. static int
  1161. kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1162. {
  1163. struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
  1164. raw_cpu_inc(*tk->nhit);
  1165. if (tk->tp.flags & TP_FLAG_TRACE)
  1166. kretprobe_trace_func(tk, ri, regs);
  1167. #ifdef CONFIG_PERF_EVENTS
  1168. if (tk->tp.flags & TP_FLAG_PROFILE)
  1169. kretprobe_perf_func(tk, ri, regs);
  1170. #endif
  1171. return 0; /* We don't tweek kernel, so just return 0 */
  1172. }
  1173. NOKPROBE_SYMBOL(kretprobe_dispatcher);
  1174. static struct trace_event_functions kretprobe_funcs = {
  1175. .trace = print_kretprobe_event
  1176. };
  1177. static struct trace_event_functions kprobe_funcs = {
  1178. .trace = print_kprobe_event
  1179. };
  1180. static inline void init_trace_event_call(struct trace_kprobe *tk,
  1181. struct trace_event_call *call)
  1182. {
  1183. INIT_LIST_HEAD(&call->class->fields);
  1184. if (trace_kprobe_is_return(tk)) {
  1185. call->event.funcs = &kretprobe_funcs;
  1186. call->class->define_fields = kretprobe_event_define_fields;
  1187. } else {
  1188. call->event.funcs = &kprobe_funcs;
  1189. call->class->define_fields = kprobe_event_define_fields;
  1190. }
  1191. call->flags = TRACE_EVENT_FL_KPROBE;
  1192. call->class->reg = kprobe_register;
  1193. call->data = tk;
  1194. }
  1195. static int register_kprobe_event(struct trace_kprobe *tk)
  1196. {
  1197. struct trace_event_call *call = &tk->tp.call;
  1198. int ret = 0;
  1199. init_trace_event_call(tk, call);
  1200. if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
  1201. return -ENOMEM;
  1202. ret = register_trace_event(&call->event);
  1203. if (!ret) {
  1204. kfree(call->print_fmt);
  1205. return -ENODEV;
  1206. }
  1207. ret = trace_add_event_call(call);
  1208. if (ret) {
  1209. pr_info("Failed to register kprobe event: %s\n",
  1210. trace_event_name(call));
  1211. kfree(call->print_fmt);
  1212. unregister_trace_event(&call->event);
  1213. }
  1214. return ret;
  1215. }
  1216. static int unregister_kprobe_event(struct trace_kprobe *tk)
  1217. {
  1218. int ret;
  1219. /* tp->event is unregistered in trace_remove_event_call() */
  1220. ret = trace_remove_event_call(&tk->tp.call);
  1221. if (!ret)
  1222. kfree(tk->tp.call.print_fmt);
  1223. return ret;
  1224. }
  1225. #ifdef CONFIG_PERF_EVENTS
  1226. /* create a trace_kprobe, but don't add it to global lists */
  1227. struct trace_event_call *
  1228. create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
  1229. bool is_return)
  1230. {
  1231. struct trace_kprobe *tk;
  1232. int ret;
  1233. char *event;
  1234. /*
  1235. * local trace_kprobes are not added to probe_list, so they are never
  1236. * searched in find_trace_kprobe(). Therefore, there is no concern of
  1237. * duplicated name here.
  1238. */
  1239. event = func ? func : "DUMMY_EVENT";
  1240. tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
  1241. offs, 0 /* maxactive */, 0 /* nargs */,
  1242. is_return);
  1243. if (IS_ERR(tk)) {
  1244. pr_info("Failed to allocate trace_probe.(%d)\n",
  1245. (int)PTR_ERR(tk));
  1246. return ERR_CAST(tk);
  1247. }
  1248. init_trace_event_call(tk, &tk->tp.call);
  1249. if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
  1250. ret = -ENOMEM;
  1251. goto error;
  1252. }
  1253. ret = __register_trace_kprobe(tk);
  1254. if (ret < 0)
  1255. goto error;
  1256. return &tk->tp.call;
  1257. error:
  1258. free_trace_kprobe(tk);
  1259. return ERR_PTR(ret);
  1260. }
  1261. void destroy_local_trace_kprobe(struct trace_event_call *event_call)
  1262. {
  1263. struct trace_kprobe *tk;
  1264. tk = container_of(event_call, struct trace_kprobe, tp.call);
  1265. if (trace_probe_is_enabled(&tk->tp)) {
  1266. WARN_ON(1);
  1267. return;
  1268. }
  1269. __unregister_trace_kprobe(tk);
  1270. free_trace_kprobe(tk);
  1271. }
  1272. #endif /* CONFIG_PERF_EVENTS */
  1273. /* Make a tracefs interface for controlling probe points */
  1274. static __init int init_kprobe_trace(void)
  1275. {
  1276. struct dentry *d_tracer;
  1277. struct dentry *entry;
  1278. if (register_module_notifier(&trace_kprobe_module_nb))
  1279. return -EINVAL;
  1280. d_tracer = tracing_init_dentry();
  1281. if (IS_ERR(d_tracer))
  1282. return 0;
  1283. entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
  1284. NULL, &kprobe_events_ops);
  1285. /* Event list interface */
  1286. if (!entry)
  1287. pr_warn("Could not create tracefs 'kprobe_events' entry\n");
  1288. /* Profile interface */
  1289. entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
  1290. NULL, &kprobe_profile_ops);
  1291. if (!entry)
  1292. pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
  1293. return 0;
  1294. }
  1295. fs_initcall(init_kprobe_trace);
  1296. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1297. /*
  1298. * The "__used" keeps gcc from removing the function symbol
  1299. * from the kallsyms table. 'noinline' makes sure that there
  1300. * isn't an inlined version used by the test method below
  1301. */
  1302. static __used __init noinline int
  1303. kprobe_trace_selftest_target(int a1, int a2, int a3, int a4, int a5, int a6)
  1304. {
  1305. return a1 + a2 + a3 + a4 + a5 + a6;
  1306. }
  1307. static __init struct trace_event_file *
  1308. find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
  1309. {
  1310. struct trace_event_file *file;
  1311. list_for_each_entry(file, &tr->events, list)
  1312. if (file->event_call == &tk->tp.call)
  1313. return file;
  1314. return NULL;
  1315. }
  1316. /*
  1317. * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
  1318. * stage, we can do this lockless.
  1319. */
  1320. static __init int kprobe_trace_self_tests_init(void)
  1321. {
  1322. int ret, warn = 0;
  1323. int (*target)(int, int, int, int, int, int);
  1324. struct trace_kprobe *tk;
  1325. struct trace_event_file *file;
  1326. if (tracing_is_disabled())
  1327. return -ENODEV;
  1328. target = kprobe_trace_selftest_target;
  1329. pr_info("Testing kprobe tracing: ");
  1330. ret = trace_run_command("p:testprobe kprobe_trace_selftest_target "
  1331. "$stack $stack0 +0($stack)",
  1332. create_trace_kprobe);
  1333. if (WARN_ON_ONCE(ret)) {
  1334. pr_warn("error on probing function entry.\n");
  1335. warn++;
  1336. } else {
  1337. /* Enable trace point */
  1338. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1339. if (WARN_ON_ONCE(tk == NULL)) {
  1340. pr_warn("error on getting new probe.\n");
  1341. warn++;
  1342. } else {
  1343. file = find_trace_probe_file(tk, top_trace_array());
  1344. if (WARN_ON_ONCE(file == NULL)) {
  1345. pr_warn("error on getting probe file.\n");
  1346. warn++;
  1347. } else
  1348. enable_trace_kprobe(tk, file);
  1349. }
  1350. }
  1351. ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target "
  1352. "$retval", create_trace_kprobe);
  1353. if (WARN_ON_ONCE(ret)) {
  1354. pr_warn("error on probing function return.\n");
  1355. warn++;
  1356. } else {
  1357. /* Enable trace point */
  1358. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1359. if (WARN_ON_ONCE(tk == NULL)) {
  1360. pr_warn("error on getting 2nd new probe.\n");
  1361. warn++;
  1362. } else {
  1363. file = find_trace_probe_file(tk, top_trace_array());
  1364. if (WARN_ON_ONCE(file == NULL)) {
  1365. pr_warn("error on getting probe file.\n");
  1366. warn++;
  1367. } else
  1368. enable_trace_kprobe(tk, file);
  1369. }
  1370. }
  1371. if (warn)
  1372. goto end;
  1373. ret = target(1, 2, 3, 4, 5, 6);
  1374. /*
  1375. * Not expecting an error here, the check is only to prevent the
  1376. * optimizer from removing the call to target() as otherwise there
  1377. * are no side-effects and the call is never performed.
  1378. */
  1379. if (ret != 21)
  1380. warn++;
  1381. /* Disable trace points before removing it */
  1382. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1383. if (WARN_ON_ONCE(tk == NULL)) {
  1384. pr_warn("error on getting test probe.\n");
  1385. warn++;
  1386. } else {
  1387. if (trace_kprobe_nhit(tk) != 1) {
  1388. pr_warn("incorrect number of testprobe hits\n");
  1389. warn++;
  1390. }
  1391. file = find_trace_probe_file(tk, top_trace_array());
  1392. if (WARN_ON_ONCE(file == NULL)) {
  1393. pr_warn("error on getting probe file.\n");
  1394. warn++;
  1395. } else
  1396. disable_trace_kprobe(tk, file);
  1397. }
  1398. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1399. if (WARN_ON_ONCE(tk == NULL)) {
  1400. pr_warn("error on getting 2nd test probe.\n");
  1401. warn++;
  1402. } else {
  1403. if (trace_kprobe_nhit(tk) != 1) {
  1404. pr_warn("incorrect number of testprobe2 hits\n");
  1405. warn++;
  1406. }
  1407. file = find_trace_probe_file(tk, top_trace_array());
  1408. if (WARN_ON_ONCE(file == NULL)) {
  1409. pr_warn("error on getting probe file.\n");
  1410. warn++;
  1411. } else
  1412. disable_trace_kprobe(tk, file);
  1413. }
  1414. ret = trace_run_command("-:testprobe", create_trace_kprobe);
  1415. if (WARN_ON_ONCE(ret)) {
  1416. pr_warn("error on deleting a probe.\n");
  1417. warn++;
  1418. }
  1419. ret = trace_run_command("-:testprobe2", create_trace_kprobe);
  1420. if (WARN_ON_ONCE(ret)) {
  1421. pr_warn("error on deleting a probe.\n");
  1422. warn++;
  1423. }
  1424. end:
  1425. release_all_trace_kprobes();
  1426. /*
  1427. * Wait for the optimizer work to finish. Otherwise it might fiddle
  1428. * with probes in already freed __init text.
  1429. */
  1430. wait_for_kprobe_optimizer();
  1431. if (warn)
  1432. pr_cont("NG: Some tests are failed. Please check them.\n");
  1433. else
  1434. pr_cont("OK\n");
  1435. return 0;
  1436. }
  1437. late_initcall(kprobe_trace_self_tests_init);
  1438. #endif