trace_kprobe.c 37 KB

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