probe-finder.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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 (!is_c_varname(pf->pvar->var)) {
  606. /* Copy raw parameters */
  607. pf->tvar->value = strdup(pf->pvar->var);
  608. if (pf->tvar->value == NULL)
  609. return -ENOMEM;
  610. if (pf->pvar->type) {
  611. pf->tvar->type = strdup(pf->pvar->type);
  612. if (pf->tvar->type == NULL)
  613. return -ENOMEM;
  614. }
  615. if (pf->pvar->name) {
  616. pf->tvar->name = strdup(pf->pvar->name);
  617. if (pf->tvar->name == NULL)
  618. return -ENOMEM;
  619. } else
  620. pf->tvar->name = NULL;
  621. return 0;
  622. }
  623. if (pf->pvar->name)
  624. pf->tvar->name = strdup(pf->pvar->name);
  625. else {
  626. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  627. if (ret < 0)
  628. return ret;
  629. ptr = strchr(buf, ':'); /* Change type separator to _ */
  630. if (ptr)
  631. *ptr = '_';
  632. pf->tvar->name = strdup(buf);
  633. }
  634. if (pf->tvar->name == NULL)
  635. return -ENOMEM;
  636. pr_debug("Searching '%s' variable in context.\n",
  637. pf->pvar->var);
  638. /* Search child die for local variables and parameters. */
  639. if (die_find_variable(sp_die, pf->pvar->var, &vr_die))
  640. ret = convert_variable(&vr_die, pf);
  641. else {
  642. /* Search upper class */
  643. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  644. if (nscopes > 0) {
  645. ret = dwarf_getscopevar(scopes, nscopes, pf->pvar->var,
  646. 0, NULL, 0, 0, &vr_die);
  647. if (ret >= 0)
  648. ret = convert_variable(&vr_die, pf);
  649. else
  650. ret = -ENOENT;
  651. free(scopes);
  652. } else
  653. ret = -ENOENT;
  654. }
  655. if (ret < 0)
  656. pr_warning("Failed to find '%s' in this function.\n",
  657. pf->pvar->var);
  658. return ret;
  659. }
  660. /* Show a probe point to output buffer */
  661. static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  662. {
  663. struct probe_trace_event *tev;
  664. Dwarf_Addr eaddr;
  665. Dwarf_Die die_mem;
  666. const char *name;
  667. int ret, i;
  668. Dwarf_Attribute fb_attr;
  669. size_t nops;
  670. if (pf->ntevs == pf->max_tevs) {
  671. pr_warning("Too many( > %d) probe point found.\n",
  672. pf->max_tevs);
  673. return -ERANGE;
  674. }
  675. tev = &pf->tevs[pf->ntevs++];
  676. /* If no real subprogram, find a real one */
  677. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  678. sp_die = die_find_real_subprogram(&pf->cu_die,
  679. pf->addr, &die_mem);
  680. if (!sp_die) {
  681. pr_warning("Failed to find probe point in any "
  682. "functions.\n");
  683. return -ENOENT;
  684. }
  685. }
  686. /* Copy the name of probe point */
  687. name = dwarf_diename(sp_die);
  688. if (name) {
  689. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  690. pr_warning("Failed to get entry pc of %s\n",
  691. dwarf_diename(sp_die));
  692. return -ENOENT;
  693. }
  694. tev->point.symbol = strdup(name);
  695. if (tev->point.symbol == NULL)
  696. return -ENOMEM;
  697. tev->point.offset = (unsigned long)(pf->addr - eaddr);
  698. } else
  699. /* This function has no name. */
  700. tev->point.offset = (unsigned long)pf->addr;
  701. /* Return probe must be on the head of a subprogram */
  702. if (pf->pev->point.retprobe) {
  703. if (tev->point.offset != 0) {
  704. pr_warning("Return probe must be on the head of"
  705. " a real function\n");
  706. return -EINVAL;
  707. }
  708. tev->point.retprobe = true;
  709. }
  710. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  711. tev->point.offset);
  712. /* Get the frame base attribute/ops */
  713. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  714. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  715. if (ret <= 0 || nops == 0) {
  716. pf->fb_ops = NULL;
  717. #if _ELFUTILS_PREREQ(0, 142)
  718. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  719. pf->cfi != NULL) {
  720. Dwarf_Frame *frame;
  721. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  722. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  723. pr_warning("Failed to get CFA on 0x%jx\n",
  724. (uintmax_t)pf->addr);
  725. return -ENOENT;
  726. }
  727. #endif
  728. }
  729. /* Find each argument */
  730. tev->nargs = pf->pev->nargs;
  731. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  732. if (tev->args == NULL)
  733. return -ENOMEM;
  734. for (i = 0; i < pf->pev->nargs; i++) {
  735. pf->pvar = &pf->pev->args[i];
  736. pf->tvar = &tev->args[i];
  737. ret = find_variable(sp_die, pf);
  738. if (ret != 0)
  739. return ret;
  740. }
  741. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  742. pf->fb_ops = NULL;
  743. return 0;
  744. }
  745. /* Find probe point from its line number */
  746. static int find_probe_point_by_line(struct probe_finder *pf)
  747. {
  748. Dwarf_Lines *lines;
  749. Dwarf_Line *line;
  750. size_t nlines, i;
  751. Dwarf_Addr addr;
  752. int lineno;
  753. int ret = 0;
  754. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  755. pr_warning("No source lines found in this CU.\n");
  756. return -ENOENT;
  757. }
  758. for (i = 0; i < nlines && ret == 0; i++) {
  759. line = dwarf_onesrcline(lines, i);
  760. if (dwarf_lineno(line, &lineno) != 0 ||
  761. lineno != pf->lno)
  762. continue;
  763. /* TODO: Get fileno from line, but how? */
  764. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  765. continue;
  766. if (dwarf_lineaddr(line, &addr) != 0) {
  767. pr_warning("Failed to get the address of the line.\n");
  768. return -ENOENT;
  769. }
  770. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  771. (int)i, lineno, (uintmax_t)addr);
  772. pf->addr = addr;
  773. ret = convert_probe_point(NULL, pf);
  774. /* Continuing, because target line might be inlined. */
  775. }
  776. return ret;
  777. }
  778. /* Find lines which match lazy pattern */
  779. static int find_lazy_match_lines(struct list_head *head,
  780. const char *fname, const char *pat)
  781. {
  782. char *fbuf, *p1, *p2;
  783. int fd, line, nlines = -1;
  784. struct stat st;
  785. fd = open(fname, O_RDONLY);
  786. if (fd < 0) {
  787. pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
  788. return -errno;
  789. }
  790. if (fstat(fd, &st) < 0) {
  791. pr_warning("Failed to get the size of %s: %s\n",
  792. fname, strerror(errno));
  793. nlines = -errno;
  794. goto out_close;
  795. }
  796. nlines = -ENOMEM;
  797. fbuf = malloc(st.st_size + 2);
  798. if (fbuf == NULL)
  799. goto out_close;
  800. if (read(fd, fbuf, st.st_size) < 0) {
  801. pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
  802. nlines = -errno;
  803. goto out_free_fbuf;
  804. }
  805. fbuf[st.st_size] = '\n'; /* Dummy line */
  806. fbuf[st.st_size + 1] = '\0';
  807. p1 = fbuf;
  808. line = 1;
  809. nlines = 0;
  810. while ((p2 = strchr(p1, '\n')) != NULL) {
  811. *p2 = '\0';
  812. if (strlazymatch(p1, pat)) {
  813. line_list__add_line(head, line);
  814. nlines++;
  815. }
  816. line++;
  817. p1 = p2 + 1;
  818. }
  819. out_free_fbuf:
  820. free(fbuf);
  821. out_close:
  822. close(fd);
  823. return nlines;
  824. }
  825. /* Find probe points from lazy pattern */
  826. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  827. {
  828. Dwarf_Lines *lines;
  829. Dwarf_Line *line;
  830. size_t nlines, i;
  831. Dwarf_Addr addr;
  832. Dwarf_Die die_mem;
  833. int lineno;
  834. int ret = 0;
  835. if (list_empty(&pf->lcache)) {
  836. /* Matching lazy line pattern */
  837. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  838. pf->pev->point.lazy_line);
  839. if (ret == 0) {
  840. pr_debug("No matched lines found in %s.\n", pf->fname);
  841. return 0;
  842. } else if (ret < 0)
  843. return ret;
  844. }
  845. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  846. pr_warning("No source lines found in this CU.\n");
  847. return -ENOENT;
  848. }
  849. for (i = 0; i < nlines && ret >= 0; i++) {
  850. line = dwarf_onesrcline(lines, i);
  851. if (dwarf_lineno(line, &lineno) != 0 ||
  852. !line_list__has_line(&pf->lcache, lineno))
  853. continue;
  854. /* TODO: Get fileno from line, but how? */
  855. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  856. continue;
  857. if (dwarf_lineaddr(line, &addr) != 0) {
  858. pr_debug("Failed to get the address of line %d.\n",
  859. lineno);
  860. continue;
  861. }
  862. if (sp_die) {
  863. /* Address filtering 1: does sp_die include addr? */
  864. if (!dwarf_haspc(sp_die, addr))
  865. continue;
  866. /* Address filtering 2: No child include addr? */
  867. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  868. continue;
  869. }
  870. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  871. (int)i, lineno, (unsigned long long)addr);
  872. pf->addr = addr;
  873. ret = convert_probe_point(sp_die, pf);
  874. /* Continuing, because target line might be inlined. */
  875. }
  876. /* TODO: deallocate lines, but how? */
  877. return ret;
  878. }
  879. /* Callback parameter with return value */
  880. struct dwarf_callback_param {
  881. void *data;
  882. int retval;
  883. };
  884. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  885. {
  886. struct dwarf_callback_param *param = data;
  887. struct probe_finder *pf = param->data;
  888. struct perf_probe_point *pp = &pf->pev->point;
  889. Dwarf_Addr addr;
  890. if (pp->lazy_line)
  891. param->retval = find_probe_point_lazy(in_die, pf);
  892. else {
  893. /* Get probe address */
  894. if (dwarf_entrypc(in_die, &addr) != 0) {
  895. pr_warning("Failed to get entry pc of %s.\n",
  896. dwarf_diename(in_die));
  897. param->retval = -ENOENT;
  898. return DWARF_CB_ABORT;
  899. }
  900. pf->addr = addr;
  901. pf->addr += pp->offset;
  902. pr_debug("found inline addr: 0x%jx\n",
  903. (uintmax_t)pf->addr);
  904. param->retval = convert_probe_point(in_die, pf);
  905. if (param->retval < 0)
  906. return DWARF_CB_ABORT;
  907. }
  908. return DWARF_CB_OK;
  909. }
  910. /* Search function from function name */
  911. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  912. {
  913. struct dwarf_callback_param *param = data;
  914. struct probe_finder *pf = param->data;
  915. struct perf_probe_point *pp = &pf->pev->point;
  916. /* Check tag and diename */
  917. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  918. !die_compare_name(sp_die, pp->function))
  919. return DWARF_CB_OK;
  920. pf->fname = dwarf_decl_file(sp_die);
  921. if (pp->line) { /* Function relative line */
  922. dwarf_decl_line(sp_die, &pf->lno);
  923. pf->lno += pp->line;
  924. param->retval = find_probe_point_by_line(pf);
  925. } else if (!dwarf_func_inline(sp_die)) {
  926. /* Real function */
  927. if (pp->lazy_line)
  928. param->retval = find_probe_point_lazy(sp_die, pf);
  929. else {
  930. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  931. pr_warning("Failed to get entry pc of %s.\n",
  932. dwarf_diename(sp_die));
  933. param->retval = -ENOENT;
  934. return DWARF_CB_ABORT;
  935. }
  936. pf->addr += pp->offset;
  937. /* TODO: Check the address in this function */
  938. param->retval = convert_probe_point(sp_die, pf);
  939. }
  940. } else {
  941. struct dwarf_callback_param _param = {.data = (void *)pf,
  942. .retval = 0};
  943. /* Inlined function: search instances */
  944. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  945. &_param);
  946. param->retval = _param.retval;
  947. }
  948. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  949. }
  950. static int find_probe_point_by_func(struct probe_finder *pf)
  951. {
  952. struct dwarf_callback_param _param = {.data = (void *)pf,
  953. .retval = 0};
  954. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  955. return _param.retval;
  956. }
  957. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  958. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  959. struct probe_trace_event **tevs, int max_tevs)
  960. {
  961. struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
  962. struct perf_probe_point *pp = &pev->point;
  963. Dwarf_Off off, noff;
  964. size_t cuhl;
  965. Dwarf_Die *diep;
  966. Dwarf *dbg;
  967. int ret = 0;
  968. pf.tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  969. if (pf.tevs == NULL)
  970. return -ENOMEM;
  971. *tevs = pf.tevs;
  972. pf.ntevs = 0;
  973. dbg = dwarf_begin(fd, DWARF_C_READ);
  974. if (!dbg) {
  975. pr_warning("No dwarf info found in the vmlinux - "
  976. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  977. free(pf.tevs);
  978. *tevs = NULL;
  979. return -EBADF;
  980. }
  981. #if _ELFUTILS_PREREQ(0, 142)
  982. /* Get the call frame information from this dwarf */
  983. pf.cfi = dwarf_getcfi(dbg);
  984. #endif
  985. off = 0;
  986. line_list__init(&pf.lcache);
  987. /* Loop on CUs (Compilation Unit) */
  988. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
  989. ret >= 0) {
  990. /* Get the DIE(Debugging Information Entry) of this CU */
  991. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  992. if (!diep)
  993. continue;
  994. /* Check if target file is included. */
  995. if (pp->file)
  996. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  997. else
  998. pf.fname = NULL;
  999. if (!pp->file || pf.fname) {
  1000. if (pp->function)
  1001. ret = find_probe_point_by_func(&pf);
  1002. else if (pp->lazy_line)
  1003. ret = find_probe_point_lazy(NULL, &pf);
  1004. else {
  1005. pf.lno = pp->line;
  1006. ret = find_probe_point_by_line(&pf);
  1007. }
  1008. }
  1009. off = noff;
  1010. }
  1011. line_list__free(&pf.lcache);
  1012. dwarf_end(dbg);
  1013. return (ret < 0) ? ret : pf.ntevs;
  1014. }
  1015. /* Reverse search */
  1016. int find_perf_probe_point(int fd, unsigned long addr,
  1017. struct perf_probe_point *ppt)
  1018. {
  1019. Dwarf_Die cudie, spdie, indie;
  1020. Dwarf *dbg;
  1021. Dwarf_Line *line;
  1022. Dwarf_Addr laddr, eaddr;
  1023. const char *tmp;
  1024. int lineno, ret = 0;
  1025. bool found = false;
  1026. dbg = dwarf_begin(fd, DWARF_C_READ);
  1027. if (!dbg)
  1028. return -EBADF;
  1029. /* Find cu die */
  1030. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
  1031. ret = -EINVAL;
  1032. goto end;
  1033. }
  1034. /* Find a corresponding line */
  1035. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  1036. if (line) {
  1037. if (dwarf_lineaddr(line, &laddr) == 0 &&
  1038. (Dwarf_Addr)addr == laddr &&
  1039. dwarf_lineno(line, &lineno) == 0) {
  1040. tmp = dwarf_linesrc(line, NULL, NULL);
  1041. if (tmp) {
  1042. ppt->line = lineno;
  1043. ppt->file = strdup(tmp);
  1044. if (ppt->file == NULL) {
  1045. ret = -ENOMEM;
  1046. goto end;
  1047. }
  1048. found = true;
  1049. }
  1050. }
  1051. }
  1052. /* Find a corresponding function */
  1053. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1054. tmp = dwarf_diename(&spdie);
  1055. if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
  1056. goto end;
  1057. if (ppt->line) {
  1058. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1059. &indie)) {
  1060. /* addr in an inline function */
  1061. tmp = dwarf_diename(&indie);
  1062. if (!tmp)
  1063. goto end;
  1064. ret = dwarf_decl_line(&indie, &lineno);
  1065. } else {
  1066. if (eaddr == addr) { /* Function entry */
  1067. lineno = ppt->line;
  1068. ret = 0;
  1069. } else
  1070. ret = dwarf_decl_line(&spdie, &lineno);
  1071. }
  1072. if (ret == 0) {
  1073. /* Make a relative line number */
  1074. ppt->line -= lineno;
  1075. goto found;
  1076. }
  1077. }
  1078. /* We don't have a line number, let's use offset */
  1079. ppt->offset = addr - (unsigned long)eaddr;
  1080. found:
  1081. ppt->function = strdup(tmp);
  1082. if (ppt->function == NULL) {
  1083. ret = -ENOMEM;
  1084. goto end;
  1085. }
  1086. found = true;
  1087. }
  1088. end:
  1089. dwarf_end(dbg);
  1090. if (ret >= 0)
  1091. ret = found ? 1 : 0;
  1092. return ret;
  1093. }
  1094. /* Add a line and store the src path */
  1095. static int line_range_add_line(const char *src, unsigned int lineno,
  1096. struct line_range *lr)
  1097. {
  1098. /* Copy source path */
  1099. if (!lr->path) {
  1100. lr->path = strdup(src);
  1101. if (lr->path == NULL)
  1102. return -ENOMEM;
  1103. }
  1104. return line_list__add_line(&lr->line_list, lineno);
  1105. }
  1106. /* Search function declaration lines */
  1107. static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
  1108. {
  1109. struct dwarf_callback_param *param = data;
  1110. struct line_finder *lf = param->data;
  1111. const char *src;
  1112. int lineno;
  1113. src = dwarf_decl_file(sp_die);
  1114. if (src && strtailcmp(src, lf->fname) != 0)
  1115. return DWARF_CB_OK;
  1116. if (dwarf_decl_line(sp_die, &lineno) != 0 ||
  1117. (lf->lno_s > lineno || lf->lno_e < lineno))
  1118. return DWARF_CB_OK;
  1119. param->retval = line_range_add_line(src, lineno, lf->lr);
  1120. if (param->retval < 0)
  1121. return DWARF_CB_ABORT;
  1122. return DWARF_CB_OK;
  1123. }
  1124. static int find_line_range_func_decl_lines(struct line_finder *lf)
  1125. {
  1126. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1127. dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
  1128. return param.retval;
  1129. }
  1130. /* Find line range from its line number */
  1131. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1132. {
  1133. Dwarf_Lines *lines;
  1134. Dwarf_Line *line;
  1135. size_t nlines, i;
  1136. Dwarf_Addr addr;
  1137. int lineno, ret = 0;
  1138. const char *src;
  1139. Dwarf_Die die_mem;
  1140. line_list__init(&lf->lr->line_list);
  1141. if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
  1142. pr_warning("No source lines found in this CU.\n");
  1143. return -ENOENT;
  1144. }
  1145. /* Search probable lines on lines list */
  1146. for (i = 0; i < nlines; i++) {
  1147. line = dwarf_onesrcline(lines, i);
  1148. if (dwarf_lineno(line, &lineno) != 0 ||
  1149. (lf->lno_s > lineno || lf->lno_e < lineno))
  1150. continue;
  1151. if (sp_die) {
  1152. /* Address filtering 1: does sp_die include addr? */
  1153. if (dwarf_lineaddr(line, &addr) != 0 ||
  1154. !dwarf_haspc(sp_die, addr))
  1155. continue;
  1156. /* Address filtering 2: No child include addr? */
  1157. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1158. continue;
  1159. }
  1160. /* TODO: Get fileno from line, but how? */
  1161. src = dwarf_linesrc(line, NULL, NULL);
  1162. if (strtailcmp(src, lf->fname) != 0)
  1163. continue;
  1164. ret = line_range_add_line(src, lineno, lf->lr);
  1165. if (ret < 0)
  1166. return ret;
  1167. }
  1168. /*
  1169. * Dwarf lines doesn't include function declarations. We have to
  1170. * check functions list or given function.
  1171. */
  1172. if (sp_die) {
  1173. src = dwarf_decl_file(sp_die);
  1174. if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
  1175. (lf->lno_s <= lineno && lf->lno_e >= lineno))
  1176. ret = line_range_add_line(src, lineno, lf->lr);
  1177. } else
  1178. ret = find_line_range_func_decl_lines(lf);
  1179. /* Update status */
  1180. if (ret >= 0)
  1181. if (!list_empty(&lf->lr->line_list))
  1182. ret = lf->found = 1;
  1183. else
  1184. ret = 0; /* Lines are not found */
  1185. else {
  1186. free(lf->lr->path);
  1187. lf->lr->path = NULL;
  1188. }
  1189. return ret;
  1190. }
  1191. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1192. {
  1193. struct dwarf_callback_param *param = data;
  1194. param->retval = find_line_range_by_line(in_die, param->data);
  1195. return DWARF_CB_ABORT; /* No need to find other instances */
  1196. }
  1197. /* Search function from function name */
  1198. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1199. {
  1200. struct dwarf_callback_param *param = data;
  1201. struct line_finder *lf = param->data;
  1202. struct line_range *lr = lf->lr;
  1203. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1204. die_compare_name(sp_die, lr->function)) {
  1205. lf->fname = dwarf_decl_file(sp_die);
  1206. dwarf_decl_line(sp_die, &lr->offset);
  1207. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1208. lf->lno_s = lr->offset + lr->start;
  1209. if (lf->lno_s < 0) /* Overflow */
  1210. lf->lno_s = INT_MAX;
  1211. lf->lno_e = lr->offset + lr->end;
  1212. if (lf->lno_e < 0) /* Overflow */
  1213. lf->lno_e = INT_MAX;
  1214. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1215. lr->start = lf->lno_s;
  1216. lr->end = lf->lno_e;
  1217. if (dwarf_func_inline(sp_die)) {
  1218. struct dwarf_callback_param _param;
  1219. _param.data = (void *)lf;
  1220. _param.retval = 0;
  1221. dwarf_func_inline_instances(sp_die,
  1222. line_range_inline_cb,
  1223. &_param);
  1224. param->retval = _param.retval;
  1225. } else
  1226. param->retval = find_line_range_by_line(sp_die, lf);
  1227. return DWARF_CB_ABORT;
  1228. }
  1229. return DWARF_CB_OK;
  1230. }
  1231. static int find_line_range_by_func(struct line_finder *lf)
  1232. {
  1233. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1234. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1235. return param.retval;
  1236. }
  1237. int find_line_range(int fd, struct line_range *lr)
  1238. {
  1239. struct line_finder lf = {.lr = lr, .found = 0};
  1240. int ret = 0;
  1241. Dwarf_Off off = 0, noff;
  1242. size_t cuhl;
  1243. Dwarf_Die *diep;
  1244. Dwarf *dbg;
  1245. const char *comp_dir;
  1246. dbg = dwarf_begin(fd, DWARF_C_READ);
  1247. if (!dbg) {
  1248. pr_warning("No dwarf info found in the vmlinux - "
  1249. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1250. return -EBADF;
  1251. }
  1252. /* Loop on CUs (Compilation Unit) */
  1253. while (!lf.found && ret >= 0) {
  1254. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1255. break;
  1256. /* Get the DIE(Debugging Information Entry) of this CU */
  1257. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1258. if (!diep)
  1259. continue;
  1260. /* Check if target file is included. */
  1261. if (lr->file)
  1262. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1263. else
  1264. lf.fname = 0;
  1265. if (!lr->file || lf.fname) {
  1266. if (lr->function)
  1267. ret = find_line_range_by_func(&lf);
  1268. else {
  1269. lf.lno_s = lr->start;
  1270. lf.lno_e = lr->end;
  1271. ret = find_line_range_by_line(NULL, &lf);
  1272. }
  1273. }
  1274. off = noff;
  1275. }
  1276. /* Store comp_dir */
  1277. if (lf.found) {
  1278. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1279. if (comp_dir) {
  1280. lr->comp_dir = strdup(comp_dir);
  1281. if (!lr->comp_dir)
  1282. ret = -ENOMEM;
  1283. }
  1284. }
  1285. pr_debug("path: %s\n", lr->path);
  1286. dwarf_end(dbg);
  1287. return (ret < 0) ? ret : lf.found;
  1288. }