trace_kprobe.c 37 KB

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