trace_kprobe.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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. tp = find_probe_event(event, group);
  829. if (!tp) {
  830. pr_info("Event %s/%s doesn't exist.\n", group, event);
  831. return -ENOENT;
  832. }
  833. /* delete an event */
  834. unregister_trace_probe(tp);
  835. free_trace_probe(tp);
  836. return 0;
  837. }
  838. if (argc < 2) {
  839. pr_info("Probe point is not specified.\n");
  840. return -EINVAL;
  841. }
  842. if (isdigit(argv[1][0])) {
  843. if (is_return) {
  844. pr_info("Return probe point must be a symbol.\n");
  845. return -EINVAL;
  846. }
  847. /* an address specified */
  848. ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr);
  849. if (ret) {
  850. pr_info("Failed to parse address.\n");
  851. return ret;
  852. }
  853. } else {
  854. /* a symbol specified */
  855. symbol = argv[1];
  856. /* TODO: support .init module functions */
  857. ret = split_symbol_offset(symbol, &offset);
  858. if (ret) {
  859. pr_info("Failed to parse symbol.\n");
  860. return ret;
  861. }
  862. if (offset && is_return) {
  863. pr_info("Return probe must be used without offset.\n");
  864. return -EINVAL;
  865. }
  866. }
  867. argc -= 2; argv += 2;
  868. /* setup a probe */
  869. if (!event) {
  870. /* Make a new event name */
  871. if (symbol)
  872. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  873. is_return ? 'r' : 'p', symbol, offset);
  874. else
  875. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  876. is_return ? 'r' : 'p', addr);
  877. event = buf;
  878. }
  879. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  880. is_return);
  881. if (IS_ERR(tp)) {
  882. pr_info("Failed to allocate trace_probe.(%d)\n",
  883. (int)PTR_ERR(tp));
  884. return PTR_ERR(tp);
  885. }
  886. /* parse arguments */
  887. ret = 0;
  888. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  889. /* Parse argument name */
  890. arg = strchr(argv[i], '=');
  891. if (arg)
  892. *arg++ = '\0';
  893. else
  894. arg = argv[i];
  895. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  896. if (!tp->args[i].name) {
  897. pr_info("Failed to allocate argument%d name '%s'.\n",
  898. i, argv[i]);
  899. ret = -ENOMEM;
  900. goto error;
  901. }
  902. tmp = strchr(tp->args[i].name, ':');
  903. if (tmp)
  904. *tmp = '_'; /* convert : to _ */
  905. if (conflict_field_name(tp->args[i].name, tp->args, i)) {
  906. pr_info("Argument%d name '%s' conflicts with "
  907. "another field.\n", i, argv[i]);
  908. ret = -EINVAL;
  909. goto error;
  910. }
  911. /* Parse fetch argument */
  912. ret = parse_probe_arg(arg, tp, &tp->args[i], is_return);
  913. if (ret) {
  914. pr_info("Parse error at argument%d. (%d)\n", i, ret);
  915. kfree(tp->args[i].name);
  916. goto error;
  917. }
  918. tp->nr_args++;
  919. }
  920. ret = register_trace_probe(tp);
  921. if (ret)
  922. goto error;
  923. return 0;
  924. error:
  925. free_trace_probe(tp);
  926. return ret;
  927. }
  928. static void cleanup_all_probes(void)
  929. {
  930. struct trace_probe *tp;
  931. mutex_lock(&probe_lock);
  932. /* TODO: Use batch unregistration */
  933. while (!list_empty(&probe_list)) {
  934. tp = list_entry(probe_list.next, struct trace_probe, list);
  935. unregister_trace_probe(tp);
  936. free_trace_probe(tp);
  937. }
  938. mutex_unlock(&probe_lock);
  939. }
  940. /* Probes listing interfaces */
  941. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  942. {
  943. mutex_lock(&probe_lock);
  944. return seq_list_start(&probe_list, *pos);
  945. }
  946. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  947. {
  948. return seq_list_next(v, &probe_list, pos);
  949. }
  950. static void probes_seq_stop(struct seq_file *m, void *v)
  951. {
  952. mutex_unlock(&probe_lock);
  953. }
  954. static int probes_seq_show(struct seq_file *m, void *v)
  955. {
  956. struct trace_probe *tp = v;
  957. int i;
  958. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  959. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  960. if (!tp->symbol)
  961. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  962. else if (tp->rp.kp.offset)
  963. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  964. else
  965. seq_printf(m, " %s", probe_symbol(tp));
  966. for (i = 0; i < tp->nr_args; i++)
  967. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  968. seq_printf(m, "\n");
  969. return 0;
  970. }
  971. static const struct seq_operations probes_seq_op = {
  972. .start = probes_seq_start,
  973. .next = probes_seq_next,
  974. .stop = probes_seq_stop,
  975. .show = probes_seq_show
  976. };
  977. static int probes_open(struct inode *inode, struct file *file)
  978. {
  979. if ((file->f_mode & FMODE_WRITE) &&
  980. (file->f_flags & O_TRUNC))
  981. cleanup_all_probes();
  982. return seq_open(file, &probes_seq_op);
  983. }
  984. static int command_trace_probe(const char *buf)
  985. {
  986. char **argv;
  987. int argc = 0, ret = 0;
  988. argv = argv_split(GFP_KERNEL, buf, &argc);
  989. if (!argv)
  990. return -ENOMEM;
  991. if (argc)
  992. ret = create_trace_probe(argc, argv);
  993. argv_free(argv);
  994. return ret;
  995. }
  996. #define WRITE_BUFSIZE 128
  997. static ssize_t probes_write(struct file *file, const char __user *buffer,
  998. size_t count, loff_t *ppos)
  999. {
  1000. char *kbuf, *tmp;
  1001. int ret;
  1002. size_t done;
  1003. size_t size;
  1004. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  1005. if (!kbuf)
  1006. return -ENOMEM;
  1007. ret = done = 0;
  1008. while (done < count) {
  1009. size = count - done;
  1010. if (size >= WRITE_BUFSIZE)
  1011. size = WRITE_BUFSIZE - 1;
  1012. if (copy_from_user(kbuf, buffer + done, size)) {
  1013. ret = -EFAULT;
  1014. goto out;
  1015. }
  1016. kbuf[size] = '\0';
  1017. tmp = strchr(kbuf, '\n');
  1018. if (tmp) {
  1019. *tmp = '\0';
  1020. size = tmp - kbuf + 1;
  1021. } else if (done + size < count) {
  1022. pr_warning("Line length is too long: "
  1023. "Should be less than %d.", WRITE_BUFSIZE);
  1024. ret = -EINVAL;
  1025. goto out;
  1026. }
  1027. done += size;
  1028. /* Remove comments */
  1029. tmp = strchr(kbuf, '#');
  1030. if (tmp)
  1031. *tmp = '\0';
  1032. ret = command_trace_probe(kbuf);
  1033. if (ret)
  1034. goto out;
  1035. }
  1036. ret = done;
  1037. out:
  1038. kfree(kbuf);
  1039. return ret;
  1040. }
  1041. static const struct file_operations kprobe_events_ops = {
  1042. .owner = THIS_MODULE,
  1043. .open = probes_open,
  1044. .read = seq_read,
  1045. .llseek = seq_lseek,
  1046. .release = seq_release,
  1047. .write = probes_write,
  1048. };
  1049. /* Probes profiling interfaces */
  1050. static int probes_profile_seq_show(struct seq_file *m, void *v)
  1051. {
  1052. struct trace_probe *tp = v;
  1053. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  1054. tp->rp.kp.nmissed);
  1055. return 0;
  1056. }
  1057. static const struct seq_operations profile_seq_op = {
  1058. .start = probes_seq_start,
  1059. .next = probes_seq_next,
  1060. .stop = probes_seq_stop,
  1061. .show = probes_profile_seq_show
  1062. };
  1063. static int profile_open(struct inode *inode, struct file *file)
  1064. {
  1065. return seq_open(file, &profile_seq_op);
  1066. }
  1067. static const struct file_operations kprobe_profile_ops = {
  1068. .owner = THIS_MODULE,
  1069. .open = profile_open,
  1070. .read = seq_read,
  1071. .llseek = seq_lseek,
  1072. .release = seq_release,
  1073. };
  1074. /* Sum up total data length for dynamic arraies (strings) */
  1075. static __kprobes int __get_data_size(struct trace_probe *tp,
  1076. struct pt_regs *regs)
  1077. {
  1078. int i, ret = 0;
  1079. u32 len;
  1080. for (i = 0; i < tp->nr_args; i++)
  1081. if (unlikely(tp->args[i].fetch_size.fn)) {
  1082. call_fetch(&tp->args[i].fetch_size, regs, &len);
  1083. ret += len;
  1084. }
  1085. return ret;
  1086. }
  1087. /* Store the value of each argument */
  1088. static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
  1089. struct pt_regs *regs,
  1090. u8 *data, int maxlen)
  1091. {
  1092. int i;
  1093. u32 end = tp->size;
  1094. u32 *dl; /* Data (relative) location */
  1095. for (i = 0; i < tp->nr_args; i++) {
  1096. if (unlikely(tp->args[i].fetch_size.fn)) {
  1097. /*
  1098. * First, we set the relative location and
  1099. * maximum data length to *dl
  1100. */
  1101. dl = (u32 *)(data + tp->args[i].offset);
  1102. *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
  1103. /* Then try to fetch string or dynamic array data */
  1104. call_fetch(&tp->args[i].fetch, regs, dl);
  1105. /* Reduce maximum length */
  1106. end += get_rloc_len(*dl);
  1107. maxlen -= get_rloc_len(*dl);
  1108. /* Trick here, convert data_rloc to data_loc */
  1109. *dl = convert_rloc_to_loc(*dl,
  1110. ent_size + tp->args[i].offset);
  1111. } else
  1112. /* Just fetching data normally */
  1113. call_fetch(&tp->args[i].fetch, regs,
  1114. data + tp->args[i].offset);
  1115. }
  1116. }
  1117. /* Kprobe handler */
  1118. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  1119. {
  1120. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1121. struct kprobe_trace_entry_head *entry;
  1122. struct ring_buffer_event *event;
  1123. struct ring_buffer *buffer;
  1124. int size, dsize, pc;
  1125. unsigned long irq_flags;
  1126. struct ftrace_event_call *call = &tp->call;
  1127. tp->nhit++;
  1128. local_save_flags(irq_flags);
  1129. pc = preempt_count();
  1130. dsize = __get_data_size(tp, regs);
  1131. size = sizeof(*entry) + tp->size + dsize;
  1132. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1133. size, irq_flags, pc);
  1134. if (!event)
  1135. return;
  1136. entry = ring_buffer_event_data(event);
  1137. entry->ip = (unsigned long)kp->addr;
  1138. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1139. if (!filter_current_check_discard(buffer, call, entry, event))
  1140. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  1141. }
  1142. /* Kretprobe handler */
  1143. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  1144. struct pt_regs *regs)
  1145. {
  1146. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1147. struct kretprobe_trace_entry_head *entry;
  1148. struct ring_buffer_event *event;
  1149. struct ring_buffer *buffer;
  1150. int size, pc, dsize;
  1151. unsigned long irq_flags;
  1152. struct ftrace_event_call *call = &tp->call;
  1153. local_save_flags(irq_flags);
  1154. pc = preempt_count();
  1155. dsize = __get_data_size(tp, regs);
  1156. size = sizeof(*entry) + tp->size + dsize;
  1157. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  1158. size, irq_flags, pc);
  1159. if (!event)
  1160. return;
  1161. entry = ring_buffer_event_data(event);
  1162. entry->func = (unsigned long)tp->rp.kp.addr;
  1163. entry->ret_ip = (unsigned long)ri->ret_addr;
  1164. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1165. if (!filter_current_check_discard(buffer, call, entry, event))
  1166. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  1167. }
  1168. /* Event entry printers */
  1169. enum print_line_t
  1170. print_kprobe_event(struct trace_iterator *iter, int flags,
  1171. struct trace_event *event)
  1172. {
  1173. struct kprobe_trace_entry_head *field;
  1174. struct trace_seq *s = &iter->seq;
  1175. struct trace_probe *tp;
  1176. u8 *data;
  1177. int i;
  1178. field = (struct kprobe_trace_entry_head *)iter->ent;
  1179. tp = container_of(event, struct trace_probe, call.event);
  1180. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1181. goto partial;
  1182. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  1183. goto partial;
  1184. if (!trace_seq_puts(s, ")"))
  1185. goto partial;
  1186. data = (u8 *)&field[1];
  1187. for (i = 0; i < tp->nr_args; i++)
  1188. if (!tp->args[i].type->print(s, tp->args[i].name,
  1189. data + tp->args[i].offset, field))
  1190. goto partial;
  1191. if (!trace_seq_puts(s, "\n"))
  1192. goto partial;
  1193. return TRACE_TYPE_HANDLED;
  1194. partial:
  1195. return TRACE_TYPE_PARTIAL_LINE;
  1196. }
  1197. enum print_line_t
  1198. print_kretprobe_event(struct trace_iterator *iter, int flags,
  1199. struct trace_event *event)
  1200. {
  1201. struct kretprobe_trace_entry_head *field;
  1202. struct trace_seq *s = &iter->seq;
  1203. struct trace_probe *tp;
  1204. u8 *data;
  1205. int i;
  1206. field = (struct kretprobe_trace_entry_head *)iter->ent;
  1207. tp = container_of(event, struct trace_probe, call.event);
  1208. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  1209. goto partial;
  1210. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  1211. goto partial;
  1212. if (!trace_seq_puts(s, " <- "))
  1213. goto partial;
  1214. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  1215. goto partial;
  1216. if (!trace_seq_puts(s, ")"))
  1217. goto partial;
  1218. data = (u8 *)&field[1];
  1219. for (i = 0; i < tp->nr_args; i++)
  1220. if (!tp->args[i].type->print(s, tp->args[i].name,
  1221. data + tp->args[i].offset, field))
  1222. goto partial;
  1223. if (!trace_seq_puts(s, "\n"))
  1224. goto partial;
  1225. return TRACE_TYPE_HANDLED;
  1226. partial:
  1227. return TRACE_TYPE_PARTIAL_LINE;
  1228. }
  1229. static int probe_event_enable(struct ftrace_event_call *call)
  1230. {
  1231. struct trace_probe *tp = (struct trace_probe *)call->data;
  1232. tp->flags |= TP_FLAG_TRACE;
  1233. if (probe_is_return(tp))
  1234. return enable_kretprobe(&tp->rp);
  1235. else
  1236. return enable_kprobe(&tp->rp.kp);
  1237. }
  1238. static void probe_event_disable(struct ftrace_event_call *call)
  1239. {
  1240. struct trace_probe *tp = (struct trace_probe *)call->data;
  1241. tp->flags &= ~TP_FLAG_TRACE;
  1242. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  1243. if (probe_is_return(tp))
  1244. disable_kretprobe(&tp->rp);
  1245. else
  1246. disable_kprobe(&tp->rp.kp);
  1247. }
  1248. }
  1249. #undef DEFINE_FIELD
  1250. #define DEFINE_FIELD(type, item, name, is_signed) \
  1251. do { \
  1252. ret = trace_define_field(event_call, #type, name, \
  1253. offsetof(typeof(field), item), \
  1254. sizeof(field.item), is_signed, \
  1255. FILTER_OTHER); \
  1256. if (ret) \
  1257. return ret; \
  1258. } while (0)
  1259. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  1260. {
  1261. int ret, i;
  1262. struct kprobe_trace_entry_head field;
  1263. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1264. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  1265. /* Set argument names as fields */
  1266. for (i = 0; i < tp->nr_args; i++) {
  1267. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1268. tp->args[i].name,
  1269. sizeof(field) + tp->args[i].offset,
  1270. tp->args[i].type->size,
  1271. tp->args[i].type->is_signed,
  1272. FILTER_OTHER);
  1273. if (ret)
  1274. return ret;
  1275. }
  1276. return 0;
  1277. }
  1278. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  1279. {
  1280. int ret, i;
  1281. struct kretprobe_trace_entry_head field;
  1282. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  1283. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  1284. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  1285. /* Set argument names as fields */
  1286. for (i = 0; i < tp->nr_args; i++) {
  1287. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  1288. tp->args[i].name,
  1289. sizeof(field) + tp->args[i].offset,
  1290. tp->args[i].type->size,
  1291. tp->args[i].type->is_signed,
  1292. FILTER_OTHER);
  1293. if (ret)
  1294. return ret;
  1295. }
  1296. return 0;
  1297. }
  1298. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  1299. {
  1300. int i;
  1301. int pos = 0;
  1302. const char *fmt, *arg;
  1303. if (!probe_is_return(tp)) {
  1304. fmt = "(%lx)";
  1305. arg = "REC->" FIELD_STRING_IP;
  1306. } else {
  1307. fmt = "(%lx <- %lx)";
  1308. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  1309. }
  1310. /* When len=0, we just calculate the needed length */
  1311. #define LEN_OR_ZERO (len ? len - pos : 0)
  1312. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  1313. for (i = 0; i < tp->nr_args; i++) {
  1314. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  1315. tp->args[i].name, tp->args[i].type->fmt);
  1316. }
  1317. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  1318. for (i = 0; i < tp->nr_args; i++) {
  1319. if (strcmp(tp->args[i].type->name, "string") == 0)
  1320. pos += snprintf(buf + pos, LEN_OR_ZERO,
  1321. ", __get_str(%s)",
  1322. tp->args[i].name);
  1323. else
  1324. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  1325. tp->args[i].name);
  1326. }
  1327. #undef LEN_OR_ZERO
  1328. /* return the length of print_fmt */
  1329. return pos;
  1330. }
  1331. static int set_print_fmt(struct trace_probe *tp)
  1332. {
  1333. int len;
  1334. char *print_fmt;
  1335. /* First: called with 0 length to calculate the needed length */
  1336. len = __set_print_fmt(tp, NULL, 0);
  1337. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  1338. if (!print_fmt)
  1339. return -ENOMEM;
  1340. /* Second: actually write the @print_fmt */
  1341. __set_print_fmt(tp, print_fmt, len + 1);
  1342. tp->call.print_fmt = print_fmt;
  1343. return 0;
  1344. }
  1345. #ifdef CONFIG_PERF_EVENTS
  1346. /* Kprobe profile handler */
  1347. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  1348. struct pt_regs *regs)
  1349. {
  1350. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1351. struct ftrace_event_call *call = &tp->call;
  1352. struct kprobe_trace_entry_head *entry;
  1353. struct hlist_head *head;
  1354. int size, __size, dsize;
  1355. int rctx;
  1356. dsize = __get_data_size(tp, regs);
  1357. __size = sizeof(*entry) + tp->size + dsize;
  1358. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1359. size -= sizeof(u32);
  1360. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1361. "profile buffer not large enough"))
  1362. return;
  1363. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1364. if (!entry)
  1365. return;
  1366. entry->ip = (unsigned long)kp->addr;
  1367. memset(&entry[1], 0, dsize);
  1368. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1369. head = this_cpu_ptr(call->perf_events);
  1370. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
  1371. }
  1372. /* Kretprobe profile handler */
  1373. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  1374. struct pt_regs *regs)
  1375. {
  1376. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1377. struct ftrace_event_call *call = &tp->call;
  1378. struct kretprobe_trace_entry_head *entry;
  1379. struct hlist_head *head;
  1380. int size, __size, dsize;
  1381. int rctx;
  1382. dsize = __get_data_size(tp, regs);
  1383. __size = sizeof(*entry) + tp->size + dsize;
  1384. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1385. size -= sizeof(u32);
  1386. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  1387. "profile buffer not large enough"))
  1388. return;
  1389. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  1390. if (!entry)
  1391. return;
  1392. entry->func = (unsigned long)tp->rp.kp.addr;
  1393. entry->ret_ip = (unsigned long)ri->ret_addr;
  1394. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  1395. head = this_cpu_ptr(call->perf_events);
  1396. perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head);
  1397. }
  1398. static int probe_perf_enable(struct ftrace_event_call *call)
  1399. {
  1400. struct trace_probe *tp = (struct trace_probe *)call->data;
  1401. tp->flags |= TP_FLAG_PROFILE;
  1402. if (probe_is_return(tp))
  1403. return enable_kretprobe(&tp->rp);
  1404. else
  1405. return enable_kprobe(&tp->rp.kp);
  1406. }
  1407. static void probe_perf_disable(struct ftrace_event_call *call)
  1408. {
  1409. struct trace_probe *tp = (struct trace_probe *)call->data;
  1410. tp->flags &= ~TP_FLAG_PROFILE;
  1411. if (!(tp->flags & TP_FLAG_TRACE)) {
  1412. if (probe_is_return(tp))
  1413. disable_kretprobe(&tp->rp);
  1414. else
  1415. disable_kprobe(&tp->rp.kp);
  1416. }
  1417. }
  1418. #endif /* CONFIG_PERF_EVENTS */
  1419. static __kprobes
  1420. int kprobe_register(struct ftrace_event_call *event, enum trace_reg type)
  1421. {
  1422. switch (type) {
  1423. case TRACE_REG_REGISTER:
  1424. return probe_event_enable(event);
  1425. case TRACE_REG_UNREGISTER:
  1426. probe_event_disable(event);
  1427. return 0;
  1428. #ifdef CONFIG_PERF_EVENTS
  1429. case TRACE_REG_PERF_REGISTER:
  1430. return probe_perf_enable(event);
  1431. case TRACE_REG_PERF_UNREGISTER:
  1432. probe_perf_disable(event);
  1433. return 0;
  1434. #endif
  1435. }
  1436. return 0;
  1437. }
  1438. static __kprobes
  1439. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1440. {
  1441. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1442. if (tp->flags & TP_FLAG_TRACE)
  1443. kprobe_trace_func(kp, regs);
  1444. #ifdef CONFIG_PERF_EVENTS
  1445. if (tp->flags & TP_FLAG_PROFILE)
  1446. kprobe_perf_func(kp, regs);
  1447. #endif
  1448. return 0; /* We don't tweek kernel, so just return 0 */
  1449. }
  1450. static __kprobes
  1451. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1452. {
  1453. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1454. if (tp->flags & TP_FLAG_TRACE)
  1455. kretprobe_trace_func(ri, regs);
  1456. #ifdef CONFIG_PERF_EVENTS
  1457. if (tp->flags & TP_FLAG_PROFILE)
  1458. kretprobe_perf_func(ri, regs);
  1459. #endif
  1460. return 0; /* We don't tweek kernel, so just return 0 */
  1461. }
  1462. static struct trace_event_functions kretprobe_funcs = {
  1463. .trace = print_kretprobe_event
  1464. };
  1465. static struct trace_event_functions kprobe_funcs = {
  1466. .trace = print_kprobe_event
  1467. };
  1468. static int register_probe_event(struct trace_probe *tp)
  1469. {
  1470. struct ftrace_event_call *call = &tp->call;
  1471. int ret;
  1472. /* Initialize ftrace_event_call */
  1473. INIT_LIST_HEAD(&call->class->fields);
  1474. if (probe_is_return(tp)) {
  1475. call->event.funcs = &kretprobe_funcs;
  1476. call->class->define_fields = kretprobe_event_define_fields;
  1477. } else {
  1478. call->event.funcs = &kprobe_funcs;
  1479. call->class->define_fields = kprobe_event_define_fields;
  1480. }
  1481. if (set_print_fmt(tp) < 0)
  1482. return -ENOMEM;
  1483. ret = register_ftrace_event(&call->event);
  1484. if (!ret) {
  1485. kfree(call->print_fmt);
  1486. return -ENODEV;
  1487. }
  1488. call->flags = 0;
  1489. call->class->reg = kprobe_register;
  1490. call->data = tp;
  1491. ret = trace_add_event_call(call);
  1492. if (ret) {
  1493. pr_info("Failed to register kprobe event: %s\n", call->name);
  1494. kfree(call->print_fmt);
  1495. unregister_ftrace_event(&call->event);
  1496. }
  1497. return ret;
  1498. }
  1499. static void unregister_probe_event(struct trace_probe *tp)
  1500. {
  1501. /* tp->event is unregistered in trace_remove_event_call() */
  1502. trace_remove_event_call(&tp->call);
  1503. kfree(tp->call.print_fmt);
  1504. }
  1505. /* Make a debugfs interface for controling probe points */
  1506. static __init int init_kprobe_trace(void)
  1507. {
  1508. struct dentry *d_tracer;
  1509. struct dentry *entry;
  1510. d_tracer = tracing_init_dentry();
  1511. if (!d_tracer)
  1512. return 0;
  1513. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1514. NULL, &kprobe_events_ops);
  1515. /* Event list interface */
  1516. if (!entry)
  1517. pr_warning("Could not create debugfs "
  1518. "'kprobe_events' entry\n");
  1519. /* Profile interface */
  1520. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1521. NULL, &kprobe_profile_ops);
  1522. if (!entry)
  1523. pr_warning("Could not create debugfs "
  1524. "'kprobe_profile' entry\n");
  1525. return 0;
  1526. }
  1527. fs_initcall(init_kprobe_trace);
  1528. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1529. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1530. int a4, int a5, int a6)
  1531. {
  1532. return a1 + a2 + a3 + a4 + a5 + a6;
  1533. }
  1534. static __init int kprobe_trace_self_tests_init(void)
  1535. {
  1536. int ret, warn = 0;
  1537. int (*target)(int, int, int, int, int, int);
  1538. struct trace_probe *tp;
  1539. target = kprobe_trace_selftest_target;
  1540. pr_info("Testing kprobe tracing: ");
  1541. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1542. "$stack $stack0 +0($stack)");
  1543. if (WARN_ON_ONCE(ret)) {
  1544. pr_warning("error on probing function entry.\n");
  1545. warn++;
  1546. } else {
  1547. /* Enable trace point */
  1548. tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM);
  1549. if (WARN_ON_ONCE(tp == NULL)) {
  1550. pr_warning("error on getting new probe.\n");
  1551. warn++;
  1552. } else
  1553. probe_event_enable(&tp->call);
  1554. }
  1555. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1556. "$retval");
  1557. if (WARN_ON_ONCE(ret)) {
  1558. pr_warning("error on probing function return.\n");
  1559. warn++;
  1560. } else {
  1561. /* Enable trace point */
  1562. tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM);
  1563. if (WARN_ON_ONCE(tp == NULL)) {
  1564. pr_warning("error on getting new probe.\n");
  1565. warn++;
  1566. } else
  1567. probe_event_enable(&tp->call);
  1568. }
  1569. if (warn)
  1570. goto end;
  1571. ret = target(1, 2, 3, 4, 5, 6);
  1572. ret = command_trace_probe("-:testprobe");
  1573. if (WARN_ON_ONCE(ret)) {
  1574. pr_warning("error on deleting a probe.\n");
  1575. warn++;
  1576. }
  1577. ret = command_trace_probe("-:testprobe2");
  1578. if (WARN_ON_ONCE(ret)) {
  1579. pr_warning("error on deleting a probe.\n");
  1580. warn++;
  1581. }
  1582. end:
  1583. cleanup_all_probes();
  1584. if (warn)
  1585. pr_cont("NG: Some tests are failed. Please check them.\n");
  1586. else
  1587. pr_cont("OK\n");
  1588. return 0;
  1589. }
  1590. late_initcall(kprobe_trace_self_tests_init);
  1591. #endif