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