trace_kprobe.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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 <linux/kprobes.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/smp.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/ctype.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/perf_event.h>
  31. #include <linux/stringify.h>
  32. #include <linux/limits.h>
  33. #include <asm/bitsperlong.h>
  34. #include "trace.h"
  35. #include "trace_output.h"
  36. #define MAX_TRACE_ARGS 128
  37. #define MAX_ARGSTR_LEN 63
  38. #define MAX_EVENT_NAME_LEN 64
  39. #define MAX_STRING_SIZE PATH_MAX
  40. #define KPROBE_EVENT_SYSTEM "kprobes"
  41. /* Reserved field names */
  42. #define FIELD_STRING_IP "__probe_ip"
  43. #define FIELD_STRING_RETIP "__probe_ret_ip"
  44. #define FIELD_STRING_FUNC "__probe_func"
  45. const char *reserved_field_names[] = {
  46. "common_type",
  47. "common_flags",
  48. "common_preempt_count",
  49. "common_pid",
  50. "common_tgid",
  51. FIELD_STRING_IP,
  52. FIELD_STRING_RETIP,
  53. FIELD_STRING_FUNC,
  54. };
  55. /* Printing function type */
  56. typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *,
  57. void *);
  58. #define PRINT_TYPE_FUNC_NAME(type) print_type_##type
  59. #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type
  60. /* Printing in basic type function template */
  61. #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \
  62. static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \
  63. const char *name, \
  64. void *data, void *ent)\
  65. { \
  66. return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\
  67. } \
  68. static const char PRINT_TYPE_FMT_NAME(type)[] = fmt;
  69. DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int)
  70. DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int)
  71. DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long)
  72. DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long)
  73. DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int)
  74. DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int)
  75. DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long)
  76. DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long)
  77. /* data_rloc: data relative location, compatible with u32 */
  78. #define make_data_rloc(len, roffs) \
  79. (((u32)(len) << 16) | ((u32)(roffs) & 0xffff))
  80. #define get_rloc_len(dl) ((u32)(dl) >> 16)
  81. #define get_rloc_offs(dl) ((u32)(dl) & 0xffff)
  82. static inline void *get_rloc_data(u32 *dl)
  83. {
  84. return (u8 *)dl + get_rloc_offs(*dl);
  85. }
  86. /* For data_loc conversion */
  87. static inline void *get_loc_data(u32 *dl, void *ent)
  88. {
  89. return (u8 *)ent + get_rloc_offs(*dl);
  90. }
  91. /*
  92. * Convert data_rloc to data_loc:
  93. * data_rloc stores the offset from data_rloc itself, but data_loc
  94. * stores the offset from event entry.
  95. */
  96. #define convert_rloc_to_loc(dl, offs) ((u32)(dl) + (offs))
  97. /* For defining macros, define string/string_size types */
  98. typedef u32 string;
  99. typedef u32 string_size;
  100. /* Print type function for string type */
  101. static __kprobes int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s,
  102. const char *name,
  103. void *data, void *ent)
  104. {
  105. int len = *(u32 *)data >> 16;
  106. if (!len)
  107. return trace_seq_printf(s, " %s=(fault)", name);
  108. else
  109. return trace_seq_printf(s, " %s=\"%s\"", name,
  110. (const char *)get_loc_data(data, ent));
  111. }
  112. static const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
  113. /* Data fetch function type */
  114. typedef void (*fetch_func_t)(struct pt_regs *, void *, void *);
  115. struct fetch_param {
  116. fetch_func_t fn;
  117. void *data;
  118. };
  119. static __kprobes void call_fetch(struct fetch_param *fprm,
  120. struct pt_regs *regs, void *dest)
  121. {
  122. return fprm->fn(regs, fprm->data, dest);
  123. }
  124. #define FETCH_FUNC_NAME(method, type) fetch_##method##_##type
  125. /*
  126. * Define macro for basic types - we don't need to define s* types, because
  127. * we have to care only about bitwidth at recording time.
  128. */
  129. #define DEFINE_BASIC_FETCH_FUNCS(method) \
  130. DEFINE_FETCH_##method(u8) \
  131. DEFINE_FETCH_##method(u16) \
  132. DEFINE_FETCH_##method(u32) \
  133. DEFINE_FETCH_##method(u64)
  134. #define CHECK_FETCH_FUNCS(method, fn) \
  135. (((FETCH_FUNC_NAME(method, u8) == fn) || \
  136. (FETCH_FUNC_NAME(method, u16) == fn) || \
  137. (FETCH_FUNC_NAME(method, u32) == fn) || \
  138. (FETCH_FUNC_NAME(method, u64) == fn) || \
  139. (FETCH_FUNC_NAME(method, string) == fn) || \
  140. (FETCH_FUNC_NAME(method, string_size) == fn)) \
  141. && (fn != NULL))
  142. /* Data fetch function templates */
  143. #define DEFINE_FETCH_reg(type) \
  144. static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \
  145. void *offset, void *dest) \
  146. { \
  147. *(type *)dest = (type)regs_get_register(regs, \
  148. (unsigned int)((unsigned long)offset)); \
  149. }
  150. DEFINE_BASIC_FETCH_FUNCS(reg)
  151. /* No string on the register */
  152. #define fetch_reg_string NULL
  153. #define fetch_reg_string_size NULL
  154. #define DEFINE_FETCH_stack(type) \
  155. static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
  156. void *offset, void *dest) \
  157. { \
  158. *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \
  159. (unsigned int)((unsigned long)offset)); \
  160. }
  161. DEFINE_BASIC_FETCH_FUNCS(stack)
  162. /* No string on the stack entry */
  163. #define fetch_stack_string NULL
  164. #define fetch_stack_string_size NULL
  165. #define DEFINE_FETCH_retval(type) \
  166. static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\
  167. void *dummy, void *dest) \
  168. { \
  169. *(type *)dest = (type)regs_return_value(regs); \
  170. }
  171. DEFINE_BASIC_FETCH_FUNCS(retval)
  172. /* No string on the retval */
  173. #define fetch_retval_string NULL
  174. #define fetch_retval_string_size NULL
  175. #define DEFINE_FETCH_memory(type) \
  176. static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
  177. void *addr, void *dest) \
  178. { \
  179. type retval; \
  180. if (probe_kernel_address(addr, retval)) \
  181. *(type *)dest = 0; \
  182. else \
  183. *(type *)dest = retval; \
  184. }
  185. DEFINE_BASIC_FETCH_FUNCS(memory)
  186. /*
  187. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  188. * length and relative data location.
  189. */
  190. static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
  191. void *addr, void *dest)
  192. {
  193. long ret;
  194. int maxlen = get_rloc_len(*(u32 *)dest);
  195. u8 *dst = get_rloc_data(dest);
  196. u8 *src = addr;
  197. mm_segment_t old_fs = get_fs();
  198. if (!maxlen)
  199. return;
  200. /*
  201. * Try to get string again, since the string can be changed while
  202. * probing.
  203. */
  204. set_fs(KERNEL_DS);
  205. pagefault_disable();
  206. do
  207. ret = __copy_from_user_inatomic(dst++, src++, 1);
  208. while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
  209. dst[-1] = '\0';
  210. pagefault_enable();
  211. set_fs(old_fs);
  212. if (ret < 0) { /* Failed to fetch string */
  213. ((u8 *)get_rloc_data(dest))[0] = '\0';
  214. *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
  215. } else
  216. *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
  217. get_rloc_offs(*(u32 *)dest));
  218. }
  219. /* Return the length of string -- including null terminal byte */
  220. static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
  221. void *addr, void *dest)
  222. {
  223. int ret, len = 0;
  224. u8 c;
  225. mm_segment_t old_fs = get_fs();
  226. set_fs(KERNEL_DS);
  227. pagefault_disable();
  228. do {
  229. ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
  230. len++;
  231. } while (c && ret == 0 && len < MAX_STRING_SIZE);
  232. pagefault_enable();
  233. set_fs(old_fs);
  234. if (ret < 0) /* Failed to check the length */
  235. *(u32 *)dest = 0;
  236. else
  237. *(u32 *)dest = len;
  238. }
  239. /* Memory fetching by symbol */
  240. struct symbol_cache {
  241. char *symbol;
  242. long offset;
  243. unsigned long addr;
  244. };
  245. static unsigned long update_symbol_cache(struct symbol_cache *sc)
  246. {
  247. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  248. if (sc->addr)
  249. sc->addr += sc->offset;
  250. return sc->addr;
  251. }
  252. static void free_symbol_cache(struct symbol_cache *sc)
  253. {
  254. kfree(sc->symbol);
  255. kfree(sc);
  256. }
  257. static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  258. {
  259. struct symbol_cache *sc;
  260. if (!sym || strlen(sym) == 0)
  261. return NULL;
  262. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  263. if (!sc)
  264. return NULL;
  265. sc->symbol = kstrdup(sym, GFP_KERNEL);
  266. if (!sc->symbol) {
  267. kfree(sc);
  268. return NULL;
  269. }
  270. sc->offset = offset;
  271. update_symbol_cache(sc);
  272. return sc;
  273. }
  274. #define DEFINE_FETCH_symbol(type) \
  275. static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\
  276. void *data, void *dest) \
  277. { \
  278. struct symbol_cache *sc = data; \
  279. if (sc->addr) \
  280. fetch_memory_##type(regs, (void *)sc->addr, dest); \
  281. else \
  282. *(type *)dest = 0; \
  283. }
  284. DEFINE_BASIC_FETCH_FUNCS(symbol)
  285. DEFINE_FETCH_symbol(string)
  286. DEFINE_FETCH_symbol(string_size)
  287. /* Dereference memory access function */
  288. struct deref_fetch_param {
  289. struct fetch_param orig;
  290. long offset;
  291. };
  292. #define DEFINE_FETCH_deref(type) \
  293. static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\
  294. void *data, void *dest) \
  295. { \
  296. struct deref_fetch_param *dprm = data; \
  297. unsigned long addr; \
  298. call_fetch(&dprm->orig, regs, &addr); \
  299. if (addr) { \
  300. addr += dprm->offset; \
  301. fetch_memory_##type(regs, (void *)addr, dest); \
  302. } else \
  303. *(type *)dest = 0; \
  304. }
  305. DEFINE_BASIC_FETCH_FUNCS(deref)
  306. DEFINE_FETCH_deref(string)
  307. DEFINE_FETCH_deref(string_size)
  308. static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
  309. {
  310. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  311. free_deref_fetch_param(data->orig.data);
  312. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  313. free_symbol_cache(data->orig.data);
  314. kfree(data);
  315. }
  316. /* Bitfield fetch function */
  317. struct bitfield_fetch_param {
  318. struct fetch_param orig;
  319. unsigned char hi_shift;
  320. unsigned char low_shift;
  321. };
  322. #define DEFINE_FETCH_bitfield(type) \
  323. static __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,\
  324. void *data, void *dest) \
  325. { \
  326. struct bitfield_fetch_param *bprm = data; \
  327. type buf = 0; \
  328. call_fetch(&bprm->orig, regs, &buf); \
  329. if (buf) { \
  330. buf <<= bprm->hi_shift; \
  331. buf >>= bprm->low_shift; \
  332. } \
  333. *(type *)dest = buf; \
  334. }
  335. DEFINE_BASIC_FETCH_FUNCS(bitfield)
  336. #define fetch_bitfield_string NULL
  337. #define fetch_bitfield_string_size NULL
  338. static __kprobes void
  339. free_bitfield_fetch_param(struct bitfield_fetch_param *data)
  340. {
  341. /*
  342. * Don't check the bitfield itself, because this must be the
  343. * last fetch function.
  344. */
  345. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  346. free_deref_fetch_param(data->orig.data);
  347. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  348. free_symbol_cache(data->orig.data);
  349. kfree(data);
  350. }
  351. /* Default (unsigned long) fetch type */
  352. #define __DEFAULT_FETCH_TYPE(t) u##t
  353. #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
  354. #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
  355. #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
  356. /* Fetch types */
  357. enum {
  358. FETCH_MTD_reg = 0,
  359. FETCH_MTD_stack,
  360. FETCH_MTD_retval,
  361. FETCH_MTD_memory,
  362. FETCH_MTD_symbol,
  363. FETCH_MTD_deref,
  364. FETCH_MTD_bitfield,
  365. FETCH_MTD_END,
  366. };
  367. #define ASSIGN_FETCH_FUNC(method, type) \
  368. [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type)
  369. #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
  370. {.name = _name, \
  371. .size = _size, \
  372. .is_signed = sign, \
  373. .print = PRINT_TYPE_FUNC_NAME(ptype), \
  374. .fmt = PRINT_TYPE_FMT_NAME(ptype), \
  375. .fmttype = _fmttype, \
  376. .fetch = { \
  377. ASSIGN_FETCH_FUNC(reg, ftype), \
  378. ASSIGN_FETCH_FUNC(stack, ftype), \
  379. ASSIGN_FETCH_FUNC(retval, ftype), \
  380. ASSIGN_FETCH_FUNC(memory, ftype), \
  381. ASSIGN_FETCH_FUNC(symbol, ftype), \
  382. ASSIGN_FETCH_FUNC(deref, ftype), \
  383. ASSIGN_FETCH_FUNC(bitfield, ftype), \
  384. } \
  385. }
  386. #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
  387. __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
  388. #define FETCH_TYPE_STRING 0
  389. #define FETCH_TYPE_STRSIZE 1
  390. /* Fetch type information table */
  391. static const struct fetch_type {
  392. const char *name; /* Name of type */
  393. size_t size; /* Byte size of type */
  394. int is_signed; /* Signed flag */
  395. print_type_func_t print; /* Print functions */
  396. const char *fmt; /* Fromat string */
  397. const char *fmttype; /* Name in format file */
  398. /* Fetch functions */
  399. fetch_func_t fetch[FETCH_MTD_END];
  400. } fetch_type_table[] = {
  401. /* Special types */
  402. [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
  403. sizeof(u32), 1, "__data_loc char[]"),
  404. [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
  405. string_size, sizeof(u32), 0, "u32"),
  406. /* Basic types */
  407. ASSIGN_FETCH_TYPE(u8, u8, 0),
  408. ASSIGN_FETCH_TYPE(u16, u16, 0),
  409. ASSIGN_FETCH_TYPE(u32, u32, 0),
  410. ASSIGN_FETCH_TYPE(u64, u64, 0),
  411. ASSIGN_FETCH_TYPE(s8, u8, 1),
  412. ASSIGN_FETCH_TYPE(s16, u16, 1),
  413. ASSIGN_FETCH_TYPE(s32, u32, 1),
  414. ASSIGN_FETCH_TYPE(s64, u64, 1),
  415. };
  416. static const struct fetch_type *find_fetch_type(const char *type)
  417. {
  418. int i;
  419. if (!type)
  420. type = DEFAULT_FETCH_TYPE_STR;
  421. /* Special case: bitfield */
  422. if (*type == 'b') {
  423. unsigned long bs;
  424. type = strchr(type, '/');
  425. if (!type)
  426. goto fail;
  427. type++;
  428. if (strict_strtoul(type, 0, &bs))
  429. goto fail;
  430. switch (bs) {
  431. case 8:
  432. return find_fetch_type("u8");
  433. case 16:
  434. return find_fetch_type("u16");
  435. case 32:
  436. return find_fetch_type("u32");
  437. case 64:
  438. return find_fetch_type("u64");
  439. default:
  440. goto fail;
  441. }
  442. }
  443. for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
  444. if (strcmp(type, fetch_type_table[i].name) == 0)
  445. return &fetch_type_table[i];
  446. fail:
  447. return NULL;
  448. }
  449. /* Special function : only accept unsigned long */
  450. static __kprobes void fetch_stack_address(struct pt_regs *regs,
  451. void *dummy, void *dest)
  452. {
  453. *(unsigned long *)dest = kernel_stack_pointer(regs);
  454. }
  455. static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
  456. fetch_func_t orig_fn)
  457. {
  458. int i;
  459. if (type != &fetch_type_table[FETCH_TYPE_STRING])
  460. return NULL; /* Only string type needs size function */
  461. for (i = 0; i < FETCH_MTD_END; i++)
  462. if (type->fetch[i] == orig_fn)
  463. return fetch_type_table[FETCH_TYPE_STRSIZE].fetch[i];
  464. WARN_ON(1); /* This should not happen */
  465. return NULL;
  466. }
  467. /**
  468. * Kprobe event core functions
  469. */
  470. struct probe_arg {
  471. struct fetch_param fetch;
  472. struct fetch_param fetch_size;
  473. unsigned int offset; /* Offset from argument entry */
  474. const char *name; /* Name of this argument */
  475. const char *comm; /* Command of this argument */
  476. const struct fetch_type *type; /* Type of this argument */
  477. };
  478. /* Flags for trace_probe */
  479. #define TP_FLAG_TRACE 1
  480. #define TP_FLAG_PROFILE 2
  481. struct trace_probe {
  482. struct list_head list;
  483. struct kretprobe rp; /* Use rp.kp for kprobe use */
  484. unsigned long nhit;
  485. unsigned int flags; /* For TP_FLAG_* */
  486. const char *symbol; /* symbol name */
  487. struct ftrace_event_class class;
  488. struct ftrace_event_call call;
  489. ssize_t size; /* trace entry size */
  490. unsigned int nr_args;
  491. struct probe_arg args[];
  492. };
  493. #define SIZEOF_TRACE_PROBE(n) \
  494. (offsetof(struct trace_probe, args) + \
  495. (sizeof(struct probe_arg) * (n)))
  496. static __kprobes int trace_probe_is_return(struct trace_probe *tp)
  497. {
  498. return tp->rp.handler != NULL;
  499. }
  500. static __kprobes const char *trace_probe_symbol(struct trace_probe *tp)
  501. {
  502. return tp->symbol ? tp->symbol : "unknown";
  503. }
  504. static int register_probe_event(struct trace_probe *tp);
  505. static void unregister_probe_event(struct trace_probe *tp);
  506. static DEFINE_MUTEX(probe_lock);
  507. static LIST_HEAD(probe_list);
  508. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  509. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  510. struct pt_regs *regs);
  511. /* Check the name is good for event/group/fields */
  512. static int is_good_name(const char *name)
  513. {
  514. if (!isalpha(*name) && *name != '_')
  515. return 0;
  516. while (*++name != '\0') {
  517. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  518. return 0;
  519. }
  520. return 1;
  521. }
  522. /*
  523. * Allocate new trace_probe and initialize it (including kprobes).
  524. */
  525. static struct trace_probe *alloc_trace_probe(const char *group,
  526. const char *event,
  527. void *addr,
  528. const char *symbol,
  529. unsigned long offs,
  530. int nargs, int is_return)
  531. {
  532. struct trace_probe *tp;
  533. int ret = -ENOMEM;
  534. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  535. if (!tp)
  536. return ERR_PTR(ret);
  537. if (symbol) {
  538. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  539. if (!tp->symbol)
  540. goto error;
  541. tp->rp.kp.symbol_name = tp->symbol;
  542. tp->rp.kp.offset = offs;
  543. } else
  544. tp->rp.kp.addr = addr;
  545. if (is_return)
  546. tp->rp.handler = kretprobe_dispatcher;
  547. else
  548. tp->rp.kp.pre_handler = kprobe_dispatcher;
  549. if (!event || !is_good_name(event)) {
  550. ret = -EINVAL;
  551. goto error;
  552. }
  553. tp->call.class = &tp->class;
  554. tp->call.name = kstrdup(event, GFP_KERNEL);
  555. if (!tp->call.name)
  556. goto error;
  557. if (!group || !is_good_name(group)) {
  558. ret = -EINVAL;
  559. goto error;
  560. }
  561. tp->class.system = kstrdup(group, GFP_KERNEL);
  562. if (!tp->class.system)
  563. goto error;
  564. INIT_LIST_HEAD(&tp->list);
  565. return tp;
  566. error:
  567. kfree(tp->call.name);
  568. kfree(tp->symbol);
  569. kfree(tp);
  570. return ERR_PTR(ret);
  571. }
  572. static void free_probe_arg(struct probe_arg *arg)
  573. {
  574. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  575. free_bitfield_fetch_param(arg->fetch.data);
  576. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  577. free_deref_fetch_param(arg->fetch.data);
  578. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  579. free_symbol_cache(arg->fetch.data);
  580. kfree(arg->name);
  581. kfree(arg->comm);
  582. }
  583. static void free_trace_probe(struct trace_probe *tp)
  584. {
  585. int i;
  586. for (i = 0; i < tp->nr_args; i++)
  587. free_probe_arg(&tp->args[i]);
  588. kfree(tp->call.class->system);
  589. kfree(tp->call.name);
  590. kfree(tp->symbol);
  591. kfree(tp);
  592. }
  593. static struct trace_probe *find_trace_probe(const char *event,
  594. const char *group)
  595. {
  596. struct trace_probe *tp;
  597. list_for_each_entry(tp, &probe_list, list)
  598. if (strcmp(tp->call.name, event) == 0 &&
  599. strcmp(tp->call.class->system, group) == 0)
  600. return tp;
  601. return NULL;
  602. }
  603. /* Enable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
  604. static int enable_trace_probe(struct trace_probe *tp, int flag)
  605. {
  606. int ret = 0;
  607. tp->flags |= flag;
  608. if (tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE)) {
  609. if (trace_probe_is_return(tp))
  610. ret = enable_kretprobe(&tp->rp);
  611. else
  612. ret = enable_kprobe(&tp->rp.kp);
  613. }
  614. return ret;
  615. }
  616. /* Disable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
  617. static void disable_trace_probe(struct trace_probe *tp, int flag)
  618. {
  619. tp->flags &= ~flag;
  620. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  621. if (trace_probe_is_return(tp))
  622. disable_kretprobe(&tp->rp);
  623. else
  624. disable_kprobe(&tp->rp.kp);
  625. }
  626. }
  627. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  628. static void unregister_trace_probe(struct trace_probe *tp)
  629. {
  630. if (trace_probe_is_return(tp))
  631. unregister_kretprobe(&tp->rp);
  632. else
  633. unregister_kprobe(&tp->rp.kp);
  634. list_del(&tp->list);
  635. unregister_probe_event(tp);
  636. }
  637. /* Register a trace_probe and probe_event */
  638. static int register_trace_probe(struct trace_probe *tp)
  639. {
  640. struct trace_probe *old_tp;
  641. int ret;
  642. mutex_lock(&probe_lock);
  643. /* register as an event */
  644. old_tp = find_trace_probe(tp->call.name, tp->call.class->system);
  645. if (old_tp) {
  646. /* delete old event */
  647. unregister_trace_probe(old_tp);
  648. free_trace_probe(old_tp);
  649. }
  650. ret = register_probe_event(tp);
  651. if (ret) {
  652. pr_warning("Failed to register probe event(%d)\n", ret);
  653. goto end;
  654. }
  655. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  656. if (trace_probe_is_return(tp))
  657. ret = register_kretprobe(&tp->rp);
  658. else
  659. ret = register_kprobe(&tp->rp.kp);
  660. if (ret) {
  661. pr_warning("Could not insert probe(%d)\n", ret);
  662. if (ret == -EILSEQ) {
  663. pr_warning("Probing address(0x%p) is not an "
  664. "instruction boundary.\n",
  665. tp->rp.kp.addr);
  666. ret = -EINVAL;
  667. }
  668. unregister_probe_event(tp);
  669. } else
  670. list_add_tail(&tp->list, &probe_list);
  671. end:
  672. mutex_unlock(&probe_lock);
  673. return ret;
  674. }
  675. /* Split symbol and offset. */
  676. static int split_symbol_offset(char *symbol, unsigned long *offset)
  677. {
  678. char *tmp;
  679. int ret;
  680. if (!offset)
  681. return -EINVAL;
  682. tmp = strchr(symbol, '+');
  683. if (tmp) {
  684. /* skip sign because strict_strtol doesn't accept '+' */
  685. ret = strict_strtoul(tmp + 1, 0, offset);
  686. if (ret)
  687. return ret;
  688. *tmp = '\0';
  689. } else
  690. *offset = 0;
  691. return 0;
  692. }
  693. #define PARAM_MAX_ARGS 16
  694. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  695. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  696. struct fetch_param *f, int is_return)
  697. {
  698. int ret = 0;
  699. unsigned long param;
  700. if (strcmp(arg, "retval") == 0) {
  701. if (is_return)
  702. f->fn = t->fetch[FETCH_MTD_retval];
  703. else
  704. ret = -EINVAL;
  705. } else if (strncmp(arg, "stack", 5) == 0) {
  706. if (arg[5] == '\0') {
  707. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
  708. f->fn = fetch_stack_address;
  709. else
  710. ret = -EINVAL;
  711. } else if (isdigit(arg[5])) {
  712. ret = strict_strtoul(arg + 5, 10, &param);
  713. if (ret || param > PARAM_MAX_STACK)
  714. ret = -EINVAL;
  715. else {
  716. f->fn = t->fetch[FETCH_MTD_stack];
  717. f->data = (void *)param;
  718. }
  719. } else
  720. ret = -EINVAL;
  721. } else
  722. ret = -EINVAL;
  723. return ret;
  724. }
  725. /* Recursive argument parser */
  726. static int __parse_probe_arg(char *arg, const struct fetch_type *t,
  727. struct fetch_param *f, int is_return)
  728. {
  729. int ret = 0;
  730. unsigned long param;
  731. long offset;
  732. char *tmp;
  733. switch (arg[0]) {
  734. case '$':
  735. ret = parse_probe_vars(arg + 1, t, f, is_return);
  736. break;
  737. case '%': /* named register */
  738. ret = regs_query_register_offset(arg + 1);
  739. if (ret >= 0) {
  740. f->fn = t->fetch[FETCH_MTD_reg];
  741. f->data = (void *)(unsigned long)ret;
  742. ret = 0;
  743. }
  744. break;
  745. case '@': /* memory or symbol */
  746. if (isdigit(arg[1])) {
  747. ret = strict_strtoul(arg + 1, 0, &param);
  748. if (ret)
  749. break;
  750. f->fn = t->fetch[FETCH_MTD_memory];
  751. f->data = (void *)param;
  752. } else {
  753. ret = split_symbol_offset(arg + 1, &offset);
  754. if (ret)
  755. break;
  756. f->data = alloc_symbol_cache(arg + 1, offset);
  757. if (f->data)
  758. f->fn = t->fetch[FETCH_MTD_symbol];
  759. }
  760. break;
  761. case '+': /* deref memory */
  762. arg++; /* Skip '+', because strict_strtol() rejects it. */
  763. case '-':
  764. tmp = strchr(arg, '(');
  765. if (!tmp)
  766. break;
  767. *tmp = '\0';
  768. ret = strict_strtol(arg, 0, &offset);
  769. if (ret)
  770. break;
  771. arg = tmp + 1;
  772. tmp = strrchr(arg, ')');
  773. if (tmp) {
  774. struct deref_fetch_param *dprm;
  775. const struct fetch_type *t2 = find_fetch_type(NULL);
  776. *tmp = '\0';
  777. dprm = kzalloc(sizeof(struct deref_fetch_param),
  778. GFP_KERNEL);
  779. if (!dprm)
  780. return -ENOMEM;
  781. dprm->offset = offset;
  782. ret = __parse_probe_arg(arg, t2, &dprm->orig,
  783. is_return);
  784. if (ret)
  785. kfree(dprm);
  786. else {
  787. f->fn = t->fetch[FETCH_MTD_deref];
  788. f->data = (void *)dprm;
  789. }
  790. }
  791. break;
  792. }
  793. if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
  794. pr_info("%s type has no corresponding fetch method.\n",
  795. t->name);
  796. ret = -EINVAL;
  797. }
  798. return ret;
  799. }
  800. #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
  801. /* Bitfield type needs to be parsed into a fetch function */
  802. static int __parse_bitfield_probe_arg(const char *bf,
  803. const struct fetch_type *t,
  804. struct fetch_param *f)
  805. {
  806. struct bitfield_fetch_param *bprm;
  807. unsigned long bw, bo;
  808. char *tail;
  809. if (*bf != 'b')
  810. return 0;
  811. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  812. if (!bprm)
  813. return -ENOMEM;
  814. bprm->orig = *f;
  815. f->fn = t->fetch[FETCH_MTD_bitfield];
  816. f->data = (void *)bprm;
  817. bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
  818. if (bw == 0 || *tail != '@')
  819. return -EINVAL;
  820. bf = tail + 1;
  821. bo = simple_strtoul(bf, &tail, 0);
  822. if (tail == bf || *tail != '/')
  823. return -EINVAL;
  824. bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
  825. bprm->low_shift = bprm->hi_shift + bo;
  826. return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
  827. }
  828. /* String length checking wrapper */
  829. static int parse_probe_arg(char *arg, struct trace_probe *tp,
  830. struct probe_arg *parg, int is_return)
  831. {
  832. const char *t;
  833. int ret;
  834. if (strlen(arg) > MAX_ARGSTR_LEN) {
  835. pr_info("Argument is too long.: %s\n", arg);
  836. return -ENOSPC;
  837. }
  838. parg->comm = kstrdup(arg, GFP_KERNEL);
  839. if (!parg->comm) {
  840. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  841. return -ENOMEM;
  842. }
  843. t = strchr(parg->comm, ':');
  844. if (t) {
  845. arg[t - parg->comm] = '\0';
  846. t++;
  847. }
  848. parg->type = find_fetch_type(t);
  849. if (!parg->type) {
  850. pr_info("Unsupported type: %s\n", t);
  851. return -EINVAL;
  852. }
  853. parg->offset = tp->size;
  854. tp->size += parg->type->size;
  855. ret = __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
  856. if (ret >= 0 && t != NULL)
  857. ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
  858. if (ret >= 0) {
  859. parg->fetch_size.fn = get_fetch_size_function(parg->type,
  860. parg->fetch.fn);
  861. parg->fetch_size.data = parg->fetch.data;
  862. }
  863. return ret;
  864. }
  865. /* Return 1 if name is reserved or already used by another argument */
  866. static int conflict_field_name(const char *name,
  867. struct probe_arg *args, int narg)
  868. {
  869. int i;
  870. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  871. if (strcmp(reserved_field_names[i], name) == 0)
  872. return 1;
  873. for (i = 0; i < narg; i++)
  874. if (strcmp(args[i].name, name) == 0)
  875. return 1;
  876. return 0;
  877. }
  878. static int create_trace_probe(int argc, char **argv)
  879. {
  880. /*
  881. * Argument syntax:
  882. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  883. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  884. * Fetch args:
  885. * $retval : fetch return value
  886. * $stack : fetch stack address
  887. * $stackN : fetch Nth of stack (N:0-)
  888. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  889. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  890. * %REG : fetch register REG
  891. * Dereferencing memory fetch:
  892. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  893. * Alias name of args:
  894. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  895. * Type of args:
  896. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  897. */
  898. struct trace_probe *tp;
  899. int i, ret = 0;
  900. int is_return = 0, is_delete = 0;
  901. char *symbol = NULL, *event = NULL, *group = NULL;
  902. char *arg;
  903. unsigned long offset = 0;
  904. void *addr = NULL;
  905. char buf[MAX_EVENT_NAME_LEN];
  906. /* argc must be >= 1 */
  907. if (argv[0][0] == 'p')
  908. is_return = 0;
  909. else if (argv[0][0] == 'r')
  910. is_return = 1;
  911. else if (argv[0][0] == '-')
  912. is_delete = 1;
  913. else {
  914. pr_info("Probe definition must be started with 'p', 'r' or"
  915. " '-'.\n");
  916. return -EINVAL;
  917. }
  918. if (argv[0][1] == ':') {
  919. event = &argv[0][2];
  920. if (strchr(event, '/')) {
  921. group = event;
  922. event = strchr(group, '/') + 1;
  923. event[-1] = '\0';
  924. if (strlen(group) == 0) {
  925. pr_info("Group name is not specified\n");
  926. return -EINVAL;
  927. }
  928. }
  929. if (strlen(event) == 0) {
  930. pr_info("Event name is not specified\n");
  931. return -EINVAL;
  932. }
  933. }
  934. if (!group)
  935. group = KPROBE_EVENT_SYSTEM;
  936. if (is_delete) {
  937. if (!event) {
  938. pr_info("Delete command needs an event name.\n");
  939. return -EINVAL;
  940. }
  941. mutex_lock(&probe_lock);
  942. tp = find_trace_probe(event, group);
  943. if (!tp) {
  944. mutex_unlock(&probe_lock);
  945. pr_info("Event %s/%s doesn't exist.\n", group, event);
  946. return -ENOENT;
  947. }
  948. /* delete an event */
  949. unregister_trace_probe(tp);
  950. free_trace_probe(tp);
  951. mutex_unlock(&probe_lock);
  952. return 0;
  953. }
  954. if (argc < 2) {
  955. pr_info("Probe point is not specified.\n");
  956. return -EINVAL;
  957. }
  958. if (isdigit(argv[1][0])) {
  959. if (is_return) {
  960. pr_info("Return probe point must be a symbol.\n");
  961. return -EINVAL;
  962. }
  963. /* an address specified */
  964. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  965. if (ret) {
  966. pr_info("Failed to parse address.\n");
  967. return ret;
  968. }
  969. } else {
  970. /* a symbol specified */
  971. symbol = argv[1];
  972. /* TODO: support .init module functions */
  973. ret = split_symbol_offset(symbol, &offset);
  974. if (ret) {
  975. pr_info("Failed to parse symbol.\n");
  976. return ret;
  977. }
  978. if (offset && is_return) {
  979. pr_info("Return probe must be used without offset.\n");
  980. return -EINVAL;
  981. }
  982. }
  983. argc -= 2; argv += 2;
  984. /* setup a probe */
  985. if (!event) {
  986. /* Make a new event name */
  987. if (symbol)
  988. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  989. is_return ? 'r' : 'p', symbol, offset);
  990. else
  991. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  992. is_return ? 'r' : 'p', addr);
  993. event = buf;
  994. }
  995. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  996. is_return);
  997. if (IS_ERR(tp)) {
  998. pr_info("Failed to allocate trace_probe.(%d)\n",
  999. (int)PTR_ERR(tp));
  1000. return PTR_ERR(tp);
  1001. }
  1002. /* parse arguments */
  1003. ret = 0;
  1004. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  1005. /* Increment count for freeing args in error case */
  1006. tp->nr_args++;
  1007. /* Parse argument name */
  1008. arg = strchr(argv[i], '=');
  1009. if (arg) {
  1010. *arg++ = '\0';
  1011. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  1012. } else {
  1013. arg = argv[i];
  1014. /* If argument name is omitted, set "argN" */
  1015. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  1016. tp->args[i].name = kstrdup(buf, GFP_KERNEL);
  1017. }
  1018. if (!tp->args[i].name) {
  1019. pr_info("Failed to allocate argument[%d] name.\n", i);
  1020. ret = -ENOMEM;
  1021. goto error;
  1022. }
  1023. if (!is_good_name(tp->args[i].name)) {
  1024. pr_info("Invalid argument[%d] name: %s\n",
  1025. i, tp->args[i].name);
  1026. ret = -EINVAL;
  1027. goto error;
  1028. }
  1029. if (conflict_field_name(tp->args[i].name, tp->args, i)) {
  1030. pr_info("Argument[%d] name '%s' conflicts with "
  1031. "another field.\n", i, argv[i]);
  1032. ret = -EINVAL;
  1033. goto error;
  1034. }
  1035. /* Parse fetch argument */
  1036. ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
  1037. if (ret) {
  1038. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  1039. goto error;
  1040. }
  1041. }
  1042. ret = register_trace_probe(tp);
  1043. if (ret)
  1044. goto error;
  1045. return 0;
  1046. error:
  1047. free_trace_probe(tp);
  1048. return ret;
  1049. }
  1050. static void release_all_trace_probes(void)
  1051. {
  1052. struct trace_probe *tp;
  1053. mutex_lock(&probe_lock);
  1054. /* TODO: Use batch unregistration */
  1055. while (!list_empty(&probe_list)) {
  1056. tp = list_entry(probe_list.next, struct trace_probe, list);
  1057. unregister_trace_probe(tp);
  1058. free_trace_probe(tp);
  1059. }
  1060. mutex_unlock(&probe_lock);
  1061. }
  1062. /* Probes listing interfaces */
  1063. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  1064. {
  1065. mutex_lock(&probe_lock);
  1066. return seq_list_start(&probe_list, *pos);
  1067. }
  1068. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1069. {
  1070. return seq_list_next(v, &probe_list, pos);
  1071. }
  1072. static void probes_seq_stop(struct seq_file *m, void *v)
  1073. {
  1074. mutex_unlock(&probe_lock);
  1075. }
  1076. static int probes_seq_show(struct seq_file *m, void *v)
  1077. {
  1078. struct trace_probe *tp = v;
  1079. int i;
  1080. seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p');
  1081. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  1082. if (!tp->symbol)
  1083. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  1084. else if (tp->rp.kp.offset)
  1085. seq_printf(m, " %s+%u", trace_probe_symbol(tp),
  1086. tp->rp.kp.offset);
  1087. else
  1088. seq_printf(m, " %s", trace_probe_symbol(tp));
  1089. for (i = 0; i < tp->nr_args; i++)
  1090. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  1091. seq_printf(m, "\n");
  1092. return 0;
  1093. }
  1094. static const struct seq_operations probes_seq_op = {
  1095. .start = probes_seq_start,
  1096. .next = probes_seq_next,
  1097. .stop = probes_seq_stop,
  1098. .show = probes_seq_show
  1099. };
  1100. static int probes_open(struct inode *inode, struct file *file)
  1101. {
  1102. if ((file->f_mode & FMODE_WRITE) &&
  1103. (file->f_flags & O_TRUNC))
  1104. release_all_trace_probes();
  1105. return seq_open(file, &probes_seq_op);
  1106. }
  1107. static int command_trace_probe(const char *buf)
  1108. {
  1109. char **argv;
  1110. int argc = 0, ret = 0;
  1111. argv = argv_split(GFP_KERNEL, buf, &argc);
  1112. if (!argv)
  1113. return -ENOMEM;
  1114. if (argc)
  1115. ret = create_trace_probe(argc, argv);
  1116. argv_free(argv);
  1117. return ret;
  1118. }
  1119. #define WRITE_BUFSIZE 4096
  1120. static ssize_t probes_write(struct file *file, const char __user *buffer,
  1121. size_t count, loff_t *ppos)
  1122. {
  1123. char *kbuf, *tmp;
  1124. int ret;
  1125. size_t done;
  1126. size_t size;
  1127. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  1128. if (!kbuf)
  1129. return -ENOMEM;
  1130. ret = done = 0;
  1131. while (done < count) {
  1132. size = count - done;
  1133. if (size >= WRITE_BUFSIZE)
  1134. size = WRITE_BUFSIZE - 1;
  1135. if (copy_from_user(kbuf, buffer + done, size)) {
  1136. ret = -EFAULT;
  1137. goto out;
  1138. }
  1139. kbuf[size] = '\0';
  1140. tmp = strchr(kbuf, '\n');
  1141. if (tmp) {
  1142. *tmp = '\0';
  1143. size = tmp - kbuf + 1;
  1144. } else if (done + size < count) {
  1145. pr_warning("Line length is too long: "
  1146. "Should be less than %d.", WRITE_BUFSIZE);
  1147. ret = -EINVAL;
  1148. goto out;
  1149. }
  1150. done += size;
  1151. /* Remove comments */
  1152. tmp = strchr(kbuf, '#');
  1153. if (tmp)
  1154. *tmp = '\0';
  1155. ret = command_trace_probe(kbuf);
  1156. if (ret)
  1157. goto out;
  1158. }
  1159. ret = done;
  1160. out:
  1161. kfree(kbuf);
  1162. return ret;
  1163. }
  1164. static const struct file_operations kprobe_events_ops = {
  1165. .owner = THIS_MODULE,
  1166. .open = probes_open,
  1167. .read = seq_read,
  1168. .llseek = seq_lseek,
  1169. .release = seq_release,
  1170. .write = probes_write,
  1171. };
  1172. /* Probes profiling interfaces */
  1173. static int probes_profile_seq_show(struct seq_file *m, void *v)
  1174. {
  1175. struct trace_probe *tp = v;
  1176. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  1177. tp->rp.kp.nmissed);
  1178. return 0;
  1179. }
  1180. static const struct seq_operations profile_seq_op = {
  1181. .start = probes_seq_start,
  1182. .next = probes_seq_next,
  1183. .stop = probes_seq_stop,
  1184. .show = probes_profile_seq_show
  1185. };
  1186. static int profile_open(struct inode *inode, struct file *file)
  1187. {
  1188. return seq_open(file, &profile_seq_op);
  1189. }
  1190. static const struct file_operations kprobe_profile_ops = {
  1191. .owner = THIS_MODULE,
  1192. .open = profile_open,
  1193. .read = seq_read,
  1194. .llseek = seq_lseek,
  1195. .release = seq_release,
  1196. };
  1197. /* Sum up total data length for dynamic arraies (strings) */
  1198. static __kprobes int __get_data_size(struct trace_probe *tp,
  1199. struct pt_regs *regs)
  1200. {
  1201. int i, ret = 0;
  1202. u32 len;
  1203. for (i = 0; i < tp->nr_args; i++)
  1204. if (unlikely(tp->args[i].fetch_size.fn)) {
  1205. call_fetch(&tp->args[i].fetch_size, regs, &len);
  1206. ret += len;
  1207. }
  1208. return ret;
  1209. }
  1210. /* Store the value of each argument */
  1211. static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
  1212. struct pt_regs *regs,
  1213. u8 *data, int maxlen)
  1214. {
  1215. int i;
  1216. u32 end = tp->size;
  1217. u32 *dl; /* Data (relative) location */
  1218. for (i = 0; i < tp->nr_args; i++) {
  1219. if (unlikely(tp->args[i].fetch_size.fn)) {
  1220. /*
  1221. * First, we set the relative location and
  1222. * maximum data length to *dl
  1223. */
  1224. dl = (u32 *)(data + tp->args[i].offset);
  1225. *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
  1226. /* Then try to fetch string or dynamic array data */
  1227. call_fetch(&tp->args[i].fetch, regs, dl);
  1228. /* Reduce maximum length */
  1229. end += get_rloc_len(*dl);
  1230. maxlen -= get_rloc_len(*dl);
  1231. /* Trick here, convert data_rloc to data_loc */
  1232. *dl = convert_rloc_to_loc(*dl,
  1233. ent_size + tp->args[i].offset);
  1234. } else
  1235. /* Just fetching data normally */
  1236. call_fetch(&tp->args[i].fetch, regs,
  1237. data + tp->args[i].offset);
  1238. }
  1239. }
  1240. /* Kprobe handler */
  1241. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  1242. {
  1243. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1244. struct kprobe_trace_entry_head *entry;
  1245. struct ring_buffer_event *event;
  1246. struct ring_buffer *buffer;
  1247. int size, dsize, pc;
  1248. unsigned long irq_flags;
  1249. struct ftrace_event_call *call = &tp->call;
  1250. tp->nhit++;
  1251. local_save_flags(irq_flags);
  1252. pc = preempt_count();
  1253. dsize = __get_data_size(tp, regs);
  1254. size = sizeof(*entry) + tp->size + dsize;
  1255. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1256. size, irq_flags, pc);
  1257. if (!event)
  1258. return;
  1259. entry = ring_buffer_event_data(event);
  1260. entry->ip = (unsigned long)kp->addr;
  1261. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1262. if (!filter_current_check_discard(buffer, call, entry, event))
  1263. trace_nowake_buffer_unlock_commit_regs(buffer, event,
  1264. irq_flags, pc, regs);
  1265. }
  1266. /* Kretprobe handler */
  1267. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  1268. struct pt_regs *regs)
  1269. {
  1270. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1271. struct kretprobe_trace_entry_head *entry;
  1272. struct ring_buffer_event *event;
  1273. struct ring_buffer *buffer;
  1274. int size, pc, dsize;
  1275. unsigned long irq_flags;
  1276. struct ftrace_event_call *call = &tp->call;
  1277. local_save_flags(irq_flags);
  1278. pc = preempt_count();
  1279. dsize = __get_data_size(tp, regs);
  1280. size = sizeof(*entry) + tp->size + dsize;
  1281. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1282. size, irq_flags, pc);
  1283. if (!event)
  1284. return;
  1285. entry = ring_buffer_event_data(event);
  1286. entry->func = (unsigned long)tp->rp.kp.addr;
  1287. entry->ret_ip = (unsigned long)ri->ret_addr;
  1288. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1289. if (!filter_current_check_discard(buffer, call, entry, event))
  1290. trace_nowake_buffer_unlock_commit_regs(buffer, event,
  1291. irq_flags, pc, regs);
  1292. }
  1293. /* Event entry printers */
  1294. enum print_line_t
  1295. print_kprobe_event(struct trace_iterator *iter, int flags,
  1296. struct trace_event *event)
  1297. {
  1298. struct kprobe_trace_entry_head *field;
  1299. struct trace_seq *s = &iter->seq;
  1300. struct trace_probe *tp;
  1301. u8 *data;
  1302. int i;
  1303. field = (struct kprobe_trace_entry_head *)iter->ent;
  1304. tp = container_of(event, struct trace_probe, call.event);
  1305. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1306. goto partial;
  1307. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  1308. goto partial;
  1309. if (!trace_seq_puts(s, ")"))
  1310. goto partial;
  1311. data = (u8 *)&field[1];
  1312. for (i = 0; i < tp->nr_args; i++)
  1313. if (!tp->args[i].type->print(s, tp->args[i].name,
  1314. data + tp->args[i].offset, field))
  1315. goto partial;
  1316. if (!trace_seq_puts(s, "\n"))
  1317. goto partial;
  1318. return TRACE_TYPE_HANDLED;
  1319. partial:
  1320. return TRACE_TYPE_PARTIAL_LINE;
  1321. }
  1322. enum print_line_t
  1323. print_kretprobe_event(struct trace_iterator *iter, int flags,
  1324. struct trace_event *event)
  1325. {
  1326. struct kretprobe_trace_entry_head *field;
  1327. struct trace_seq *s = &iter->seq;
  1328. struct trace_probe *tp;
  1329. u8 *data;
  1330. int i;
  1331. field = (struct kretprobe_trace_entry_head *)iter->ent;
  1332. tp = container_of(event, struct trace_probe, call.event);
  1333. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1334. goto partial;
  1335. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  1336. goto partial;
  1337. if (!trace_seq_puts(s, " <- "))
  1338. goto partial;
  1339. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  1340. goto partial;
  1341. if (!trace_seq_puts(s, ")"))
  1342. goto partial;
  1343. data = (u8 *)&field[1];
  1344. for (i = 0; i < tp->nr_args; i++)
  1345. if (!tp->args[i].type->print(s, tp->args[i].name,
  1346. data + tp->args[i].offset, field))
  1347. goto partial;
  1348. if (!trace_seq_puts(s, "\n"))
  1349. goto partial;
  1350. return TRACE_TYPE_HANDLED;
  1351. partial:
  1352. return TRACE_TYPE_PARTIAL_LINE;
  1353. }
  1354. #undef DEFINE_FIELD
  1355. #define DEFINE_FIELD(type, item, name, is_signed) \
  1356. do { \
  1357. ret = trace_define_field(event_call, #type, name, \
  1358. offsetof(typeof(field), item), \
  1359. sizeof(field.item), is_signed, \
  1360. FILTER_OTHER); \
  1361. if (ret) \
  1362. return ret; \
  1363. } while (0)
  1364. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  1365. {
  1366. int ret, i;
  1367. struct kprobe_trace_entry_head field;
  1368. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1369. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  1370. /* Set argument names as fields */
  1371. for (i = 0; i < tp->nr_args; i++) {
  1372. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1373. tp->args[i].name,
  1374. sizeof(field) + tp->args[i].offset,
  1375. tp->args[i].type->size,
  1376. tp->args[i].type->is_signed,
  1377. FILTER_OTHER);
  1378. if (ret)
  1379. return ret;
  1380. }
  1381. return 0;
  1382. }
  1383. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  1384. {
  1385. int ret, i;
  1386. struct kretprobe_trace_entry_head field;
  1387. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1388. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1389. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1390. /* Set argument names as fields */
  1391. for (i = 0; i < tp->nr_args; i++) {
  1392. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1393. tp->args[i].name,
  1394. sizeof(field) + tp->args[i].offset,
  1395. tp->args[i].type->size,
  1396. tp->args[i].type->is_signed,
  1397. FILTER_OTHER);
  1398. if (ret)
  1399. return ret;
  1400. }
  1401. return 0;
  1402. }
  1403. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  1404. {
  1405. int i;
  1406. int pos = 0;
  1407. const char *fmt, *arg;
  1408. if (!trace_probe_is_return(tp)) {
  1409. fmt = "(%lx)";
  1410. arg = "REC->" FIELD_STRING_IP;
  1411. } else {
  1412. fmt = "(%lx <- %lx)";
  1413. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1414. }
  1415. /* When len=0, we just calculate the needed length */
  1416. #define LEN_OR_ZERO (len ? len - pos : 0)
  1417. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1418. for (i = 0; i < tp->nr_args; i++) {
  1419. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  1420. tp->args[i].name, tp->args[i].type->fmt);
  1421. }
  1422. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  1423. for (i = 0; i < tp->nr_args; i++) {
  1424. if (strcmp(tp->args[i].type->name, "string") == 0)
  1425. pos += snprintf(buf + pos, LEN_OR_ZERO,
  1426. ", __get_str(%s)",
  1427. tp->args[i].name);
  1428. else
  1429. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  1430. tp->args[i].name);
  1431. }
  1432. #undef LEN_OR_ZERO
  1433. /* return the length of print_fmt */
  1434. return pos;
  1435. }
  1436. static int set_print_fmt(struct trace_probe *tp)
  1437. {
  1438. int len;
  1439. char *print_fmt;
  1440. /* First: called with 0 length to calculate the needed length */
  1441. len = __set_print_fmt(tp, NULL, 0);
  1442. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1443. if (!print_fmt)
  1444. return -ENOMEM;
  1445. /* Second: actually write the @print_fmt */
  1446. __set_print_fmt(tp, print_fmt, len + 1);
  1447. tp->call.print_fmt = print_fmt;
  1448. return 0;
  1449. }
  1450. #ifdef CONFIG_PERF_EVENTS
  1451. /* Kprobe profile handler */
  1452. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  1453. struct pt_regs *regs)
  1454. {
  1455. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1456. struct ftrace_event_call *call = &tp->call;
  1457. struct kprobe_trace_entry_head *entry;
  1458. struct hlist_head *head;
  1459. int size, __size, dsize;
  1460. int rctx;
  1461. dsize = __get_data_size(tp, regs);
  1462. __size = sizeof(*entry) + tp->size + dsize;
  1463. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1464. size -= sizeof(u32);
  1465. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1466. "profile buffer not large enough"))
  1467. return;
  1468. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1469. if (!entry)
  1470. return;
  1471. entry->ip = (unsigned long)kp->addr;
  1472. memset(&entry[1], 0, dsize);
  1473. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1474. head = this_cpu_ptr(call->perf_events);
  1475. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
  1476. }
  1477. /* Kretprobe profile handler */
  1478. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1479. struct pt_regs *regs)
  1480. {
  1481. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1482. struct ftrace_event_call *call = &tp->call;
  1483. struct kretprobe_trace_entry_head *entry;
  1484. struct hlist_head *head;
  1485. int size, __size, dsize;
  1486. int rctx;
  1487. dsize = __get_data_size(tp, regs);
  1488. __size = sizeof(*entry) + tp->size + dsize;
  1489. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1490. size -= sizeof(u32);
  1491. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1492. "profile buffer not large enough"))
  1493. return;
  1494. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1495. if (!entry)
  1496. return;
  1497. entry->func = (unsigned long)tp->rp.kp.addr;
  1498. entry->ret_ip = (unsigned long)ri->ret_addr;
  1499. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1500. head = this_cpu_ptr(call->perf_events);
  1501. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
  1502. }
  1503. #endif /* CONFIG_PERF_EVENTS */
  1504. static __kprobes
  1505. int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
  1506. {
  1507. struct trace_probe *tp = (struct trace_probe *)event->data;
  1508. switch (type) {
  1509. case TRACE_REG_REGISTER:
  1510. return enable_trace_probe(tp, TP_FLAG_TRACE);
  1511. case TRACE_REG_UNREGISTER:
  1512. disable_trace_probe(tp, TP_FLAG_TRACE);
  1513. return 0;
  1514. #ifdef CONFIG_PERF_EVENTS
  1515. case TRACE_REG_PERF_REGISTER:
  1516. return enable_trace_probe(tp, TP_FLAG_PROFILE);
  1517. case TRACE_REG_PERF_UNREGISTER:
  1518. disable_trace_probe(tp, TP_FLAG_PROFILE);
  1519. return 0;
  1520. #endif
  1521. }
  1522. return 0;
  1523. }
  1524. static __kprobes
  1525. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1526. {
  1527. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1528. if (tp->flags & TP_FLAG_TRACE)
  1529. kprobe_trace_func(kp, regs);
  1530. #ifdef CONFIG_PERF_EVENTS
  1531. if (tp->flags & TP_FLAG_PROFILE)
  1532. kprobe_perf_func(kp, regs);
  1533. #endif
  1534. return 0; /* We don't tweek kernel, so just return 0 */
  1535. }
  1536. static __kprobes
  1537. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1538. {
  1539. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1540. if (tp->flags & TP_FLAG_TRACE)
  1541. kretprobe_trace_func(ri, regs);
  1542. #ifdef CONFIG_PERF_EVENTS
  1543. if (tp->flags & TP_FLAG_PROFILE)
  1544. kretprobe_perf_func(ri, regs);
  1545. #endif
  1546. return 0; /* We don't tweek kernel, so just return 0 */
  1547. }
  1548. static struct trace_event_functions kretprobe_funcs = {
  1549. .trace = print_kretprobe_event
  1550. };
  1551. static struct trace_event_functions kprobe_funcs = {
  1552. .trace = print_kprobe_event
  1553. };
  1554. static int register_probe_event(struct trace_probe *tp)
  1555. {
  1556. struct ftrace_event_call *call = &tp->call;
  1557. int ret;
  1558. /* Initialize ftrace_event_call */
  1559. INIT_LIST_HEAD(&call->class->fields);
  1560. if (trace_probe_is_return(tp)) {
  1561. call->event.funcs = &kretprobe_funcs;
  1562. call->class->define_fields = kretprobe_event_define_fields;
  1563. } else {
  1564. call->event.funcs = &kprobe_funcs;
  1565. call->class->define_fields = kprobe_event_define_fields;
  1566. }
  1567. if (set_print_fmt(tp) < 0)
  1568. return -ENOMEM;
  1569. ret = register_ftrace_event(&call->event);
  1570. if (!ret) {
  1571. kfree(call->print_fmt);
  1572. return -ENODEV;
  1573. }
  1574. call->flags = 0;
  1575. call->class->reg = kprobe_register;
  1576. call->data = tp;
  1577. ret = trace_add_event_call(call);
  1578. if (ret) {
  1579. pr_info("Failed to register kprobe event: %s\n", call->name);
  1580. kfree(call->print_fmt);
  1581. unregister_ftrace_event(&call->event);
  1582. }
  1583. return ret;
  1584. }
  1585. static void unregister_probe_event(struct trace_probe *tp)
  1586. {
  1587. /* tp->event is unregistered in trace_remove_event_call() */
  1588. trace_remove_event_call(&tp->call);
  1589. kfree(tp->call.print_fmt);
  1590. }
  1591. /* Make a debugfs interface for controlling probe points */
  1592. static __init int init_kprobe_trace(void)
  1593. {
  1594. struct dentry *d_tracer;
  1595. struct dentry *entry;
  1596. d_tracer = tracing_init_dentry();
  1597. if (!d_tracer)
  1598. return 0;
  1599. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1600. NULL, &kprobe_events_ops);
  1601. /* Event list interface */
  1602. if (!entry)
  1603. pr_warning("Could not create debugfs "
  1604. "'kprobe_events' entry\n");
  1605. /* Profile interface */
  1606. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1607. NULL, &kprobe_profile_ops);
  1608. if (!entry)
  1609. pr_warning("Could not create debugfs "
  1610. "'kprobe_profile' entry\n");
  1611. return 0;
  1612. }
  1613. fs_initcall(init_kprobe_trace);
  1614. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1615. /*
  1616. * The "__used" keeps gcc from removing the function symbol
  1617. * from the kallsyms table.
  1618. */
  1619. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1620. int a4, int a5, int a6)
  1621. {
  1622. return a1 + a2 + a3 + a4 + a5 + a6;
  1623. }
  1624. static __init int kprobe_trace_self_tests_init(void)
  1625. {
  1626. int ret, warn = 0;
  1627. int (*target)(int, int, int, int, int, int);
  1628. struct trace_probe *tp;
  1629. target = kprobe_trace_selftest_target;
  1630. pr_info("Testing kprobe tracing: ");
  1631. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1632. "$stack $stack0 +0($stack)");
  1633. if (WARN_ON_ONCE(ret)) {
  1634. pr_warning("error on probing function entry.\n");
  1635. warn++;
  1636. } else {
  1637. /* Enable trace point */
  1638. tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
  1639. if (WARN_ON_ONCE(tp == NULL)) {
  1640. pr_warning("error on getting new probe.\n");
  1641. warn++;
  1642. } else
  1643. enable_trace_probe(tp, TP_FLAG_TRACE);
  1644. }
  1645. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1646. "$retval");
  1647. if (WARN_ON_ONCE(ret)) {
  1648. pr_warning("error on probing function return.\n");
  1649. warn++;
  1650. } else {
  1651. /* Enable trace point */
  1652. tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
  1653. if (WARN_ON_ONCE(tp == NULL)) {
  1654. pr_warning("error on getting new probe.\n");
  1655. warn++;
  1656. } else
  1657. enable_trace_probe(tp, TP_FLAG_TRACE);
  1658. }
  1659. if (warn)
  1660. goto end;
  1661. ret = target(1, 2, 3, 4, 5, 6);
  1662. ret = command_trace_probe("-:testprobe");
  1663. if (WARN_ON_ONCE(ret)) {
  1664. pr_warning("error on deleting a probe.\n");
  1665. warn++;
  1666. }
  1667. ret = command_trace_probe("-:testprobe2");
  1668. if (WARN_ON_ONCE(ret)) {
  1669. pr_warning("error on deleting a probe.\n");
  1670. warn++;
  1671. }
  1672. end:
  1673. release_all_trace_probes();
  1674. if (warn)
  1675. pr_cont("NG: Some tests are failed. Please check them.\n");
  1676. else
  1677. pr_cont("OK\n");
  1678. return 0;
  1679. }
  1680. late_initcall(kprobe_trace_self_tests_init);
  1681. #endif