trace_kprobe.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  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 update_deref_fetch_param(struct deref_fetch_param *data)
  309. {
  310. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  311. update_deref_fetch_param(data->orig.data);
  312. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  313. update_symbol_cache(data->orig.data);
  314. }
  315. static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data)
  316. {
  317. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  318. free_deref_fetch_param(data->orig.data);
  319. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  320. free_symbol_cache(data->orig.data);
  321. kfree(data);
  322. }
  323. /* Bitfield fetch function */
  324. struct bitfield_fetch_param {
  325. struct fetch_param orig;
  326. unsigned char hi_shift;
  327. unsigned char low_shift;
  328. };
  329. #define DEFINE_FETCH_bitfield(type) \
  330. static __kprobes void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,\
  331. void *data, void *dest) \
  332. { \
  333. struct bitfield_fetch_param *bprm = data; \
  334. type buf = 0; \
  335. call_fetch(&bprm->orig, regs, &buf); \
  336. if (buf) { \
  337. buf <<= bprm->hi_shift; \
  338. buf >>= bprm->low_shift; \
  339. } \
  340. *(type *)dest = buf; \
  341. }
  342. DEFINE_BASIC_FETCH_FUNCS(bitfield)
  343. #define fetch_bitfield_string NULL
  344. #define fetch_bitfield_string_size NULL
  345. static __kprobes void
  346. update_bitfield_fetch_param(struct bitfield_fetch_param *data)
  347. {
  348. /*
  349. * Don't check the bitfield itself, because this must be the
  350. * last fetch function.
  351. */
  352. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  353. update_deref_fetch_param(data->orig.data);
  354. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  355. update_symbol_cache(data->orig.data);
  356. }
  357. static __kprobes void
  358. free_bitfield_fetch_param(struct bitfield_fetch_param *data)
  359. {
  360. /*
  361. * Don't check the bitfield itself, because this must be the
  362. * last fetch function.
  363. */
  364. if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
  365. free_deref_fetch_param(data->orig.data);
  366. else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
  367. free_symbol_cache(data->orig.data);
  368. kfree(data);
  369. }
  370. /* Default (unsigned long) fetch type */
  371. #define __DEFAULT_FETCH_TYPE(t) u##t
  372. #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t)
  373. #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG)
  374. #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE)
  375. /* Fetch types */
  376. enum {
  377. FETCH_MTD_reg = 0,
  378. FETCH_MTD_stack,
  379. FETCH_MTD_retval,
  380. FETCH_MTD_memory,
  381. FETCH_MTD_symbol,
  382. FETCH_MTD_deref,
  383. FETCH_MTD_bitfield,
  384. FETCH_MTD_END,
  385. };
  386. #define ASSIGN_FETCH_FUNC(method, type) \
  387. [FETCH_MTD_##method] = FETCH_FUNC_NAME(method, type)
  388. #define __ASSIGN_FETCH_TYPE(_name, ptype, ftype, _size, sign, _fmttype) \
  389. {.name = _name, \
  390. .size = _size, \
  391. .is_signed = sign, \
  392. .print = PRINT_TYPE_FUNC_NAME(ptype), \
  393. .fmt = PRINT_TYPE_FMT_NAME(ptype), \
  394. .fmttype = _fmttype, \
  395. .fetch = { \
  396. ASSIGN_FETCH_FUNC(reg, ftype), \
  397. ASSIGN_FETCH_FUNC(stack, ftype), \
  398. ASSIGN_FETCH_FUNC(retval, ftype), \
  399. ASSIGN_FETCH_FUNC(memory, ftype), \
  400. ASSIGN_FETCH_FUNC(symbol, ftype), \
  401. ASSIGN_FETCH_FUNC(deref, ftype), \
  402. ASSIGN_FETCH_FUNC(bitfield, ftype), \
  403. } \
  404. }
  405. #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \
  406. __ASSIGN_FETCH_TYPE(#ptype, ptype, ftype, sizeof(ftype), sign, #ptype)
  407. #define FETCH_TYPE_STRING 0
  408. #define FETCH_TYPE_STRSIZE 1
  409. /* Fetch type information table */
  410. static const struct fetch_type {
  411. const char *name; /* Name of type */
  412. size_t size; /* Byte size of type */
  413. int is_signed; /* Signed flag */
  414. print_type_func_t print; /* Print functions */
  415. const char *fmt; /* Fromat string */
  416. const char *fmttype; /* Name in format file */
  417. /* Fetch functions */
  418. fetch_func_t fetch[FETCH_MTD_END];
  419. } fetch_type_table[] = {
  420. /* Special types */
  421. [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
  422. sizeof(u32), 1, "__data_loc char[]"),
  423. [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
  424. string_size, sizeof(u32), 0, "u32"),
  425. /* Basic types */
  426. ASSIGN_FETCH_TYPE(u8, u8, 0),
  427. ASSIGN_FETCH_TYPE(u16, u16, 0),
  428. ASSIGN_FETCH_TYPE(u32, u32, 0),
  429. ASSIGN_FETCH_TYPE(u64, u64, 0),
  430. ASSIGN_FETCH_TYPE(s8, u8, 1),
  431. ASSIGN_FETCH_TYPE(s16, u16, 1),
  432. ASSIGN_FETCH_TYPE(s32, u32, 1),
  433. ASSIGN_FETCH_TYPE(s64, u64, 1),
  434. };
  435. static const struct fetch_type *find_fetch_type(const char *type)
  436. {
  437. int i;
  438. if (!type)
  439. type = DEFAULT_FETCH_TYPE_STR;
  440. /* Special case: bitfield */
  441. if (*type == 'b') {
  442. unsigned long bs;
  443. type = strchr(type, '/');
  444. if (!type)
  445. goto fail;
  446. type++;
  447. if (strict_strtoul(type, 0, &bs))
  448. goto fail;
  449. switch (bs) {
  450. case 8:
  451. return find_fetch_type("u8");
  452. case 16:
  453. return find_fetch_type("u16");
  454. case 32:
  455. return find_fetch_type("u32");
  456. case 64:
  457. return find_fetch_type("u64");
  458. default:
  459. goto fail;
  460. }
  461. }
  462. for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++)
  463. if (strcmp(type, fetch_type_table[i].name) == 0)
  464. return &fetch_type_table[i];
  465. fail:
  466. return NULL;
  467. }
  468. /* Special function : only accept unsigned long */
  469. static __kprobes void fetch_stack_address(struct pt_regs *regs,
  470. void *dummy, void *dest)
  471. {
  472. *(unsigned long *)dest = kernel_stack_pointer(regs);
  473. }
  474. static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
  475. fetch_func_t orig_fn)
  476. {
  477. int i;
  478. if (type != &fetch_type_table[FETCH_TYPE_STRING])
  479. return NULL; /* Only string type needs size function */
  480. for (i = 0; i < FETCH_MTD_END; i++)
  481. if (type->fetch[i] == orig_fn)
  482. return fetch_type_table[FETCH_TYPE_STRSIZE].fetch[i];
  483. WARN_ON(1); /* This should not happen */
  484. return NULL;
  485. }
  486. /**
  487. * Kprobe event core functions
  488. */
  489. struct probe_arg {
  490. struct fetch_param fetch;
  491. struct fetch_param fetch_size;
  492. unsigned int offset; /* Offset from argument entry */
  493. const char *name; /* Name of this argument */
  494. const char *comm; /* Command of this argument */
  495. const struct fetch_type *type; /* Type of this argument */
  496. };
  497. /* Flags for trace_probe */
  498. #define TP_FLAG_TRACE 1
  499. #define TP_FLAG_PROFILE 2
  500. #define TP_FLAG_REGISTERED 4
  501. struct trace_probe {
  502. struct list_head list;
  503. struct kretprobe rp; /* Use rp.kp for kprobe use */
  504. unsigned long nhit;
  505. unsigned int flags; /* For TP_FLAG_* */
  506. const char *symbol; /* symbol name */
  507. struct ftrace_event_class class;
  508. struct ftrace_event_call call;
  509. ssize_t size; /* trace entry size */
  510. unsigned int nr_args;
  511. struct probe_arg args[];
  512. };
  513. #define SIZEOF_TRACE_PROBE(n) \
  514. (offsetof(struct trace_probe, args) + \
  515. (sizeof(struct probe_arg) * (n)))
  516. static __kprobes int trace_probe_is_return(struct trace_probe *tp)
  517. {
  518. return tp->rp.handler != NULL;
  519. }
  520. static __kprobes const char *trace_probe_symbol(struct trace_probe *tp)
  521. {
  522. return tp->symbol ? tp->symbol : "unknown";
  523. }
  524. static __kprobes unsigned long trace_probe_offset(struct trace_probe *tp)
  525. {
  526. return tp->rp.kp.offset;
  527. }
  528. static __kprobes bool trace_probe_is_enabled(struct trace_probe *tp)
  529. {
  530. return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
  531. }
  532. static __kprobes bool trace_probe_is_registered(struct trace_probe *tp)
  533. {
  534. return !!(tp->flags & TP_FLAG_REGISTERED);
  535. }
  536. static __kprobes bool trace_probe_has_gone(struct trace_probe *tp)
  537. {
  538. return !!(kprobe_gone(&tp->rp.kp));
  539. }
  540. static __kprobes bool trace_probe_within_module(struct trace_probe *tp,
  541. struct module *mod)
  542. {
  543. int len = strlen(mod->name);
  544. const char *name = trace_probe_symbol(tp);
  545. return strncmp(mod->name, name, len) == 0 && name[len] == ':';
  546. }
  547. static __kprobes bool trace_probe_is_on_module(struct trace_probe *tp)
  548. {
  549. return !!strchr(trace_probe_symbol(tp), ':');
  550. }
  551. static int register_probe_event(struct trace_probe *tp);
  552. static void unregister_probe_event(struct trace_probe *tp);
  553. static DEFINE_MUTEX(probe_lock);
  554. static LIST_HEAD(probe_list);
  555. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  556. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  557. struct pt_regs *regs);
  558. /* Check the name is good for event/group/fields */
  559. static int is_good_name(const char *name)
  560. {
  561. if (!isalpha(*name) && *name != '_')
  562. return 0;
  563. while (*++name != '\0') {
  564. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  565. return 0;
  566. }
  567. return 1;
  568. }
  569. /*
  570. * Allocate new trace_probe and initialize it (including kprobes).
  571. */
  572. static struct trace_probe *alloc_trace_probe(const char *group,
  573. const char *event,
  574. void *addr,
  575. const char *symbol,
  576. unsigned long offs,
  577. int nargs, int is_return)
  578. {
  579. struct trace_probe *tp;
  580. int ret = -ENOMEM;
  581. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  582. if (!tp)
  583. return ERR_PTR(ret);
  584. if (symbol) {
  585. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  586. if (!tp->symbol)
  587. goto error;
  588. tp->rp.kp.symbol_name = tp->symbol;
  589. tp->rp.kp.offset = offs;
  590. } else
  591. tp->rp.kp.addr = addr;
  592. if (is_return)
  593. tp->rp.handler = kretprobe_dispatcher;
  594. else
  595. tp->rp.kp.pre_handler = kprobe_dispatcher;
  596. if (!event || !is_good_name(event)) {
  597. ret = -EINVAL;
  598. goto error;
  599. }
  600. tp->call.class = &tp->class;
  601. tp->call.name = kstrdup(event, GFP_KERNEL);
  602. if (!tp->call.name)
  603. goto error;
  604. if (!group || !is_good_name(group)) {
  605. ret = -EINVAL;
  606. goto error;
  607. }
  608. tp->class.system = kstrdup(group, GFP_KERNEL);
  609. if (!tp->class.system)
  610. goto error;
  611. INIT_LIST_HEAD(&tp->list);
  612. return tp;
  613. error:
  614. kfree(tp->call.name);
  615. kfree(tp->symbol);
  616. kfree(tp);
  617. return ERR_PTR(ret);
  618. }
  619. static void update_probe_arg(struct probe_arg *arg)
  620. {
  621. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  622. update_bitfield_fetch_param(arg->fetch.data);
  623. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  624. update_deref_fetch_param(arg->fetch.data);
  625. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  626. update_symbol_cache(arg->fetch.data);
  627. }
  628. static void free_probe_arg(struct probe_arg *arg)
  629. {
  630. if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
  631. free_bitfield_fetch_param(arg->fetch.data);
  632. else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
  633. free_deref_fetch_param(arg->fetch.data);
  634. else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
  635. free_symbol_cache(arg->fetch.data);
  636. kfree(arg->name);
  637. kfree(arg->comm);
  638. }
  639. static void free_trace_probe(struct trace_probe *tp)
  640. {
  641. int i;
  642. for (i = 0; i < tp->nr_args; i++)
  643. free_probe_arg(&tp->args[i]);
  644. kfree(tp->call.class->system);
  645. kfree(tp->call.name);
  646. kfree(tp->symbol);
  647. kfree(tp);
  648. }
  649. static struct trace_probe *find_trace_probe(const char *event,
  650. const char *group)
  651. {
  652. struct trace_probe *tp;
  653. list_for_each_entry(tp, &probe_list, list)
  654. if (strcmp(tp->call.name, event) == 0 &&
  655. strcmp(tp->call.class->system, group) == 0)
  656. return tp;
  657. return NULL;
  658. }
  659. /* Enable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
  660. static int enable_trace_probe(struct trace_probe *tp, int flag)
  661. {
  662. int ret = 0;
  663. tp->flags |= flag;
  664. if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) &&
  665. !trace_probe_has_gone(tp)) {
  666. if (trace_probe_is_return(tp))
  667. ret = enable_kretprobe(&tp->rp);
  668. else
  669. ret = enable_kprobe(&tp->rp.kp);
  670. }
  671. return ret;
  672. }
  673. /* Disable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
  674. static void disable_trace_probe(struct trace_probe *tp, int flag)
  675. {
  676. tp->flags &= ~flag;
  677. if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) {
  678. if (trace_probe_is_return(tp))
  679. disable_kretprobe(&tp->rp);
  680. else
  681. disable_kprobe(&tp->rp.kp);
  682. }
  683. }
  684. /* Internal register function - just handle k*probes and flags */
  685. static int __register_trace_probe(struct trace_probe *tp)
  686. {
  687. int i, ret;
  688. if (trace_probe_is_registered(tp))
  689. return -EINVAL;
  690. for (i = 0; i < tp->nr_args; i++)
  691. update_probe_arg(&tp->args[i]);
  692. /* Set/clear disabled flag according to tp->flag */
  693. if (trace_probe_is_enabled(tp))
  694. tp->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
  695. else
  696. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  697. if (trace_probe_is_return(tp))
  698. ret = register_kretprobe(&tp->rp);
  699. else
  700. ret = register_kprobe(&tp->rp.kp);
  701. if (ret == 0)
  702. tp->flags |= TP_FLAG_REGISTERED;
  703. else {
  704. pr_warning("Could not insert probe at %s+%lu: %d\n",
  705. trace_probe_symbol(tp), trace_probe_offset(tp), ret);
  706. if (ret == -ENOENT && trace_probe_is_on_module(tp)) {
  707. pr_warning("This probe might be able to register after"
  708. "target module is loaded. Continue.\n");
  709. ret = 0;
  710. } else if (ret == -EILSEQ) {
  711. pr_warning("Probing address(0x%p) is not an "
  712. "instruction boundary.\n",
  713. tp->rp.kp.addr);
  714. ret = -EINVAL;
  715. }
  716. }
  717. return ret;
  718. }
  719. /* Internal unregister function - just handle k*probes and flags */
  720. static void __unregister_trace_probe(struct trace_probe *tp)
  721. {
  722. if (trace_probe_is_registered(tp)) {
  723. if (trace_probe_is_return(tp))
  724. unregister_kretprobe(&tp->rp);
  725. else
  726. unregister_kprobe(&tp->rp.kp);
  727. tp->flags &= ~TP_FLAG_REGISTERED;
  728. /* Cleanup kprobe for reuse */
  729. if (tp->rp.kp.symbol_name)
  730. tp->rp.kp.addr = NULL;
  731. }
  732. }
  733. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  734. static void unregister_trace_probe(struct trace_probe *tp)
  735. {
  736. __unregister_trace_probe(tp);
  737. list_del(&tp->list);
  738. unregister_probe_event(tp);
  739. }
  740. /* Register a trace_probe and probe_event */
  741. static int register_trace_probe(struct trace_probe *tp)
  742. {
  743. struct trace_probe *old_tp;
  744. int ret;
  745. mutex_lock(&probe_lock);
  746. /* Delete old (same name) event if exist */
  747. old_tp = find_trace_probe(tp->call.name, tp->call.class->system);
  748. if (old_tp) {
  749. unregister_trace_probe(old_tp);
  750. free_trace_probe(old_tp);
  751. }
  752. /* Register new event */
  753. ret = register_probe_event(tp);
  754. if (ret) {
  755. pr_warning("Failed to register probe event(%d)\n", ret);
  756. goto end;
  757. }
  758. /* Register k*probe */
  759. ret = __register_trace_probe(tp);
  760. if (ret < 0)
  761. unregister_probe_event(tp);
  762. else
  763. list_add_tail(&tp->list, &probe_list);
  764. end:
  765. mutex_unlock(&probe_lock);
  766. return ret;
  767. }
  768. /* Module notifier call back, checking event on the module */
  769. static int trace_probe_module_callback(struct notifier_block *nb,
  770. unsigned long val, void *data)
  771. {
  772. struct module *mod = data;
  773. struct trace_probe *tp;
  774. int ret;
  775. if (val != MODULE_STATE_COMING)
  776. return NOTIFY_DONE;
  777. /* Update probes on coming module */
  778. mutex_lock(&probe_lock);
  779. list_for_each_entry(tp, &probe_list, list) {
  780. if (trace_probe_within_module(tp, mod)) {
  781. __unregister_trace_probe(tp);
  782. ret = __register_trace_probe(tp);
  783. if (ret)
  784. pr_warning("Failed to re-register probe %s on"
  785. "%s: %d\n",
  786. tp->call.name, mod->name, ret);
  787. }
  788. }
  789. mutex_unlock(&probe_lock);
  790. return NOTIFY_DONE;
  791. }
  792. static struct notifier_block trace_probe_module_nb = {
  793. .notifier_call = trace_probe_module_callback,
  794. .priority = 1 /* Invoked after kprobe module callback */
  795. };
  796. /* Split symbol and offset. */
  797. static int split_symbol_offset(char *symbol, unsigned long *offset)
  798. {
  799. char *tmp;
  800. int ret;
  801. if (!offset)
  802. return -EINVAL;
  803. tmp = strchr(symbol, '+');
  804. if (tmp) {
  805. /* skip sign because strict_strtol doesn't accept '+' */
  806. ret = strict_strtoul(tmp + 1, 0, offset);
  807. if (ret)
  808. return ret;
  809. *tmp = '\0';
  810. } else
  811. *offset = 0;
  812. return 0;
  813. }
  814. #define PARAM_MAX_ARGS 16
  815. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  816. static int parse_probe_vars(char *arg, const struct fetch_type *t,
  817. struct fetch_param *f, int is_return)
  818. {
  819. int ret = 0;
  820. unsigned long param;
  821. if (strcmp(arg, "retval") == 0) {
  822. if (is_return)
  823. f->fn = t->fetch[FETCH_MTD_retval];
  824. else
  825. ret = -EINVAL;
  826. } else if (strncmp(arg, "stack", 5) == 0) {
  827. if (arg[5] == '\0') {
  828. if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0)
  829. f->fn = fetch_stack_address;
  830. else
  831. ret = -EINVAL;
  832. } else if (isdigit(arg[5])) {
  833. ret = strict_strtoul(arg + 5, 10, &param);
  834. if (ret || param > PARAM_MAX_STACK)
  835. ret = -EINVAL;
  836. else {
  837. f->fn = t->fetch[FETCH_MTD_stack];
  838. f->data = (void *)param;
  839. }
  840. } else
  841. ret = -EINVAL;
  842. } else
  843. ret = -EINVAL;
  844. return ret;
  845. }
  846. /* Recursive argument parser */
  847. static int __parse_probe_arg(char *arg, const struct fetch_type *t,
  848. struct fetch_param *f, int is_return)
  849. {
  850. int ret = 0;
  851. unsigned long param;
  852. long offset;
  853. char *tmp;
  854. switch (arg[0]) {
  855. case '$':
  856. ret = parse_probe_vars(arg + 1, t, f, is_return);
  857. break;
  858. case '%': /* named register */
  859. ret = regs_query_register_offset(arg + 1);
  860. if (ret >= 0) {
  861. f->fn = t->fetch[FETCH_MTD_reg];
  862. f->data = (void *)(unsigned long)ret;
  863. ret = 0;
  864. }
  865. break;
  866. case '@': /* memory or symbol */
  867. if (isdigit(arg[1])) {
  868. ret = strict_strtoul(arg + 1, 0, &param);
  869. if (ret)
  870. break;
  871. f->fn = t->fetch[FETCH_MTD_memory];
  872. f->data = (void *)param;
  873. } else {
  874. ret = split_symbol_offset(arg + 1, &offset);
  875. if (ret)
  876. break;
  877. f->data = alloc_symbol_cache(arg + 1, offset);
  878. if (f->data)
  879. f->fn = t->fetch[FETCH_MTD_symbol];
  880. }
  881. break;
  882. case '+': /* deref memory */
  883. arg++; /* Skip '+', because strict_strtol() rejects it. */
  884. case '-':
  885. tmp = strchr(arg, '(');
  886. if (!tmp)
  887. break;
  888. *tmp = '\0';
  889. ret = strict_strtol(arg, 0, &offset);
  890. if (ret)
  891. break;
  892. arg = tmp + 1;
  893. tmp = strrchr(arg, ')');
  894. if (tmp) {
  895. struct deref_fetch_param *dprm;
  896. const struct fetch_type *t2 = find_fetch_type(NULL);
  897. *tmp = '\0';
  898. dprm = kzalloc(sizeof(struct deref_fetch_param),
  899. GFP_KERNEL);
  900. if (!dprm)
  901. return -ENOMEM;
  902. dprm->offset = offset;
  903. ret = __parse_probe_arg(arg, t2, &dprm->orig,
  904. is_return);
  905. if (ret)
  906. kfree(dprm);
  907. else {
  908. f->fn = t->fetch[FETCH_MTD_deref];
  909. f->data = (void *)dprm;
  910. }
  911. }
  912. break;
  913. }
  914. if (!ret && !f->fn) { /* Parsed, but do not find fetch method */
  915. pr_info("%s type has no corresponding fetch method.\n",
  916. t->name);
  917. ret = -EINVAL;
  918. }
  919. return ret;
  920. }
  921. #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
  922. /* Bitfield type needs to be parsed into a fetch function */
  923. static int __parse_bitfield_probe_arg(const char *bf,
  924. const struct fetch_type *t,
  925. struct fetch_param *f)
  926. {
  927. struct bitfield_fetch_param *bprm;
  928. unsigned long bw, bo;
  929. char *tail;
  930. if (*bf != 'b')
  931. return 0;
  932. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  933. if (!bprm)
  934. return -ENOMEM;
  935. bprm->orig = *f;
  936. f->fn = t->fetch[FETCH_MTD_bitfield];
  937. f->data = (void *)bprm;
  938. bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
  939. if (bw == 0 || *tail != '@')
  940. return -EINVAL;
  941. bf = tail + 1;
  942. bo = simple_strtoul(bf, &tail, 0);
  943. if (tail == bf || *tail != '/')
  944. return -EINVAL;
  945. bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
  946. bprm->low_shift = bprm->hi_shift + bo;
  947. return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
  948. }
  949. /* String length checking wrapper */
  950. static int parse_probe_arg(char *arg, struct trace_probe *tp,
  951. struct probe_arg *parg, int is_return)
  952. {
  953. const char *t;
  954. int ret;
  955. if (strlen(arg) > MAX_ARGSTR_LEN) {
  956. pr_info("Argument is too long.: %s\n", arg);
  957. return -ENOSPC;
  958. }
  959. parg->comm = kstrdup(arg, GFP_KERNEL);
  960. if (!parg->comm) {
  961. pr_info("Failed to allocate memory for command '%s'.\n", arg);
  962. return -ENOMEM;
  963. }
  964. t = strchr(parg->comm, ':');
  965. if (t) {
  966. arg[t - parg->comm] = '\0';
  967. t++;
  968. }
  969. parg->type = find_fetch_type(t);
  970. if (!parg->type) {
  971. pr_info("Unsupported type: %s\n", t);
  972. return -EINVAL;
  973. }
  974. parg->offset = tp->size;
  975. tp->size += parg->type->size;
  976. ret = __parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
  977. if (ret >= 0 && t != NULL)
  978. ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
  979. if (ret >= 0) {
  980. parg->fetch_size.fn = get_fetch_size_function(parg->type,
  981. parg->fetch.fn);
  982. parg->fetch_size.data = parg->fetch.data;
  983. }
  984. return ret;
  985. }
  986. /* Return 1 if name is reserved or already used by another argument */
  987. static int conflict_field_name(const char *name,
  988. struct probe_arg *args, int narg)
  989. {
  990. int i;
  991. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  992. if (strcmp(reserved_field_names[i], name) == 0)
  993. return 1;
  994. for (i = 0; i < narg; i++)
  995. if (strcmp(args[i].name, name) == 0)
  996. return 1;
  997. return 0;
  998. }
  999. static int create_trace_probe(int argc, char **argv)
  1000. {
  1001. /*
  1002. * Argument syntax:
  1003. * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  1004. * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
  1005. * Fetch args:
  1006. * $retval : fetch return value
  1007. * $stack : fetch stack address
  1008. * $stackN : fetch Nth of stack (N:0-)
  1009. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  1010. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  1011. * %REG : fetch register REG
  1012. * Dereferencing memory fetch:
  1013. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  1014. * Alias name of args:
  1015. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  1016. * Type of args:
  1017. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  1018. */
  1019. struct trace_probe *tp;
  1020. int i, ret = 0;
  1021. int is_return = 0, is_delete = 0;
  1022. char *symbol = NULL, *event = NULL, *group = NULL;
  1023. char *arg;
  1024. unsigned long offset = 0;
  1025. void *addr = NULL;
  1026. char buf[MAX_EVENT_NAME_LEN];
  1027. /* argc must be >= 1 */
  1028. if (argv[0][0] == 'p')
  1029. is_return = 0;
  1030. else if (argv[0][0] == 'r')
  1031. is_return = 1;
  1032. else if (argv[0][0] == '-')
  1033. is_delete = 1;
  1034. else {
  1035. pr_info("Probe definition must be started with 'p', 'r' or"
  1036. " '-'.\n");
  1037. return -EINVAL;
  1038. }
  1039. if (argv[0][1] == ':') {
  1040. event = &argv[0][2];
  1041. if (strchr(event, '/')) {
  1042. group = event;
  1043. event = strchr(group, '/') + 1;
  1044. event[-1] = '\0';
  1045. if (strlen(group) == 0) {
  1046. pr_info("Group name is not specified\n");
  1047. return -EINVAL;
  1048. }
  1049. }
  1050. if (strlen(event) == 0) {
  1051. pr_info("Event name is not specified\n");
  1052. return -EINVAL;
  1053. }
  1054. }
  1055. if (!group)
  1056. group = KPROBE_EVENT_SYSTEM;
  1057. if (is_delete) {
  1058. if (!event) {
  1059. pr_info("Delete command needs an event name.\n");
  1060. return -EINVAL;
  1061. }
  1062. mutex_lock(&probe_lock);
  1063. tp = find_trace_probe(event, group);
  1064. if (!tp) {
  1065. mutex_unlock(&probe_lock);
  1066. pr_info("Event %s/%s doesn't exist.\n", group, event);
  1067. return -ENOENT;
  1068. }
  1069. /* delete an event */
  1070. unregister_trace_probe(tp);
  1071. free_trace_probe(tp);
  1072. mutex_unlock(&probe_lock);
  1073. return 0;
  1074. }
  1075. if (argc < 2) {
  1076. pr_info("Probe point is not specified.\n");
  1077. return -EINVAL;
  1078. }
  1079. if (isdigit(argv[1][0])) {
  1080. if (is_return) {
  1081. pr_info("Return probe point must be a symbol.\n");
  1082. return -EINVAL;
  1083. }
  1084. /* an address specified */
  1085. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  1086. if (ret) {
  1087. pr_info("Failed to parse address.\n");
  1088. return ret;
  1089. }
  1090. } else {
  1091. /* a symbol specified */
  1092. symbol = argv[1];
  1093. /* TODO: support .init module functions */
  1094. ret = split_symbol_offset(symbol, &offset);
  1095. if (ret) {
  1096. pr_info("Failed to parse symbol.\n");
  1097. return ret;
  1098. }
  1099. if (offset && is_return) {
  1100. pr_info("Return probe must be used without offset.\n");
  1101. return -EINVAL;
  1102. }
  1103. }
  1104. argc -= 2; argv += 2;
  1105. /* setup a probe */
  1106. if (!event) {
  1107. /* Make a new event name */
  1108. if (symbol)
  1109. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  1110. is_return ? 'r' : 'p', symbol, offset);
  1111. else
  1112. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  1113. is_return ? 'r' : 'p', addr);
  1114. event = buf;
  1115. }
  1116. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  1117. is_return);
  1118. if (IS_ERR(tp)) {
  1119. pr_info("Failed to allocate trace_probe.(%d)\n",
  1120. (int)PTR_ERR(tp));
  1121. return PTR_ERR(tp);
  1122. }
  1123. /* parse arguments */
  1124. ret = 0;
  1125. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  1126. /* Increment count for freeing args in error case */
  1127. tp->nr_args++;
  1128. /* Parse argument name */
  1129. arg = strchr(argv[i], '=');
  1130. if (arg) {
  1131. *arg++ = '\0';
  1132. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  1133. } else {
  1134. arg = argv[i];
  1135. /* If argument name is omitted, set "argN" */
  1136. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  1137. tp->args[i].name = kstrdup(buf, GFP_KERNEL);
  1138. }
  1139. if (!tp->args[i].name) {
  1140. pr_info("Failed to allocate argument[%d] name.\n", i);
  1141. ret = -ENOMEM;
  1142. goto error;
  1143. }
  1144. if (!is_good_name(tp->args[i].name)) {
  1145. pr_info("Invalid argument[%d] name: %s\n",
  1146. i, tp->args[i].name);
  1147. ret = -EINVAL;
  1148. goto error;
  1149. }
  1150. if (conflict_field_name(tp->args[i].name, tp->args, i)) {
  1151. pr_info("Argument[%d] name '%s' conflicts with "
  1152. "another field.\n", i, argv[i]);
  1153. ret = -EINVAL;
  1154. goto error;
  1155. }
  1156. /* Parse fetch argument */
  1157. ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
  1158. if (ret) {
  1159. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  1160. goto error;
  1161. }
  1162. }
  1163. ret = register_trace_probe(tp);
  1164. if (ret)
  1165. goto error;
  1166. return 0;
  1167. error:
  1168. free_trace_probe(tp);
  1169. return ret;
  1170. }
  1171. static void release_all_trace_probes(void)
  1172. {
  1173. struct trace_probe *tp;
  1174. mutex_lock(&probe_lock);
  1175. /* TODO: Use batch unregistration */
  1176. while (!list_empty(&probe_list)) {
  1177. tp = list_entry(probe_list.next, struct trace_probe, list);
  1178. unregister_trace_probe(tp);
  1179. free_trace_probe(tp);
  1180. }
  1181. mutex_unlock(&probe_lock);
  1182. }
  1183. /* Probes listing interfaces */
  1184. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  1185. {
  1186. mutex_lock(&probe_lock);
  1187. return seq_list_start(&probe_list, *pos);
  1188. }
  1189. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1190. {
  1191. return seq_list_next(v, &probe_list, pos);
  1192. }
  1193. static void probes_seq_stop(struct seq_file *m, void *v)
  1194. {
  1195. mutex_unlock(&probe_lock);
  1196. }
  1197. static int probes_seq_show(struct seq_file *m, void *v)
  1198. {
  1199. struct trace_probe *tp = v;
  1200. int i;
  1201. seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p');
  1202. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  1203. if (!tp->symbol)
  1204. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  1205. else if (tp->rp.kp.offset)
  1206. seq_printf(m, " %s+%u", trace_probe_symbol(tp),
  1207. tp->rp.kp.offset);
  1208. else
  1209. seq_printf(m, " %s", trace_probe_symbol(tp));
  1210. for (i = 0; i < tp->nr_args; i++)
  1211. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  1212. seq_printf(m, "\n");
  1213. return 0;
  1214. }
  1215. static const struct seq_operations probes_seq_op = {
  1216. .start = probes_seq_start,
  1217. .next = probes_seq_next,
  1218. .stop = probes_seq_stop,
  1219. .show = probes_seq_show
  1220. };
  1221. static int probes_open(struct inode *inode, struct file *file)
  1222. {
  1223. if ((file->f_mode & FMODE_WRITE) &&
  1224. (file->f_flags & O_TRUNC))
  1225. release_all_trace_probes();
  1226. return seq_open(file, &probes_seq_op);
  1227. }
  1228. static int command_trace_probe(const char *buf)
  1229. {
  1230. char **argv;
  1231. int argc = 0, ret = 0;
  1232. argv = argv_split(GFP_KERNEL, buf, &argc);
  1233. if (!argv)
  1234. return -ENOMEM;
  1235. if (argc)
  1236. ret = create_trace_probe(argc, argv);
  1237. argv_free(argv);
  1238. return ret;
  1239. }
  1240. #define WRITE_BUFSIZE 4096
  1241. static ssize_t probes_write(struct file *file, const char __user *buffer,
  1242. size_t count, loff_t *ppos)
  1243. {
  1244. char *kbuf, *tmp;
  1245. int ret;
  1246. size_t done;
  1247. size_t size;
  1248. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  1249. if (!kbuf)
  1250. return -ENOMEM;
  1251. ret = done = 0;
  1252. while (done < count) {
  1253. size = count - done;
  1254. if (size >= WRITE_BUFSIZE)
  1255. size = WRITE_BUFSIZE - 1;
  1256. if (copy_from_user(kbuf, buffer + done, size)) {
  1257. ret = -EFAULT;
  1258. goto out;
  1259. }
  1260. kbuf[size] = '\0';
  1261. tmp = strchr(kbuf, '\n');
  1262. if (tmp) {
  1263. *tmp = '\0';
  1264. size = tmp - kbuf + 1;
  1265. } else if (done + size < count) {
  1266. pr_warning("Line length is too long: "
  1267. "Should be less than %d.", WRITE_BUFSIZE);
  1268. ret = -EINVAL;
  1269. goto out;
  1270. }
  1271. done += size;
  1272. /* Remove comments */
  1273. tmp = strchr(kbuf, '#');
  1274. if (tmp)
  1275. *tmp = '\0';
  1276. ret = command_trace_probe(kbuf);
  1277. if (ret)
  1278. goto out;
  1279. }
  1280. ret = done;
  1281. out:
  1282. kfree(kbuf);
  1283. return ret;
  1284. }
  1285. static const struct file_operations kprobe_events_ops = {
  1286. .owner = THIS_MODULE,
  1287. .open = probes_open,
  1288. .read = seq_read,
  1289. .llseek = seq_lseek,
  1290. .release = seq_release,
  1291. .write = probes_write,
  1292. };
  1293. /* Probes profiling interfaces */
  1294. static int probes_profile_seq_show(struct seq_file *m, void *v)
  1295. {
  1296. struct trace_probe *tp = v;
  1297. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  1298. tp->rp.kp.nmissed);
  1299. return 0;
  1300. }
  1301. static const struct seq_operations profile_seq_op = {
  1302. .start = probes_seq_start,
  1303. .next = probes_seq_next,
  1304. .stop = probes_seq_stop,
  1305. .show = probes_profile_seq_show
  1306. };
  1307. static int profile_open(struct inode *inode, struct file *file)
  1308. {
  1309. return seq_open(file, &profile_seq_op);
  1310. }
  1311. static const struct file_operations kprobe_profile_ops = {
  1312. .owner = THIS_MODULE,
  1313. .open = profile_open,
  1314. .read = seq_read,
  1315. .llseek = seq_lseek,
  1316. .release = seq_release,
  1317. };
  1318. /* Sum up total data length for dynamic arraies (strings) */
  1319. static __kprobes int __get_data_size(struct trace_probe *tp,
  1320. struct pt_regs *regs)
  1321. {
  1322. int i, ret = 0;
  1323. u32 len;
  1324. for (i = 0; i < tp->nr_args; i++)
  1325. if (unlikely(tp->args[i].fetch_size.fn)) {
  1326. call_fetch(&tp->args[i].fetch_size, regs, &len);
  1327. ret += len;
  1328. }
  1329. return ret;
  1330. }
  1331. /* Store the value of each argument */
  1332. static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
  1333. struct pt_regs *regs,
  1334. u8 *data, int maxlen)
  1335. {
  1336. int i;
  1337. u32 end = tp->size;
  1338. u32 *dl; /* Data (relative) location */
  1339. for (i = 0; i < tp->nr_args; i++) {
  1340. if (unlikely(tp->args[i].fetch_size.fn)) {
  1341. /*
  1342. * First, we set the relative location and
  1343. * maximum data length to *dl
  1344. */
  1345. dl = (u32 *)(data + tp->args[i].offset);
  1346. *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
  1347. /* Then try to fetch string or dynamic array data */
  1348. call_fetch(&tp->args[i].fetch, regs, dl);
  1349. /* Reduce maximum length */
  1350. end += get_rloc_len(*dl);
  1351. maxlen -= get_rloc_len(*dl);
  1352. /* Trick here, convert data_rloc to data_loc */
  1353. *dl = convert_rloc_to_loc(*dl,
  1354. ent_size + tp->args[i].offset);
  1355. } else
  1356. /* Just fetching data normally */
  1357. call_fetch(&tp->args[i].fetch, regs,
  1358. data + tp->args[i].offset);
  1359. }
  1360. }
  1361. /* Kprobe handler */
  1362. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  1363. {
  1364. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1365. struct kprobe_trace_entry_head *entry;
  1366. struct ring_buffer_event *event;
  1367. struct ring_buffer *buffer;
  1368. int size, dsize, pc;
  1369. unsigned long irq_flags;
  1370. struct ftrace_event_call *call = &tp->call;
  1371. tp->nhit++;
  1372. local_save_flags(irq_flags);
  1373. pc = preempt_count();
  1374. dsize = __get_data_size(tp, regs);
  1375. size = sizeof(*entry) + tp->size + dsize;
  1376. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1377. size, irq_flags, pc);
  1378. if (!event)
  1379. return;
  1380. entry = ring_buffer_event_data(event);
  1381. entry->ip = (unsigned long)kp->addr;
  1382. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1383. if (!filter_current_check_discard(buffer, call, entry, event))
  1384. trace_nowake_buffer_unlock_commit_regs(buffer, event,
  1385. irq_flags, pc, regs);
  1386. }
  1387. /* Kretprobe handler */
  1388. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  1389. struct pt_regs *regs)
  1390. {
  1391. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1392. struct kretprobe_trace_entry_head *entry;
  1393. struct ring_buffer_event *event;
  1394. struct ring_buffer *buffer;
  1395. int size, pc, dsize;
  1396. unsigned long irq_flags;
  1397. struct ftrace_event_call *call = &tp->call;
  1398. local_save_flags(irq_flags);
  1399. pc = preempt_count();
  1400. dsize = __get_data_size(tp, regs);
  1401. size = sizeof(*entry) + tp->size + dsize;
  1402. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1403. size, irq_flags, pc);
  1404. if (!event)
  1405. return;
  1406. entry = ring_buffer_event_data(event);
  1407. entry->func = (unsigned long)tp->rp.kp.addr;
  1408. entry->ret_ip = (unsigned long)ri->ret_addr;
  1409. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1410. if (!filter_current_check_discard(buffer, call, entry, event))
  1411. trace_nowake_buffer_unlock_commit_regs(buffer, event,
  1412. irq_flags, pc, regs);
  1413. }
  1414. /* Event entry printers */
  1415. enum print_line_t
  1416. print_kprobe_event(struct trace_iterator *iter, int flags,
  1417. struct trace_event *event)
  1418. {
  1419. struct kprobe_trace_entry_head *field;
  1420. struct trace_seq *s = &iter->seq;
  1421. struct trace_probe *tp;
  1422. u8 *data;
  1423. int i;
  1424. field = (struct kprobe_trace_entry_head *)iter->ent;
  1425. tp = container_of(event, struct trace_probe, call.event);
  1426. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1427. goto partial;
  1428. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  1429. goto partial;
  1430. if (!trace_seq_puts(s, ")"))
  1431. goto partial;
  1432. data = (u8 *)&field[1];
  1433. for (i = 0; i < tp->nr_args; i++)
  1434. if (!tp->args[i].type->print(s, tp->args[i].name,
  1435. data + tp->args[i].offset, field))
  1436. goto partial;
  1437. if (!trace_seq_puts(s, "\n"))
  1438. goto partial;
  1439. return TRACE_TYPE_HANDLED;
  1440. partial:
  1441. return TRACE_TYPE_PARTIAL_LINE;
  1442. }
  1443. enum print_line_t
  1444. print_kretprobe_event(struct trace_iterator *iter, int flags,
  1445. struct trace_event *event)
  1446. {
  1447. struct kretprobe_trace_entry_head *field;
  1448. struct trace_seq *s = &iter->seq;
  1449. struct trace_probe *tp;
  1450. u8 *data;
  1451. int i;
  1452. field = (struct kretprobe_trace_entry_head *)iter->ent;
  1453. tp = container_of(event, struct trace_probe, call.event);
  1454. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1455. goto partial;
  1456. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  1457. goto partial;
  1458. if (!trace_seq_puts(s, " <- "))
  1459. goto partial;
  1460. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  1461. goto partial;
  1462. if (!trace_seq_puts(s, ")"))
  1463. goto partial;
  1464. data = (u8 *)&field[1];
  1465. for (i = 0; i < tp->nr_args; i++)
  1466. if (!tp->args[i].type->print(s, tp->args[i].name,
  1467. data + tp->args[i].offset, field))
  1468. goto partial;
  1469. if (!trace_seq_puts(s, "\n"))
  1470. goto partial;
  1471. return TRACE_TYPE_HANDLED;
  1472. partial:
  1473. return TRACE_TYPE_PARTIAL_LINE;
  1474. }
  1475. #undef DEFINE_FIELD
  1476. #define DEFINE_FIELD(type, item, name, is_signed) \
  1477. do { \
  1478. ret = trace_define_field(event_call, #type, name, \
  1479. offsetof(typeof(field), item), \
  1480. sizeof(field.item), is_signed, \
  1481. FILTER_OTHER); \
  1482. if (ret) \
  1483. return ret; \
  1484. } while (0)
  1485. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  1486. {
  1487. int ret, i;
  1488. struct kprobe_trace_entry_head field;
  1489. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1490. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  1491. /* Set argument names as fields */
  1492. for (i = 0; i < tp->nr_args; i++) {
  1493. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1494. tp->args[i].name,
  1495. sizeof(field) + tp->args[i].offset,
  1496. tp->args[i].type->size,
  1497. tp->args[i].type->is_signed,
  1498. FILTER_OTHER);
  1499. if (ret)
  1500. return ret;
  1501. }
  1502. return 0;
  1503. }
  1504. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  1505. {
  1506. int ret, i;
  1507. struct kretprobe_trace_entry_head field;
  1508. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1509. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1510. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1511. /* Set argument names as fields */
  1512. for (i = 0; i < tp->nr_args; i++) {
  1513. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1514. tp->args[i].name,
  1515. sizeof(field) + tp->args[i].offset,
  1516. tp->args[i].type->size,
  1517. tp->args[i].type->is_signed,
  1518. FILTER_OTHER);
  1519. if (ret)
  1520. return ret;
  1521. }
  1522. return 0;
  1523. }
  1524. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  1525. {
  1526. int i;
  1527. int pos = 0;
  1528. const char *fmt, *arg;
  1529. if (!trace_probe_is_return(tp)) {
  1530. fmt = "(%lx)";
  1531. arg = "REC->" FIELD_STRING_IP;
  1532. } else {
  1533. fmt = "(%lx <- %lx)";
  1534. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1535. }
  1536. /* When len=0, we just calculate the needed length */
  1537. #define LEN_OR_ZERO (len ? len - pos : 0)
  1538. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1539. for (i = 0; i < tp->nr_args; i++) {
  1540. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  1541. tp->args[i].name, tp->args[i].type->fmt);
  1542. }
  1543. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  1544. for (i = 0; i < tp->nr_args; i++) {
  1545. if (strcmp(tp->args[i].type->name, "string") == 0)
  1546. pos += snprintf(buf + pos, LEN_OR_ZERO,
  1547. ", __get_str(%s)",
  1548. tp->args[i].name);
  1549. else
  1550. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  1551. tp->args[i].name);
  1552. }
  1553. #undef LEN_OR_ZERO
  1554. /* return the length of print_fmt */
  1555. return pos;
  1556. }
  1557. static int set_print_fmt(struct trace_probe *tp)
  1558. {
  1559. int len;
  1560. char *print_fmt;
  1561. /* First: called with 0 length to calculate the needed length */
  1562. len = __set_print_fmt(tp, NULL, 0);
  1563. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1564. if (!print_fmt)
  1565. return -ENOMEM;
  1566. /* Second: actually write the @print_fmt */
  1567. __set_print_fmt(tp, print_fmt, len + 1);
  1568. tp->call.print_fmt = print_fmt;
  1569. return 0;
  1570. }
  1571. #ifdef CONFIG_PERF_EVENTS
  1572. /* Kprobe profile handler */
  1573. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  1574. struct pt_regs *regs)
  1575. {
  1576. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1577. struct ftrace_event_call *call = &tp->call;
  1578. struct kprobe_trace_entry_head *entry;
  1579. struct hlist_head *head;
  1580. int size, __size, dsize;
  1581. int rctx;
  1582. dsize = __get_data_size(tp, regs);
  1583. __size = sizeof(*entry) + tp->size + dsize;
  1584. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1585. size -= sizeof(u32);
  1586. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1587. "profile buffer not large enough"))
  1588. return;
  1589. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1590. if (!entry)
  1591. return;
  1592. entry->ip = (unsigned long)kp->addr;
  1593. memset(&entry[1], 0, dsize);
  1594. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1595. head = this_cpu_ptr(call->perf_events);
  1596. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
  1597. }
  1598. /* Kretprobe profile handler */
  1599. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1600. struct pt_regs *regs)
  1601. {
  1602. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1603. struct ftrace_event_call *call = &tp->call;
  1604. struct kretprobe_trace_entry_head *entry;
  1605. struct hlist_head *head;
  1606. int size, __size, dsize;
  1607. int rctx;
  1608. dsize = __get_data_size(tp, regs);
  1609. __size = sizeof(*entry) + tp->size + dsize;
  1610. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1611. size -= sizeof(u32);
  1612. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1613. "profile buffer not large enough"))
  1614. return;
  1615. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1616. if (!entry)
  1617. return;
  1618. entry->func = (unsigned long)tp->rp.kp.addr;
  1619. entry->ret_ip = (unsigned long)ri->ret_addr;
  1620. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1621. head = this_cpu_ptr(call->perf_events);
  1622. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
  1623. }
  1624. #endif /* CONFIG_PERF_EVENTS */
  1625. static __kprobes
  1626. int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
  1627. {
  1628. struct trace_probe *tp = (struct trace_probe *)event->data;
  1629. switch (type) {
  1630. case TRACE_REG_REGISTER:
  1631. return enable_trace_probe(tp, TP_FLAG_TRACE);
  1632. case TRACE_REG_UNREGISTER:
  1633. disable_trace_probe(tp, TP_FLAG_TRACE);
  1634. return 0;
  1635. #ifdef CONFIG_PERF_EVENTS
  1636. case TRACE_REG_PERF_REGISTER:
  1637. return enable_trace_probe(tp, TP_FLAG_PROFILE);
  1638. case TRACE_REG_PERF_UNREGISTER:
  1639. disable_trace_probe(tp, TP_FLAG_PROFILE);
  1640. return 0;
  1641. #endif
  1642. }
  1643. return 0;
  1644. }
  1645. static __kprobes
  1646. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1647. {
  1648. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1649. if (tp->flags & TP_FLAG_TRACE)
  1650. kprobe_trace_func(kp, regs);
  1651. #ifdef CONFIG_PERF_EVENTS
  1652. if (tp->flags & TP_FLAG_PROFILE)
  1653. kprobe_perf_func(kp, regs);
  1654. #endif
  1655. return 0; /* We don't tweek kernel, so just return 0 */
  1656. }
  1657. static __kprobes
  1658. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1659. {
  1660. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1661. if (tp->flags & TP_FLAG_TRACE)
  1662. kretprobe_trace_func(ri, regs);
  1663. #ifdef CONFIG_PERF_EVENTS
  1664. if (tp->flags & TP_FLAG_PROFILE)
  1665. kretprobe_perf_func(ri, regs);
  1666. #endif
  1667. return 0; /* We don't tweek kernel, so just return 0 */
  1668. }
  1669. static struct trace_event_functions kretprobe_funcs = {
  1670. .trace = print_kretprobe_event
  1671. };
  1672. static struct trace_event_functions kprobe_funcs = {
  1673. .trace = print_kprobe_event
  1674. };
  1675. static int register_probe_event(struct trace_probe *tp)
  1676. {
  1677. struct ftrace_event_call *call = &tp->call;
  1678. int ret;
  1679. /* Initialize ftrace_event_call */
  1680. INIT_LIST_HEAD(&call->class->fields);
  1681. if (trace_probe_is_return(tp)) {
  1682. call->event.funcs = &kretprobe_funcs;
  1683. call->class->define_fields = kretprobe_event_define_fields;
  1684. } else {
  1685. call->event.funcs = &kprobe_funcs;
  1686. call->class->define_fields = kprobe_event_define_fields;
  1687. }
  1688. if (set_print_fmt(tp) < 0)
  1689. return -ENOMEM;
  1690. ret = register_ftrace_event(&call->event);
  1691. if (!ret) {
  1692. kfree(call->print_fmt);
  1693. return -ENODEV;
  1694. }
  1695. call->flags = 0;
  1696. call->class->reg = kprobe_register;
  1697. call->data = tp;
  1698. ret = trace_add_event_call(call);
  1699. if (ret) {
  1700. pr_info("Failed to register kprobe event: %s\n", call->name);
  1701. kfree(call->print_fmt);
  1702. unregister_ftrace_event(&call->event);
  1703. }
  1704. return ret;
  1705. }
  1706. static void unregister_probe_event(struct trace_probe *tp)
  1707. {
  1708. /* tp->event is unregistered in trace_remove_event_call() */
  1709. trace_remove_event_call(&tp->call);
  1710. kfree(tp->call.print_fmt);
  1711. }
  1712. /* Make a debugfs interface for controlling probe points */
  1713. static __init int init_kprobe_trace(void)
  1714. {
  1715. struct dentry *d_tracer;
  1716. struct dentry *entry;
  1717. if (register_module_notifier(&trace_probe_module_nb))
  1718. return -EINVAL;
  1719. d_tracer = tracing_init_dentry();
  1720. if (!d_tracer)
  1721. return 0;
  1722. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1723. NULL, &kprobe_events_ops);
  1724. /* Event list interface */
  1725. if (!entry)
  1726. pr_warning("Could not create debugfs "
  1727. "'kprobe_events' entry\n");
  1728. /* Profile interface */
  1729. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1730. NULL, &kprobe_profile_ops);
  1731. if (!entry)
  1732. pr_warning("Could not create debugfs "
  1733. "'kprobe_profile' entry\n");
  1734. return 0;
  1735. }
  1736. fs_initcall(init_kprobe_trace);
  1737. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1738. /*
  1739. * The "__used" keeps gcc from removing the function symbol
  1740. * from the kallsyms table.
  1741. */
  1742. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1743. int a4, int a5, int a6)
  1744. {
  1745. return a1 + a2 + a3 + a4 + a5 + a6;
  1746. }
  1747. static __init int kprobe_trace_self_tests_init(void)
  1748. {
  1749. int ret, warn = 0;
  1750. int (*target)(int, int, int, int, int, int);
  1751. struct trace_probe *tp;
  1752. target = kprobe_trace_selftest_target;
  1753. pr_info("Testing kprobe tracing: ");
  1754. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1755. "$stack $stack0 +0($stack)");
  1756. if (WARN_ON_ONCE(ret)) {
  1757. pr_warning("error on probing function entry.\n");
  1758. warn++;
  1759. } else {
  1760. /* Enable trace point */
  1761. tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
  1762. if (WARN_ON_ONCE(tp == NULL)) {
  1763. pr_warning("error on getting new probe.\n");
  1764. warn++;
  1765. } else
  1766. enable_trace_probe(tp, TP_FLAG_TRACE);
  1767. }
  1768. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1769. "$retval");
  1770. if (WARN_ON_ONCE(ret)) {
  1771. pr_warning("error on probing function return.\n");
  1772. warn++;
  1773. } else {
  1774. /* Enable trace point */
  1775. tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
  1776. if (WARN_ON_ONCE(tp == NULL)) {
  1777. pr_warning("error on getting new probe.\n");
  1778. warn++;
  1779. } else
  1780. enable_trace_probe(tp, TP_FLAG_TRACE);
  1781. }
  1782. if (warn)
  1783. goto end;
  1784. ret = target(1, 2, 3, 4, 5, 6);
  1785. ret = command_trace_probe("-:testprobe");
  1786. if (WARN_ON_ONCE(ret)) {
  1787. pr_warning("error on deleting a probe.\n");
  1788. warn++;
  1789. }
  1790. ret = command_trace_probe("-:testprobe2");
  1791. if (WARN_ON_ONCE(ret)) {
  1792. pr_warning("error on deleting a probe.\n");
  1793. warn++;
  1794. }
  1795. end:
  1796. release_all_trace_probes();
  1797. if (warn)
  1798. pr_cont("NG: Some tests are failed. Please check them.\n");
  1799. else
  1800. pr_cont("OK\n");
  1801. return 0;
  1802. }
  1803. late_initcall(kprobe_trace_self_tests_init);
  1804. #endif