trace_kprobe.c 37 KB

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