trace_kprobe.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Kprobes-based tracing events
  4. *
  5. * Created by Masami Hiramatsu <mhiramat@redhat.com>
  6. *
  7. */
  8. #define pr_fmt(fmt) "trace_kprobe: " fmt
  9. #include <linux/bpf-cgroup.h>
  10. #include <linux/security.h>
  11. #include <linux/module.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/rculist.h>
  14. #include <linux/error-injection.h>
  15. #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
  16. #include "trace_dynevent.h"
  17. #include "trace_kprobe_selftest.h"
  18. #include "trace_probe.h"
  19. #include "trace_probe_tmpl.h"
  20. #include "trace_probe_kernel.h"
  21. #define KPROBE_EVENT_SYSTEM "kprobes"
  22. #define KRETPROBE_MAXACTIVE_MAX 4096
  23. /* Kprobe early definition from command line */
  24. static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
  25. static int __init set_kprobe_boot_events(char *str)
  26. {
  27. strscpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
  28. disable_tracing_selftest("running kprobe events");
  29. return 1;
  30. }
  31. __setup("kprobe_event=", set_kprobe_boot_events);
  32. static int trace_kprobe_create(const char *raw_command);
  33. static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
  34. static int trace_kprobe_release(struct dyn_event *ev);
  35. static bool trace_kprobe_is_busy(struct dyn_event *ev);
  36. static bool trace_kprobe_match(const char *system, const char *event,
  37. int argc, const char **argv, struct dyn_event *ev);
  38. static struct dyn_event_operations trace_kprobe_ops = {
  39. .create = trace_kprobe_create,
  40. .show = trace_kprobe_show,
  41. .is_busy = trace_kprobe_is_busy,
  42. .free = trace_kprobe_release,
  43. .match = trace_kprobe_match,
  44. };
  45. /*
  46. * Kprobe event core functions
  47. */
  48. struct trace_kprobe {
  49. struct dyn_event devent;
  50. struct kretprobe rp; /* Use rp.kp for kprobe use */
  51. unsigned long __percpu *nhit;
  52. const char *symbol; /* symbol name */
  53. struct trace_probe tp;
  54. };
  55. static bool is_trace_kprobe(struct dyn_event *ev)
  56. {
  57. return ev->ops == &trace_kprobe_ops;
  58. }
  59. static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
  60. {
  61. return container_of(ev, struct trace_kprobe, devent);
  62. }
  63. /**
  64. * for_each_trace_kprobe - iterate over the trace_kprobe list
  65. * @pos: the struct trace_kprobe * for each entry
  66. * @dpos: the struct dyn_event * to use as a loop cursor
  67. */
  68. #define for_each_trace_kprobe(pos, dpos) \
  69. for_each_dyn_event(dpos) \
  70. if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
  71. static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
  72. {
  73. return tk->rp.handler != NULL;
  74. }
  75. static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
  76. {
  77. return tk->symbol ? tk->symbol : "unknown";
  78. }
  79. static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
  80. {
  81. return tk->rp.kp.offset;
  82. }
  83. static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
  84. {
  85. return kprobe_gone(&tk->rp.kp);
  86. }
  87. static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
  88. struct module *mod)
  89. {
  90. int len = strlen(module_name(mod));
  91. const char *name = trace_kprobe_symbol(tk);
  92. return strncmp(module_name(mod), name, len) == 0 && name[len] == ':';
  93. }
  94. #ifdef CONFIG_MODULES
  95. static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
  96. {
  97. char *p;
  98. bool ret;
  99. if (!tk->symbol)
  100. return false;
  101. p = strchr(tk->symbol, ':');
  102. if (!p)
  103. return true;
  104. *p = '\0';
  105. rcu_read_lock_sched();
  106. ret = !!find_module(tk->symbol);
  107. rcu_read_unlock_sched();
  108. *p = ':';
  109. return ret;
  110. }
  111. #else
  112. static inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
  113. {
  114. return false;
  115. }
  116. #endif
  117. static bool trace_kprobe_is_busy(struct dyn_event *ev)
  118. {
  119. struct trace_kprobe *tk = to_trace_kprobe(ev);
  120. return trace_probe_is_enabled(&tk->tp);
  121. }
  122. static bool trace_kprobe_match_command_head(struct trace_kprobe *tk,
  123. int argc, const char **argv)
  124. {
  125. char buf[MAX_ARGSTR_LEN + 1];
  126. if (!argc)
  127. return true;
  128. if (!tk->symbol)
  129. snprintf(buf, sizeof(buf), "0x%p", tk->rp.kp.addr);
  130. else if (tk->rp.kp.offset)
  131. snprintf(buf, sizeof(buf), "%s+%u",
  132. trace_kprobe_symbol(tk), tk->rp.kp.offset);
  133. else
  134. snprintf(buf, sizeof(buf), "%s", trace_kprobe_symbol(tk));
  135. if (strcmp(buf, argv[0]))
  136. return false;
  137. argc--; argv++;
  138. return trace_probe_match_command_args(&tk->tp, argc, argv);
  139. }
  140. static bool trace_kprobe_match(const char *system, const char *event,
  141. int argc, const char **argv, struct dyn_event *ev)
  142. {
  143. struct trace_kprobe *tk = to_trace_kprobe(ev);
  144. return (event[0] == '\0' ||
  145. strcmp(trace_probe_name(&tk->tp), event) == 0) &&
  146. (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) &&
  147. trace_kprobe_match_command_head(tk, argc, argv);
  148. }
  149. static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
  150. {
  151. unsigned long nhit = 0;
  152. int cpu;
  153. for_each_possible_cpu(cpu)
  154. nhit += *per_cpu_ptr(tk->nhit, cpu);
  155. return nhit;
  156. }
  157. static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk)
  158. {
  159. return !(list_empty(&tk->rp.kp.list) &&
  160. hlist_unhashed(&tk->rp.kp.hlist));
  161. }
  162. /* Return 0 if it fails to find the symbol address */
  163. static nokprobe_inline
  164. unsigned long trace_kprobe_address(struct trace_kprobe *tk)
  165. {
  166. unsigned long addr;
  167. if (tk->symbol) {
  168. addr = (unsigned long)
  169. kallsyms_lookup_name(trace_kprobe_symbol(tk));
  170. if (addr)
  171. addr += tk->rp.kp.offset;
  172. } else {
  173. addr = (unsigned long)tk->rp.kp.addr;
  174. }
  175. return addr;
  176. }
  177. static nokprobe_inline struct trace_kprobe *
  178. trace_kprobe_primary_from_call(struct trace_event_call *call)
  179. {
  180. struct trace_probe *tp;
  181. tp = trace_probe_primary_from_call(call);
  182. if (WARN_ON_ONCE(!tp))
  183. return NULL;
  184. return container_of(tp, struct trace_kprobe, tp);
  185. }
  186. bool trace_kprobe_on_func_entry(struct trace_event_call *call)
  187. {
  188. struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
  189. return tk ? (kprobe_on_func_entry(tk->rp.kp.addr,
  190. tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
  191. tk->rp.kp.addr ? 0 : tk->rp.kp.offset) == 0) : false;
  192. }
  193. bool trace_kprobe_error_injectable(struct trace_event_call *call)
  194. {
  195. struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
  196. return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
  197. false;
  198. }
  199. static int register_kprobe_event(struct trace_kprobe *tk);
  200. static int unregister_kprobe_event(struct trace_kprobe *tk);
  201. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  202. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  203. struct pt_regs *regs);
  204. static void free_trace_kprobe(struct trace_kprobe *tk)
  205. {
  206. if (tk) {
  207. trace_probe_cleanup(&tk->tp);
  208. kfree(tk->symbol);
  209. free_percpu(tk->nhit);
  210. kfree(tk);
  211. }
  212. }
  213. /*
  214. * Allocate new trace_probe and initialize it (including kprobes).
  215. */
  216. static struct trace_kprobe *alloc_trace_kprobe(const char *group,
  217. const char *event,
  218. void *addr,
  219. const char *symbol,
  220. unsigned long offs,
  221. int maxactive,
  222. int nargs, bool is_return)
  223. {
  224. struct trace_kprobe *tk;
  225. int ret = -ENOMEM;
  226. tk = kzalloc(struct_size(tk, tp.args, nargs), GFP_KERNEL);
  227. if (!tk)
  228. return ERR_PTR(ret);
  229. tk->nhit = alloc_percpu(unsigned long);
  230. if (!tk->nhit)
  231. goto error;
  232. if (symbol) {
  233. tk->symbol = kstrdup(symbol, GFP_KERNEL);
  234. if (!tk->symbol)
  235. goto error;
  236. tk->rp.kp.symbol_name = tk->symbol;
  237. tk->rp.kp.offset = offs;
  238. } else
  239. tk->rp.kp.addr = addr;
  240. if (is_return)
  241. tk->rp.handler = kretprobe_dispatcher;
  242. else
  243. tk->rp.kp.pre_handler = kprobe_dispatcher;
  244. tk->rp.maxactive = maxactive;
  245. INIT_HLIST_NODE(&tk->rp.kp.hlist);
  246. INIT_LIST_HEAD(&tk->rp.kp.list);
  247. ret = trace_probe_init(&tk->tp, event, group, false, nargs);
  248. if (ret < 0)
  249. goto error;
  250. dyn_event_init(&tk->devent, &trace_kprobe_ops);
  251. return tk;
  252. error:
  253. free_trace_kprobe(tk);
  254. return ERR_PTR(ret);
  255. }
  256. static struct trace_kprobe *find_trace_kprobe(const char *event,
  257. const char *group)
  258. {
  259. struct dyn_event *pos;
  260. struct trace_kprobe *tk;
  261. for_each_trace_kprobe(tk, pos)
  262. if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
  263. strcmp(trace_probe_group_name(&tk->tp), group) == 0)
  264. return tk;
  265. return NULL;
  266. }
  267. static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
  268. {
  269. int ret = 0;
  270. if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
  271. if (trace_kprobe_is_return(tk))
  272. ret = enable_kretprobe(&tk->rp);
  273. else
  274. ret = enable_kprobe(&tk->rp.kp);
  275. }
  276. return ret;
  277. }
  278. static void __disable_trace_kprobe(struct trace_probe *tp)
  279. {
  280. struct trace_kprobe *tk;
  281. list_for_each_entry(tk, trace_probe_probe_list(tp), tp.list) {
  282. if (!trace_kprobe_is_registered(tk))
  283. continue;
  284. if (trace_kprobe_is_return(tk))
  285. disable_kretprobe(&tk->rp);
  286. else
  287. disable_kprobe(&tk->rp.kp);
  288. }
  289. }
  290. /*
  291. * Enable trace_probe
  292. * if the file is NULL, enable "perf" handler, or enable "trace" handler.
  293. */
  294. static int enable_trace_kprobe(struct trace_event_call *call,
  295. struct trace_event_file *file)
  296. {
  297. struct trace_probe *tp;
  298. struct trace_kprobe *tk;
  299. bool enabled;
  300. int ret = 0;
  301. tp = trace_probe_primary_from_call(call);
  302. if (WARN_ON_ONCE(!tp))
  303. return -ENODEV;
  304. enabled = trace_probe_is_enabled(tp);
  305. /* This also changes "enabled" state */
  306. if (file) {
  307. ret = trace_probe_add_file(tp, file);
  308. if (ret)
  309. return ret;
  310. } else
  311. trace_probe_set_flag(tp, TP_FLAG_PROFILE);
  312. if (enabled)
  313. return 0;
  314. list_for_each_entry(tk, trace_probe_probe_list(tp), tp.list) {
  315. if (trace_kprobe_has_gone(tk))
  316. continue;
  317. ret = __enable_trace_kprobe(tk);
  318. if (ret)
  319. break;
  320. enabled = true;
  321. }
  322. if (ret) {
  323. /* Failed to enable one of them. Roll back all */
  324. if (enabled)
  325. __disable_trace_kprobe(tp);
  326. if (file)
  327. trace_probe_remove_file(tp, file);
  328. else
  329. trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
  330. }
  331. return ret;
  332. }
  333. /*
  334. * Disable trace_probe
  335. * if the file is NULL, disable "perf" handler, or disable "trace" handler.
  336. */
  337. static int disable_trace_kprobe(struct trace_event_call *call,
  338. struct trace_event_file *file)
  339. {
  340. struct trace_probe *tp;
  341. tp = trace_probe_primary_from_call(call);
  342. if (WARN_ON_ONCE(!tp))
  343. return -ENODEV;
  344. if (file) {
  345. if (!trace_probe_get_file_link(tp, file))
  346. return -ENOENT;
  347. if (!trace_probe_has_single_file(tp))
  348. goto out;
  349. trace_probe_clear_flag(tp, TP_FLAG_TRACE);
  350. } else
  351. trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
  352. if (!trace_probe_is_enabled(tp))
  353. __disable_trace_kprobe(tp);
  354. out:
  355. if (file)
  356. /*
  357. * Synchronization is done in below function. For perf event,
  358. * file == NULL and perf_trace_event_unreg() calls
  359. * tracepoint_synchronize_unregister() to ensure synchronize
  360. * event. We don't need to care about it.
  361. */
  362. trace_probe_remove_file(tp, file);
  363. return 0;
  364. }
  365. #if defined(CONFIG_DYNAMIC_FTRACE) && \
  366. !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
  367. static bool __within_notrace_func(unsigned long addr)
  368. {
  369. unsigned long offset, size;
  370. if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
  371. return false;
  372. /* Get the entry address of the target function */
  373. addr -= offset;
  374. /*
  375. * Since ftrace_location_range() does inclusive range check, we need
  376. * to subtract 1 byte from the end address.
  377. */
  378. return !ftrace_location_range(addr, addr + size - 1);
  379. }
  380. static bool within_notrace_func(struct trace_kprobe *tk)
  381. {
  382. unsigned long addr = trace_kprobe_address(tk);
  383. char symname[KSYM_NAME_LEN], *p;
  384. if (!__within_notrace_func(addr))
  385. return false;
  386. /* Check if the address is on a suffixed-symbol */
  387. if (!lookup_symbol_name(addr, symname)) {
  388. p = strchr(symname, '.');
  389. if (!p)
  390. return true;
  391. *p = '\0';
  392. addr = (unsigned long)kprobe_lookup_name(symname, 0);
  393. if (addr)
  394. return __within_notrace_func(addr);
  395. }
  396. return true;
  397. }
  398. #else
  399. #define within_notrace_func(tk) (false)
  400. #endif
  401. /* Internal register function - just handle k*probes and flags */
  402. static int __register_trace_kprobe(struct trace_kprobe *tk)
  403. {
  404. int i, ret;
  405. ret = security_locked_down(LOCKDOWN_KPROBES);
  406. if (ret)
  407. return ret;
  408. if (trace_kprobe_is_registered(tk))
  409. return -EINVAL;
  410. if (within_notrace_func(tk)) {
  411. pr_warn("Could not probe notrace function %ps\n",
  412. (void *)trace_kprobe_address(tk));
  413. return -EINVAL;
  414. }
  415. for (i = 0; i < tk->tp.nr_args; i++) {
  416. ret = traceprobe_update_arg(&tk->tp.args[i]);
  417. if (ret)
  418. return ret;
  419. }
  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. return ret;
  430. }
  431. /* Internal unregister function - just handle k*probes and flags */
  432. static void __unregister_trace_kprobe(struct trace_kprobe *tk)
  433. {
  434. if (trace_kprobe_is_registered(tk)) {
  435. if (trace_kprobe_is_return(tk))
  436. unregister_kretprobe(&tk->rp);
  437. else
  438. unregister_kprobe(&tk->rp.kp);
  439. /* Cleanup kprobe for reuse and mark it unregistered */
  440. INIT_HLIST_NODE(&tk->rp.kp.hlist);
  441. INIT_LIST_HEAD(&tk->rp.kp.list);
  442. if (tk->rp.kp.symbol_name)
  443. tk->rp.kp.addr = NULL;
  444. }
  445. }
  446. /* Unregister a trace_probe and probe_event */
  447. static int unregister_trace_kprobe(struct trace_kprobe *tk)
  448. {
  449. /* If other probes are on the event, just unregister kprobe */
  450. if (trace_probe_has_sibling(&tk->tp))
  451. goto unreg;
  452. /* Enabled event can not be unregistered */
  453. if (trace_probe_is_enabled(&tk->tp))
  454. return -EBUSY;
  455. /* If there's a reference to the dynamic event */
  456. if (trace_event_dyn_busy(trace_probe_event_call(&tk->tp)))
  457. return -EBUSY;
  458. /* Will fail if probe is being used by ftrace or perf */
  459. if (unregister_kprobe_event(tk))
  460. return -EBUSY;
  461. unreg:
  462. __unregister_trace_kprobe(tk);
  463. dyn_event_remove(&tk->devent);
  464. trace_probe_unlink(&tk->tp);
  465. return 0;
  466. }
  467. static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig,
  468. struct trace_kprobe *comp)
  469. {
  470. struct trace_probe_event *tpe = orig->tp.event;
  471. int i;
  472. list_for_each_entry(orig, &tpe->probes, tp.list) {
  473. if (strcmp(trace_kprobe_symbol(orig),
  474. trace_kprobe_symbol(comp)) ||
  475. trace_kprobe_offset(orig) != trace_kprobe_offset(comp))
  476. continue;
  477. /*
  478. * trace_probe_compare_arg_type() ensured that nr_args and
  479. * each argument name and type are same. Let's compare comm.
  480. */
  481. for (i = 0; i < orig->tp.nr_args; i++) {
  482. if (strcmp(orig->tp.args[i].comm,
  483. comp->tp.args[i].comm))
  484. break;
  485. }
  486. if (i == orig->tp.nr_args)
  487. return true;
  488. }
  489. return false;
  490. }
  491. static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
  492. {
  493. int ret;
  494. ret = trace_probe_compare_arg_type(&tk->tp, &to->tp);
  495. if (ret) {
  496. /* Note that argument starts index = 2 */
  497. trace_probe_log_set_index(ret + 1);
  498. trace_probe_log_err(0, DIFF_ARG_TYPE);
  499. return -EEXIST;
  500. }
  501. if (trace_kprobe_has_same_kprobe(to, tk)) {
  502. trace_probe_log_set_index(0);
  503. trace_probe_log_err(0, SAME_PROBE);
  504. return -EEXIST;
  505. }
  506. /* Append to existing event */
  507. ret = trace_probe_append(&tk->tp, &to->tp);
  508. if (ret)
  509. return ret;
  510. /* Register k*probe */
  511. ret = __register_trace_kprobe(tk);
  512. if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
  513. pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
  514. ret = 0;
  515. }
  516. if (ret)
  517. trace_probe_unlink(&tk->tp);
  518. else
  519. dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp));
  520. return ret;
  521. }
  522. /* Register a trace_probe and probe_event */
  523. static int register_trace_kprobe(struct trace_kprobe *tk)
  524. {
  525. struct trace_kprobe *old_tk;
  526. int ret;
  527. mutex_lock(&event_mutex);
  528. old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
  529. trace_probe_group_name(&tk->tp));
  530. if (old_tk) {
  531. if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
  532. trace_probe_log_set_index(0);
  533. trace_probe_log_err(0, DIFF_PROBE_TYPE);
  534. ret = -EEXIST;
  535. } else {
  536. ret = append_trace_kprobe(tk, old_tk);
  537. }
  538. goto end;
  539. }
  540. /* Register new event */
  541. ret = register_kprobe_event(tk);
  542. if (ret) {
  543. if (ret == -EEXIST) {
  544. trace_probe_log_set_index(0);
  545. trace_probe_log_err(0, EVENT_EXIST);
  546. } else
  547. pr_warn("Failed to register probe event(%d)\n", ret);
  548. goto end;
  549. }
  550. /* Register k*probe */
  551. ret = __register_trace_kprobe(tk);
  552. if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
  553. pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
  554. ret = 0;
  555. }
  556. if (ret < 0)
  557. unregister_kprobe_event(tk);
  558. else
  559. dyn_event_add(&tk->devent, trace_probe_event_call(&tk->tp));
  560. end:
  561. mutex_unlock(&event_mutex);
  562. return ret;
  563. }
  564. #ifdef CONFIG_MODULES
  565. static int validate_module_probe_symbol(const char *modname, const char *symbol);
  566. static int register_module_trace_kprobe(struct module *mod, struct trace_kprobe *tk)
  567. {
  568. const char *p;
  569. int ret = 0;
  570. p = strchr(trace_kprobe_symbol(tk), ':');
  571. if (p)
  572. ret = validate_module_probe_symbol(module_name(mod), p + 1);
  573. if (!ret)
  574. ret = __register_trace_kprobe(tk);
  575. return ret;
  576. }
  577. /* Module notifier call back, checking event on the module */
  578. static int trace_kprobe_module_callback(struct notifier_block *nb,
  579. unsigned long val, void *data)
  580. {
  581. struct module *mod = data;
  582. struct dyn_event *pos;
  583. struct trace_kprobe *tk;
  584. int ret;
  585. if (val != MODULE_STATE_COMING)
  586. return NOTIFY_DONE;
  587. /* Update probes on coming module */
  588. mutex_lock(&event_mutex);
  589. for_each_trace_kprobe(tk, pos) {
  590. if (trace_kprobe_within_module(tk, mod)) {
  591. /* Don't need to check busy - this should have gone. */
  592. __unregister_trace_kprobe(tk);
  593. ret = register_module_trace_kprobe(mod, tk);
  594. if (ret)
  595. pr_warn("Failed to re-register probe %s on %s: %d\n",
  596. trace_probe_name(&tk->tp),
  597. module_name(mod), ret);
  598. }
  599. }
  600. mutex_unlock(&event_mutex);
  601. return NOTIFY_DONE;
  602. }
  603. static struct notifier_block trace_kprobe_module_nb = {
  604. .notifier_call = trace_kprobe_module_callback,
  605. .priority = 2 /* Invoked after kprobe and jump_label module callback */
  606. };
  607. static int trace_kprobe_register_module_notifier(void)
  608. {
  609. return register_module_notifier(&trace_kprobe_module_nb);
  610. }
  611. #else
  612. static int trace_kprobe_register_module_notifier(void)
  613. {
  614. return 0;
  615. }
  616. #endif /* CONFIG_MODULES */
  617. static int count_symbols(void *data, unsigned long unused)
  618. {
  619. unsigned int *count = data;
  620. (*count)++;
  621. return 0;
  622. }
  623. struct sym_count_ctx {
  624. unsigned int count;
  625. const char *name;
  626. };
  627. static int count_mod_symbols(void *data, const char *name, unsigned long unused)
  628. {
  629. struct sym_count_ctx *ctx = data;
  630. if (strcmp(name, ctx->name) == 0)
  631. ctx->count++;
  632. return 0;
  633. }
  634. static unsigned int number_of_same_symbols(const char *mod, const char *func_name)
  635. {
  636. struct sym_count_ctx ctx = { .count = 0, .name = func_name };
  637. if (!mod)
  638. kallsyms_on_each_match_symbol(count_symbols, func_name, &ctx.count);
  639. module_kallsyms_on_each_symbol(mod, count_mod_symbols, &ctx);
  640. return ctx.count;
  641. }
  642. static int validate_module_probe_symbol(const char *modname, const char *symbol)
  643. {
  644. unsigned int count = number_of_same_symbols(modname, symbol);
  645. if (count > 1) {
  646. /*
  647. * Users should use ADDR to remove the ambiguity of
  648. * using KSYM only.
  649. */
  650. return -EADDRNOTAVAIL;
  651. } else if (count == 0) {
  652. /*
  653. * We can return ENOENT earlier than when register the
  654. * kprobe.
  655. */
  656. return -ENOENT;
  657. }
  658. return 0;
  659. }
  660. #ifdef CONFIG_MODULES
  661. /* Return NULL if the module is not loaded or under unloading. */
  662. static struct module *try_module_get_by_name(const char *name)
  663. {
  664. struct module *mod;
  665. rcu_read_lock_sched();
  666. mod = find_module(name);
  667. if (mod && !try_module_get(mod))
  668. mod = NULL;
  669. rcu_read_unlock_sched();
  670. return mod;
  671. }
  672. #else
  673. #define try_module_get_by_name(name) (NULL)
  674. #endif
  675. static int validate_probe_symbol(char *symbol)
  676. {
  677. struct module *mod = NULL;
  678. char *modname = NULL, *p;
  679. int ret = 0;
  680. p = strchr(symbol, ':');
  681. if (p) {
  682. modname = symbol;
  683. symbol = p + 1;
  684. *p = '\0';
  685. mod = try_module_get_by_name(modname);
  686. if (!mod)
  687. goto out;
  688. }
  689. ret = validate_module_probe_symbol(modname, symbol);
  690. out:
  691. if (p)
  692. *p = ':';
  693. if (mod)
  694. module_put(mod);
  695. return ret;
  696. }
  697. static int trace_kprobe_entry_handler(struct kretprobe_instance *ri,
  698. struct pt_regs *regs);
  699. static int __trace_kprobe_create(int argc, const char *argv[])
  700. {
  701. /*
  702. * Argument syntax:
  703. * - Add kprobe:
  704. * p[:[GRP/][EVENT]] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  705. * - Add kretprobe:
  706. * r[MAXACTIVE][:[GRP/][EVENT]] [MOD:]KSYM[+0] [FETCHARGS]
  707. * Or
  708. * p[:[GRP/][EVENT]] [MOD:]KSYM[+0]%return [FETCHARGS]
  709. *
  710. * Fetch args:
  711. * $retval : fetch return value
  712. * $stack : fetch stack address
  713. * $stackN : fetch Nth of stack (N:0-)
  714. * $comm : fetch current task comm
  715. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  716. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  717. * %REG : fetch register REG
  718. * Dereferencing memory fetch:
  719. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  720. * Alias name of args:
  721. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  722. * Type of args:
  723. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  724. */
  725. struct trace_kprobe *tk = NULL;
  726. int i, len, new_argc = 0, ret = 0;
  727. bool is_return = false;
  728. char *symbol = NULL, *tmp = NULL;
  729. const char **new_argv = NULL;
  730. const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
  731. enum probe_print_type ptype;
  732. int maxactive = 0;
  733. long offset = 0;
  734. void *addr = NULL;
  735. char buf[MAX_EVENT_NAME_LEN];
  736. char gbuf[MAX_EVENT_NAME_LEN];
  737. char abuf[MAX_BTF_ARGS_LEN];
  738. char *dbuf = NULL;
  739. struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL };
  740. switch (argv[0][0]) {
  741. case 'r':
  742. is_return = true;
  743. break;
  744. case 'p':
  745. break;
  746. default:
  747. return -ECANCELED;
  748. }
  749. if (argc < 2)
  750. return -ECANCELED;
  751. trace_probe_log_init("trace_kprobe", argc, argv);
  752. event = strchr(&argv[0][1], ':');
  753. if (event)
  754. event++;
  755. if (isdigit(argv[0][1])) {
  756. if (!is_return) {
  757. trace_probe_log_err(1, BAD_MAXACT_TYPE);
  758. goto parse_error;
  759. }
  760. if (event)
  761. len = event - &argv[0][1] - 1;
  762. else
  763. len = strlen(&argv[0][1]);
  764. if (len > MAX_EVENT_NAME_LEN - 1) {
  765. trace_probe_log_err(1, BAD_MAXACT);
  766. goto parse_error;
  767. }
  768. memcpy(buf, &argv[0][1], len);
  769. buf[len] = '\0';
  770. ret = kstrtouint(buf, 0, &maxactive);
  771. if (ret || !maxactive) {
  772. trace_probe_log_err(1, BAD_MAXACT);
  773. goto parse_error;
  774. }
  775. /* kretprobes instances are iterated over via a list. The
  776. * maximum should stay reasonable.
  777. */
  778. if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
  779. trace_probe_log_err(1, MAXACT_TOO_BIG);
  780. goto parse_error;
  781. }
  782. }
  783. /* try to parse an address. if that fails, try to read the
  784. * input as a symbol. */
  785. if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
  786. trace_probe_log_set_index(1);
  787. /* Check whether uprobe event specified */
  788. if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
  789. ret = -ECANCELED;
  790. goto error;
  791. }
  792. /* a symbol specified */
  793. symbol = kstrdup(argv[1], GFP_KERNEL);
  794. if (!symbol)
  795. return -ENOMEM;
  796. tmp = strchr(symbol, '%');
  797. if (tmp) {
  798. if (!strcmp(tmp, "%return")) {
  799. *tmp = '\0';
  800. is_return = true;
  801. } else {
  802. trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX);
  803. goto parse_error;
  804. }
  805. }
  806. /* TODO: support .init module functions */
  807. ret = traceprobe_split_symbol_offset(symbol, &offset);
  808. if (ret || offset < 0 || offset > UINT_MAX) {
  809. trace_probe_log_err(0, BAD_PROBE_ADDR);
  810. goto parse_error;
  811. }
  812. ret = validate_probe_symbol(symbol);
  813. if (ret) {
  814. if (ret == -EADDRNOTAVAIL)
  815. trace_probe_log_err(0, NON_UNIQ_SYMBOL);
  816. else
  817. trace_probe_log_err(0, BAD_PROBE_ADDR);
  818. goto parse_error;
  819. }
  820. if (is_return)
  821. ctx.flags |= TPARG_FL_RETURN;
  822. ret = kprobe_on_func_entry(NULL, symbol, offset);
  823. if (ret == 0 && !is_return)
  824. ctx.flags |= TPARG_FL_FENTRY;
  825. /* Defer the ENOENT case until register kprobe */
  826. if (ret == -EINVAL && is_return) {
  827. trace_probe_log_err(0, BAD_RETPROBE);
  828. goto parse_error;
  829. }
  830. }
  831. trace_probe_log_set_index(0);
  832. if (event) {
  833. ret = traceprobe_parse_event_name(&event, &group, gbuf,
  834. event - argv[0]);
  835. if (ret)
  836. goto parse_error;
  837. }
  838. if (!event) {
  839. /* Make a new event name */
  840. if (symbol)
  841. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  842. is_return ? 'r' : 'p', symbol, offset);
  843. else
  844. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  845. is_return ? 'r' : 'p', addr);
  846. sanitize_event_name(buf);
  847. event = buf;
  848. }
  849. argc -= 2; argv += 2;
  850. ctx.funcname = symbol;
  851. new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc,
  852. abuf, MAX_BTF_ARGS_LEN, &ctx);
  853. if (IS_ERR(new_argv)) {
  854. ret = PTR_ERR(new_argv);
  855. new_argv = NULL;
  856. goto out;
  857. }
  858. if (new_argv) {
  859. argc = new_argc;
  860. argv = new_argv;
  861. }
  862. if (argc > MAX_TRACE_ARGS) {
  863. ret = -E2BIG;
  864. goto out;
  865. }
  866. ret = traceprobe_expand_dentry_args(argc, argv, &dbuf);
  867. if (ret)
  868. goto out;
  869. /* setup a probe */
  870. tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
  871. argc, is_return);
  872. if (IS_ERR(tk)) {
  873. ret = PTR_ERR(tk);
  874. /* This must return -ENOMEM, else there is a bug */
  875. WARN_ON_ONCE(ret != -ENOMEM);
  876. goto out; /* We know tk is not allocated */
  877. }
  878. /* parse arguments */
  879. for (i = 0; i < argc; i++) {
  880. trace_probe_log_set_index(i + 2);
  881. ctx.offset = 0;
  882. ret = traceprobe_parse_probe_arg(&tk->tp, i, argv[i], &ctx);
  883. if (ret)
  884. goto error; /* This can be -ENOMEM */
  885. }
  886. /* entry handler for kretprobe */
  887. if (is_return && tk->tp.entry_arg) {
  888. tk->rp.entry_handler = trace_kprobe_entry_handler;
  889. tk->rp.data_size = traceprobe_get_entry_data_size(&tk->tp);
  890. }
  891. ptype = is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
  892. ret = traceprobe_set_print_fmt(&tk->tp, ptype);
  893. if (ret < 0)
  894. goto error;
  895. ret = register_trace_kprobe(tk);
  896. if (ret) {
  897. trace_probe_log_set_index(1);
  898. if (ret == -EILSEQ)
  899. trace_probe_log_err(0, BAD_INSN_BNDRY);
  900. else if (ret == -ENOENT)
  901. trace_probe_log_err(0, BAD_PROBE_ADDR);
  902. else if (ret != -ENOMEM && ret != -EEXIST)
  903. trace_probe_log_err(0, FAIL_REG_PROBE);
  904. goto error;
  905. }
  906. out:
  907. traceprobe_finish_parse(&ctx);
  908. trace_probe_log_clear();
  909. kfree(new_argv);
  910. kfree(symbol);
  911. kfree(dbuf);
  912. return ret;
  913. parse_error:
  914. ret = -EINVAL;
  915. error:
  916. free_trace_kprobe(tk);
  917. goto out;
  918. }
  919. static int trace_kprobe_create(const char *raw_command)
  920. {
  921. return trace_probe_create(raw_command, __trace_kprobe_create);
  922. }
  923. static int create_or_delete_trace_kprobe(const char *raw_command)
  924. {
  925. int ret;
  926. if (raw_command[0] == '-')
  927. return dyn_event_release(raw_command, &trace_kprobe_ops);
  928. ret = trace_kprobe_create(raw_command);
  929. return ret == -ECANCELED ? -EINVAL : ret;
  930. }
  931. static int trace_kprobe_run_command(struct dynevent_cmd *cmd)
  932. {
  933. return create_or_delete_trace_kprobe(cmd->seq.buffer);
  934. }
  935. /**
  936. * kprobe_event_cmd_init - Initialize a kprobe event command object
  937. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  938. * @buf: A pointer to the buffer used to build the command
  939. * @maxlen: The length of the buffer passed in @buf
  940. *
  941. * Initialize a synthetic event command object. Use this before
  942. * calling any of the other kprobe_event functions.
  943. */
  944. void kprobe_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
  945. {
  946. dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_KPROBE,
  947. trace_kprobe_run_command);
  948. }
  949. EXPORT_SYMBOL_GPL(kprobe_event_cmd_init);
  950. /**
  951. * __kprobe_event_gen_cmd_start - Generate a kprobe event command from arg list
  952. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  953. * @kretprobe: Is this a return probe?
  954. * @name: The name of the kprobe event
  955. * @loc: The location of the kprobe event
  956. * @...: Variable number of arg (pairs), one pair for each field
  957. *
  958. * NOTE: Users normally won't want to call this function directly, but
  959. * rather use the kprobe_event_gen_cmd_start() wrapper, which automatically
  960. * adds a NULL to the end of the arg list. If this function is used
  961. * directly, make sure the last arg in the variable arg list is NULL.
  962. *
  963. * Generate a kprobe event command to be executed by
  964. * kprobe_event_gen_cmd_end(). This function can be used to generate the
  965. * complete command or only the first part of it; in the latter case,
  966. * kprobe_event_add_fields() can be used to add more fields following this.
  967. *
  968. * Unlikely the synth_event_gen_cmd_start(), @loc must be specified. This
  969. * returns -EINVAL if @loc == NULL.
  970. *
  971. * Return: 0 if successful, error otherwise.
  972. */
  973. int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd, bool kretprobe,
  974. const char *name, const char *loc, ...)
  975. {
  976. char buf[MAX_EVENT_NAME_LEN];
  977. struct dynevent_arg arg;
  978. va_list args;
  979. int ret;
  980. if (cmd->type != DYNEVENT_TYPE_KPROBE)
  981. return -EINVAL;
  982. if (!loc)
  983. return -EINVAL;
  984. if (kretprobe)
  985. snprintf(buf, MAX_EVENT_NAME_LEN, "r:kprobes/%s", name);
  986. else
  987. snprintf(buf, MAX_EVENT_NAME_LEN, "p:kprobes/%s", name);
  988. ret = dynevent_str_add(cmd, buf);
  989. if (ret)
  990. return ret;
  991. dynevent_arg_init(&arg, 0);
  992. arg.str = loc;
  993. ret = dynevent_arg_add(cmd, &arg, NULL);
  994. if (ret)
  995. return ret;
  996. va_start(args, loc);
  997. for (;;) {
  998. const char *field;
  999. field = va_arg(args, const char *);
  1000. if (!field)
  1001. break;
  1002. if (++cmd->n_fields > MAX_TRACE_ARGS) {
  1003. ret = -EINVAL;
  1004. break;
  1005. }
  1006. arg.str = field;
  1007. ret = dynevent_arg_add(cmd, &arg, NULL);
  1008. if (ret)
  1009. break;
  1010. }
  1011. va_end(args);
  1012. return ret;
  1013. }
  1014. EXPORT_SYMBOL_GPL(__kprobe_event_gen_cmd_start);
  1015. /**
  1016. * __kprobe_event_add_fields - Add probe fields to a kprobe command from arg list
  1017. * @cmd: A pointer to the dynevent_cmd struct representing the new event
  1018. * @...: Variable number of arg (pairs), one pair for each field
  1019. *
  1020. * NOTE: Users normally won't want to call this function directly, but
  1021. * rather use the kprobe_event_add_fields() wrapper, which
  1022. * automatically adds a NULL to the end of the arg list. If this
  1023. * function is used directly, make sure the last arg in the variable
  1024. * arg list is NULL.
  1025. *
  1026. * Add probe fields to an existing kprobe command using a variable
  1027. * list of args. Fields are added in the same order they're listed.
  1028. *
  1029. * Return: 0 if successful, error otherwise.
  1030. */
  1031. int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...)
  1032. {
  1033. struct dynevent_arg arg;
  1034. va_list args;
  1035. int ret = 0;
  1036. if (cmd->type != DYNEVENT_TYPE_KPROBE)
  1037. return -EINVAL;
  1038. dynevent_arg_init(&arg, 0);
  1039. va_start(args, cmd);
  1040. for (;;) {
  1041. const char *field;
  1042. field = va_arg(args, const char *);
  1043. if (!field)
  1044. break;
  1045. if (++cmd->n_fields > MAX_TRACE_ARGS) {
  1046. ret = -EINVAL;
  1047. break;
  1048. }
  1049. arg.str = field;
  1050. ret = dynevent_arg_add(cmd, &arg, NULL);
  1051. if (ret)
  1052. break;
  1053. }
  1054. va_end(args);
  1055. return ret;
  1056. }
  1057. EXPORT_SYMBOL_GPL(__kprobe_event_add_fields);
  1058. /**
  1059. * kprobe_event_delete - Delete a kprobe event
  1060. * @name: The name of the kprobe event to delete
  1061. *
  1062. * Delete a kprobe event with the give @name from kernel code rather
  1063. * than directly from the command line.
  1064. *
  1065. * Return: 0 if successful, error otherwise.
  1066. */
  1067. int kprobe_event_delete(const char *name)
  1068. {
  1069. char buf[MAX_EVENT_NAME_LEN];
  1070. snprintf(buf, MAX_EVENT_NAME_LEN, "-:%s", name);
  1071. return create_or_delete_trace_kprobe(buf);
  1072. }
  1073. EXPORT_SYMBOL_GPL(kprobe_event_delete);
  1074. static int trace_kprobe_release(struct dyn_event *ev)
  1075. {
  1076. struct trace_kprobe *tk = to_trace_kprobe(ev);
  1077. int ret = unregister_trace_kprobe(tk);
  1078. if (!ret)
  1079. free_trace_kprobe(tk);
  1080. return ret;
  1081. }
  1082. static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
  1083. {
  1084. struct trace_kprobe *tk = to_trace_kprobe(ev);
  1085. int i;
  1086. seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
  1087. if (trace_kprobe_is_return(tk) && tk->rp.maxactive)
  1088. seq_printf(m, "%d", tk->rp.maxactive);
  1089. seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
  1090. trace_probe_name(&tk->tp));
  1091. if (!tk->symbol)
  1092. seq_printf(m, " 0x%p", tk->rp.kp.addr);
  1093. else if (tk->rp.kp.offset)
  1094. seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
  1095. tk->rp.kp.offset);
  1096. else
  1097. seq_printf(m, " %s", trace_kprobe_symbol(tk));
  1098. for (i = 0; i < tk->tp.nr_args; i++)
  1099. seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
  1100. seq_putc(m, '\n');
  1101. return 0;
  1102. }
  1103. static int probes_seq_show(struct seq_file *m, void *v)
  1104. {
  1105. struct dyn_event *ev = v;
  1106. if (!is_trace_kprobe(ev))
  1107. return 0;
  1108. return trace_kprobe_show(m, ev);
  1109. }
  1110. static const struct seq_operations probes_seq_op = {
  1111. .start = dyn_event_seq_start,
  1112. .next = dyn_event_seq_next,
  1113. .stop = dyn_event_seq_stop,
  1114. .show = probes_seq_show
  1115. };
  1116. static int probes_open(struct inode *inode, struct file *file)
  1117. {
  1118. int ret;
  1119. ret = security_locked_down(LOCKDOWN_TRACEFS);
  1120. if (ret)
  1121. return ret;
  1122. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  1123. ret = dyn_events_release_all(&trace_kprobe_ops);
  1124. if (ret < 0)
  1125. return ret;
  1126. }
  1127. return seq_open(file, &probes_seq_op);
  1128. }
  1129. static ssize_t probes_write(struct file *file, const char __user *buffer,
  1130. size_t count, loff_t *ppos)
  1131. {
  1132. return trace_parse_run_command(file, buffer, count, ppos,
  1133. create_or_delete_trace_kprobe);
  1134. }
  1135. static const struct file_operations kprobe_events_ops = {
  1136. .owner = THIS_MODULE,
  1137. .open = probes_open,
  1138. .read = seq_read,
  1139. .llseek = seq_lseek,
  1140. .release = seq_release,
  1141. .write = probes_write,
  1142. };
  1143. static unsigned long trace_kprobe_missed(struct trace_kprobe *tk)
  1144. {
  1145. return trace_kprobe_is_return(tk) ?
  1146. tk->rp.kp.nmissed + tk->rp.nmissed : tk->rp.kp.nmissed;
  1147. }
  1148. /* Probes profiling interfaces */
  1149. static int probes_profile_seq_show(struct seq_file *m, void *v)
  1150. {
  1151. struct dyn_event *ev = v;
  1152. struct trace_kprobe *tk;
  1153. unsigned long nmissed;
  1154. if (!is_trace_kprobe(ev))
  1155. return 0;
  1156. tk = to_trace_kprobe(ev);
  1157. nmissed = trace_kprobe_missed(tk);
  1158. seq_printf(m, " %-44s %15lu %15lu\n",
  1159. trace_probe_name(&tk->tp),
  1160. trace_kprobe_nhit(tk),
  1161. nmissed);
  1162. return 0;
  1163. }
  1164. static const struct seq_operations profile_seq_op = {
  1165. .start = dyn_event_seq_start,
  1166. .next = dyn_event_seq_next,
  1167. .stop = dyn_event_seq_stop,
  1168. .show = probes_profile_seq_show
  1169. };
  1170. static int profile_open(struct inode *inode, struct file *file)
  1171. {
  1172. int ret;
  1173. ret = security_locked_down(LOCKDOWN_TRACEFS);
  1174. if (ret)
  1175. return ret;
  1176. return seq_open(file, &profile_seq_op);
  1177. }
  1178. static const struct file_operations kprobe_profile_ops = {
  1179. .owner = THIS_MODULE,
  1180. .open = profile_open,
  1181. .read = seq_read,
  1182. .llseek = seq_lseek,
  1183. .release = seq_release,
  1184. };
  1185. /* Note that we don't verify it, since the code does not come from user space */
  1186. static int
  1187. process_fetch_insn(struct fetch_insn *code, void *rec, void *edata,
  1188. void *dest, void *base)
  1189. {
  1190. struct pt_regs *regs = rec;
  1191. unsigned long val;
  1192. int ret;
  1193. retry:
  1194. /* 1st stage: get value from context */
  1195. switch (code->op) {
  1196. case FETCH_OP_REG:
  1197. val = regs_get_register(regs, code->param);
  1198. break;
  1199. case FETCH_OP_STACK:
  1200. val = regs_get_kernel_stack_nth(regs, code->param);
  1201. break;
  1202. case FETCH_OP_STACKP:
  1203. val = kernel_stack_pointer(regs);
  1204. break;
  1205. case FETCH_OP_RETVAL:
  1206. val = regs_return_value(regs);
  1207. break;
  1208. #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
  1209. case FETCH_OP_ARG:
  1210. val = regs_get_kernel_argument(regs, code->param);
  1211. break;
  1212. case FETCH_OP_EDATA:
  1213. val = *(unsigned long *)((unsigned long)edata + code->offset);
  1214. break;
  1215. #endif
  1216. case FETCH_NOP_SYMBOL: /* Ignore a place holder */
  1217. code++;
  1218. goto retry;
  1219. default:
  1220. ret = process_common_fetch_insn(code, &val);
  1221. if (ret < 0)
  1222. return ret;
  1223. }
  1224. code++;
  1225. return process_fetch_insn_bottom(code, val, dest, base);
  1226. }
  1227. NOKPROBE_SYMBOL(process_fetch_insn)
  1228. /* Kprobe handler */
  1229. static nokprobe_inline void
  1230. __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
  1231. struct trace_event_file *trace_file)
  1232. {
  1233. struct kprobe_trace_entry_head *entry;
  1234. struct trace_event_call *call = trace_probe_event_call(&tk->tp);
  1235. struct trace_event_buffer fbuffer;
  1236. int dsize;
  1237. WARN_ON(call != trace_file->event_call);
  1238. if (trace_trigger_soft_disabled(trace_file))
  1239. return;
  1240. dsize = __get_data_size(&tk->tp, regs, NULL);
  1241. entry = trace_event_buffer_reserve(&fbuffer, trace_file,
  1242. sizeof(*entry) + tk->tp.size + dsize);
  1243. if (!entry)
  1244. return;
  1245. fbuffer.regs = regs;
  1246. entry->ip = (unsigned long)tk->rp.kp.addr;
  1247. store_trace_args(&entry[1], &tk->tp, regs, NULL, sizeof(*entry), dsize);
  1248. trace_event_buffer_commit(&fbuffer);
  1249. }
  1250. static void
  1251. kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
  1252. {
  1253. struct event_file_link *link;
  1254. trace_probe_for_each_link_rcu(link, &tk->tp)
  1255. __kprobe_trace_func(tk, regs, link->file);
  1256. }
  1257. NOKPROBE_SYMBOL(kprobe_trace_func);
  1258. /* Kretprobe handler */
  1259. static int trace_kprobe_entry_handler(struct kretprobe_instance *ri,
  1260. struct pt_regs *regs)
  1261. {
  1262. struct kretprobe *rp = get_kretprobe(ri);
  1263. struct trace_kprobe *tk;
  1264. /*
  1265. * There is a small chance that get_kretprobe(ri) returns NULL when
  1266. * the kretprobe is unregister on another CPU between kretprobe's
  1267. * trampoline_handler and this function.
  1268. */
  1269. if (unlikely(!rp))
  1270. return -ENOENT;
  1271. tk = container_of(rp, struct trace_kprobe, rp);
  1272. /* store argument values into ri->data as entry data */
  1273. if (tk->tp.entry_arg)
  1274. store_trace_entry_data(ri->data, &tk->tp, regs);
  1275. return 0;
  1276. }
  1277. static nokprobe_inline void
  1278. __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  1279. struct pt_regs *regs,
  1280. struct trace_event_file *trace_file)
  1281. {
  1282. struct kretprobe_trace_entry_head *entry;
  1283. struct trace_event_buffer fbuffer;
  1284. struct trace_event_call *call = trace_probe_event_call(&tk->tp);
  1285. int dsize;
  1286. WARN_ON(call != trace_file->event_call);
  1287. if (trace_trigger_soft_disabled(trace_file))
  1288. return;
  1289. dsize = __get_data_size(&tk->tp, regs, ri->data);
  1290. entry = trace_event_buffer_reserve(&fbuffer, trace_file,
  1291. sizeof(*entry) + tk->tp.size + dsize);
  1292. if (!entry)
  1293. return;
  1294. fbuffer.regs = regs;
  1295. entry->func = (unsigned long)tk->rp.kp.addr;
  1296. entry->ret_ip = get_kretprobe_retaddr(ri);
  1297. store_trace_args(&entry[1], &tk->tp, regs, ri->data, sizeof(*entry), dsize);
  1298. trace_event_buffer_commit(&fbuffer);
  1299. }
  1300. static void
  1301. kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  1302. struct pt_regs *regs)
  1303. {
  1304. struct event_file_link *link;
  1305. trace_probe_for_each_link_rcu(link, &tk->tp)
  1306. __kretprobe_trace_func(tk, ri, regs, link->file);
  1307. }
  1308. NOKPROBE_SYMBOL(kretprobe_trace_func);
  1309. /* Event entry printers */
  1310. static enum print_line_t
  1311. print_kprobe_event(struct trace_iterator *iter, int flags,
  1312. struct trace_event *event)
  1313. {
  1314. struct kprobe_trace_entry_head *field;
  1315. struct trace_seq *s = &iter->seq;
  1316. struct trace_probe *tp;
  1317. field = (struct kprobe_trace_entry_head *)iter->ent;
  1318. tp = trace_probe_primary_from_call(
  1319. container_of(event, struct trace_event_call, event));
  1320. if (WARN_ON_ONCE(!tp))
  1321. goto out;
  1322. trace_seq_printf(s, "%s: (", trace_probe_name(tp));
  1323. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  1324. goto out;
  1325. trace_seq_putc(s, ')');
  1326. if (trace_probe_print_args(s, tp->args, tp->nr_args,
  1327. (u8 *)&field[1], field) < 0)
  1328. goto out;
  1329. trace_seq_putc(s, '\n');
  1330. out:
  1331. return trace_handle_return(s);
  1332. }
  1333. static enum print_line_t
  1334. print_kretprobe_event(struct trace_iterator *iter, int flags,
  1335. struct trace_event *event)
  1336. {
  1337. struct kretprobe_trace_entry_head *field;
  1338. struct trace_seq *s = &iter->seq;
  1339. struct trace_probe *tp;
  1340. field = (struct kretprobe_trace_entry_head *)iter->ent;
  1341. tp = trace_probe_primary_from_call(
  1342. container_of(event, struct trace_event_call, event));
  1343. if (WARN_ON_ONCE(!tp))
  1344. goto out;
  1345. trace_seq_printf(s, "%s: (", trace_probe_name(tp));
  1346. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  1347. goto out;
  1348. trace_seq_puts(s, " <- ");
  1349. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  1350. goto out;
  1351. trace_seq_putc(s, ')');
  1352. if (trace_probe_print_args(s, tp->args, tp->nr_args,
  1353. (u8 *)&field[1], field) < 0)
  1354. goto out;
  1355. trace_seq_putc(s, '\n');
  1356. out:
  1357. return trace_handle_return(s);
  1358. }
  1359. static int kprobe_event_define_fields(struct trace_event_call *event_call)
  1360. {
  1361. int ret;
  1362. struct kprobe_trace_entry_head field;
  1363. struct trace_probe *tp;
  1364. tp = trace_probe_primary_from_call(event_call);
  1365. if (WARN_ON_ONCE(!tp))
  1366. return -ENOENT;
  1367. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  1368. return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
  1369. }
  1370. static int kretprobe_event_define_fields(struct trace_event_call *event_call)
  1371. {
  1372. int ret;
  1373. struct kretprobe_trace_entry_head field;
  1374. struct trace_probe *tp;
  1375. tp = trace_probe_primary_from_call(event_call);
  1376. if (WARN_ON_ONCE(!tp))
  1377. return -ENOENT;
  1378. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1379. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1380. return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
  1381. }
  1382. #ifdef CONFIG_PERF_EVENTS
  1383. /* Kprobe profile handler */
  1384. static int
  1385. kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
  1386. {
  1387. struct trace_event_call *call = trace_probe_event_call(&tk->tp);
  1388. struct kprobe_trace_entry_head *entry;
  1389. struct hlist_head *head;
  1390. int size, __size, dsize;
  1391. int rctx;
  1392. if (bpf_prog_array_valid(call)) {
  1393. unsigned long orig_ip = instruction_pointer(regs);
  1394. int ret;
  1395. ret = trace_call_bpf(call, regs);
  1396. /*
  1397. * We need to check and see if we modified the pc of the
  1398. * pt_regs, and if so return 1 so that we don't do the
  1399. * single stepping.
  1400. */
  1401. if (orig_ip != instruction_pointer(regs))
  1402. return 1;
  1403. if (!ret)
  1404. return 0;
  1405. }
  1406. head = this_cpu_ptr(call->perf_events);
  1407. if (hlist_empty(head))
  1408. return 0;
  1409. dsize = __get_data_size(&tk->tp, regs, NULL);
  1410. __size = sizeof(*entry) + tk->tp.size + dsize;
  1411. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1412. size -= sizeof(u32);
  1413. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  1414. if (!entry)
  1415. return 0;
  1416. entry->ip = (unsigned long)tk->rp.kp.addr;
  1417. memset(&entry[1], 0, dsize);
  1418. store_trace_args(&entry[1], &tk->tp, regs, NULL, sizeof(*entry), dsize);
  1419. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  1420. head, NULL);
  1421. return 0;
  1422. }
  1423. NOKPROBE_SYMBOL(kprobe_perf_func);
  1424. /* Kretprobe profile handler */
  1425. static void
  1426. kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
  1427. struct pt_regs *regs)
  1428. {
  1429. struct trace_event_call *call = trace_probe_event_call(&tk->tp);
  1430. struct kretprobe_trace_entry_head *entry;
  1431. struct hlist_head *head;
  1432. int size, __size, dsize;
  1433. int rctx;
  1434. if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
  1435. return;
  1436. head = this_cpu_ptr(call->perf_events);
  1437. if (hlist_empty(head))
  1438. return;
  1439. dsize = __get_data_size(&tk->tp, regs, ri->data);
  1440. __size = sizeof(*entry) + tk->tp.size + dsize;
  1441. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1442. size -= sizeof(u32);
  1443. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  1444. if (!entry)
  1445. return;
  1446. entry->func = (unsigned long)tk->rp.kp.addr;
  1447. entry->ret_ip = get_kretprobe_retaddr(ri);
  1448. store_trace_args(&entry[1], &tk->tp, regs, ri->data, sizeof(*entry), dsize);
  1449. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  1450. head, NULL);
  1451. }
  1452. NOKPROBE_SYMBOL(kretprobe_perf_func);
  1453. int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
  1454. const char **symbol, u64 *probe_offset,
  1455. u64 *probe_addr, unsigned long *missed,
  1456. bool perf_type_tracepoint)
  1457. {
  1458. const char *pevent = trace_event_name(event->tp_event);
  1459. const char *group = event->tp_event->class->system;
  1460. struct trace_kprobe *tk;
  1461. if (perf_type_tracepoint)
  1462. tk = find_trace_kprobe(pevent, group);
  1463. else
  1464. tk = trace_kprobe_primary_from_call(event->tp_event);
  1465. if (!tk)
  1466. return -EINVAL;
  1467. *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
  1468. : BPF_FD_TYPE_KPROBE;
  1469. *probe_offset = tk->rp.kp.offset;
  1470. *probe_addr = kallsyms_show_value(current_cred()) ?
  1471. (unsigned long)tk->rp.kp.addr : 0;
  1472. *symbol = tk->symbol;
  1473. if (missed)
  1474. *missed = trace_kprobe_missed(tk);
  1475. return 0;
  1476. }
  1477. #endif /* CONFIG_PERF_EVENTS */
  1478. /*
  1479. * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
  1480. *
  1481. * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
  1482. * lockless, but we can't race with this __init function.
  1483. */
  1484. static int kprobe_register(struct trace_event_call *event,
  1485. enum trace_reg type, void *data)
  1486. {
  1487. struct trace_event_file *file = data;
  1488. switch (type) {
  1489. case TRACE_REG_REGISTER:
  1490. return enable_trace_kprobe(event, file);
  1491. case TRACE_REG_UNREGISTER:
  1492. return disable_trace_kprobe(event, file);
  1493. #ifdef CONFIG_PERF_EVENTS
  1494. case TRACE_REG_PERF_REGISTER:
  1495. return enable_trace_kprobe(event, NULL);
  1496. case TRACE_REG_PERF_UNREGISTER:
  1497. return disable_trace_kprobe(event, NULL);
  1498. case TRACE_REG_PERF_OPEN:
  1499. case TRACE_REG_PERF_CLOSE:
  1500. case TRACE_REG_PERF_ADD:
  1501. case TRACE_REG_PERF_DEL:
  1502. return 0;
  1503. #endif
  1504. }
  1505. return 0;
  1506. }
  1507. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1508. {
  1509. struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
  1510. int ret = 0;
  1511. raw_cpu_inc(*tk->nhit);
  1512. if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
  1513. kprobe_trace_func(tk, regs);
  1514. #ifdef CONFIG_PERF_EVENTS
  1515. if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
  1516. ret = kprobe_perf_func(tk, regs);
  1517. #endif
  1518. return ret;
  1519. }
  1520. NOKPROBE_SYMBOL(kprobe_dispatcher);
  1521. static int
  1522. kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1523. {
  1524. struct kretprobe *rp = get_kretprobe(ri);
  1525. struct trace_kprobe *tk;
  1526. /*
  1527. * There is a small chance that get_kretprobe(ri) returns NULL when
  1528. * the kretprobe is unregister on another CPU between kretprobe's
  1529. * trampoline_handler and this function.
  1530. */
  1531. if (unlikely(!rp))
  1532. return 0;
  1533. tk = container_of(rp, struct trace_kprobe, rp);
  1534. raw_cpu_inc(*tk->nhit);
  1535. if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
  1536. kretprobe_trace_func(tk, ri, regs);
  1537. #ifdef CONFIG_PERF_EVENTS
  1538. if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
  1539. kretprobe_perf_func(tk, ri, regs);
  1540. #endif
  1541. return 0; /* We don't tweak kernel, so just return 0 */
  1542. }
  1543. NOKPROBE_SYMBOL(kretprobe_dispatcher);
  1544. static struct trace_event_functions kretprobe_funcs = {
  1545. .trace = print_kretprobe_event
  1546. };
  1547. static struct trace_event_functions kprobe_funcs = {
  1548. .trace = print_kprobe_event
  1549. };
  1550. static struct trace_event_fields kretprobe_fields_array[] = {
  1551. { .type = TRACE_FUNCTION_TYPE,
  1552. .define_fields = kretprobe_event_define_fields },
  1553. {}
  1554. };
  1555. static struct trace_event_fields kprobe_fields_array[] = {
  1556. { .type = TRACE_FUNCTION_TYPE,
  1557. .define_fields = kprobe_event_define_fields },
  1558. {}
  1559. };
  1560. static inline void init_trace_event_call(struct trace_kprobe *tk)
  1561. {
  1562. struct trace_event_call *call = trace_probe_event_call(&tk->tp);
  1563. if (trace_kprobe_is_return(tk)) {
  1564. call->event.funcs = &kretprobe_funcs;
  1565. call->class->fields_array = kretprobe_fields_array;
  1566. } else {
  1567. call->event.funcs = &kprobe_funcs;
  1568. call->class->fields_array = kprobe_fields_array;
  1569. }
  1570. call->flags = TRACE_EVENT_FL_KPROBE;
  1571. call->class->reg = kprobe_register;
  1572. }
  1573. static int register_kprobe_event(struct trace_kprobe *tk)
  1574. {
  1575. init_trace_event_call(tk);
  1576. return trace_probe_register_event_call(&tk->tp);
  1577. }
  1578. static int unregister_kprobe_event(struct trace_kprobe *tk)
  1579. {
  1580. return trace_probe_unregister_event_call(&tk->tp);
  1581. }
  1582. #ifdef CONFIG_PERF_EVENTS
  1583. /* create a trace_kprobe, but don't add it to global lists */
  1584. struct trace_event_call *
  1585. create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
  1586. bool is_return)
  1587. {
  1588. enum probe_print_type ptype;
  1589. struct trace_kprobe *tk;
  1590. int ret;
  1591. char *event;
  1592. if (func) {
  1593. ret = validate_probe_symbol(func);
  1594. if (ret)
  1595. return ERR_PTR(ret);
  1596. }
  1597. /*
  1598. * local trace_kprobes are not added to dyn_event, so they are never
  1599. * searched in find_trace_kprobe(). Therefore, there is no concern of
  1600. * duplicated name here.
  1601. */
  1602. event = func ? func : "DUMMY_EVENT";
  1603. tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
  1604. offs, 0 /* maxactive */, 0 /* nargs */,
  1605. is_return);
  1606. if (IS_ERR(tk)) {
  1607. pr_info("Failed to allocate trace_probe.(%d)\n",
  1608. (int)PTR_ERR(tk));
  1609. return ERR_CAST(tk);
  1610. }
  1611. init_trace_event_call(tk);
  1612. ptype = trace_kprobe_is_return(tk) ?
  1613. PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
  1614. if (traceprobe_set_print_fmt(&tk->tp, ptype) < 0) {
  1615. ret = -ENOMEM;
  1616. goto error;
  1617. }
  1618. ret = __register_trace_kprobe(tk);
  1619. if (ret < 0)
  1620. goto error;
  1621. return trace_probe_event_call(&tk->tp);
  1622. error:
  1623. free_trace_kprobe(tk);
  1624. return ERR_PTR(ret);
  1625. }
  1626. void destroy_local_trace_kprobe(struct trace_event_call *event_call)
  1627. {
  1628. struct trace_kprobe *tk;
  1629. tk = trace_kprobe_primary_from_call(event_call);
  1630. if (unlikely(!tk))
  1631. return;
  1632. if (trace_probe_is_enabled(&tk->tp)) {
  1633. WARN_ON(1);
  1634. return;
  1635. }
  1636. __unregister_trace_kprobe(tk);
  1637. free_trace_kprobe(tk);
  1638. }
  1639. #endif /* CONFIG_PERF_EVENTS */
  1640. static __init void enable_boot_kprobe_events(void)
  1641. {
  1642. struct trace_array *tr = top_trace_array();
  1643. struct trace_event_file *file;
  1644. struct trace_kprobe *tk;
  1645. struct dyn_event *pos;
  1646. mutex_lock(&event_mutex);
  1647. for_each_trace_kprobe(tk, pos) {
  1648. list_for_each_entry(file, &tr->events, list)
  1649. if (file->event_call == trace_probe_event_call(&tk->tp))
  1650. trace_event_enable_disable(file, 1, 0);
  1651. }
  1652. mutex_unlock(&event_mutex);
  1653. }
  1654. static __init void setup_boot_kprobe_events(void)
  1655. {
  1656. char *p, *cmd = kprobe_boot_events_buf;
  1657. int ret;
  1658. strreplace(kprobe_boot_events_buf, ',', ' ');
  1659. while (cmd && *cmd != '\0') {
  1660. p = strchr(cmd, ';');
  1661. if (p)
  1662. *p++ = '\0';
  1663. ret = create_or_delete_trace_kprobe(cmd);
  1664. if (ret)
  1665. pr_warn("Failed to add event(%d): %s\n", ret, cmd);
  1666. cmd = p;
  1667. }
  1668. enable_boot_kprobe_events();
  1669. }
  1670. /*
  1671. * Register dynevent at core_initcall. This allows kernel to setup kprobe
  1672. * events in postcore_initcall without tracefs.
  1673. */
  1674. static __init int init_kprobe_trace_early(void)
  1675. {
  1676. int ret;
  1677. ret = dyn_event_register(&trace_kprobe_ops);
  1678. if (ret)
  1679. return ret;
  1680. if (trace_kprobe_register_module_notifier())
  1681. return -EINVAL;
  1682. return 0;
  1683. }
  1684. core_initcall(init_kprobe_trace_early);
  1685. /* Make a tracefs interface for controlling probe points */
  1686. static __init int init_kprobe_trace(void)
  1687. {
  1688. int ret;
  1689. ret = tracing_init_dentry();
  1690. if (ret)
  1691. return 0;
  1692. /* Event list interface */
  1693. trace_create_file("kprobe_events", TRACE_MODE_WRITE,
  1694. NULL, NULL, &kprobe_events_ops);
  1695. /* Profile interface */
  1696. trace_create_file("kprobe_profile", TRACE_MODE_READ,
  1697. NULL, NULL, &kprobe_profile_ops);
  1698. setup_boot_kprobe_events();
  1699. return 0;
  1700. }
  1701. fs_initcall(init_kprobe_trace);
  1702. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1703. static __init struct trace_event_file *
  1704. find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
  1705. {
  1706. struct trace_event_file *file;
  1707. list_for_each_entry(file, &tr->events, list)
  1708. if (file->event_call == trace_probe_event_call(&tk->tp))
  1709. return file;
  1710. return NULL;
  1711. }
  1712. /*
  1713. * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
  1714. * stage, we can do this lockless.
  1715. */
  1716. static __init int kprobe_trace_self_tests_init(void)
  1717. {
  1718. int ret, warn = 0;
  1719. int (*target)(int, int, int, int, int, int);
  1720. struct trace_kprobe *tk;
  1721. struct trace_event_file *file;
  1722. if (tracing_is_disabled())
  1723. return -ENODEV;
  1724. if (tracing_selftest_disabled)
  1725. return 0;
  1726. target = kprobe_trace_selftest_target;
  1727. pr_info("Testing kprobe tracing: ");
  1728. ret = create_or_delete_trace_kprobe("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)");
  1729. if (WARN_ONCE(ret, "error on probing function entry.")) {
  1730. warn++;
  1731. } else {
  1732. /* Enable trace point */
  1733. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1734. if (WARN_ONCE(tk == NULL, "error on probing function entry.")) {
  1735. warn++;
  1736. } else {
  1737. file = find_trace_probe_file(tk, top_trace_array());
  1738. if (WARN_ONCE(file == NULL, "error on getting probe file.")) {
  1739. warn++;
  1740. } else
  1741. enable_trace_kprobe(
  1742. trace_probe_event_call(&tk->tp), file);
  1743. }
  1744. }
  1745. ret = create_or_delete_trace_kprobe("r:testprobe2 kprobe_trace_selftest_target $retval");
  1746. if (WARN_ONCE(ret, "error on probing function return.")) {
  1747. warn++;
  1748. } else {
  1749. /* Enable trace point */
  1750. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1751. if (WARN_ONCE(tk == NULL, "error on getting 2nd new probe.")) {
  1752. warn++;
  1753. } else {
  1754. file = find_trace_probe_file(tk, top_trace_array());
  1755. if (WARN_ONCE(file == NULL, "error on getting probe file.")) {
  1756. warn++;
  1757. } else
  1758. enable_trace_kprobe(
  1759. trace_probe_event_call(&tk->tp), file);
  1760. }
  1761. }
  1762. if (warn)
  1763. goto end;
  1764. ret = target(1, 2, 3, 4, 5, 6);
  1765. /*
  1766. * Not expecting an error here, the check is only to prevent the
  1767. * optimizer from removing the call to target() as otherwise there
  1768. * are no side-effects and the call is never performed.
  1769. */
  1770. if (ret != 21)
  1771. warn++;
  1772. /* Disable trace points before removing it */
  1773. tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
  1774. if (WARN_ONCE(tk == NULL, "error on getting test probe.")) {
  1775. warn++;
  1776. } else {
  1777. if (WARN_ONCE(trace_kprobe_nhit(tk) != 1,
  1778. "incorrect number of testprobe hits."))
  1779. warn++;
  1780. file = find_trace_probe_file(tk, top_trace_array());
  1781. if (WARN_ONCE(file == NULL, "error on getting probe file.")) {
  1782. warn++;
  1783. } else
  1784. disable_trace_kprobe(
  1785. trace_probe_event_call(&tk->tp), file);
  1786. }
  1787. tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
  1788. if (WARN_ONCE(tk == NULL, "error on getting 2nd test probe.")) {
  1789. warn++;
  1790. } else {
  1791. if (WARN_ONCE(trace_kprobe_nhit(tk) != 1,
  1792. "incorrect number of testprobe2 hits."))
  1793. warn++;
  1794. file = find_trace_probe_file(tk, top_trace_array());
  1795. if (WARN_ONCE(file == NULL, "error on getting probe file.")) {
  1796. warn++;
  1797. } else
  1798. disable_trace_kprobe(
  1799. trace_probe_event_call(&tk->tp), file);
  1800. }
  1801. ret = create_or_delete_trace_kprobe("-:testprobe");
  1802. if (WARN_ONCE(ret, "error on deleting a probe."))
  1803. warn++;
  1804. ret = create_or_delete_trace_kprobe("-:testprobe2");
  1805. if (WARN_ONCE(ret, "error on deleting a probe."))
  1806. warn++;
  1807. end:
  1808. /*
  1809. * Wait for the optimizer work to finish. Otherwise it might fiddle
  1810. * with probes in already freed __init text.
  1811. */
  1812. wait_for_kprobe_optimizer();
  1813. if (warn)
  1814. pr_cont("NG: Some tests are failed. Please check them.\n");
  1815. else
  1816. pr_cont("OK\n");
  1817. return 0;
  1818. }
  1819. late_initcall(kprobe_trace_self_tests_init);
  1820. #endif