probe-finder.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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 <dwarf-regs.h>
  34. #include "event.h"
  35. #include "debug.h"
  36. #include "util.h"
  37. #include "symbol.h"
  38. #include "probe-finder.h"
  39. /* Kprobe tracer basic type is up to u64 */
  40. #define MAX_BASIC_TYPE_BITS 64
  41. /*
  42. * Compare the tail of two strings.
  43. * Return 0 if whole of either string is same as another's tail part.
  44. */
  45. static int strtailcmp(const char *s1, const char *s2)
  46. {
  47. int i1 = strlen(s1);
  48. int i2 = strlen(s2);
  49. while (--i1 >= 0 && --i2 >= 0) {
  50. if (s1[i1] != s2[i2])
  51. return s1[i1] - s2[i2];
  52. }
  53. return 0;
  54. }
  55. /* Line number list operations */
  56. /* Add a line to line number list */
  57. static int line_list__add_line(struct list_head *head, int line)
  58. {
  59. struct line_node *ln;
  60. struct list_head *p;
  61. /* Reverse search, because new line will be the last one */
  62. list_for_each_entry_reverse(ln, head, list) {
  63. if (ln->line < line) {
  64. p = &ln->list;
  65. goto found;
  66. } else if (ln->line == line) /* Already exist */
  67. return 1;
  68. }
  69. /* List is empty, or the smallest entry */
  70. p = head;
  71. found:
  72. pr_debug("line list: add a line %u\n", line);
  73. ln = zalloc(sizeof(struct line_node));
  74. if (ln == NULL)
  75. return -ENOMEM;
  76. ln->line = line;
  77. INIT_LIST_HEAD(&ln->list);
  78. list_add(&ln->list, p);
  79. return 0;
  80. }
  81. /* Check if the line in line number list */
  82. static int line_list__has_line(struct list_head *head, int line)
  83. {
  84. struct line_node *ln;
  85. /* Reverse search, because new line will be the last one */
  86. list_for_each_entry(ln, head, list)
  87. if (ln->line == line)
  88. return 1;
  89. return 0;
  90. }
  91. /* Init line number list */
  92. static void line_list__init(struct list_head *head)
  93. {
  94. INIT_LIST_HEAD(head);
  95. }
  96. /* Free line number list */
  97. static void line_list__free(struct list_head *head)
  98. {
  99. struct line_node *ln;
  100. while (!list_empty(head)) {
  101. ln = list_first_entry(head, struct line_node, list);
  102. list_del(&ln->list);
  103. free(ln);
  104. }
  105. }
  106. /* Dwarf wrappers */
  107. /* Find the realpath of the target file. */
  108. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  109. {
  110. Dwarf_Files *files;
  111. size_t nfiles, i;
  112. const char *src = NULL;
  113. int ret;
  114. if (!fname)
  115. return NULL;
  116. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  117. if (ret != 0)
  118. return NULL;
  119. for (i = 0; i < nfiles; i++) {
  120. src = dwarf_filesrc(files, i, NULL, NULL);
  121. if (strtailcmp(src, fname) == 0)
  122. break;
  123. }
  124. if (i == nfiles)
  125. return NULL;
  126. return src;
  127. }
  128. /* Get DW_AT_comp_dir (should be NULL with older gcc) */
  129. static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
  130. {
  131. Dwarf_Attribute attr;
  132. if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
  133. return NULL;
  134. return dwarf_formstring(&attr);
  135. }
  136. /* Compare diename and tname */
  137. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  138. {
  139. const char *name;
  140. name = dwarf_diename(dw_die);
  141. return name ? (strcmp(tname, name) == 0) : false;
  142. }
  143. /* Get type die, but skip qualifiers and typedef */
  144. static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  145. {
  146. Dwarf_Attribute attr;
  147. int tag;
  148. do {
  149. if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
  150. dwarf_formref_die(&attr, die_mem) == NULL)
  151. return NULL;
  152. tag = dwarf_tag(die_mem);
  153. vr_die = die_mem;
  154. } while (tag == DW_TAG_const_type ||
  155. tag == DW_TAG_restrict_type ||
  156. tag == DW_TAG_volatile_type ||
  157. tag == DW_TAG_shared_type ||
  158. tag == DW_TAG_typedef);
  159. return die_mem;
  160. }
  161. static bool die_is_signed_type(Dwarf_Die *tp_die)
  162. {
  163. Dwarf_Attribute attr;
  164. Dwarf_Word ret;
  165. if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
  166. dwarf_formudata(&attr, &ret) != 0)
  167. return false;
  168. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  169. ret == DW_ATE_signed_fixed);
  170. }
  171. static int die_get_byte_size(Dwarf_Die *tp_die)
  172. {
  173. Dwarf_Attribute attr;
  174. Dwarf_Word ret;
  175. if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
  176. dwarf_formudata(&attr, &ret) != 0)
  177. return 0;
  178. return (int)ret;
  179. }
  180. /* Get data_member_location offset */
  181. static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
  182. {
  183. Dwarf_Attribute attr;
  184. Dwarf_Op *expr;
  185. size_t nexpr;
  186. int ret;
  187. if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
  188. return -ENOENT;
  189. if (dwarf_formudata(&attr, offs) != 0) {
  190. /* DW_AT_data_member_location should be DW_OP_plus_uconst */
  191. ret = dwarf_getlocation(&attr, &expr, &nexpr);
  192. if (ret < 0 || nexpr == 0)
  193. return -ENOENT;
  194. if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
  195. pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
  196. expr[0].atom, nexpr);
  197. return -ENOTSUP;
  198. }
  199. *offs = (Dwarf_Word)expr[0].number;
  200. }
  201. return 0;
  202. }
  203. /* Return values for die_find callbacks */
  204. enum {
  205. DIE_FIND_CB_FOUND = 0, /* End of Search */
  206. DIE_FIND_CB_CHILD = 1, /* Search only children */
  207. DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
  208. DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
  209. };
  210. /* Search a child die */
  211. static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  212. int (*callback)(Dwarf_Die *, void *),
  213. void *data, Dwarf_Die *die_mem)
  214. {
  215. Dwarf_Die child_die;
  216. int ret;
  217. ret = dwarf_child(rt_die, die_mem);
  218. if (ret != 0)
  219. return NULL;
  220. do {
  221. ret = callback(die_mem, data);
  222. if (ret == DIE_FIND_CB_FOUND)
  223. return die_mem;
  224. if ((ret & DIE_FIND_CB_CHILD) &&
  225. die_find_child(die_mem, callback, data, &child_die)) {
  226. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  227. return die_mem;
  228. }
  229. } while ((ret & DIE_FIND_CB_SIBLING) &&
  230. dwarf_siblingof(die_mem, die_mem) == 0);
  231. return NULL;
  232. }
  233. struct __addr_die_search_param {
  234. Dwarf_Addr addr;
  235. Dwarf_Die *die_mem;
  236. };
  237. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  238. {
  239. struct __addr_die_search_param *ad = data;
  240. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  241. dwarf_haspc(fn_die, ad->addr)) {
  242. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  243. return DWARF_CB_ABORT;
  244. }
  245. return DWARF_CB_OK;
  246. }
  247. /* Search a real subprogram including this line, */
  248. static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  249. Dwarf_Die *die_mem)
  250. {
  251. struct __addr_die_search_param ad;
  252. ad.addr = addr;
  253. ad.die_mem = die_mem;
  254. /* dwarf_getscopes can't find subprogram. */
  255. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  256. return NULL;
  257. else
  258. return die_mem;
  259. }
  260. /* die_find callback for inline function search */
  261. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  262. {
  263. Dwarf_Addr *addr = data;
  264. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  265. dwarf_haspc(die_mem, *addr))
  266. return DIE_FIND_CB_FOUND;
  267. return DIE_FIND_CB_CONTINUE;
  268. }
  269. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  270. static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  271. Dwarf_Die *die_mem)
  272. {
  273. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  274. }
  275. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  276. {
  277. const char *name = data;
  278. int tag;
  279. tag = dwarf_tag(die_mem);
  280. if ((tag == DW_TAG_formal_parameter ||
  281. tag == DW_TAG_variable) &&
  282. die_compare_name(die_mem, name))
  283. return DIE_FIND_CB_FOUND;
  284. return DIE_FIND_CB_CONTINUE;
  285. }
  286. /* Find a variable called 'name' */
  287. static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
  288. Dwarf_Die *die_mem)
  289. {
  290. return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
  291. die_mem);
  292. }
  293. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  294. {
  295. const char *name = data;
  296. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  297. die_compare_name(die_mem, name))
  298. return DIE_FIND_CB_FOUND;
  299. return DIE_FIND_CB_SIBLING;
  300. }
  301. /* Find a member called 'name' */
  302. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  303. Dwarf_Die *die_mem)
  304. {
  305. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  306. die_mem);
  307. }
  308. /*
  309. * Probe finder related functions
  310. */
  311. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  312. {
  313. struct probe_trace_arg_ref *ref;
  314. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  315. if (ref != NULL)
  316. ref->offset = offs;
  317. return ref;
  318. }
  319. /* Show a location */
  320. static int convert_variable_location(Dwarf_Die *vr_die, struct probe_finder *pf)
  321. {
  322. Dwarf_Attribute attr;
  323. Dwarf_Op *op;
  324. size_t nops;
  325. unsigned int regn;
  326. Dwarf_Word offs = 0;
  327. bool ref = false;
  328. const char *regs;
  329. struct probe_trace_arg *tvar = pf->tvar;
  330. int ret;
  331. /* TODO: handle more than 1 exprs */
  332. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  333. dwarf_getlocation_addr(&attr, pf->addr, &op, &nops, 1) <= 0 ||
  334. nops == 0) {
  335. /* TODO: Support const_value */
  336. pr_err("Failed to find the location of %s at this address.\n"
  337. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  338. return -ENOENT;
  339. }
  340. if (op->atom == DW_OP_addr) {
  341. /* Static variables on memory (not stack), make @varname */
  342. ret = strlen(dwarf_diename(vr_die));
  343. tvar->value = zalloc(ret + 2);
  344. if (tvar->value == NULL)
  345. return -ENOMEM;
  346. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  347. tvar->ref = alloc_trace_arg_ref((long)offs);
  348. if (tvar->ref == NULL)
  349. return -ENOMEM;
  350. return 0;
  351. }
  352. /* If this is based on frame buffer, set the offset */
  353. if (op->atom == DW_OP_fbreg) {
  354. if (pf->fb_ops == NULL) {
  355. pr_warning("The attribute of frame base is not "
  356. "supported.\n");
  357. return -ENOTSUP;
  358. }
  359. ref = true;
  360. offs = op->number;
  361. op = &pf->fb_ops[0];
  362. }
  363. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  364. regn = op->atom - DW_OP_breg0;
  365. offs += op->number;
  366. ref = true;
  367. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  368. regn = op->atom - DW_OP_reg0;
  369. } else if (op->atom == DW_OP_bregx) {
  370. regn = op->number;
  371. offs += op->number2;
  372. ref = true;
  373. } else if (op->atom == DW_OP_regx) {
  374. regn = op->number;
  375. } else {
  376. pr_warning("DW_OP %x is not supported.\n", op->atom);
  377. return -ENOTSUP;
  378. }
  379. regs = get_arch_regstr(regn);
  380. if (!regs) {
  381. pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
  382. return -ERANGE;
  383. }
  384. tvar->value = strdup(regs);
  385. if (tvar->value == NULL)
  386. return -ENOMEM;
  387. if (ref) {
  388. tvar->ref = alloc_trace_arg_ref((long)offs);
  389. if (tvar->ref == NULL)
  390. return -ENOMEM;
  391. }
  392. return 0;
  393. }
  394. static int convert_variable_type(Dwarf_Die *vr_die,
  395. struct probe_trace_arg *tvar,
  396. const char *cast)
  397. {
  398. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  399. Dwarf_Die type;
  400. char buf[16];
  401. int ret;
  402. /* TODO: check all types */
  403. if (cast && strcmp(cast, "string") != 0) {
  404. /* Non string type is OK */
  405. tvar->type = strdup(cast);
  406. return (tvar->type == NULL) ? -ENOMEM : 0;
  407. }
  408. if (die_get_real_type(vr_die, &type) == NULL) {
  409. pr_warning("Failed to get a type information of %s.\n",
  410. dwarf_diename(vr_die));
  411. return -ENOENT;
  412. }
  413. pr_debug("%s type is %s.\n",
  414. dwarf_diename(vr_die), dwarf_diename(&type));
  415. if (cast && strcmp(cast, "string") == 0) { /* String type */
  416. ret = dwarf_tag(&type);
  417. if (ret != DW_TAG_pointer_type &&
  418. ret != DW_TAG_array_type) {
  419. pr_warning("Failed to cast into string: "
  420. "%s(%s) is not a pointer nor array.",
  421. dwarf_diename(vr_die), dwarf_diename(&type));
  422. return -EINVAL;
  423. }
  424. if (ret == DW_TAG_pointer_type) {
  425. if (die_get_real_type(&type, &type) == NULL) {
  426. pr_warning("Failed to get a type information.");
  427. return -ENOENT;
  428. }
  429. while (*ref_ptr)
  430. ref_ptr = &(*ref_ptr)->next;
  431. /* Add new reference with offset +0 */
  432. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  433. if (*ref_ptr == NULL) {
  434. pr_warning("Out of memory error\n");
  435. return -ENOMEM;
  436. }
  437. }
  438. if (!die_compare_name(&type, "char") &&
  439. !die_compare_name(&type, "unsigned char")) {
  440. pr_warning("Failed to cast into string: "
  441. "%s is not (unsigned) char *.",
  442. dwarf_diename(vr_die));
  443. return -EINVAL;
  444. }
  445. tvar->type = strdup(cast);
  446. return (tvar->type == NULL) ? -ENOMEM : 0;
  447. }
  448. ret = die_get_byte_size(&type) * 8;
  449. if (ret) {
  450. /* Check the bitwidth */
  451. if (ret > MAX_BASIC_TYPE_BITS) {
  452. pr_info("%s exceeds max-bitwidth."
  453. " Cut down to %d bits.\n",
  454. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  455. ret = MAX_BASIC_TYPE_BITS;
  456. }
  457. ret = snprintf(buf, 16, "%c%d",
  458. die_is_signed_type(&type) ? 's' : 'u', ret);
  459. if (ret < 0 || ret >= 16) {
  460. if (ret >= 16)
  461. ret = -E2BIG;
  462. pr_warning("Failed to convert variable type: %s\n",
  463. strerror(-ret));
  464. return ret;
  465. }
  466. tvar->type = strdup(buf);
  467. if (tvar->type == NULL)
  468. return -ENOMEM;
  469. }
  470. return 0;
  471. }
  472. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  473. struct perf_probe_arg_field *field,
  474. struct probe_trace_arg_ref **ref_ptr,
  475. Dwarf_Die *die_mem)
  476. {
  477. struct probe_trace_arg_ref *ref = *ref_ptr;
  478. Dwarf_Die type;
  479. Dwarf_Word offs;
  480. int ret, tag;
  481. pr_debug("converting %s in %s\n", field->name, varname);
  482. if (die_get_real_type(vr_die, &type) == NULL) {
  483. pr_warning("Failed to get the type of %s.\n", varname);
  484. return -ENOENT;
  485. }
  486. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  487. tag = dwarf_tag(&type);
  488. if (field->name[0] == '[' &&
  489. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  490. if (field->next)
  491. /* Save original type for next field */
  492. memcpy(die_mem, &type, sizeof(*die_mem));
  493. /* Get the type of this array */
  494. if (die_get_real_type(&type, &type) == NULL) {
  495. pr_warning("Failed to get the type of %s.\n", varname);
  496. return -ENOENT;
  497. }
  498. pr_debug2("Array real type: (%x)\n",
  499. (unsigned)dwarf_dieoffset(&type));
  500. if (tag == DW_TAG_pointer_type) {
  501. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  502. if (ref == NULL)
  503. return -ENOMEM;
  504. if (*ref_ptr)
  505. (*ref_ptr)->next = ref;
  506. else
  507. *ref_ptr = ref;
  508. }
  509. ref->offset += die_get_byte_size(&type) * field->index;
  510. if (!field->next)
  511. /* Save vr_die for converting types */
  512. memcpy(die_mem, vr_die, sizeof(*die_mem));
  513. goto next;
  514. } else if (tag == DW_TAG_pointer_type) {
  515. /* Check the pointer and dereference */
  516. if (!field->ref) {
  517. pr_err("Semantic error: %s must be referred by '->'\n",
  518. field->name);
  519. return -EINVAL;
  520. }
  521. /* Get the type pointed by this pointer */
  522. if (die_get_real_type(&type, &type) == NULL) {
  523. pr_warning("Failed to get the type of %s.\n", varname);
  524. return -ENOENT;
  525. }
  526. /* Verify it is a data structure */
  527. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  528. pr_warning("%s is not a data structure.\n", varname);
  529. return -EINVAL;
  530. }
  531. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  532. if (ref == NULL)
  533. return -ENOMEM;
  534. if (*ref_ptr)
  535. (*ref_ptr)->next = ref;
  536. else
  537. *ref_ptr = ref;
  538. } else {
  539. /* Verify it is a data structure */
  540. if (tag != DW_TAG_structure_type) {
  541. pr_warning("%s is not a data structure.\n", varname);
  542. return -EINVAL;
  543. }
  544. if (field->name[0] == '[') {
  545. pr_err("Semantic error: %s is not a pointor nor array.",
  546. varname);
  547. return -EINVAL;
  548. }
  549. if (field->ref) {
  550. pr_err("Semantic error: %s must be referred by '.'\n",
  551. field->name);
  552. return -EINVAL;
  553. }
  554. if (!ref) {
  555. pr_warning("Structure on a register is not "
  556. "supported yet.\n");
  557. return -ENOTSUP;
  558. }
  559. }
  560. if (die_find_member(&type, field->name, die_mem) == NULL) {
  561. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  562. dwarf_diename(&type), field->name);
  563. return -EINVAL;
  564. }
  565. /* Get the offset of the field */
  566. ret = die_get_data_member_location(die_mem, &offs);
  567. if (ret < 0) {
  568. pr_warning("Failed to get the offset of %s.\n", field->name);
  569. return ret;
  570. }
  571. ref->offset += (long)offs;
  572. next:
  573. /* Converting next field */
  574. if (field->next)
  575. return convert_variable_fields(die_mem, field->name,
  576. field->next, &ref, die_mem);
  577. else
  578. return 0;
  579. }
  580. /* Show a variables in kprobe event format */
  581. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  582. {
  583. Dwarf_Die die_mem;
  584. int ret;
  585. pr_debug("Converting variable %s into trace event.\n",
  586. dwarf_diename(vr_die));
  587. ret = convert_variable_location(vr_die, pf);
  588. if (ret == 0 && pf->pvar->field) {
  589. ret = convert_variable_fields(vr_die, pf->pvar->var,
  590. pf->pvar->field, &pf->tvar->ref,
  591. &die_mem);
  592. vr_die = &die_mem;
  593. }
  594. if (ret == 0)
  595. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  596. /* *expr will be cached in libdw. Don't free it. */
  597. return ret;
  598. }
  599. /* Find a variable in a subprogram die */
  600. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  601. {
  602. Dwarf_Die vr_die, *scopes;
  603. char buf[32], *ptr;
  604. int ret, nscopes;
  605. if (pf->pvar->name)
  606. pf->tvar->name = strdup(pf->pvar->name);
  607. else {
  608. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  609. if (ret < 0)
  610. return ret;
  611. ptr = strchr(buf, ':'); /* Change type separator to _ */
  612. if (ptr)
  613. *ptr = '_';
  614. pf->tvar->name = strdup(buf);
  615. }
  616. if (pf->tvar->name == NULL)
  617. return -ENOMEM;
  618. if (!is_c_varname(pf->pvar->var)) {
  619. /* Copy raw parameters */
  620. pf->tvar->value = strdup(pf->pvar->var);
  621. if (pf->tvar->value == NULL)
  622. return -ENOMEM;
  623. if (pf->pvar->type) {
  624. pf->tvar->type = strdup(pf->pvar->type);
  625. if (pf->tvar->type == NULL)
  626. return -ENOMEM;
  627. }
  628. return 0;
  629. }
  630. pr_debug("Searching '%s' variable in context.\n",
  631. pf->pvar->var);
  632. /* Search child die for local variables and parameters. */
  633. if (die_find_variable(sp_die, pf->pvar->var, &vr_die))
  634. ret = convert_variable(&vr_die, pf);
  635. else {
  636. /* Search upper class */
  637. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  638. if (nscopes > 0) {
  639. ret = dwarf_getscopevar(scopes, nscopes, pf->pvar->var,
  640. 0, NULL, 0, 0, &vr_die);
  641. if (ret >= 0)
  642. ret = convert_variable(&vr_die, pf);
  643. else
  644. ret = -ENOENT;
  645. free(scopes);
  646. } else
  647. ret = -ENOENT;
  648. }
  649. if (ret < 0)
  650. pr_warning("Failed to find '%s' in this function.\n",
  651. pf->pvar->var);
  652. return ret;
  653. }
  654. /* Show a probe point to output buffer */
  655. static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  656. {
  657. struct probe_trace_event *tev;
  658. Dwarf_Addr eaddr;
  659. Dwarf_Die die_mem;
  660. const char *name;
  661. int ret, i;
  662. Dwarf_Attribute fb_attr;
  663. size_t nops;
  664. if (pf->ntevs == pf->max_tevs) {
  665. pr_warning("Too many( > %d) probe point found.\n",
  666. pf->max_tevs);
  667. return -ERANGE;
  668. }
  669. tev = &pf->tevs[pf->ntevs++];
  670. /* If no real subprogram, find a real one */
  671. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  672. sp_die = die_find_real_subprogram(&pf->cu_die,
  673. pf->addr, &die_mem);
  674. if (!sp_die) {
  675. pr_warning("Failed to find probe point in any "
  676. "functions.\n");
  677. return -ENOENT;
  678. }
  679. }
  680. /* Copy the name of probe point */
  681. name = dwarf_diename(sp_die);
  682. if (name) {
  683. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  684. pr_warning("Failed to get entry pc of %s\n",
  685. dwarf_diename(sp_die));
  686. return -ENOENT;
  687. }
  688. tev->point.symbol = strdup(name);
  689. if (tev->point.symbol == NULL)
  690. return -ENOMEM;
  691. tev->point.offset = (unsigned long)(pf->addr - eaddr);
  692. } else
  693. /* This function has no name. */
  694. tev->point.offset = (unsigned long)pf->addr;
  695. /* Return probe must be on the head of a subprogram */
  696. if (pf->pev->point.retprobe) {
  697. if (tev->point.offset != 0) {
  698. pr_warning("Return probe must be on the head of"
  699. " a real function\n");
  700. return -EINVAL;
  701. }
  702. tev->point.retprobe = true;
  703. }
  704. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  705. tev->point.offset);
  706. /* Get the frame base attribute/ops */
  707. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  708. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  709. if (ret <= 0 || nops == 0) {
  710. pf->fb_ops = NULL;
  711. #if _ELFUTILS_PREREQ(0, 142)
  712. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  713. pf->cfi != NULL) {
  714. Dwarf_Frame *frame;
  715. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  716. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  717. pr_warning("Failed to get CFA on 0x%jx\n",
  718. (uintmax_t)pf->addr);
  719. return -ENOENT;
  720. }
  721. #endif
  722. }
  723. /* Find each argument */
  724. tev->nargs = pf->pev->nargs;
  725. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  726. if (tev->args == NULL)
  727. return -ENOMEM;
  728. for (i = 0; i < pf->pev->nargs; i++) {
  729. pf->pvar = &pf->pev->args[i];
  730. pf->tvar = &tev->args[i];
  731. ret = find_variable(sp_die, pf);
  732. if (ret != 0)
  733. return ret;
  734. }
  735. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  736. pf->fb_ops = NULL;
  737. return 0;
  738. }
  739. /* Find probe point from its line number */
  740. static int find_probe_point_by_line(struct probe_finder *pf)
  741. {
  742. Dwarf_Lines *lines;
  743. Dwarf_Line *line;
  744. size_t nlines, i;
  745. Dwarf_Addr addr;
  746. int lineno;
  747. int ret = 0;
  748. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  749. pr_warning("No source lines found in this CU.\n");
  750. return -ENOENT;
  751. }
  752. for (i = 0; i < nlines && ret == 0; i++) {
  753. line = dwarf_onesrcline(lines, i);
  754. if (dwarf_lineno(line, &lineno) != 0 ||
  755. lineno != pf->lno)
  756. continue;
  757. /* TODO: Get fileno from line, but how? */
  758. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  759. continue;
  760. if (dwarf_lineaddr(line, &addr) != 0) {
  761. pr_warning("Failed to get the address of the line.\n");
  762. return -ENOENT;
  763. }
  764. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  765. (int)i, lineno, (uintmax_t)addr);
  766. pf->addr = addr;
  767. ret = convert_probe_point(NULL, pf);
  768. /* Continuing, because target line might be inlined. */
  769. }
  770. return ret;
  771. }
  772. /* Find lines which match lazy pattern */
  773. static int find_lazy_match_lines(struct list_head *head,
  774. const char *fname, const char *pat)
  775. {
  776. char *fbuf, *p1, *p2;
  777. int fd, line, nlines = -1;
  778. struct stat st;
  779. fd = open(fname, O_RDONLY);
  780. if (fd < 0) {
  781. pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
  782. return -errno;
  783. }
  784. if (fstat(fd, &st) < 0) {
  785. pr_warning("Failed to get the size of %s: %s\n",
  786. fname, strerror(errno));
  787. nlines = -errno;
  788. goto out_close;
  789. }
  790. nlines = -ENOMEM;
  791. fbuf = malloc(st.st_size + 2);
  792. if (fbuf == NULL)
  793. goto out_close;
  794. if (read(fd, fbuf, st.st_size) < 0) {
  795. pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
  796. nlines = -errno;
  797. goto out_free_fbuf;
  798. }
  799. fbuf[st.st_size] = '\n'; /* Dummy line */
  800. fbuf[st.st_size + 1] = '\0';
  801. p1 = fbuf;
  802. line = 1;
  803. nlines = 0;
  804. while ((p2 = strchr(p1, '\n')) != NULL) {
  805. *p2 = '\0';
  806. if (strlazymatch(p1, pat)) {
  807. line_list__add_line(head, line);
  808. nlines++;
  809. }
  810. line++;
  811. p1 = p2 + 1;
  812. }
  813. out_free_fbuf:
  814. free(fbuf);
  815. out_close:
  816. close(fd);
  817. return nlines;
  818. }
  819. /* Find probe points from lazy pattern */
  820. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  821. {
  822. Dwarf_Lines *lines;
  823. Dwarf_Line *line;
  824. size_t nlines, i;
  825. Dwarf_Addr addr;
  826. Dwarf_Die die_mem;
  827. int lineno;
  828. int ret = 0;
  829. if (list_empty(&pf->lcache)) {
  830. /* Matching lazy line pattern */
  831. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  832. pf->pev->point.lazy_line);
  833. if (ret == 0) {
  834. pr_debug("No matched lines found in %s.\n", pf->fname);
  835. return 0;
  836. } else if (ret < 0)
  837. return ret;
  838. }
  839. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  840. pr_warning("No source lines found in this CU.\n");
  841. return -ENOENT;
  842. }
  843. for (i = 0; i < nlines && ret >= 0; i++) {
  844. line = dwarf_onesrcline(lines, i);
  845. if (dwarf_lineno(line, &lineno) != 0 ||
  846. !line_list__has_line(&pf->lcache, lineno))
  847. continue;
  848. /* TODO: Get fileno from line, but how? */
  849. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  850. continue;
  851. if (dwarf_lineaddr(line, &addr) != 0) {
  852. pr_debug("Failed to get the address of line %d.\n",
  853. lineno);
  854. continue;
  855. }
  856. if (sp_die) {
  857. /* Address filtering 1: does sp_die include addr? */
  858. if (!dwarf_haspc(sp_die, addr))
  859. continue;
  860. /* Address filtering 2: No child include addr? */
  861. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  862. continue;
  863. }
  864. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  865. (int)i, lineno, (unsigned long long)addr);
  866. pf->addr = addr;
  867. ret = convert_probe_point(sp_die, pf);
  868. /* Continuing, because target line might be inlined. */
  869. }
  870. /* TODO: deallocate lines, but how? */
  871. return ret;
  872. }
  873. /* Callback parameter with return value */
  874. struct dwarf_callback_param {
  875. void *data;
  876. int retval;
  877. };
  878. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  879. {
  880. struct dwarf_callback_param *param = data;
  881. struct probe_finder *pf = param->data;
  882. struct perf_probe_point *pp = &pf->pev->point;
  883. Dwarf_Addr addr;
  884. if (pp->lazy_line)
  885. param->retval = find_probe_point_lazy(in_die, pf);
  886. else {
  887. /* Get probe address */
  888. if (dwarf_entrypc(in_die, &addr) != 0) {
  889. pr_warning("Failed to get entry pc of %s.\n",
  890. dwarf_diename(in_die));
  891. param->retval = -ENOENT;
  892. return DWARF_CB_ABORT;
  893. }
  894. pf->addr = addr;
  895. pf->addr += pp->offset;
  896. pr_debug("found inline addr: 0x%jx\n",
  897. (uintmax_t)pf->addr);
  898. param->retval = convert_probe_point(in_die, pf);
  899. if (param->retval < 0)
  900. return DWARF_CB_ABORT;
  901. }
  902. return DWARF_CB_OK;
  903. }
  904. /* Search function from function name */
  905. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  906. {
  907. struct dwarf_callback_param *param = data;
  908. struct probe_finder *pf = param->data;
  909. struct perf_probe_point *pp = &pf->pev->point;
  910. /* Check tag and diename */
  911. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  912. !die_compare_name(sp_die, pp->function))
  913. return DWARF_CB_OK;
  914. pf->fname = dwarf_decl_file(sp_die);
  915. if (pp->line) { /* Function relative line */
  916. dwarf_decl_line(sp_die, &pf->lno);
  917. pf->lno += pp->line;
  918. param->retval = find_probe_point_by_line(pf);
  919. } else if (!dwarf_func_inline(sp_die)) {
  920. /* Real function */
  921. if (pp->lazy_line)
  922. param->retval = find_probe_point_lazy(sp_die, pf);
  923. else {
  924. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  925. pr_warning("Failed to get entry pc of %s.\n",
  926. dwarf_diename(sp_die));
  927. param->retval = -ENOENT;
  928. return DWARF_CB_ABORT;
  929. }
  930. pf->addr += pp->offset;
  931. /* TODO: Check the address in this function */
  932. param->retval = convert_probe_point(sp_die, pf);
  933. }
  934. } else {
  935. struct dwarf_callback_param _param = {.data = (void *)pf,
  936. .retval = 0};
  937. /* Inlined function: search instances */
  938. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  939. &_param);
  940. param->retval = _param.retval;
  941. }
  942. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  943. }
  944. static int find_probe_point_by_func(struct probe_finder *pf)
  945. {
  946. struct dwarf_callback_param _param = {.data = (void *)pf,
  947. .retval = 0};
  948. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  949. return _param.retval;
  950. }
  951. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  952. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  953. struct probe_trace_event **tevs, int max_tevs)
  954. {
  955. struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
  956. struct perf_probe_point *pp = &pev->point;
  957. Dwarf_Off off, noff;
  958. size_t cuhl;
  959. Dwarf_Die *diep;
  960. Dwarf *dbg;
  961. int ret = 0;
  962. pf.tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  963. if (pf.tevs == NULL)
  964. return -ENOMEM;
  965. *tevs = pf.tevs;
  966. pf.ntevs = 0;
  967. dbg = dwarf_begin(fd, DWARF_C_READ);
  968. if (!dbg) {
  969. pr_warning("No dwarf info found in the vmlinux - "
  970. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  971. free(pf.tevs);
  972. *tevs = NULL;
  973. return -EBADF;
  974. }
  975. #if _ELFUTILS_PREREQ(0, 142)
  976. /* Get the call frame information from this dwarf */
  977. pf.cfi = dwarf_getcfi(dbg);
  978. #endif
  979. off = 0;
  980. line_list__init(&pf.lcache);
  981. /* Loop on CUs (Compilation Unit) */
  982. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
  983. ret >= 0) {
  984. /* Get the DIE(Debugging Information Entry) of this CU */
  985. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  986. if (!diep)
  987. continue;
  988. /* Check if target file is included. */
  989. if (pp->file)
  990. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  991. else
  992. pf.fname = NULL;
  993. if (!pp->file || pf.fname) {
  994. if (pp->function)
  995. ret = find_probe_point_by_func(&pf);
  996. else if (pp->lazy_line)
  997. ret = find_probe_point_lazy(NULL, &pf);
  998. else {
  999. pf.lno = pp->line;
  1000. ret = find_probe_point_by_line(&pf);
  1001. }
  1002. }
  1003. off = noff;
  1004. }
  1005. line_list__free(&pf.lcache);
  1006. dwarf_end(dbg);
  1007. return (ret < 0) ? ret : pf.ntevs;
  1008. }
  1009. /* Reverse search */
  1010. int find_perf_probe_point(int fd, unsigned long addr,
  1011. struct perf_probe_point *ppt)
  1012. {
  1013. Dwarf_Die cudie, spdie, indie;
  1014. Dwarf *dbg;
  1015. Dwarf_Line *line;
  1016. Dwarf_Addr laddr, eaddr;
  1017. const char *tmp;
  1018. int lineno, ret = 0;
  1019. bool found = false;
  1020. dbg = dwarf_begin(fd, DWARF_C_READ);
  1021. if (!dbg)
  1022. return -EBADF;
  1023. /* Find cu die */
  1024. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
  1025. ret = -EINVAL;
  1026. goto end;
  1027. }
  1028. /* Find a corresponding line */
  1029. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  1030. if (line) {
  1031. if (dwarf_lineaddr(line, &laddr) == 0 &&
  1032. (Dwarf_Addr)addr == laddr &&
  1033. dwarf_lineno(line, &lineno) == 0) {
  1034. tmp = dwarf_linesrc(line, NULL, NULL);
  1035. if (tmp) {
  1036. ppt->line = lineno;
  1037. ppt->file = strdup(tmp);
  1038. if (ppt->file == NULL) {
  1039. ret = -ENOMEM;
  1040. goto end;
  1041. }
  1042. found = true;
  1043. }
  1044. }
  1045. }
  1046. /* Find a corresponding function */
  1047. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1048. tmp = dwarf_diename(&spdie);
  1049. if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
  1050. goto end;
  1051. if (ppt->line) {
  1052. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1053. &indie)) {
  1054. /* addr in an inline function */
  1055. tmp = dwarf_diename(&indie);
  1056. if (!tmp)
  1057. goto end;
  1058. ret = dwarf_decl_line(&indie, &lineno);
  1059. } else {
  1060. if (eaddr == addr) { /* Function entry */
  1061. lineno = ppt->line;
  1062. ret = 0;
  1063. } else
  1064. ret = dwarf_decl_line(&spdie, &lineno);
  1065. }
  1066. if (ret == 0) {
  1067. /* Make a relative line number */
  1068. ppt->line -= lineno;
  1069. goto found;
  1070. }
  1071. }
  1072. /* We don't have a line number, let's use offset */
  1073. ppt->offset = addr - (unsigned long)eaddr;
  1074. found:
  1075. ppt->function = strdup(tmp);
  1076. if (ppt->function == NULL) {
  1077. ret = -ENOMEM;
  1078. goto end;
  1079. }
  1080. found = true;
  1081. }
  1082. end:
  1083. dwarf_end(dbg);
  1084. if (ret >= 0)
  1085. ret = found ? 1 : 0;
  1086. return ret;
  1087. }
  1088. /* Add a line and store the src path */
  1089. static int line_range_add_line(const char *src, unsigned int lineno,
  1090. struct line_range *lr)
  1091. {
  1092. /* Copy source path */
  1093. if (!lr->path) {
  1094. lr->path = strdup(src);
  1095. if (lr->path == NULL)
  1096. return -ENOMEM;
  1097. }
  1098. return line_list__add_line(&lr->line_list, lineno);
  1099. }
  1100. /* Search function declaration lines */
  1101. static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
  1102. {
  1103. struct dwarf_callback_param *param = data;
  1104. struct line_finder *lf = param->data;
  1105. const char *src;
  1106. int lineno;
  1107. src = dwarf_decl_file(sp_die);
  1108. if (src && strtailcmp(src, lf->fname) != 0)
  1109. return DWARF_CB_OK;
  1110. if (dwarf_decl_line(sp_die, &lineno) != 0 ||
  1111. (lf->lno_s > lineno || lf->lno_e < lineno))
  1112. return DWARF_CB_OK;
  1113. param->retval = line_range_add_line(src, lineno, lf->lr);
  1114. if (param->retval < 0)
  1115. return DWARF_CB_ABORT;
  1116. return DWARF_CB_OK;
  1117. }
  1118. static int find_line_range_func_decl_lines(struct line_finder *lf)
  1119. {
  1120. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1121. dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
  1122. return param.retval;
  1123. }
  1124. /* Find line range from its line number */
  1125. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1126. {
  1127. Dwarf_Lines *lines;
  1128. Dwarf_Line *line;
  1129. size_t nlines, i;
  1130. Dwarf_Addr addr;
  1131. int lineno, ret = 0;
  1132. const char *src;
  1133. Dwarf_Die die_mem;
  1134. line_list__init(&lf->lr->line_list);
  1135. if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
  1136. pr_warning("No source lines found in this CU.\n");
  1137. return -ENOENT;
  1138. }
  1139. /* Search probable lines on lines list */
  1140. for (i = 0; i < nlines; i++) {
  1141. line = dwarf_onesrcline(lines, i);
  1142. if (dwarf_lineno(line, &lineno) != 0 ||
  1143. (lf->lno_s > lineno || lf->lno_e < lineno))
  1144. continue;
  1145. if (sp_die) {
  1146. /* Address filtering 1: does sp_die include addr? */
  1147. if (dwarf_lineaddr(line, &addr) != 0 ||
  1148. !dwarf_haspc(sp_die, addr))
  1149. continue;
  1150. /* Address filtering 2: No child include addr? */
  1151. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1152. continue;
  1153. }
  1154. /* TODO: Get fileno from line, but how? */
  1155. src = dwarf_linesrc(line, NULL, NULL);
  1156. if (strtailcmp(src, lf->fname) != 0)
  1157. continue;
  1158. ret = line_range_add_line(src, lineno, lf->lr);
  1159. if (ret < 0)
  1160. return ret;
  1161. }
  1162. /*
  1163. * Dwarf lines doesn't include function declarations. We have to
  1164. * check functions list or given function.
  1165. */
  1166. if (sp_die) {
  1167. src = dwarf_decl_file(sp_die);
  1168. if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
  1169. (lf->lno_s <= lineno && lf->lno_e >= lineno))
  1170. ret = line_range_add_line(src, lineno, lf->lr);
  1171. } else
  1172. ret = find_line_range_func_decl_lines(lf);
  1173. /* Update status */
  1174. if (ret >= 0)
  1175. if (!list_empty(&lf->lr->line_list))
  1176. ret = lf->found = 1;
  1177. else
  1178. ret = 0; /* Lines are not found */
  1179. else {
  1180. free(lf->lr->path);
  1181. lf->lr->path = NULL;
  1182. }
  1183. return ret;
  1184. }
  1185. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1186. {
  1187. struct dwarf_callback_param *param = data;
  1188. param->retval = find_line_range_by_line(in_die, param->data);
  1189. return DWARF_CB_ABORT; /* No need to find other instances */
  1190. }
  1191. /* Search function from function name */
  1192. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1193. {
  1194. struct dwarf_callback_param *param = data;
  1195. struct line_finder *lf = param->data;
  1196. struct line_range *lr = lf->lr;
  1197. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1198. die_compare_name(sp_die, lr->function)) {
  1199. lf->fname = dwarf_decl_file(sp_die);
  1200. dwarf_decl_line(sp_die, &lr->offset);
  1201. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1202. lf->lno_s = lr->offset + lr->start;
  1203. if (lf->lno_s < 0) /* Overflow */
  1204. lf->lno_s = INT_MAX;
  1205. lf->lno_e = lr->offset + lr->end;
  1206. if (lf->lno_e < 0) /* Overflow */
  1207. lf->lno_e = INT_MAX;
  1208. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1209. lr->start = lf->lno_s;
  1210. lr->end = lf->lno_e;
  1211. if (dwarf_func_inline(sp_die)) {
  1212. struct dwarf_callback_param _param;
  1213. _param.data = (void *)lf;
  1214. _param.retval = 0;
  1215. dwarf_func_inline_instances(sp_die,
  1216. line_range_inline_cb,
  1217. &_param);
  1218. param->retval = _param.retval;
  1219. } else
  1220. param->retval = find_line_range_by_line(sp_die, lf);
  1221. return DWARF_CB_ABORT;
  1222. }
  1223. return DWARF_CB_OK;
  1224. }
  1225. static int find_line_range_by_func(struct line_finder *lf)
  1226. {
  1227. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1228. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1229. return param.retval;
  1230. }
  1231. int find_line_range(int fd, struct line_range *lr)
  1232. {
  1233. struct line_finder lf = {.lr = lr, .found = 0};
  1234. int ret = 0;
  1235. Dwarf_Off off = 0, noff;
  1236. size_t cuhl;
  1237. Dwarf_Die *diep;
  1238. Dwarf *dbg;
  1239. const char *comp_dir;
  1240. dbg = dwarf_begin(fd, DWARF_C_READ);
  1241. if (!dbg) {
  1242. pr_warning("No dwarf info found in the vmlinux - "
  1243. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1244. return -EBADF;
  1245. }
  1246. /* Loop on CUs (Compilation Unit) */
  1247. while (!lf.found && ret >= 0) {
  1248. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1249. break;
  1250. /* Get the DIE(Debugging Information Entry) of this CU */
  1251. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1252. if (!diep)
  1253. continue;
  1254. /* Check if target file is included. */
  1255. if (lr->file)
  1256. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1257. else
  1258. lf.fname = 0;
  1259. if (!lr->file || lf.fname) {
  1260. if (lr->function)
  1261. ret = find_line_range_by_func(&lf);
  1262. else {
  1263. lf.lno_s = lr->start;
  1264. lf.lno_e = lr->end;
  1265. ret = find_line_range_by_line(NULL, &lf);
  1266. }
  1267. }
  1268. off = noff;
  1269. }
  1270. /* Store comp_dir */
  1271. if (lf.found) {
  1272. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1273. if (comp_dir) {
  1274. lr->comp_dir = strdup(comp_dir);
  1275. if (!lr->comp_dir)
  1276. ret = -ENOMEM;
  1277. }
  1278. }
  1279. pr_debug("path: %s\n", lr->path);
  1280. dwarf_end(dbg);
  1281. return (ret < 0) ? ret : lf.found;
  1282. }