probe-finder.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /*
  2. * probe-finder.c : C expression to kprobe event converter
  3. *
  4. * Written 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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #include <sys/utsname.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <getopt.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #include "string.h"
  34. #include "event.h"
  35. #include "debug.h"
  36. #include "util.h"
  37. #include "probe-finder.h"
  38. /*
  39. * Generic dwarf analysis helpers
  40. */
  41. #define X86_32_MAX_REGS 8
  42. const char *x86_32_regs_table[X86_32_MAX_REGS] = {
  43. "%ax",
  44. "%cx",
  45. "%dx",
  46. "%bx",
  47. "$stack", /* Stack address instead of %sp */
  48. "%bp",
  49. "%si",
  50. "%di",
  51. };
  52. #define X86_64_MAX_REGS 16
  53. const char *x86_64_regs_table[X86_64_MAX_REGS] = {
  54. "%ax",
  55. "%dx",
  56. "%cx",
  57. "%bx",
  58. "%si",
  59. "%di",
  60. "%bp",
  61. "%sp",
  62. "%r8",
  63. "%r9",
  64. "%r10",
  65. "%r11",
  66. "%r12",
  67. "%r13",
  68. "%r14",
  69. "%r15",
  70. };
  71. /* TODO: switching by dwarf address size */
  72. #ifdef __x86_64__
  73. #define ARCH_MAX_REGS X86_64_MAX_REGS
  74. #define arch_regs_table x86_64_regs_table
  75. #else
  76. #define ARCH_MAX_REGS X86_32_MAX_REGS
  77. #define arch_regs_table x86_32_regs_table
  78. #endif
  79. /* Kprobe tracer basic type is up to u64 */
  80. #define MAX_BASIC_TYPE_BITS 64
  81. /* Return architecture dependent register string (for kprobe-tracer) */
  82. static const char *get_arch_regstr(unsigned int n)
  83. {
  84. return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
  85. }
  86. /*
  87. * Compare the tail of two strings.
  88. * Return 0 if whole of either string is same as another's tail part.
  89. */
  90. static int strtailcmp(const char *s1, const char *s2)
  91. {
  92. int i1 = strlen(s1);
  93. int i2 = strlen(s2);
  94. while (--i1 >= 0 && --i2 >= 0) {
  95. if (s1[i1] != s2[i2])
  96. return s1[i1] - s2[i2];
  97. }
  98. return 0;
  99. }
  100. /* Line number list operations */
  101. /* Add a line to line number list */
  102. static void line_list__add_line(struct list_head *head, unsigned int line)
  103. {
  104. struct line_node *ln;
  105. struct list_head *p;
  106. /* Reverse search, because new line will be the last one */
  107. list_for_each_entry_reverse(ln, head, list) {
  108. if (ln->line < line) {
  109. p = &ln->list;
  110. goto found;
  111. } else if (ln->line == line) /* Already exist */
  112. return ;
  113. }
  114. /* List is empty, or the smallest entry */
  115. p = head;
  116. found:
  117. pr_debug("line list: add a line %u\n", line);
  118. ln = xzalloc(sizeof(struct line_node));
  119. ln->line = line;
  120. INIT_LIST_HEAD(&ln->list);
  121. list_add(&ln->list, p);
  122. }
  123. /* Check if the line in line number list */
  124. static int line_list__has_line(struct list_head *head, unsigned int line)
  125. {
  126. struct line_node *ln;
  127. /* Reverse search, because new line will be the last one */
  128. list_for_each_entry(ln, head, list)
  129. if (ln->line == line)
  130. return 1;
  131. return 0;
  132. }
  133. /* Init line number list */
  134. static void line_list__init(struct list_head *head)
  135. {
  136. INIT_LIST_HEAD(head);
  137. }
  138. /* Free line number list */
  139. static void line_list__free(struct list_head *head)
  140. {
  141. struct line_node *ln;
  142. while (!list_empty(head)) {
  143. ln = list_first_entry(head, struct line_node, list);
  144. list_del(&ln->list);
  145. free(ln);
  146. }
  147. }
  148. /* Dwarf wrappers */
  149. /* Find the realpath of the target file. */
  150. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  151. {
  152. Dwarf_Files *files;
  153. size_t nfiles, i;
  154. const char *src = NULL;
  155. int ret;
  156. if (!fname)
  157. return NULL;
  158. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  159. if (ret != 0)
  160. return NULL;
  161. for (i = 0; i < nfiles; i++) {
  162. src = dwarf_filesrc(files, i, NULL, NULL);
  163. if (strtailcmp(src, fname) == 0)
  164. break;
  165. }
  166. if (i == nfiles)
  167. return NULL;
  168. return src;
  169. }
  170. /* Compare diename and tname */
  171. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  172. {
  173. const char *name;
  174. name = dwarf_diename(dw_die);
  175. DIE_IF(name == NULL);
  176. return strcmp(tname, name);
  177. }
  178. /* Get entry pc(or low pc, 1st entry of ranges) of the die */
  179. static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
  180. {
  181. Dwarf_Addr epc;
  182. int ret;
  183. ret = dwarf_entrypc(dw_die, &epc);
  184. DIE_IF(ret == -1);
  185. return epc;
  186. }
  187. /* Get type die, but skip qualifiers and typedef */
  188. static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  189. {
  190. Dwarf_Attribute attr;
  191. int tag;
  192. do {
  193. if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
  194. dwarf_formref_die(&attr, die_mem) == NULL)
  195. return NULL;
  196. tag = dwarf_tag(die_mem);
  197. vr_die = die_mem;
  198. } while (tag == DW_TAG_const_type ||
  199. tag == DW_TAG_restrict_type ||
  200. tag == DW_TAG_volatile_type ||
  201. tag == DW_TAG_shared_type ||
  202. tag == DW_TAG_typedef);
  203. return die_mem;
  204. }
  205. static bool die_is_signed_type(Dwarf_Die *tp_die)
  206. {
  207. Dwarf_Attribute attr;
  208. Dwarf_Word ret;
  209. if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
  210. dwarf_formudata(&attr, &ret) != 0)
  211. return false;
  212. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  213. ret == DW_ATE_signed_fixed);
  214. }
  215. static int die_get_byte_size(Dwarf_Die *tp_die)
  216. {
  217. Dwarf_Attribute attr;
  218. Dwarf_Word ret;
  219. if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
  220. dwarf_formudata(&attr, &ret) != 0)
  221. return 0;
  222. return (int)ret;
  223. }
  224. /* Return values for die_find callbacks */
  225. enum {
  226. DIE_FIND_CB_FOUND = 0, /* End of Search */
  227. DIE_FIND_CB_CHILD = 1, /* Search only children */
  228. DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
  229. DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
  230. };
  231. /* Search a child die */
  232. static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  233. int (*callback)(Dwarf_Die *, void *),
  234. void *data, Dwarf_Die *die_mem)
  235. {
  236. Dwarf_Die child_die;
  237. int ret;
  238. ret = dwarf_child(rt_die, die_mem);
  239. if (ret != 0)
  240. return NULL;
  241. do {
  242. ret = callback(die_mem, data);
  243. if (ret == DIE_FIND_CB_FOUND)
  244. return die_mem;
  245. if ((ret & DIE_FIND_CB_CHILD) &&
  246. die_find_child(die_mem, callback, data, &child_die)) {
  247. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  248. return die_mem;
  249. }
  250. } while ((ret & DIE_FIND_CB_SIBLING) &&
  251. dwarf_siblingof(die_mem, die_mem) == 0);
  252. return NULL;
  253. }
  254. struct __addr_die_search_param {
  255. Dwarf_Addr addr;
  256. Dwarf_Die *die_mem;
  257. };
  258. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  259. {
  260. struct __addr_die_search_param *ad = data;
  261. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  262. dwarf_haspc(fn_die, ad->addr)) {
  263. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  264. return DWARF_CB_ABORT;
  265. }
  266. return DWARF_CB_OK;
  267. }
  268. /* Search a real subprogram including this line, */
  269. static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  270. Dwarf_Die *die_mem)
  271. {
  272. struct __addr_die_search_param ad;
  273. ad.addr = addr;
  274. ad.die_mem = die_mem;
  275. /* dwarf_getscopes can't find subprogram. */
  276. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  277. return NULL;
  278. else
  279. return die_mem;
  280. }
  281. /* die_find callback for inline function search */
  282. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  283. {
  284. Dwarf_Addr *addr = data;
  285. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  286. dwarf_haspc(die_mem, *addr))
  287. return DIE_FIND_CB_FOUND;
  288. return DIE_FIND_CB_CONTINUE;
  289. }
  290. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  291. static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  292. Dwarf_Die *die_mem)
  293. {
  294. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  295. }
  296. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  297. {
  298. const char *name = data;
  299. int tag;
  300. tag = dwarf_tag(die_mem);
  301. if ((tag == DW_TAG_formal_parameter ||
  302. tag == DW_TAG_variable) &&
  303. (die_compare_name(die_mem, name) == 0))
  304. return DIE_FIND_CB_FOUND;
  305. return DIE_FIND_CB_CONTINUE;
  306. }
  307. /* Find a variable called 'name' */
  308. static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
  309. Dwarf_Die *die_mem)
  310. {
  311. return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
  312. die_mem);
  313. }
  314. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  315. {
  316. const char *name = data;
  317. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  318. (die_compare_name(die_mem, name) == 0))
  319. return DIE_FIND_CB_FOUND;
  320. return DIE_FIND_CB_SIBLING;
  321. }
  322. /* Find a member called 'name' */
  323. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  324. Dwarf_Die *die_mem)
  325. {
  326. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  327. die_mem);
  328. }
  329. /*
  330. * Probe finder related functions
  331. */
  332. /* Show a location */
  333. static void convert_location(Dwarf_Op *op, struct probe_finder *pf)
  334. {
  335. unsigned int regn;
  336. Dwarf_Word offs = 0;
  337. bool ref = false;
  338. const char *regs;
  339. struct kprobe_trace_arg *tvar = pf->tvar;
  340. /* TODO: support CFA */
  341. /* If this is based on frame buffer, set the offset */
  342. if (op->atom == DW_OP_fbreg) {
  343. if (pf->fb_ops == NULL)
  344. die("The attribute of frame base is not supported.\n");
  345. ref = true;
  346. offs = op->number;
  347. op = &pf->fb_ops[0];
  348. }
  349. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  350. regn = op->atom - DW_OP_breg0;
  351. offs += op->number;
  352. ref = true;
  353. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  354. regn = op->atom - DW_OP_reg0;
  355. } else if (op->atom == DW_OP_bregx) {
  356. regn = op->number;
  357. offs += op->number2;
  358. ref = true;
  359. } else if (op->atom == DW_OP_regx) {
  360. regn = op->number;
  361. } else
  362. die("DW_OP %d is not supported.", op->atom);
  363. regs = get_arch_regstr(regn);
  364. if (!regs)
  365. die("%u exceeds max register number.", regn);
  366. tvar->value = xstrdup(regs);
  367. if (ref) {
  368. tvar->ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
  369. tvar->ref->offset = (long)offs;
  370. }
  371. }
  372. static void convert_variable_type(Dwarf_Die *vr_die,
  373. struct kprobe_trace_arg *targ)
  374. {
  375. Dwarf_Die type;
  376. char buf[16];
  377. int ret;
  378. if (die_get_real_type(vr_die, &type) == NULL)
  379. die("Failed to get a type information of %s.",
  380. dwarf_diename(vr_die));
  381. ret = die_get_byte_size(&type) * 8;
  382. if (ret) {
  383. /* Check the bitwidth */
  384. if (ret > MAX_BASIC_TYPE_BITS) {
  385. pr_warning(" Warning: %s exceeds max-bitwidth."
  386. " Cut down to %d bits.\n",
  387. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  388. ret = MAX_BASIC_TYPE_BITS;
  389. }
  390. ret = snprintf(buf, 16, "%c%d",
  391. die_is_signed_type(&type) ? 's' : 'u', ret);
  392. if (ret < 0 || ret >= 16)
  393. die("Failed to convert variable type.");
  394. targ->type = xstrdup(buf);
  395. }
  396. }
  397. static void convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  398. struct perf_probe_arg_field *field,
  399. struct kprobe_trace_arg_ref **ref_ptr,
  400. Dwarf_Die *die_mem)
  401. {
  402. struct kprobe_trace_arg_ref *ref = *ref_ptr;
  403. Dwarf_Attribute attr;
  404. Dwarf_Die type;
  405. Dwarf_Word offs;
  406. pr_debug("converting %s in %s\n", field->name, varname);
  407. if (die_get_real_type(vr_die, &type) == NULL)
  408. die("Failed to get a type information of %s.", varname);
  409. /* Check the pointer and dereference */
  410. if (dwarf_tag(&type) == DW_TAG_pointer_type) {
  411. if (!field->ref)
  412. die("Semantic error: %s must be referred by '->'",
  413. field->name);
  414. /* Get the type pointed by this pointer */
  415. if (die_get_real_type(&type, &type) == NULL)
  416. die("Failed to get a type information of %s.", varname);
  417. /* Verify it is a data structure */
  418. if (dwarf_tag(&type) != DW_TAG_structure_type)
  419. die("%s is not a data structure.", varname);
  420. ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
  421. if (*ref_ptr)
  422. (*ref_ptr)->next = ref;
  423. else
  424. *ref_ptr = ref;
  425. } else {
  426. /* Verify it is a data structure */
  427. if (dwarf_tag(&type) != DW_TAG_structure_type)
  428. die("%s is not a data structure.", varname);
  429. if (field->ref)
  430. die("Semantic error: %s must be referred by '.'",
  431. field->name);
  432. if (!ref)
  433. die("Structure on a register is not supported yet.");
  434. }
  435. if (die_find_member(&type, field->name, die_mem) == NULL)
  436. die("%s(tyep:%s) has no member %s.", varname,
  437. dwarf_diename(&type), field->name);
  438. /* Get the offset of the field */
  439. if (dwarf_attr(die_mem, DW_AT_data_member_location, &attr) == NULL ||
  440. dwarf_formudata(&attr, &offs) != 0)
  441. die("Failed to get the offset of %s.", field->name);
  442. ref->offset += (long)offs;
  443. /* Converting next field */
  444. if (field->next)
  445. convert_variable_fields(die_mem, field->name, field->next,
  446. &ref, die_mem);
  447. }
  448. /* Show a variables in kprobe event format */
  449. static void convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  450. {
  451. Dwarf_Attribute attr;
  452. Dwarf_Die die_mem;
  453. Dwarf_Op *expr;
  454. size_t nexpr;
  455. int ret;
  456. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  457. goto error;
  458. /* TODO: handle more than 1 exprs */
  459. ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
  460. if (ret <= 0 || nexpr == 0)
  461. goto error;
  462. convert_location(expr, pf);
  463. if (pf->pvar->field) {
  464. convert_variable_fields(vr_die, pf->pvar->var,
  465. pf->pvar->field, &pf->tvar->ref,
  466. &die_mem);
  467. vr_die = &die_mem;
  468. }
  469. if (pf->pvar->type)
  470. pf->tvar->type = xstrdup(pf->pvar->type);
  471. else
  472. convert_variable_type(vr_die, pf->tvar);
  473. /* *expr will be cached in libdw. Don't free it. */
  474. return ;
  475. error:
  476. /* TODO: Support const_value */
  477. die("Failed to find the location of %s at this address.\n"
  478. " Perhaps, it has been optimized out.", pf->pvar->var);
  479. }
  480. /* Find a variable in a subprogram die */
  481. static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  482. {
  483. Dwarf_Die vr_die;
  484. char buf[32], *ptr;
  485. /* TODO: Support arrays */
  486. if (pf->pvar->name)
  487. pf->tvar->name = xstrdup(pf->pvar->name);
  488. else {
  489. synthesize_perf_probe_arg(pf->pvar, buf, 32);
  490. ptr = strchr(buf, ':'); /* Change type separator to _ */
  491. if (ptr)
  492. *ptr = '_';
  493. pf->tvar->name = xstrdup(buf);
  494. }
  495. if (!is_c_varname(pf->pvar->var)) {
  496. /* Copy raw parameters */
  497. pf->tvar->value = xstrdup(pf->pvar->var);
  498. } else {
  499. pr_debug("Searching '%s' variable in context.\n",
  500. pf->pvar->var);
  501. /* Search child die for local variables and parameters. */
  502. if (!die_find_variable(sp_die, pf->pvar->var, &vr_die))
  503. die("Failed to find '%s' in this function.",
  504. pf->pvar->var);
  505. convert_variable(&vr_die, pf);
  506. }
  507. }
  508. /* Show a probe point to output buffer */
  509. static void convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  510. {
  511. struct kprobe_trace_event *tev;
  512. Dwarf_Addr eaddr;
  513. Dwarf_Die die_mem;
  514. const char *name;
  515. int ret, i;
  516. Dwarf_Attribute fb_attr;
  517. size_t nops;
  518. if (pf->ntevs == MAX_PROBES)
  519. die("Too many( > %d) probe point found.\n", MAX_PROBES);
  520. tev = &pf->tevs[pf->ntevs++];
  521. /* If no real subprogram, find a real one */
  522. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  523. sp_die = die_find_real_subprogram(&pf->cu_die,
  524. pf->addr, &die_mem);
  525. if (!sp_die)
  526. die("Probe point is not found in subprograms.");
  527. }
  528. /* Copy the name of probe point */
  529. name = dwarf_diename(sp_die);
  530. if (name) {
  531. dwarf_entrypc(sp_die, &eaddr);
  532. tev->point.symbol = xstrdup(name);
  533. tev->point.offset = (unsigned long)(pf->addr - eaddr);
  534. } else
  535. /* This function has no name. */
  536. tev->point.offset = (unsigned long)pf->addr;
  537. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  538. tev->point.offset);
  539. /* Get the frame base attribute/ops */
  540. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  541. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  542. if (ret <= 0 || nops == 0)
  543. pf->fb_ops = NULL;
  544. /* Find each argument */
  545. /* TODO: use dwarf_cfi_addrframe */
  546. tev->nargs = pf->pev->nargs;
  547. tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  548. for (i = 0; i < pf->pev->nargs; i++) {
  549. pf->pvar = &pf->pev->args[i];
  550. pf->tvar = &tev->args[i];
  551. find_variable(sp_die, pf);
  552. }
  553. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  554. pf->fb_ops = NULL;
  555. }
  556. /* Find probe point from its line number */
  557. static void find_probe_point_by_line(struct probe_finder *pf)
  558. {
  559. Dwarf_Lines *lines;
  560. Dwarf_Line *line;
  561. size_t nlines, i;
  562. Dwarf_Addr addr;
  563. int lineno;
  564. int ret;
  565. ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
  566. DIE_IF(ret != 0);
  567. for (i = 0; i < nlines; i++) {
  568. line = dwarf_onesrcline(lines, i);
  569. dwarf_lineno(line, &lineno);
  570. if (lineno != pf->lno)
  571. continue;
  572. /* TODO: Get fileno from line, but how? */
  573. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  574. continue;
  575. ret = dwarf_lineaddr(line, &addr);
  576. DIE_IF(ret != 0);
  577. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  578. (int)i, lineno, (uintmax_t)addr);
  579. pf->addr = addr;
  580. convert_probe_point(NULL, pf);
  581. /* Continuing, because target line might be inlined. */
  582. }
  583. }
  584. /* Find lines which match lazy pattern */
  585. static int find_lazy_match_lines(struct list_head *head,
  586. const char *fname, const char *pat)
  587. {
  588. char *fbuf, *p1, *p2;
  589. int fd, line, nlines = 0;
  590. struct stat st;
  591. fd = open(fname, O_RDONLY);
  592. if (fd < 0)
  593. die("failed to open %s", fname);
  594. DIE_IF(fstat(fd, &st) < 0);
  595. fbuf = xmalloc(st.st_size + 2);
  596. DIE_IF(read(fd, fbuf, st.st_size) < 0);
  597. close(fd);
  598. fbuf[st.st_size] = '\n'; /* Dummy line */
  599. fbuf[st.st_size + 1] = '\0';
  600. p1 = fbuf;
  601. line = 1;
  602. while ((p2 = strchr(p1, '\n')) != NULL) {
  603. *p2 = '\0';
  604. if (strlazymatch(p1, pat)) {
  605. line_list__add_line(head, line);
  606. nlines++;
  607. }
  608. line++;
  609. p1 = p2 + 1;
  610. }
  611. free(fbuf);
  612. return nlines;
  613. }
  614. /* Find probe points from lazy pattern */
  615. static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  616. {
  617. Dwarf_Lines *lines;
  618. Dwarf_Line *line;
  619. size_t nlines, i;
  620. Dwarf_Addr addr;
  621. Dwarf_Die die_mem;
  622. int lineno;
  623. int ret;
  624. if (list_empty(&pf->lcache)) {
  625. /* Matching lazy line pattern */
  626. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  627. pf->pev->point.lazy_line);
  628. if (ret <= 0)
  629. die("No matched lines found in %s.", pf->fname);
  630. }
  631. ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
  632. DIE_IF(ret != 0);
  633. for (i = 0; i < nlines; i++) {
  634. line = dwarf_onesrcline(lines, i);
  635. dwarf_lineno(line, &lineno);
  636. if (!line_list__has_line(&pf->lcache, lineno))
  637. continue;
  638. /* TODO: Get fileno from line, but how? */
  639. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  640. continue;
  641. ret = dwarf_lineaddr(line, &addr);
  642. DIE_IF(ret != 0);
  643. if (sp_die) {
  644. /* Address filtering 1: does sp_die include addr? */
  645. if (!dwarf_haspc(sp_die, addr))
  646. continue;
  647. /* Address filtering 2: No child include addr? */
  648. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  649. continue;
  650. }
  651. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  652. (int)i, lineno, (unsigned long long)addr);
  653. pf->addr = addr;
  654. convert_probe_point(sp_die, pf);
  655. /* Continuing, because target line might be inlined. */
  656. }
  657. /* TODO: deallocate lines, but how? */
  658. }
  659. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  660. {
  661. struct probe_finder *pf = (struct probe_finder *)data;
  662. struct perf_probe_point *pp = &pf->pev->point;
  663. if (pp->lazy_line)
  664. find_probe_point_lazy(in_die, pf);
  665. else {
  666. /* Get probe address */
  667. pf->addr = die_get_entrypc(in_die);
  668. pf->addr += pp->offset;
  669. pr_debug("found inline addr: 0x%jx\n",
  670. (uintmax_t)pf->addr);
  671. convert_probe_point(in_die, pf);
  672. }
  673. return DWARF_CB_OK;
  674. }
  675. /* Search function from function name */
  676. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  677. {
  678. struct probe_finder *pf = (struct probe_finder *)data;
  679. struct perf_probe_point *pp = &pf->pev->point;
  680. /* Check tag and diename */
  681. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  682. die_compare_name(sp_die, pp->function) != 0)
  683. return 0;
  684. pf->fname = dwarf_decl_file(sp_die);
  685. if (pp->line) { /* Function relative line */
  686. dwarf_decl_line(sp_die, &pf->lno);
  687. pf->lno += pp->line;
  688. find_probe_point_by_line(pf);
  689. } else if (!dwarf_func_inline(sp_die)) {
  690. /* Real function */
  691. if (pp->lazy_line)
  692. find_probe_point_lazy(sp_die, pf);
  693. else {
  694. pf->addr = die_get_entrypc(sp_die);
  695. pf->addr += pp->offset;
  696. /* TODO: Check the address in this function */
  697. convert_probe_point(sp_die, pf);
  698. }
  699. } else
  700. /* Inlined function: search instances */
  701. dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
  702. return 1; /* Exit; no same symbol in this CU. */
  703. }
  704. static void find_probe_point_by_func(struct probe_finder *pf)
  705. {
  706. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
  707. }
  708. /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
  709. int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
  710. struct kprobe_trace_event **tevs)
  711. {
  712. struct probe_finder pf = {.pev = pev};
  713. struct perf_probe_point *pp = &pev->point;
  714. Dwarf_Off off, noff;
  715. size_t cuhl;
  716. Dwarf_Die *diep;
  717. Dwarf *dbg;
  718. pf.tevs = xzalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
  719. *tevs = pf.tevs;
  720. pf.ntevs = 0;
  721. dbg = dwarf_begin(fd, DWARF_C_READ);
  722. if (!dbg)
  723. return -ENOENT;
  724. off = 0;
  725. line_list__init(&pf.lcache);
  726. /* Loop on CUs (Compilation Unit) */
  727. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  728. /* Get the DIE(Debugging Information Entry) of this CU */
  729. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  730. if (!diep)
  731. continue;
  732. /* Check if target file is included. */
  733. if (pp->file)
  734. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  735. else
  736. pf.fname = NULL;
  737. if (!pp->file || pf.fname) {
  738. if (pp->function)
  739. find_probe_point_by_func(&pf);
  740. else if (pp->lazy_line)
  741. find_probe_point_lazy(NULL, &pf);
  742. else {
  743. pf.lno = pp->line;
  744. find_probe_point_by_line(&pf);
  745. }
  746. }
  747. off = noff;
  748. }
  749. line_list__free(&pf.lcache);
  750. dwarf_end(dbg);
  751. return pf.ntevs;
  752. }
  753. /* Reverse search */
  754. int find_perf_probe_point(int fd, unsigned long addr,
  755. struct perf_probe_point *ppt)
  756. {
  757. Dwarf_Die cudie, spdie, indie;
  758. Dwarf *dbg;
  759. Dwarf_Line *line;
  760. Dwarf_Addr laddr, eaddr;
  761. const char *tmp;
  762. int lineno, ret = 0;
  763. dbg = dwarf_begin(fd, DWARF_C_READ);
  764. if (!dbg)
  765. return -ENOENT;
  766. /* Find cu die */
  767. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
  768. ret = -EINVAL;
  769. goto end;
  770. }
  771. /* Find a corresponding line */
  772. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  773. if (line) {
  774. dwarf_lineaddr(line, &laddr);
  775. if ((Dwarf_Addr)addr == laddr) {
  776. dwarf_lineno(line, &lineno);
  777. ppt->line = lineno;
  778. tmp = dwarf_linesrc(line, NULL, NULL);
  779. DIE_IF(!tmp);
  780. ppt->file = xstrdup(tmp);
  781. ret = 1;
  782. }
  783. }
  784. /* Find a corresponding function */
  785. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  786. tmp = dwarf_diename(&spdie);
  787. if (!tmp)
  788. goto end;
  789. dwarf_entrypc(&spdie, &eaddr);
  790. if (!lineno) {
  791. /* We don't have a line number, let's use offset */
  792. ppt->function = xstrdup(tmp);
  793. ppt->offset = addr - (unsigned long)eaddr;
  794. ret = 1;
  795. goto end;
  796. }
  797. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr, &indie)) {
  798. /* addr in an inline function */
  799. tmp = dwarf_diename(&indie);
  800. if (!tmp)
  801. goto end;
  802. dwarf_decl_line(&indie, &lineno);
  803. } else {
  804. if (eaddr == addr) /* No offset: function entry */
  805. lineno = ppt->line;
  806. else
  807. dwarf_decl_line(&spdie, &lineno);
  808. }
  809. ppt->function = xstrdup(tmp);
  810. ppt->line -= lineno; /* Make a relative line number */
  811. }
  812. end:
  813. dwarf_end(dbg);
  814. return ret;
  815. }
  816. /* Find line range from its line number */
  817. static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  818. {
  819. Dwarf_Lines *lines;
  820. Dwarf_Line *line;
  821. size_t nlines, i;
  822. Dwarf_Addr addr;
  823. int lineno;
  824. int ret;
  825. const char *src;
  826. Dwarf_Die die_mem;
  827. line_list__init(&lf->lr->line_list);
  828. ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
  829. DIE_IF(ret != 0);
  830. for (i = 0; i < nlines; i++) {
  831. line = dwarf_onesrcline(lines, i);
  832. ret = dwarf_lineno(line, &lineno);
  833. DIE_IF(ret != 0);
  834. if (lf->lno_s > lineno || lf->lno_e < lineno)
  835. continue;
  836. if (sp_die) {
  837. /* Address filtering 1: does sp_die include addr? */
  838. ret = dwarf_lineaddr(line, &addr);
  839. DIE_IF(ret != 0);
  840. if (!dwarf_haspc(sp_die, addr))
  841. continue;
  842. /* Address filtering 2: No child include addr? */
  843. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  844. continue;
  845. }
  846. /* TODO: Get fileno from line, but how? */
  847. src = dwarf_linesrc(line, NULL, NULL);
  848. if (strtailcmp(src, lf->fname) != 0)
  849. continue;
  850. /* Copy real path */
  851. if (!lf->lr->path)
  852. lf->lr->path = xstrdup(src);
  853. line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
  854. }
  855. /* Update status */
  856. if (!list_empty(&lf->lr->line_list))
  857. lf->found = 1;
  858. else {
  859. free(lf->lr->path);
  860. lf->lr->path = NULL;
  861. }
  862. }
  863. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  864. {
  865. find_line_range_by_line(in_die, (struct line_finder *)data);
  866. return DWARF_CB_ABORT; /* No need to find other instances */
  867. }
  868. /* Search function from function name */
  869. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  870. {
  871. struct line_finder *lf = (struct line_finder *)data;
  872. struct line_range *lr = lf->lr;
  873. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  874. die_compare_name(sp_die, lr->function) == 0) {
  875. lf->fname = dwarf_decl_file(sp_die);
  876. dwarf_decl_line(sp_die, &lr->offset);
  877. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  878. lf->lno_s = lr->offset + lr->start;
  879. if (!lr->end)
  880. lf->lno_e = INT_MAX;
  881. else
  882. lf->lno_e = lr->offset + lr->end;
  883. lr->start = lf->lno_s;
  884. lr->end = lf->lno_e;
  885. if (dwarf_func_inline(sp_die))
  886. dwarf_func_inline_instances(sp_die,
  887. line_range_inline_cb, lf);
  888. else
  889. find_line_range_by_line(sp_die, lf);
  890. return 1;
  891. }
  892. return 0;
  893. }
  894. static void find_line_range_by_func(struct line_finder *lf)
  895. {
  896. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
  897. }
  898. int find_line_range(int fd, struct line_range *lr)
  899. {
  900. struct line_finder lf = {.lr = lr, .found = 0};
  901. int ret;
  902. Dwarf_Off off = 0, noff;
  903. size_t cuhl;
  904. Dwarf_Die *diep;
  905. Dwarf *dbg;
  906. dbg = dwarf_begin(fd, DWARF_C_READ);
  907. if (!dbg)
  908. return -ENOENT;
  909. /* Loop on CUs (Compilation Unit) */
  910. while (!lf.found) {
  911. ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
  912. if (ret != 0)
  913. break;
  914. /* Get the DIE(Debugging Information Entry) of this CU */
  915. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  916. if (!diep)
  917. continue;
  918. /* Check if target file is included. */
  919. if (lr->file)
  920. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  921. else
  922. lf.fname = 0;
  923. if (!lr->file || lf.fname) {
  924. if (lr->function)
  925. find_line_range_by_func(&lf);
  926. else {
  927. lf.lno_s = lr->start;
  928. if (!lr->end)
  929. lf.lno_e = INT_MAX;
  930. else
  931. lf.lno_e = lr->end;
  932. find_line_range_by_line(NULL, &lf);
  933. }
  934. }
  935. off = noff;
  936. }
  937. pr_debug("path: %lx\n", (unsigned long)lr->path);
  938. dwarf_end(dbg);
  939. return lf.found;
  940. }