trace_kprobe.c 51 KB

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