trace_kprobe.c 45 KB

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