trace_kprobe.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  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 probe_is_return(struct trace_probe *tp)
  497. {
  498. return tp->rp.handler != NULL;
  499. }
  500. static __kprobes const char *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_probe_event(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. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  604. static void unregister_trace_probe(struct trace_probe *tp)
  605. {
  606. if (probe_is_return(tp))
  607. unregister_kretprobe(&tp->rp);
  608. else
  609. unregister_kprobe(&tp->rp.kp);
  610. list_del(&tp->list);
  611. unregister_probe_event(tp);
  612. }
  613. /* Register a trace_probe and probe_event */
  614. static int register_trace_probe(struct trace_probe *tp)
  615. {
  616. struct trace_probe *old_tp;
  617. int ret;
  618. mutex_lock(&probe_lock);
  619. /* register as an event */
  620. old_tp = find_probe_event(tp->call.name, tp->call.class->system);
  621. if (old_tp) {
  622. /* delete old event */
  623. unregister_trace_probe(old_tp);
  624. free_trace_probe(old_tp);
  625. }
  626. ret = register_probe_event(tp);
  627. if (ret) {
  628. pr_warning("Failed to register probe event(%d)\n", ret);
  629. goto end;
  630. }
  631. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  632. if (probe_is_return(tp))
  633. ret = register_kretprobe(&tp->rp);
  634. else
  635. ret = register_kprobe(&tp->rp.kp);
  636. if (ret) {
  637. pr_warning("Could not insert probe(%d)\n", ret);
  638. if (ret == -EILSEQ) {
  639. pr_warning("Probing address(0x%p) is not an "
  640. "instruction boundary.\n",
  641. tp->rp.kp.addr);
  642. ret = -EINVAL;
  643. }
  644. unregister_probe_event(tp);
  645. } else
  646. list_add_tail(&tp->list, &probe_list);
  647. end:
  648. mutex_unlock(&probe_lock);
  649. return ret;
  650. }
  651. /* Split symbol and offset. */
  652. static int split_symbol_offset(char *symbol, unsigned long *offset)
  653. {
  654. char *tmp;
  655. int ret;
  656. if (!offset)
  657. return -EINVAL;
  658. tmp = strchr(symbol, '+');
  659. if (tmp) {
  660. /* skip sign because strict_strtol doesn't accept '+' */
  661. ret = strict_strtoul(tmp + 1, 0, offset);
  662. if (ret)
  663. return ret;
  664. *tmp = '\0';
  665. } else
  666. *offset = 0;
  667. return 0;
  668. }
  669. #define PARAM_MAX_ARGS 16
  670. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  671. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  672. struct fetch_param *f, int is_return)
  673. {
  674. int ret = 0;
  675. unsigned long param;
  676. if (strcmp(arg, "retval") == 0) {
  677. if (is_return)
  678. f->fn = t->fetch[FETCH_MTD_retval];
  679. else
  680. ret = -EINVAL;
  681. } else if (strncmp(arg, "stack", 5) == 0) {
  682. if (arg[5] == '\0') {
  683. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
  684. f->fn = fetch_stack_address;
  685. else
  686. ret = -EINVAL;
  687. } else if (isdigit(arg[5])) {
  688. ret = strict_strtoul(arg + 5, 10, &param);
  689. if (ret || param > PARAM_MAX_STACK)
  690. ret = -EINVAL;
  691. else {
  692. f->fn = t->fetch[FETCH_MTD_stack];
  693. f->data = (void *)param;
  694. }
  695. } else
  696. ret = -EINVAL;
  697. } else
  698. ret = -EINVAL;
  699. return ret;
  700. }
  701. /* Recursive argument parser */
  702. static int __parse_probe_arg(char *arg, const struct fetch_type *t,
  703. struct fetch_param *f, int is_return)
  704. {
  705. int ret = 0;
  706. unsigned long param;
  707. long offset;
  708. char *tmp;
  709. switch (arg[0]) {
  710. case '$':
  711. ret = parse_probe_vars(arg + 1, t, f, is_return);
  712. break;
  713. case '%': /* named register */
  714. ret = regs_query_register_offset(arg + 1);
  715. if (ret >= 0) {
  716. f->fn = t->fetch[FETCH_MTD_reg];
  717. f->data = (void *)(unsigned long)ret;
  718. ret = 0;
  719. }
  720. break;
  721. case '@': /* memory or symbol */
  722. if (isdigit(arg[1])) {
  723. ret = strict_strtoul(arg + 1, 0, &param);
  724. if (ret)
  725. break;
  726. f->fn = t->fetch[FETCH_MTD_memory];
  727. f->data = (void *)param;
  728. } else {
  729. ret = split_symbol_offset(arg + 1, &offset);
  730. if (ret)
  731. break;
  732. f->data = alloc_symbol_cache(arg + 1, offset);
  733. if (f->data)
  734. f->fn = t->fetch[FETCH_MTD_symbol];
  735. }
  736. break;
  737. case '+': /* deref memory */
  738. arg++; /* Skip '+', because strict_strtol() rejects it. */
  739. case '-':
  740. tmp = strchr(arg, '(');
  741. if (!tmp)
  742. break;
  743. *tmp = '\0';
  744. ret = strict_strtol(arg, 0, &offset);
  745. if (ret)
  746. break;
  747. arg = tmp + 1;
  748. tmp = strrchr(arg, ')');
  749. if (tmp) {
  750. struct deref_fetch_param *dprm;
  751. const struct fetch_type *t2 = find_fetch_type(NULL);
  752. *tmp = '\0';
  753. dprm = kzalloc(sizeof(struct deref_fetch_param),
  754. GFP_KERNEL);
  755. if (!dprm)
  756. return -ENOMEM;
  757. dprm->offset = offset;
  758. ret = __parse_probe_arg(arg, t2, &dprm->orig,
  759. is_return);
  760. if (ret)
  761. kfree(dprm);
  762. else {
  763. f->fn = t->fetch[FETCH_MTD_deref];
  764. f->data = (void *)dprm;
  765. }
  766. }
  767. break;
  768. }
  769. if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
  770. pr_info("%s type has no corresponding fetch method.\n",
  771. t->name);
  772. ret = -EINVAL;
  773. }
  774. return ret;
  775. }
  776. #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
  777. /* Bitfield type needs to be parsed into a fetch function */
  778. static int __parse_bitfield_probe_arg(const char *bf,
  779. const struct fetch_type *t,
  780. struct fetch_param *f)
  781. {
  782. struct bitfield_fetch_param *bprm;
  783. unsigned long bw, bo;
  784. char *tail;
  785. if (*bf != 'b')
  786. return 0;
  787. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  788. if (!bprm)
  789. return -ENOMEM;
  790. bprm->orig = *f;
  791. f->fn = t->fetch[FETCH_MTD_bitfield];
  792. f->data = (void *)bprm;
  793. bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
  794. if (bw == 0 || *tail != '@')
  795. return -EINVAL;
  796. bf = tail + 1;
  797. bo = simple_strtoul(bf, &tail, 0);
  798. if (tail == bf || *tail != '/')
  799. return -EINVAL;
  800. bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
  801. bprm->low_shift = bprm->hi_shift + bo;
  802. return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
  803. }
  804. /* String length checking wrapper */
  805. static int parse_probe_arg(char *arg, struct trace_probe *tp,
  806. struct probe_arg *parg, int is_return)
  807. {
  808. const char *t;
  809. int ret;
  810. if (strlen(arg) > MAX_ARGSTR_LEN) {
  811. pr_info("Argument is too long.: %s\n", arg);
  812. return -ENOSPC;
  813. }
  814. parg->comm = kstrdup(arg, GFP_KERNEL);
  815. if (!parg->comm) {
  816. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  817. return -ENOMEM;
  818. }
  819. t = strchr(parg->comm, ':');
  820. if (t) {
  821. arg[t - parg->comm] = '\0';
  822. t++;
  823. }
  824. parg->type = find_fetch_type(t);
  825. if (!parg->type) {
  826. pr_info("Unsupported type: %s\n", t);
  827. return -EINVAL;
  828. }
  829. parg->offset = tp->size;
  830. tp->size += parg->type->size;
  831. ret = __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
  832. if (ret >= 0 && t != NULL)
  833. ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
  834. if (ret >= 0) {
  835. parg->fetch_size.fn = get_fetch_size_function(parg->type,
  836. parg->fetch.fn);
  837. parg->fetch_size.data = parg->fetch.data;
  838. }
  839. return ret;
  840. }
  841. /* Return 1 if name is reserved or already used by another argument */
  842. static int conflict_field_name(const char *name,
  843. struct probe_arg *args, int narg)
  844. {
  845. int i;
  846. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  847. if (strcmp(reserved_field_names[i], name) == 0)
  848. return 1;
  849. for (i = 0; i < narg; i++)
  850. if (strcmp(args[i].name, name) == 0)
  851. return 1;
  852. return 0;
  853. }
  854. static int create_trace_probe(int argc, char **argv)
  855. {
  856. /*
  857. * Argument syntax:
  858. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  859. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  860. * Fetch args:
  861. * $retval : fetch return value
  862. * $stack : fetch stack address
  863. * $stackN : fetch Nth of stack (N:0-)
  864. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  865. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  866. * %REG : fetch register REG
  867. * Dereferencing memory fetch:
  868. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  869. * Alias name of args:
  870. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  871. * Type of args:
  872. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  873. */
  874. struct trace_probe *tp;
  875. int i, ret = 0;
  876. int is_return = 0, is_delete = 0;
  877. char *symbol = NULL, *event = NULL, *group = NULL;
  878. char *arg;
  879. unsigned long offset = 0;
  880. void *addr = NULL;
  881. char buf[MAX_EVENT_NAME_LEN];
  882. /* argc must be >= 1 */
  883. if (argv[0][0] == 'p')
  884. is_return = 0;
  885. else if (argv[0][0] == 'r')
  886. is_return = 1;
  887. else if (argv[0][0] == '-')
  888. is_delete = 1;
  889. else {
  890. pr_info("Probe definition must be started with 'p', 'r' or"
  891. " '-'.\n");
  892. return -EINVAL;
  893. }
  894. if (argv[0][1] == ':') {
  895. event = &argv[0][2];
  896. if (strchr(event, '/')) {
  897. group = event;
  898. event = strchr(group, '/') + 1;
  899. event[-1] = '\0';
  900. if (strlen(group) == 0) {
  901. pr_info("Group name is not specified\n");
  902. return -EINVAL;
  903. }
  904. }
  905. if (strlen(event) == 0) {
  906. pr_info("Event name is not specified\n");
  907. return -EINVAL;
  908. }
  909. }
  910. if (!group)
  911. group = KPROBE_EVENT_SYSTEM;
  912. if (is_delete) {
  913. if (!event) {
  914. pr_info("Delete command needs an event name.\n");
  915. return -EINVAL;
  916. }
  917. mutex_lock(&probe_lock);
  918. tp = find_probe_event(event, group);
  919. if (!tp) {
  920. mutex_unlock(&probe_lock);
  921. pr_info("Event %s/%s doesn't exist.\n", group, event);
  922. return -ENOENT;
  923. }
  924. /* delete an event */
  925. unregister_trace_probe(tp);
  926. free_trace_probe(tp);
  927. mutex_unlock(&probe_lock);
  928. return 0;
  929. }
  930. if (argc < 2) {
  931. pr_info("Probe point is not specified.\n");
  932. return -EINVAL;
  933. }
  934. if (isdigit(argv[1][0])) {
  935. if (is_return) {
  936. pr_info("Return probe point must be a symbol.\n");
  937. return -EINVAL;
  938. }
  939. /* an address specified */
  940. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  941. if (ret) {
  942. pr_info("Failed to parse address.\n");
  943. return ret;
  944. }
  945. } else {
  946. /* a symbol specified */
  947. symbol = argv[1];
  948. /* TODO: support .init module functions */
  949. ret = split_symbol_offset(symbol, &offset);
  950. if (ret) {
  951. pr_info("Failed to parse symbol.\n");
  952. return ret;
  953. }
  954. if (offset && is_return) {
  955. pr_info("Return probe must be used without offset.\n");
  956. return -EINVAL;
  957. }
  958. }
  959. argc -= 2; argv += 2;
  960. /* setup a probe */
  961. if (!event) {
  962. /* Make a new event name */
  963. if (symbol)
  964. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  965. is_return ? 'r' : 'p', symbol, offset);
  966. else
  967. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  968. is_return ? 'r' : 'p', addr);
  969. event = buf;
  970. }
  971. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  972. is_return);
  973. if (IS_ERR(tp)) {
  974. pr_info("Failed to allocate trace_probe.(%d)\n",
  975. (int)PTR_ERR(tp));
  976. return PTR_ERR(tp);
  977. }
  978. /* parse arguments */
  979. ret = 0;
  980. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  981. /* Increment count for freeing args in error case */
  982. tp->nr_args++;
  983. /* Parse argument name */
  984. arg = strchr(argv[i], '=');
  985. if (arg) {
  986. *arg++ = '\0';
  987. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  988. } else {
  989. arg = argv[i];
  990. /* If argument name is omitted, set "argN" */
  991. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  992. tp->args[i].name = kstrdup(buf, GFP_KERNEL);
  993. }
  994. if (!tp->args[i].name) {
  995. pr_info("Failed to allocate argument[%d] name.\n", i);
  996. ret = -ENOMEM;
  997. goto error;
  998. }
  999. if (!is_good_name(tp->args[i].name)) {
  1000. pr_info("Invalid argument[%d] name: %s\n",
  1001. i, tp->args[i].name);
  1002. ret = -EINVAL;
  1003. goto error;
  1004. }
  1005. if (conflict_field_name(tp->args[i].name, tp->args, i)) {
  1006. pr_info("Argument[%d] name '%s' conflicts with "
  1007. "another field.\n", i, argv[i]);
  1008. ret = -EINVAL;
  1009. goto error;
  1010. }
  1011. /* Parse fetch argument */
  1012. ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
  1013. if (ret) {
  1014. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  1015. goto error;
  1016. }
  1017. }
  1018. ret = register_trace_probe(tp);
  1019. if (ret)
  1020. goto error;
  1021. return 0;
  1022. error:
  1023. free_trace_probe(tp);
  1024. return ret;
  1025. }
  1026. static void cleanup_all_probes(void)
  1027. {
  1028. struct trace_probe *tp;
  1029. mutex_lock(&probe_lock);
  1030. /* TODO: Use batch unregistration */
  1031. while (!list_empty(&probe_list)) {
  1032. tp = list_entry(probe_list.next, struct trace_probe, list);
  1033. unregister_trace_probe(tp);
  1034. free_trace_probe(tp);
  1035. }
  1036. mutex_unlock(&probe_lock);
  1037. }
  1038. /* Probes listing interfaces */
  1039. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  1040. {
  1041. mutex_lock(&probe_lock);
  1042. return seq_list_start(&probe_list, *pos);
  1043. }
  1044. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1045. {
  1046. return seq_list_next(v, &probe_list, pos);
  1047. }
  1048. static void probes_seq_stop(struct seq_file *m, void *v)
  1049. {
  1050. mutex_unlock(&probe_lock);
  1051. }
  1052. static int probes_seq_show(struct seq_file *m, void *v)
  1053. {
  1054. struct trace_probe *tp = v;
  1055. int i;
  1056. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  1057. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  1058. if (!tp->symbol)
  1059. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  1060. else if (tp->rp.kp.offset)
  1061. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  1062. else
  1063. seq_printf(m, " %s", probe_symbol(tp));
  1064. for (i = 0; i < tp->nr_args; i++)
  1065. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  1066. seq_printf(m, "\n");
  1067. return 0;
  1068. }
  1069. static const struct seq_operations probes_seq_op = {
  1070. .start = probes_seq_start,
  1071. .next = probes_seq_next,
  1072. .stop = probes_seq_stop,
  1073. .show = probes_seq_show
  1074. };
  1075. static int probes_open(struct inode *inode, struct file *file)
  1076. {
  1077. if ((file->f_mode & FMODE_WRITE) &&
  1078. (file->f_flags & O_TRUNC))
  1079. cleanup_all_probes();
  1080. return seq_open(file, &probes_seq_op);
  1081. }
  1082. static int command_trace_probe(const char *buf)
  1083. {
  1084. char **argv;
  1085. int argc = 0, ret = 0;
  1086. argv = argv_split(GFP_KERNEL, buf, &argc);
  1087. if (!argv)
  1088. return -ENOMEM;
  1089. if (argc)
  1090. ret = create_trace_probe(argc, argv);
  1091. argv_free(argv);
  1092. return ret;
  1093. }
  1094. #define WRITE_BUFSIZE 4096
  1095. static ssize_t probes_write(struct file *file, const char __user *buffer,
  1096. size_t count, loff_t *ppos)
  1097. {
  1098. char *kbuf, *tmp;
  1099. int ret;
  1100. size_t done;
  1101. size_t size;
  1102. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  1103. if (!kbuf)
  1104. return -ENOMEM;
  1105. ret = done = 0;
  1106. while (done < count) {
  1107. size = count - done;
  1108. if (size >= WRITE_BUFSIZE)
  1109. size = WRITE_BUFSIZE - 1;
  1110. if (copy_from_user(kbuf, buffer + done, size)) {
  1111. ret = -EFAULT;
  1112. goto out;
  1113. }
  1114. kbuf[size] = '\0';
  1115. tmp = strchr(kbuf, '\n');
  1116. if (tmp) {
  1117. *tmp = '\0';
  1118. size = tmp - kbuf + 1;
  1119. } else if (done + size < count) {
  1120. pr_warning("Line length is too long: "
  1121. "Should be less than %d.", WRITE_BUFSIZE);
  1122. ret = -EINVAL;
  1123. goto out;
  1124. }
  1125. done += size;
  1126. /* Remove comments */
  1127. tmp = strchr(kbuf, '#');
  1128. if (tmp)
  1129. *tmp = '\0';
  1130. ret = command_trace_probe(kbuf);
  1131. if (ret)
  1132. goto out;
  1133. }
  1134. ret = done;
  1135. out:
  1136. kfree(kbuf);
  1137. return ret;
  1138. }
  1139. static const struct file_operations kprobe_events_ops = {
  1140. .owner = THIS_MODULE,
  1141. .open = probes_open,
  1142. .read = seq_read,
  1143. .llseek = seq_lseek,
  1144. .release = seq_release,
  1145. .write = probes_write,
  1146. };
  1147. /* Probes profiling interfaces */
  1148. static int probes_profile_seq_show(struct seq_file *m, void *v)
  1149. {
  1150. struct trace_probe *tp = v;
  1151. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  1152. tp->rp.kp.nmissed);
  1153. return 0;
  1154. }
  1155. static const struct seq_operations profile_seq_op = {
  1156. .start = probes_seq_start,
  1157. .next = probes_seq_next,
  1158. .stop = probes_seq_stop,
  1159. .show = probes_profile_seq_show
  1160. };
  1161. static int profile_open(struct inode *inode, struct file *file)
  1162. {
  1163. return seq_open(file, &profile_seq_op);
  1164. }
  1165. static const struct file_operations kprobe_profile_ops = {
  1166. .owner = THIS_MODULE,
  1167. .open = profile_open,
  1168. .read = seq_read,
  1169. .llseek = seq_lseek,
  1170. .release = seq_release,
  1171. };
  1172. /* Sum up total data length for dynamic arraies (strings) */
  1173. static __kprobes int __get_data_size(struct trace_probe *tp,
  1174. struct pt_regs *regs)
  1175. {
  1176. int i, ret = 0;
  1177. u32 len;
  1178. for (i = 0; i < tp->nr_args; i++)
  1179. if (unlikely(tp->args[i].fetch_size.fn)) {
  1180. call_fetch(&tp->args[i].fetch_size, regs, &len);
  1181. ret += len;
  1182. }
  1183. return ret;
  1184. }
  1185. /* Store the value of each argument */
  1186. static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
  1187. struct pt_regs *regs,
  1188. u8 *data, int maxlen)
  1189. {
  1190. int i;
  1191. u32 end = tp->size;
  1192. u32 *dl; /* Data (relative) location */
  1193. for (i = 0; i < tp->nr_args; i++) {
  1194. if (unlikely(tp->args[i].fetch_size.fn)) {
  1195. /*
  1196. * First, we set the relative location and
  1197. * maximum data length to *dl
  1198. */
  1199. dl = (u32 *)(data + tp->args[i].offset);
  1200. *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
  1201. /* Then try to fetch string or dynamic array data */
  1202. call_fetch(&tp->args[i].fetch, regs, dl);
  1203. /* Reduce maximum length */
  1204. end += get_rloc_len(*dl);
  1205. maxlen -= get_rloc_len(*dl);
  1206. /* Trick here, convert data_rloc to data_loc */
  1207. *dl = convert_rloc_to_loc(*dl,
  1208. ent_size + tp->args[i].offset);
  1209. } else
  1210. /* Just fetching data normally */
  1211. call_fetch(&tp->args[i].fetch, regs,
  1212. data + tp->args[i].offset);
  1213. }
  1214. }
  1215. /* Kprobe handler */
  1216. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  1217. {
  1218. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1219. struct kprobe_trace_entry_head *entry;
  1220. struct ring_buffer_event *event;
  1221. struct ring_buffer *buffer;
  1222. int size, dsize, pc;
  1223. unsigned long irq_flags;
  1224. struct ftrace_event_call *call = &tp->call;
  1225. tp->nhit++;
  1226. local_save_flags(irq_flags);
  1227. pc = preempt_count();
  1228. dsize = __get_data_size(tp, regs);
  1229. size = sizeof(*entry) + tp->size + dsize;
  1230. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1231. size, irq_flags, pc);
  1232. if (!event)
  1233. return;
  1234. entry = ring_buffer_event_data(event);
  1235. entry->ip = (unsigned long)kp->addr;
  1236. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1237. if (!filter_current_check_discard(buffer, call, entry, event))
  1238. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  1239. }
  1240. /* Kretprobe handler */
  1241. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  1242. struct pt_regs *regs)
  1243. {
  1244. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1245. struct kretprobe_trace_entry_head *entry;
  1246. struct ring_buffer_event *event;
  1247. struct ring_buffer *buffer;
  1248. int size, pc, dsize;
  1249. unsigned long irq_flags;
  1250. struct ftrace_event_call *call = &tp->call;
  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->func = (unsigned long)tp->rp.kp.addr;
  1261. entry->ret_ip = (unsigned long)ri->ret_addr;
  1262. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1263. if (!filter_current_check_discard(buffer, call, entry, event))
  1264. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  1265. }
  1266. /* Event entry printers */
  1267. enum print_line_t
  1268. print_kprobe_event(struct trace_iterator *iter, int flags,
  1269. struct trace_event *event)
  1270. {
  1271. struct kprobe_trace_entry_head *field;
  1272. struct trace_seq *s = &iter->seq;
  1273. struct trace_probe *tp;
  1274. u8 *data;
  1275. int i;
  1276. field = (struct kprobe_trace_entry_head *)iter->ent;
  1277. tp = container_of(event, struct trace_probe, call.event);
  1278. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1279. goto partial;
  1280. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  1281. goto partial;
  1282. if (!trace_seq_puts(s, ")"))
  1283. goto partial;
  1284. data = (u8 *)&field[1];
  1285. for (i = 0; i < tp->nr_args; i++)
  1286. if (!tp->args[i].type->print(s, tp->args[i].name,
  1287. data + tp->args[i].offset, field))
  1288. goto partial;
  1289. if (!trace_seq_puts(s, "\n"))
  1290. goto partial;
  1291. return TRACE_TYPE_HANDLED;
  1292. partial:
  1293. return TRACE_TYPE_PARTIAL_LINE;
  1294. }
  1295. enum print_line_t
  1296. print_kretprobe_event(struct trace_iterator *iter, int flags,
  1297. struct trace_event *event)
  1298. {
  1299. struct kretprobe_trace_entry_head *field;
  1300. struct trace_seq *s = &iter->seq;
  1301. struct trace_probe *tp;
  1302. u8 *data;
  1303. int i;
  1304. field = (struct kretprobe_trace_entry_head *)iter->ent;
  1305. tp = container_of(event, struct trace_probe, call.event);
  1306. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1307. goto partial;
  1308. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  1309. goto partial;
  1310. if (!trace_seq_puts(s, " <- "))
  1311. goto partial;
  1312. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  1313. goto partial;
  1314. if (!trace_seq_puts(s, ")"))
  1315. goto partial;
  1316. data = (u8 *)&field[1];
  1317. for (i = 0; i < tp->nr_args; i++)
  1318. if (!tp->args[i].type->print(s, tp->args[i].name,
  1319. data + tp->args[i].offset, field))
  1320. goto partial;
  1321. if (!trace_seq_puts(s, "\n"))
  1322. goto partial;
  1323. return TRACE_TYPE_HANDLED;
  1324. partial:
  1325. return TRACE_TYPE_PARTIAL_LINE;
  1326. }
  1327. static int probe_event_enable(struct ftrace_event_call *call)
  1328. {
  1329. struct trace_probe *tp = (struct trace_probe *)call->data;
  1330. tp->flags |= TP_FLAG_TRACE;
  1331. if (probe_is_return(tp))
  1332. return enable_kretprobe(&tp->rp);
  1333. else
  1334. return enable_kprobe(&tp->rp.kp);
  1335. }
  1336. static void probe_event_disable(struct ftrace_event_call *call)
  1337. {
  1338. struct trace_probe *tp = (struct trace_probe *)call->data;
  1339. tp->flags &= ~TP_FLAG_TRACE;
  1340. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  1341. if (probe_is_return(tp))
  1342. disable_kretprobe(&tp->rp);
  1343. else
  1344. disable_kprobe(&tp->rp.kp);
  1345. }
  1346. }
  1347. #undef DEFINE_FIELD
  1348. #define DEFINE_FIELD(type, item, name, is_signed) \
  1349. do { \
  1350. ret = trace_define_field(event_call, #type, name, \
  1351. offsetof(typeof(field), item), \
  1352. sizeof(field.item), is_signed, \
  1353. FILTER_OTHER); \
  1354. if (ret) \
  1355. return ret; \
  1356. } while (0)
  1357. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  1358. {
  1359. int ret, i;
  1360. struct kprobe_trace_entry_head field;
  1361. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1362. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  1363. /* Set argument names as fields */
  1364. for (i = 0; i < tp->nr_args; i++) {
  1365. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1366. tp->args[i].name,
  1367. sizeof(field) + tp->args[i].offset,
  1368. tp->args[i].type->size,
  1369. tp->args[i].type->is_signed,
  1370. FILTER_OTHER);
  1371. if (ret)
  1372. return ret;
  1373. }
  1374. return 0;
  1375. }
  1376. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  1377. {
  1378. int ret, i;
  1379. struct kretprobe_trace_entry_head field;
  1380. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1381. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1382. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1383. /* Set argument names as fields */
  1384. for (i = 0; i < tp->nr_args; i++) {
  1385. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1386. tp->args[i].name,
  1387. sizeof(field) + tp->args[i].offset,
  1388. tp->args[i].type->size,
  1389. tp->args[i].type->is_signed,
  1390. FILTER_OTHER);
  1391. if (ret)
  1392. return ret;
  1393. }
  1394. return 0;
  1395. }
  1396. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  1397. {
  1398. int i;
  1399. int pos = 0;
  1400. const char *fmt, *arg;
  1401. if (!probe_is_return(tp)) {
  1402. fmt = "(%lx)";
  1403. arg = "REC->" FIELD_STRING_IP;
  1404. } else {
  1405. fmt = "(%lx <- %lx)";
  1406. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1407. }
  1408. /* When len=0, we just calculate the needed length */
  1409. #define LEN_OR_ZERO (len ? len - pos : 0)
  1410. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1411. for (i = 0; i < tp->nr_args; i++) {
  1412. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  1413. tp->args[i].name, tp->args[i].type->fmt);
  1414. }
  1415. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  1416. for (i = 0; i < tp->nr_args; i++) {
  1417. if (strcmp(tp->args[i].type->name, "string") == 0)
  1418. pos += snprintf(buf + pos, LEN_OR_ZERO,
  1419. ", __get_str(%s)",
  1420. tp->args[i].name);
  1421. else
  1422. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  1423. tp->args[i].name);
  1424. }
  1425. #undef LEN_OR_ZERO
  1426. /* return the length of print_fmt */
  1427. return pos;
  1428. }
  1429. static int set_print_fmt(struct trace_probe *tp)
  1430. {
  1431. int len;
  1432. char *print_fmt;
  1433. /* First: called with 0 length to calculate the needed length */
  1434. len = __set_print_fmt(tp, NULL, 0);
  1435. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1436. if (!print_fmt)
  1437. return -ENOMEM;
  1438. /* Second: actually write the @print_fmt */
  1439. __set_print_fmt(tp, print_fmt, len + 1);
  1440. tp->call.print_fmt = print_fmt;
  1441. return 0;
  1442. }
  1443. #ifdef CONFIG_PERF_EVENTS
  1444. /* Kprobe profile handler */
  1445. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  1446. struct pt_regs *regs)
  1447. {
  1448. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1449. struct ftrace_event_call *call = &tp->call;
  1450. struct kprobe_trace_entry_head *entry;
  1451. struct hlist_head *head;
  1452. int size, __size, dsize;
  1453. int rctx;
  1454. dsize = __get_data_size(tp, regs);
  1455. __size = sizeof(*entry) + tp->size + dsize;
  1456. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1457. size -= sizeof(u32);
  1458. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1459. "profile buffer not large enough"))
  1460. return;
  1461. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1462. if (!entry)
  1463. return;
  1464. entry->ip = (unsigned long)kp->addr;
  1465. memset(&entry[1], 0, dsize);
  1466. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1467. head = this_cpu_ptr(call->perf_events);
  1468. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
  1469. }
  1470. /* Kretprobe profile handler */
  1471. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1472. struct pt_regs *regs)
  1473. {
  1474. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1475. struct ftrace_event_call *call = &tp->call;
  1476. struct kretprobe_trace_entry_head *entry;
  1477. struct hlist_head *head;
  1478. int size, __size, dsize;
  1479. int rctx;
  1480. dsize = __get_data_size(tp, regs);
  1481. __size = sizeof(*entry) + tp->size + dsize;
  1482. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1483. size -= sizeof(u32);
  1484. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1485. "profile buffer not large enough"))
  1486. return;
  1487. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1488. if (!entry)
  1489. return;
  1490. entry->func = (unsigned long)tp->rp.kp.addr;
  1491. entry->ret_ip = (unsigned long)ri->ret_addr;
  1492. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1493. head = this_cpu_ptr(call->perf_events);
  1494. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
  1495. }
  1496. static int probe_perf_enable(struct ftrace_event_call *call)
  1497. {
  1498. struct trace_probe *tp = (struct trace_probe *)call->data;
  1499. tp->flags |= TP_FLAG_PROFILE;
  1500. if (probe_is_return(tp))
  1501. return enable_kretprobe(&tp->rp);
  1502. else
  1503. return enable_kprobe(&tp->rp.kp);
  1504. }
  1505. static void probe_perf_disable(struct ftrace_event_call *call)
  1506. {
  1507. struct trace_probe *tp = (struct trace_probe *)call->data;
  1508. tp->flags &= ~TP_FLAG_PROFILE;
  1509. if (!(tp->flags & TP_FLAG_TRACE)) {
  1510. if (probe_is_return(tp))
  1511. disable_kretprobe(&tp->rp);
  1512. else
  1513. disable_kprobe(&tp->rp.kp);
  1514. }
  1515. }
  1516. #endif /* CONFIG_PERF_EVENTS */
  1517. static __kprobes
  1518. int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
  1519. {
  1520. switch (type) {
  1521. case TRACE_REG_REGISTER:
  1522. return probe_event_enable(event);
  1523. case TRACE_REG_UNREGISTER:
  1524. probe_event_disable(event);
  1525. return 0;
  1526. #ifdef CONFIG_PERF_EVENTS
  1527. case TRACE_REG_PERF_REGISTER:
  1528. return probe_perf_enable(event);
  1529. case TRACE_REG_PERF_UNREGISTER:
  1530. probe_perf_disable(event);
  1531. return 0;
  1532. #endif
  1533. }
  1534. return 0;
  1535. }
  1536. static __kprobes
  1537. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1538. {
  1539. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1540. if (tp->flags & TP_FLAG_TRACE)
  1541. kprobe_trace_func(kp, regs);
  1542. #ifdef CONFIG_PERF_EVENTS
  1543. if (tp->flags & TP_FLAG_PROFILE)
  1544. kprobe_perf_func(kp, regs);
  1545. #endif
  1546. return 0; /* We don't tweek kernel, so just return 0 */
  1547. }
  1548. static __kprobes
  1549. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1550. {
  1551. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1552. if (tp->flags & TP_FLAG_TRACE)
  1553. kretprobe_trace_func(ri, regs);
  1554. #ifdef CONFIG_PERF_EVENTS
  1555. if (tp->flags & TP_FLAG_PROFILE)
  1556. kretprobe_perf_func(ri, regs);
  1557. #endif
  1558. return 0; /* We don't tweek kernel, so just return 0 */
  1559. }
  1560. static struct trace_event_functions kretprobe_funcs = {
  1561. .trace = print_kretprobe_event
  1562. };
  1563. static struct trace_event_functions kprobe_funcs = {
  1564. .trace = print_kprobe_event
  1565. };
  1566. static int register_probe_event(struct trace_probe *tp)
  1567. {
  1568. struct ftrace_event_call *call = &tp->call;
  1569. int ret;
  1570. /* Initialize ftrace_event_call */
  1571. INIT_LIST_HEAD(&call->class->fields);
  1572. if (probe_is_return(tp)) {
  1573. call->event.funcs = &kretprobe_funcs;
  1574. call->class->define_fields = kretprobe_event_define_fields;
  1575. } else {
  1576. call->event.funcs = &kprobe_funcs;
  1577. call->class->define_fields = kprobe_event_define_fields;
  1578. }
  1579. if (set_print_fmt(tp) < 0)
  1580. return -ENOMEM;
  1581. ret = register_ftrace_event(&call->event);
  1582. if (!ret) {
  1583. kfree(call->print_fmt);
  1584. return -ENODEV;
  1585. }
  1586. call->flags = 0;
  1587. call->class->reg = kprobe_register;
  1588. call->data = tp;
  1589. ret = trace_add_event_call(call);
  1590. if (ret) {
  1591. pr_info("Failed to register kprobe event: %s\n", call->name);
  1592. kfree(call->print_fmt);
  1593. unregister_ftrace_event(&call->event);
  1594. }
  1595. return ret;
  1596. }
  1597. static void unregister_probe_event(struct trace_probe *tp)
  1598. {
  1599. /* tp->event is unregistered in trace_remove_event_call() */
  1600. trace_remove_event_call(&tp->call);
  1601. kfree(tp->call.print_fmt);
  1602. }
  1603. /* Make a debugfs interface for controlling probe points */
  1604. static __init int init_kprobe_trace(void)
  1605. {
  1606. struct dentry *d_tracer;
  1607. struct dentry *entry;
  1608. d_tracer = tracing_init_dentry();
  1609. if (!d_tracer)
  1610. return 0;
  1611. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1612. NULL, &kprobe_events_ops);
  1613. /* Event list interface */
  1614. if (!entry)
  1615. pr_warning("Could not create debugfs "
  1616. "'kprobe_events' entry\n");
  1617. /* Profile interface */
  1618. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1619. NULL, &kprobe_profile_ops);
  1620. if (!entry)
  1621. pr_warning("Could not create debugfs "
  1622. "'kprobe_profile' entry\n");
  1623. return 0;
  1624. }
  1625. fs_initcall(init_kprobe_trace);
  1626. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1627. /*
  1628. * The "__used" keeps gcc from removing the function symbol
  1629. * from the kallsyms table.
  1630. */
  1631. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1632. int a4, int a5, int a6)
  1633. {
  1634. return a1 + a2 + a3 + a4 + a5 + a6;
  1635. }
  1636. static __init int kprobe_trace_self_tests_init(void)
  1637. {
  1638. int ret, warn = 0;
  1639. int (*target)(int, int, int, int, int, int);
  1640. struct trace_probe *tp;
  1641. target = kprobe_trace_selftest_target;
  1642. pr_info("Testing kprobe tracing: ");
  1643. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1644. "$stack $stack0 +0($stack)");
  1645. if (WARN_ON_ONCE(ret)) {
  1646. pr_warning("error on probing function entry.\n");
  1647. warn++;
  1648. } else {
  1649. /* Enable trace point */
  1650. tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
  1651. if (WARN_ON_ONCE(tp == NULL)) {
  1652. pr_warning("error on getting new probe.\n");
  1653. warn++;
  1654. } else
  1655. probe_event_enable(&tp->call);
  1656. }
  1657. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1658. "$retval");
  1659. if (WARN_ON_ONCE(ret)) {
  1660. pr_warning("error on probing function return.\n");
  1661. warn++;
  1662. } else {
  1663. /* Enable trace point */
  1664. tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
  1665. if (WARN_ON_ONCE(tp == NULL)) {
  1666. pr_warning("error on getting new probe.\n");
  1667. warn++;
  1668. } else
  1669. probe_event_enable(&tp->call);
  1670. }
  1671. if (warn)
  1672. goto end;
  1673. ret = target(1, 2, 3, 4, 5, 6);
  1674. ret = command_trace_probe("-:testprobe");
  1675. if (WARN_ON_ONCE(ret)) {
  1676. pr_warning("error on deleting a probe.\n");
  1677. warn++;
  1678. }
  1679. ret = command_trace_probe("-:testprobe2");
  1680. if (WARN_ON_ONCE(ret)) {
  1681. pr_warning("error on deleting a probe.\n");
  1682. warn++;
  1683. }
  1684. end:
  1685. cleanup_all_probes();
  1686. if (warn)
  1687. pr_cont("NG: Some tests are failed. Please check them.\n");
  1688. else
  1689. pr_cont("OK\n");
  1690. return 0;
  1691. }
  1692. late_initcall(kprobe_trace_self_tests_init);
  1693. #endif