probe-finder.c 36 KB

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