probe-finder.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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 <dwarf-regs.h>
  33. #include <linux/bitops.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. /* Line number list operations */
  42. /* Add a line to line number list */
  43. static int line_list__add_line(struct list_head *head, int line)
  44. {
  45. struct line_node *ln;
  46. struct list_head *p;
  47. /* Reverse search, because new line will be the last one */
  48. list_for_each_entry_reverse(ln, head, list) {
  49. if (ln->line < line) {
  50. p = &ln->list;
  51. goto found;
  52. } else if (ln->line == line) /* Already exist */
  53. return 1;
  54. }
  55. /* List is empty, or the smallest entry */
  56. p = head;
  57. found:
  58. pr_debug("line list: add a line %u\n", line);
  59. ln = zalloc(sizeof(struct line_node));
  60. if (ln == NULL)
  61. return -ENOMEM;
  62. ln->line = line;
  63. INIT_LIST_HEAD(&ln->list);
  64. list_add(&ln->list, p);
  65. return 0;
  66. }
  67. /* Check if the line in line number list */
  68. static int line_list__has_line(struct list_head *head, int line)
  69. {
  70. struct line_node *ln;
  71. /* Reverse search, because new line will be the last one */
  72. list_for_each_entry(ln, head, list)
  73. if (ln->line == line)
  74. return 1;
  75. return 0;
  76. }
  77. /* Init line number list */
  78. static void line_list__init(struct list_head *head)
  79. {
  80. INIT_LIST_HEAD(head);
  81. }
  82. /* Free line number list */
  83. static void line_list__free(struct list_head *head)
  84. {
  85. struct line_node *ln;
  86. while (!list_empty(head)) {
  87. ln = list_first_entry(head, struct line_node, list);
  88. list_del(&ln->list);
  89. free(ln);
  90. }
  91. }
  92. /* Dwarf FL wrappers */
  93. static char *debuginfo_path; /* Currently dummy */
  94. static const Dwfl_Callbacks offline_callbacks = {
  95. .find_debuginfo = dwfl_standard_find_debuginfo,
  96. .debuginfo_path = &debuginfo_path,
  97. .section_address = dwfl_offline_section_address,
  98. /* We use this table for core files too. */
  99. .find_elf = dwfl_build_id_find_elf,
  100. };
  101. /* Get a Dwarf from offline image */
  102. static int debuginfo__init_offline_dwarf(struct debuginfo *self,
  103. const char *path)
  104. {
  105. Dwfl_Module *mod;
  106. int fd;
  107. fd = open(path, O_RDONLY);
  108. if (fd < 0)
  109. return fd;
  110. self->dwfl = dwfl_begin(&offline_callbacks);
  111. if (!self->dwfl)
  112. goto error;
  113. mod = dwfl_report_offline(self->dwfl, "", "", fd);
  114. if (!mod)
  115. goto error;
  116. self->dbg = dwfl_module_getdwarf(mod, &self->bias);
  117. if (!self->dbg)
  118. goto error;
  119. return 0;
  120. error:
  121. if (self->dwfl)
  122. dwfl_end(self->dwfl);
  123. else
  124. close(fd);
  125. memset(self, 0, sizeof(*self));
  126. return -ENOENT;
  127. }
  128. #if _ELFUTILS_PREREQ(0, 148)
  129. /* This method is buggy if elfutils is older than 0.148 */
  130. static int __linux_kernel_find_elf(Dwfl_Module *mod,
  131. void **userdata,
  132. const char *module_name,
  133. Dwarf_Addr base,
  134. char **file_name, Elf **elfp)
  135. {
  136. int fd;
  137. const char *path = kernel_get_module_path(module_name);
  138. pr_debug2("Use file %s for %s\n", path, module_name);
  139. if (path) {
  140. fd = open(path, O_RDONLY);
  141. if (fd >= 0) {
  142. *file_name = strdup(path);
  143. return fd;
  144. }
  145. }
  146. /* If failed, try to call standard method */
  147. return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
  148. file_name, elfp);
  149. }
  150. static const Dwfl_Callbacks kernel_callbacks = {
  151. .find_debuginfo = dwfl_standard_find_debuginfo,
  152. .debuginfo_path = &debuginfo_path,
  153. .find_elf = __linux_kernel_find_elf,
  154. .section_address = dwfl_linux_kernel_module_section_address,
  155. };
  156. /* Get a Dwarf from live kernel image */
  157. static int debuginfo__init_online_kernel_dwarf(struct debuginfo *self,
  158. Dwarf_Addr addr)
  159. {
  160. self->dwfl = dwfl_begin(&kernel_callbacks);
  161. if (!self->dwfl)
  162. return -EINVAL;
  163. /* Load the kernel dwarves: Don't care the result here */
  164. dwfl_linux_kernel_report_kernel(self->dwfl);
  165. dwfl_linux_kernel_report_modules(self->dwfl);
  166. self->dbg = dwfl_addrdwarf(self->dwfl, addr, &self->bias);
  167. /* Here, check whether we could get a real dwarf */
  168. if (!self->dbg) {
  169. pr_debug("Failed to find kernel dwarf at %lx\n",
  170. (unsigned long)addr);
  171. dwfl_end(self->dwfl);
  172. memset(self, 0, sizeof(*self));
  173. return -ENOENT;
  174. }
  175. return 0;
  176. }
  177. #else
  178. /* With older elfutils, this just support kernel module... */
  179. static int debuginfo__init_online_kernel_dwarf(struct debuginfo *self,
  180. Dwarf_Addr addr __used)
  181. {
  182. const char *path = kernel_get_module_path("kernel");
  183. if (!path) {
  184. pr_err("Failed to find vmlinux path\n");
  185. return -ENOENT;
  186. }
  187. pr_debug2("Use file %s for debuginfo\n", path);
  188. return debuginfo__init_offline_dwarf(self, path);
  189. }
  190. #endif
  191. struct debuginfo *debuginfo__new(const char *path)
  192. {
  193. struct debuginfo *self = zalloc(sizeof(struct debuginfo));
  194. if (!self)
  195. return NULL;
  196. if (debuginfo__init_offline_dwarf(self, path) < 0) {
  197. free(self);
  198. self = NULL;
  199. }
  200. return self;
  201. }
  202. struct debuginfo *debuginfo__new_online_kernel(unsigned long addr)
  203. {
  204. struct debuginfo *self = zalloc(sizeof(struct debuginfo));
  205. if (!self)
  206. return NULL;
  207. if (debuginfo__init_online_kernel_dwarf(self, (Dwarf_Addr)addr) < 0) {
  208. free(self);
  209. self = NULL;
  210. }
  211. return self;
  212. }
  213. void debuginfo__delete(struct debuginfo *self)
  214. {
  215. if (self) {
  216. if (self->dwfl)
  217. dwfl_end(self->dwfl);
  218. free(self);
  219. }
  220. }
  221. /*
  222. * Probe finder related functions
  223. */
  224. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  225. {
  226. struct probe_trace_arg_ref *ref;
  227. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  228. if (ref != NULL)
  229. ref->offset = offs;
  230. return ref;
  231. }
  232. /*
  233. * Convert a location into trace_arg.
  234. * If tvar == NULL, this just checks variable can be converted.
  235. */
  236. static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
  237. Dwarf_Op *fb_ops,
  238. struct probe_trace_arg *tvar)
  239. {
  240. Dwarf_Attribute attr;
  241. Dwarf_Op *op;
  242. size_t nops;
  243. unsigned int regn;
  244. Dwarf_Word offs = 0;
  245. bool ref = false;
  246. const char *regs;
  247. int ret;
  248. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  249. goto static_var;
  250. /* TODO: handle more than 1 exprs */
  251. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  252. dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
  253. nops == 0) {
  254. /* TODO: Support const_value */
  255. return -ENOENT;
  256. }
  257. if (op->atom == DW_OP_addr) {
  258. static_var:
  259. if (!tvar)
  260. return 0;
  261. /* Static variables on memory (not stack), make @varname */
  262. ret = strlen(dwarf_diename(vr_die));
  263. tvar->value = zalloc(ret + 2);
  264. if (tvar->value == NULL)
  265. return -ENOMEM;
  266. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  267. tvar->ref = alloc_trace_arg_ref((long)offs);
  268. if (tvar->ref == NULL)
  269. return -ENOMEM;
  270. return 0;
  271. }
  272. /* If this is based on frame buffer, set the offset */
  273. if (op->atom == DW_OP_fbreg) {
  274. if (fb_ops == NULL)
  275. return -ENOTSUP;
  276. ref = true;
  277. offs = op->number;
  278. op = &fb_ops[0];
  279. }
  280. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  281. regn = op->atom - DW_OP_breg0;
  282. offs += op->number;
  283. ref = true;
  284. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  285. regn = op->atom - DW_OP_reg0;
  286. } else if (op->atom == DW_OP_bregx) {
  287. regn = op->number;
  288. offs += op->number2;
  289. ref = true;
  290. } else if (op->atom == DW_OP_regx) {
  291. regn = op->number;
  292. } else {
  293. pr_debug("DW_OP %x is not supported.\n", op->atom);
  294. return -ENOTSUP;
  295. }
  296. if (!tvar)
  297. return 0;
  298. regs = get_arch_regstr(regn);
  299. if (!regs) {
  300. /* This should be a bug in DWARF or this tool */
  301. pr_warning("Mapping for the register number %u "
  302. "missing on this architecture.\n", regn);
  303. return -ERANGE;
  304. }
  305. tvar->value = strdup(regs);
  306. if (tvar->value == NULL)
  307. return -ENOMEM;
  308. if (ref) {
  309. tvar->ref = alloc_trace_arg_ref((long)offs);
  310. if (tvar->ref == NULL)
  311. return -ENOMEM;
  312. }
  313. return 0;
  314. }
  315. #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
  316. static int convert_variable_type(Dwarf_Die *vr_die,
  317. struct probe_trace_arg *tvar,
  318. const char *cast)
  319. {
  320. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  321. Dwarf_Die type;
  322. char buf[16];
  323. int bsize, boffs, total;
  324. int ret;
  325. /* TODO: check all types */
  326. if (cast && strcmp(cast, "string") != 0) {
  327. /* Non string type is OK */
  328. tvar->type = strdup(cast);
  329. return (tvar->type == NULL) ? -ENOMEM : 0;
  330. }
  331. bsize = dwarf_bitsize(vr_die);
  332. if (bsize > 0) {
  333. /* This is a bitfield */
  334. boffs = dwarf_bitoffset(vr_die);
  335. total = dwarf_bytesize(vr_die);
  336. if (boffs < 0 || total < 0)
  337. return -ENOENT;
  338. ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
  339. BYTES_TO_BITS(total));
  340. goto formatted;
  341. }
  342. if (die_get_real_type(vr_die, &type) == NULL) {
  343. pr_warning("Failed to get a type information of %s.\n",
  344. dwarf_diename(vr_die));
  345. return -ENOENT;
  346. }
  347. pr_debug("%s type is %s.\n",
  348. dwarf_diename(vr_die), dwarf_diename(&type));
  349. if (cast && strcmp(cast, "string") == 0) { /* String type */
  350. ret = dwarf_tag(&type);
  351. if (ret != DW_TAG_pointer_type &&
  352. ret != DW_TAG_array_type) {
  353. pr_warning("Failed to cast into string: "
  354. "%s(%s) is not a pointer nor array.\n",
  355. dwarf_diename(vr_die), dwarf_diename(&type));
  356. return -EINVAL;
  357. }
  358. if (ret == DW_TAG_pointer_type) {
  359. if (die_get_real_type(&type, &type) == NULL) {
  360. pr_warning("Failed to get a type"
  361. " information.\n");
  362. return -ENOENT;
  363. }
  364. while (*ref_ptr)
  365. ref_ptr = &(*ref_ptr)->next;
  366. /* Add new reference with offset +0 */
  367. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  368. if (*ref_ptr == NULL) {
  369. pr_warning("Out of memory error\n");
  370. return -ENOMEM;
  371. }
  372. }
  373. if (!die_compare_name(&type, "char") &&
  374. !die_compare_name(&type, "unsigned char")) {
  375. pr_warning("Failed to cast into string: "
  376. "%s is not (unsigned) char *.\n",
  377. dwarf_diename(vr_die));
  378. return -EINVAL;
  379. }
  380. tvar->type = strdup(cast);
  381. return (tvar->type == NULL) ? -ENOMEM : 0;
  382. }
  383. ret = dwarf_bytesize(&type);
  384. if (ret <= 0)
  385. /* No size ... try to use default type */
  386. return 0;
  387. ret = BYTES_TO_BITS(ret);
  388. /* Check the bitwidth */
  389. if (ret > MAX_BASIC_TYPE_BITS) {
  390. pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
  391. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  392. ret = MAX_BASIC_TYPE_BITS;
  393. }
  394. ret = snprintf(buf, 16, "%c%d",
  395. die_is_signed_type(&type) ? 's' : 'u', ret);
  396. formatted:
  397. if (ret < 0 || ret >= 16) {
  398. if (ret >= 16)
  399. ret = -E2BIG;
  400. pr_warning("Failed to convert variable type: %s\n",
  401. strerror(-ret));
  402. return ret;
  403. }
  404. tvar->type = strdup(buf);
  405. if (tvar->type == NULL)
  406. return -ENOMEM;
  407. return 0;
  408. }
  409. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  410. struct perf_probe_arg_field *field,
  411. struct probe_trace_arg_ref **ref_ptr,
  412. Dwarf_Die *die_mem)
  413. {
  414. struct probe_trace_arg_ref *ref = *ref_ptr;
  415. Dwarf_Die type;
  416. Dwarf_Word offs;
  417. int ret, tag;
  418. pr_debug("converting %s in %s\n", field->name, varname);
  419. if (die_get_real_type(vr_die, &type) == NULL) {
  420. pr_warning("Failed to get the type of %s.\n", varname);
  421. return -ENOENT;
  422. }
  423. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  424. tag = dwarf_tag(&type);
  425. if (field->name[0] == '[' &&
  426. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  427. if (field->next)
  428. /* Save original type for next field */
  429. memcpy(die_mem, &type, sizeof(*die_mem));
  430. /* Get the type of this array */
  431. if (die_get_real_type(&type, &type) == NULL) {
  432. pr_warning("Failed to get the type of %s.\n", varname);
  433. return -ENOENT;
  434. }
  435. pr_debug2("Array real type: (%x)\n",
  436. (unsigned)dwarf_dieoffset(&type));
  437. if (tag == DW_TAG_pointer_type) {
  438. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  439. if (ref == NULL)
  440. return -ENOMEM;
  441. if (*ref_ptr)
  442. (*ref_ptr)->next = ref;
  443. else
  444. *ref_ptr = ref;
  445. }
  446. ref->offset += dwarf_bytesize(&type) * field->index;
  447. if (!field->next)
  448. /* Save vr_die for converting types */
  449. memcpy(die_mem, vr_die, sizeof(*die_mem));
  450. goto next;
  451. } else if (tag == DW_TAG_pointer_type) {
  452. /* Check the pointer and dereference */
  453. if (!field->ref) {
  454. pr_err("Semantic error: %s must be referred by '->'\n",
  455. field->name);
  456. return -EINVAL;
  457. }
  458. /* Get the type pointed by this pointer */
  459. if (die_get_real_type(&type, &type) == NULL) {
  460. pr_warning("Failed to get the type of %s.\n", varname);
  461. return -ENOENT;
  462. }
  463. /* Verify it is a data structure */
  464. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  465. pr_warning("%s is not a data structure.\n", varname);
  466. return -EINVAL;
  467. }
  468. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  469. if (ref == NULL)
  470. return -ENOMEM;
  471. if (*ref_ptr)
  472. (*ref_ptr)->next = ref;
  473. else
  474. *ref_ptr = ref;
  475. } else {
  476. /* Verify it is a data structure */
  477. if (tag != DW_TAG_structure_type) {
  478. pr_warning("%s is not a data structure.\n", varname);
  479. return -EINVAL;
  480. }
  481. if (field->name[0] == '[') {
  482. pr_err("Semantic error: %s is not a pointor"
  483. " nor array.\n", varname);
  484. return -EINVAL;
  485. }
  486. if (field->ref) {
  487. pr_err("Semantic error: %s must be referred by '.'\n",
  488. field->name);
  489. return -EINVAL;
  490. }
  491. if (!ref) {
  492. pr_warning("Structure on a register is not "
  493. "supported yet.\n");
  494. return -ENOTSUP;
  495. }
  496. }
  497. if (die_find_member(&type, field->name, die_mem) == NULL) {
  498. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  499. dwarf_diename(&type), field->name);
  500. return -EINVAL;
  501. }
  502. /* Get the offset of the field */
  503. ret = die_get_data_member_location(die_mem, &offs);
  504. if (ret < 0) {
  505. pr_warning("Failed to get the offset of %s.\n", field->name);
  506. return ret;
  507. }
  508. ref->offset += (long)offs;
  509. next:
  510. /* Converting next field */
  511. if (field->next)
  512. return convert_variable_fields(die_mem, field->name,
  513. field->next, &ref, die_mem);
  514. else
  515. return 0;
  516. }
  517. /* Show a variables in kprobe event format */
  518. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  519. {
  520. Dwarf_Die die_mem;
  521. int ret;
  522. pr_debug("Converting variable %s into trace event.\n",
  523. dwarf_diename(vr_die));
  524. ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
  525. pf->tvar);
  526. if (ret == -ENOENT)
  527. pr_err("Failed to find the location of %s at this address.\n"
  528. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  529. else if (ret == -ENOTSUP)
  530. pr_err("Sorry, we don't support this variable location yet.\n");
  531. else if (pf->pvar->field) {
  532. ret = convert_variable_fields(vr_die, pf->pvar->var,
  533. pf->pvar->field, &pf->tvar->ref,
  534. &die_mem);
  535. vr_die = &die_mem;
  536. }
  537. if (ret == 0)
  538. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  539. /* *expr will be cached in libdw. Don't free it. */
  540. return ret;
  541. }
  542. /* Find a variable in a scope DIE */
  543. static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf)
  544. {
  545. Dwarf_Die vr_die;
  546. char buf[32], *ptr;
  547. int ret = 0;
  548. if (!is_c_varname(pf->pvar->var)) {
  549. /* Copy raw parameters */
  550. pf->tvar->value = strdup(pf->pvar->var);
  551. if (pf->tvar->value == NULL)
  552. return -ENOMEM;
  553. if (pf->pvar->type) {
  554. pf->tvar->type = strdup(pf->pvar->type);
  555. if (pf->tvar->type == NULL)
  556. return -ENOMEM;
  557. }
  558. if (pf->pvar->name) {
  559. pf->tvar->name = strdup(pf->pvar->name);
  560. if (pf->tvar->name == NULL)
  561. return -ENOMEM;
  562. } else
  563. pf->tvar->name = NULL;
  564. return 0;
  565. }
  566. if (pf->pvar->name)
  567. pf->tvar->name = strdup(pf->pvar->name);
  568. else {
  569. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  570. if (ret < 0)
  571. return ret;
  572. ptr = strchr(buf, ':'); /* Change type separator to _ */
  573. if (ptr)
  574. *ptr = '_';
  575. pf->tvar->name = strdup(buf);
  576. }
  577. if (pf->tvar->name == NULL)
  578. return -ENOMEM;
  579. pr_debug("Searching '%s' variable in context.\n", pf->pvar->var);
  580. /* Search child die for local variables and parameters. */
  581. if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) {
  582. /* Search again in global variables */
  583. if (!die_find_variable_at(&pf->cu_die, pf->pvar->var, 0, &vr_die))
  584. ret = -ENOENT;
  585. }
  586. if (ret >= 0)
  587. ret = convert_variable(&vr_die, pf);
  588. if (ret < 0)
  589. pr_warning("Failed to find '%s' in this function.\n",
  590. pf->pvar->var);
  591. return ret;
  592. }
  593. /* Convert subprogram DIE to trace point */
  594. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  595. bool retprobe, struct probe_trace_point *tp)
  596. {
  597. Dwarf_Addr eaddr, highaddr;
  598. const char *name;
  599. /* Copy the name of probe point */
  600. name = dwarf_diename(sp_die);
  601. if (name) {
  602. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  603. pr_warning("Failed to get entry address of %s\n",
  604. dwarf_diename(sp_die));
  605. return -ENOENT;
  606. }
  607. if (dwarf_highpc(sp_die, &highaddr) != 0) {
  608. pr_warning("Failed to get end address of %s\n",
  609. dwarf_diename(sp_die));
  610. return -ENOENT;
  611. }
  612. if (paddr > highaddr) {
  613. pr_warning("Offset specified is greater than size of %s\n",
  614. dwarf_diename(sp_die));
  615. return -EINVAL;
  616. }
  617. tp->symbol = strdup(name);
  618. if (tp->symbol == NULL)
  619. return -ENOMEM;
  620. tp->offset = (unsigned long)(paddr - eaddr);
  621. } else
  622. /* This function has no name. */
  623. tp->offset = (unsigned long)paddr;
  624. /* Return probe must be on the head of a subprogram */
  625. if (retprobe) {
  626. if (eaddr != paddr) {
  627. pr_warning("Return probe must be on the head of"
  628. " a real function.\n");
  629. return -EINVAL;
  630. }
  631. tp->retprobe = true;
  632. }
  633. return 0;
  634. }
  635. /* Call probe_finder callback with scope DIE */
  636. static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf)
  637. {
  638. Dwarf_Attribute fb_attr;
  639. size_t nops;
  640. int ret;
  641. if (!sc_die) {
  642. pr_err("Caller must pass a scope DIE. Program error.\n");
  643. return -EINVAL;
  644. }
  645. /* If not a real subprogram, find a real one */
  646. if (dwarf_tag(sc_die) != DW_TAG_subprogram) {
  647. if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) {
  648. pr_warning("Failed to find probe point in any "
  649. "functions.\n");
  650. return -ENOENT;
  651. }
  652. } else
  653. memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die));
  654. /* Get the frame base attribute/ops from subprogram */
  655. dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr);
  656. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  657. if (ret <= 0 || nops == 0) {
  658. pf->fb_ops = NULL;
  659. #if _ELFUTILS_PREREQ(0, 142)
  660. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  661. pf->cfi != NULL) {
  662. Dwarf_Frame *frame;
  663. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  664. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  665. pr_warning("Failed to get call frame on 0x%jx\n",
  666. (uintmax_t)pf->addr);
  667. return -ENOENT;
  668. }
  669. #endif
  670. }
  671. /* Call finder's callback handler */
  672. ret = pf->callback(sc_die, pf);
  673. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  674. pf->fb_ops = NULL;
  675. return ret;
  676. }
  677. struct find_scope_param {
  678. const char *function;
  679. const char *file;
  680. int line;
  681. int diff;
  682. Dwarf_Die *die_mem;
  683. bool found;
  684. };
  685. static int find_best_scope_cb(Dwarf_Die *fn_die, void *data)
  686. {
  687. struct find_scope_param *fsp = data;
  688. const char *file;
  689. int lno;
  690. /* Skip if declared file name does not match */
  691. if (fsp->file) {
  692. file = dwarf_decl_file(fn_die);
  693. if (!file || strcmp(fsp->file, file) != 0)
  694. return 0;
  695. }
  696. /* If the function name is given, that's what user expects */
  697. if (fsp->function) {
  698. if (die_compare_name(fn_die, fsp->function)) {
  699. memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
  700. fsp->found = true;
  701. return 1;
  702. }
  703. } else {
  704. /* With the line number, find the nearest declared DIE */
  705. dwarf_decl_line(fn_die, &lno);
  706. if (lno < fsp->line && fsp->diff > fsp->line - lno) {
  707. /* Keep a candidate and continue */
  708. fsp->diff = fsp->line - lno;
  709. memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die));
  710. fsp->found = true;
  711. }
  712. }
  713. return 0;
  714. }
  715. /* Find an appropriate scope fits to given conditions */
  716. static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem)
  717. {
  718. struct find_scope_param fsp = {
  719. .function = pf->pev->point.function,
  720. .file = pf->fname,
  721. .line = pf->lno,
  722. .diff = INT_MAX,
  723. .die_mem = die_mem,
  724. .found = false,
  725. };
  726. cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb, &fsp);
  727. return fsp.found ? die_mem : NULL;
  728. }
  729. static int probe_point_line_walker(const char *fname, int lineno,
  730. Dwarf_Addr addr, void *data)
  731. {
  732. struct probe_finder *pf = data;
  733. Dwarf_Die *sc_die, die_mem;
  734. int ret;
  735. if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
  736. return 0;
  737. pf->addr = addr;
  738. sc_die = find_best_scope(pf, &die_mem);
  739. if (!sc_die) {
  740. pr_warning("Failed to find scope of probe point.\n");
  741. return -ENOENT;
  742. }
  743. ret = call_probe_finder(sc_die, pf);
  744. /* Continue if no error, because the line will be in inline function */
  745. return ret < 0 ? ret : 0;
  746. }
  747. /* Find probe point from its line number */
  748. static int find_probe_point_by_line(struct probe_finder *pf)
  749. {
  750. return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
  751. }
  752. /* Find lines which match lazy pattern */
  753. static int find_lazy_match_lines(struct list_head *head,
  754. const char *fname, const char *pat)
  755. {
  756. FILE *fp;
  757. char *line = NULL;
  758. size_t line_len;
  759. ssize_t len;
  760. int count = 0, linenum = 1;
  761. fp = fopen(fname, "r");
  762. if (!fp) {
  763. pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
  764. return -errno;
  765. }
  766. while ((len = getline(&line, &line_len, fp)) > 0) {
  767. if (line[len - 1] == '\n')
  768. line[len - 1] = '\0';
  769. if (strlazymatch(line, pat)) {
  770. line_list__add_line(head, linenum);
  771. count++;
  772. }
  773. linenum++;
  774. }
  775. if (ferror(fp))
  776. count = -errno;
  777. free(line);
  778. fclose(fp);
  779. if (count == 0)
  780. pr_debug("No matched lines found in %s.\n", fname);
  781. return count;
  782. }
  783. static int probe_point_lazy_walker(const char *fname, int lineno,
  784. Dwarf_Addr addr, void *data)
  785. {
  786. struct probe_finder *pf = data;
  787. Dwarf_Die *sc_die, die_mem;
  788. int ret;
  789. if (!line_list__has_line(&pf->lcache, lineno) ||
  790. strtailcmp(fname, pf->fname) != 0)
  791. return 0;
  792. pr_debug("Probe line found: line:%d addr:0x%llx\n",
  793. lineno, (unsigned long long)addr);
  794. pf->addr = addr;
  795. pf->lno = lineno;
  796. sc_die = find_best_scope(pf, &die_mem);
  797. if (!sc_die) {
  798. pr_warning("Failed to find scope of probe point.\n");
  799. return -ENOENT;
  800. }
  801. ret = call_probe_finder(sc_die, pf);
  802. /*
  803. * Continue if no error, because the lazy pattern will match
  804. * to other lines
  805. */
  806. return ret < 0 ? ret : 0;
  807. }
  808. /* Find probe points from lazy pattern */
  809. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  810. {
  811. int ret = 0;
  812. if (list_empty(&pf->lcache)) {
  813. /* Matching lazy line pattern */
  814. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  815. pf->pev->point.lazy_line);
  816. if (ret <= 0)
  817. return ret;
  818. }
  819. return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
  820. }
  821. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  822. {
  823. struct probe_finder *pf = data;
  824. struct perf_probe_point *pp = &pf->pev->point;
  825. Dwarf_Addr addr;
  826. int ret;
  827. if (pp->lazy_line)
  828. ret = find_probe_point_lazy(in_die, pf);
  829. else {
  830. /* Get probe address */
  831. if (dwarf_entrypc(in_die, &addr) != 0) {
  832. pr_warning("Failed to get entry address of %s.\n",
  833. dwarf_diename(in_die));
  834. return -ENOENT;
  835. }
  836. pf->addr = addr;
  837. pf->addr += pp->offset;
  838. pr_debug("found inline addr: 0x%jx\n",
  839. (uintmax_t)pf->addr);
  840. ret = call_probe_finder(in_die, pf);
  841. }
  842. return ret;
  843. }
  844. /* Callback parameter with return value for libdw */
  845. struct dwarf_callback_param {
  846. void *data;
  847. int retval;
  848. };
  849. /* Search function from function name */
  850. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  851. {
  852. struct dwarf_callback_param *param = data;
  853. struct probe_finder *pf = param->data;
  854. struct perf_probe_point *pp = &pf->pev->point;
  855. Dwarf_Attribute attr;
  856. /* Check tag and diename */
  857. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  858. !die_compare_name(sp_die, pp->function) ||
  859. dwarf_attr(sp_die, DW_AT_declaration, &attr))
  860. return DWARF_CB_OK;
  861. /* Check declared file */
  862. if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
  863. return DWARF_CB_OK;
  864. pf->fname = dwarf_decl_file(sp_die);
  865. if (pp->line) { /* Function relative line */
  866. dwarf_decl_line(sp_die, &pf->lno);
  867. pf->lno += pp->line;
  868. param->retval = find_probe_point_by_line(pf);
  869. } else if (!dwarf_func_inline(sp_die)) {
  870. /* Real function */
  871. if (pp->lazy_line)
  872. param->retval = find_probe_point_lazy(sp_die, pf);
  873. else {
  874. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  875. pr_warning("Failed to get entry address of "
  876. "%s.\n", dwarf_diename(sp_die));
  877. param->retval = -ENOENT;
  878. return DWARF_CB_ABORT;
  879. }
  880. pf->addr += pp->offset;
  881. /* TODO: Check the address in this function */
  882. param->retval = call_probe_finder(sp_die, pf);
  883. }
  884. } else
  885. /* Inlined function: search instances */
  886. param->retval = die_walk_instances(sp_die,
  887. probe_point_inline_cb, (void *)pf);
  888. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  889. }
  890. static int find_probe_point_by_func(struct probe_finder *pf)
  891. {
  892. struct dwarf_callback_param _param = {.data = (void *)pf,
  893. .retval = 0};
  894. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  895. return _param.retval;
  896. }
  897. struct pubname_callback_param {
  898. char *function;
  899. char *file;
  900. Dwarf_Die *cu_die;
  901. Dwarf_Die *sp_die;
  902. int found;
  903. };
  904. static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
  905. {
  906. struct pubname_callback_param *param = data;
  907. if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
  908. if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
  909. return DWARF_CB_OK;
  910. if (die_compare_name(param->sp_die, param->function)) {
  911. if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
  912. return DWARF_CB_OK;
  913. if (param->file &&
  914. strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
  915. return DWARF_CB_OK;
  916. param->found = 1;
  917. return DWARF_CB_ABORT;
  918. }
  919. }
  920. return DWARF_CB_OK;
  921. }
  922. /* Find probe points from debuginfo */
  923. static int debuginfo__find_probes(struct debuginfo *self,
  924. struct probe_finder *pf)
  925. {
  926. struct perf_probe_point *pp = &pf->pev->point;
  927. Dwarf_Off off, noff;
  928. size_t cuhl;
  929. Dwarf_Die *diep;
  930. int ret = 0;
  931. #if _ELFUTILS_PREREQ(0, 142)
  932. /* Get the call frame information from this dwarf */
  933. pf->cfi = dwarf_getcfi(self->dbg);
  934. #endif
  935. off = 0;
  936. line_list__init(&pf->lcache);
  937. /* Fastpath: lookup by function name from .debug_pubnames section */
  938. if (pp->function) {
  939. struct pubname_callback_param pubname_param = {
  940. .function = pp->function,
  941. .file = pp->file,
  942. .cu_die = &pf->cu_die,
  943. .sp_die = &pf->sp_die,
  944. .found = 0,
  945. };
  946. struct dwarf_callback_param probe_param = {
  947. .data = pf,
  948. };
  949. dwarf_getpubnames(self->dbg, pubname_search_cb,
  950. &pubname_param, 0);
  951. if (pubname_param.found) {
  952. ret = probe_point_search_cb(&pf->sp_die, &probe_param);
  953. if (ret)
  954. goto found;
  955. }
  956. }
  957. /* Loop on CUs (Compilation Unit) */
  958. while (!dwarf_nextcu(self->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  959. /* Get the DIE(Debugging Information Entry) of this CU */
  960. diep = dwarf_offdie(self->dbg, off + cuhl, &pf->cu_die);
  961. if (!diep)
  962. continue;
  963. /* Check if target file is included. */
  964. if (pp->file)
  965. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  966. else
  967. pf->fname = NULL;
  968. if (!pp->file || pf->fname) {
  969. if (pp->function)
  970. ret = find_probe_point_by_func(pf);
  971. else if (pp->lazy_line)
  972. ret = find_probe_point_lazy(NULL, pf);
  973. else {
  974. pf->lno = pp->line;
  975. ret = find_probe_point_by_line(pf);
  976. }
  977. if (ret < 0)
  978. break;
  979. }
  980. off = noff;
  981. }
  982. found:
  983. line_list__free(&pf->lcache);
  984. return ret;
  985. }
  986. /* Add a found probe point into trace event list */
  987. static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)
  988. {
  989. struct trace_event_finder *tf =
  990. container_of(pf, struct trace_event_finder, pf);
  991. struct probe_trace_event *tev;
  992. int ret, i;
  993. /* Check number of tevs */
  994. if (tf->ntevs == tf->max_tevs) {
  995. pr_warning("Too many( > %d) probe point found.\n",
  996. tf->max_tevs);
  997. return -ERANGE;
  998. }
  999. tev = &tf->tevs[tf->ntevs++];
  1000. /* Trace point should be converted from subprogram DIE */
  1001. ret = convert_to_trace_point(&pf->sp_die, pf->addr,
  1002. pf->pev->point.retprobe, &tev->point);
  1003. if (ret < 0)
  1004. return ret;
  1005. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  1006. tev->point.offset);
  1007. /* Find each argument */
  1008. tev->nargs = pf->pev->nargs;
  1009. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1010. if (tev->args == NULL)
  1011. return -ENOMEM;
  1012. for (i = 0; i < pf->pev->nargs; i++) {
  1013. pf->pvar = &pf->pev->args[i];
  1014. pf->tvar = &tev->args[i];
  1015. /* Variable should be found from scope DIE */
  1016. ret = find_variable(sc_die, pf);
  1017. if (ret != 0)
  1018. return ret;
  1019. }
  1020. return 0;
  1021. }
  1022. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1023. int debuginfo__find_trace_events(struct debuginfo *self,
  1024. struct perf_probe_event *pev,
  1025. struct probe_trace_event **tevs, int max_tevs)
  1026. {
  1027. struct trace_event_finder tf = {
  1028. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1029. .max_tevs = max_tevs};
  1030. int ret;
  1031. /* Allocate result tevs array */
  1032. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1033. if (*tevs == NULL)
  1034. return -ENOMEM;
  1035. tf.tevs = *tevs;
  1036. tf.ntevs = 0;
  1037. ret = debuginfo__find_probes(self, &tf.pf);
  1038. if (ret < 0) {
  1039. free(*tevs);
  1040. *tevs = NULL;
  1041. return ret;
  1042. }
  1043. return (ret < 0) ? ret : tf.ntevs;
  1044. }
  1045. #define MAX_VAR_LEN 64
  1046. /* Collect available variables in this scope */
  1047. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1048. {
  1049. struct available_var_finder *af = data;
  1050. struct variable_list *vl;
  1051. char buf[MAX_VAR_LEN];
  1052. int tag, ret;
  1053. vl = &af->vls[af->nvls - 1];
  1054. tag = dwarf_tag(die_mem);
  1055. if (tag == DW_TAG_formal_parameter ||
  1056. tag == DW_TAG_variable) {
  1057. ret = convert_variable_location(die_mem, af->pf.addr,
  1058. af->pf.fb_ops, NULL);
  1059. if (ret == 0) {
  1060. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1061. pr_debug2("Add new var: %s\n", buf);
  1062. if (ret > 0)
  1063. strlist__add(vl->vars, buf);
  1064. }
  1065. }
  1066. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1067. return DIE_FIND_CB_CONTINUE;
  1068. else
  1069. return DIE_FIND_CB_SIBLING;
  1070. }
  1071. /* Add a found vars into available variables list */
  1072. static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf)
  1073. {
  1074. struct available_var_finder *af =
  1075. container_of(pf, struct available_var_finder, pf);
  1076. struct variable_list *vl;
  1077. Dwarf_Die die_mem;
  1078. int ret;
  1079. /* Check number of tevs */
  1080. if (af->nvls == af->max_vls) {
  1081. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1082. return -ERANGE;
  1083. }
  1084. vl = &af->vls[af->nvls++];
  1085. /* Trace point should be converted from subprogram DIE */
  1086. ret = convert_to_trace_point(&pf->sp_die, pf->addr,
  1087. pf->pev->point.retprobe, &vl->point);
  1088. if (ret < 0)
  1089. return ret;
  1090. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1091. vl->point.offset);
  1092. /* Find local variables */
  1093. vl->vars = strlist__new(true, NULL);
  1094. if (vl->vars == NULL)
  1095. return -ENOMEM;
  1096. af->child = true;
  1097. die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem);
  1098. /* Find external variables */
  1099. if (!af->externs)
  1100. goto out;
  1101. /* Don't need to search child DIE for externs. */
  1102. af->child = false;
  1103. die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem);
  1104. out:
  1105. if (strlist__empty(vl->vars)) {
  1106. strlist__delete(vl->vars);
  1107. vl->vars = NULL;
  1108. }
  1109. return ret;
  1110. }
  1111. /* Find available variables at given probe point */
  1112. int debuginfo__find_available_vars_at(struct debuginfo *self,
  1113. struct perf_probe_event *pev,
  1114. struct variable_list **vls,
  1115. int max_vls, bool externs)
  1116. {
  1117. struct available_var_finder af = {
  1118. .pf = {.pev = pev, .callback = add_available_vars},
  1119. .max_vls = max_vls, .externs = externs};
  1120. int ret;
  1121. /* Allocate result vls array */
  1122. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1123. if (*vls == NULL)
  1124. return -ENOMEM;
  1125. af.vls = *vls;
  1126. af.nvls = 0;
  1127. ret = debuginfo__find_probes(self, &af.pf);
  1128. if (ret < 0) {
  1129. /* Free vlist for error */
  1130. while (af.nvls--) {
  1131. if (af.vls[af.nvls].point.symbol)
  1132. free(af.vls[af.nvls].point.symbol);
  1133. if (af.vls[af.nvls].vars)
  1134. strlist__delete(af.vls[af.nvls].vars);
  1135. }
  1136. free(af.vls);
  1137. *vls = NULL;
  1138. return ret;
  1139. }
  1140. return (ret < 0) ? ret : af.nvls;
  1141. }
  1142. /* Reverse search */
  1143. int debuginfo__find_probe_point(struct debuginfo *self, unsigned long addr,
  1144. struct perf_probe_point *ppt)
  1145. {
  1146. Dwarf_Die cudie, spdie, indie;
  1147. Dwarf_Addr _addr, baseaddr;
  1148. const char *fname = NULL, *func = NULL, *tmp;
  1149. int baseline = 0, lineno = 0, ret = 0;
  1150. /* Adjust address with bias */
  1151. addr += self->bias;
  1152. /* Find cu die */
  1153. if (!dwarf_addrdie(self->dbg, (Dwarf_Addr)addr - self->bias, &cudie)) {
  1154. pr_warning("Failed to find debug information for address %lx\n",
  1155. addr);
  1156. ret = -EINVAL;
  1157. goto end;
  1158. }
  1159. /* Find a corresponding line (filename and lineno) */
  1160. cu_find_lineinfo(&cudie, addr, &fname, &lineno);
  1161. /* Don't care whether it failed or not */
  1162. /* Find a corresponding function (name, baseline and baseaddr) */
  1163. if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1164. /* Get function entry information */
  1165. tmp = dwarf_diename(&spdie);
  1166. if (!tmp ||
  1167. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1168. dwarf_decl_line(&spdie, &baseline) != 0)
  1169. goto post;
  1170. func = tmp;
  1171. if (addr == (unsigned long)baseaddr)
  1172. /* Function entry - Relative line number is 0 */
  1173. lineno = baseline;
  1174. else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1175. &indie)) {
  1176. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1177. _addr == addr)
  1178. /*
  1179. * addr is at an inline function entry.
  1180. * In this case, lineno should be the call-site
  1181. * line number.
  1182. */
  1183. lineno = die_get_call_lineno(&indie);
  1184. else {
  1185. /*
  1186. * addr is in an inline function body.
  1187. * Since lineno points one of the lines
  1188. * of the inline function, baseline should
  1189. * be the entry line of the inline function.
  1190. */
  1191. tmp = dwarf_diename(&indie);
  1192. if (tmp &&
  1193. dwarf_decl_line(&spdie, &baseline) == 0)
  1194. func = tmp;
  1195. }
  1196. }
  1197. }
  1198. post:
  1199. /* Make a relative line number or an offset */
  1200. if (lineno)
  1201. ppt->line = lineno - baseline;
  1202. else if (func)
  1203. ppt->offset = addr - (unsigned long)baseaddr;
  1204. /* Duplicate strings */
  1205. if (func) {
  1206. ppt->function = strdup(func);
  1207. if (ppt->function == NULL) {
  1208. ret = -ENOMEM;
  1209. goto end;
  1210. }
  1211. }
  1212. if (fname) {
  1213. ppt->file = strdup(fname);
  1214. if (ppt->file == NULL) {
  1215. if (ppt->function) {
  1216. free(ppt->function);
  1217. ppt->function = NULL;
  1218. }
  1219. ret = -ENOMEM;
  1220. goto end;
  1221. }
  1222. }
  1223. end:
  1224. if (ret == 0 && (fname || func))
  1225. ret = 1; /* Found a point */
  1226. return ret;
  1227. }
  1228. /* Add a line and store the src path */
  1229. static int line_range_add_line(const char *src, unsigned int lineno,
  1230. struct line_range *lr)
  1231. {
  1232. /* Copy source path */
  1233. if (!lr->path) {
  1234. lr->path = strdup(src);
  1235. if (lr->path == NULL)
  1236. return -ENOMEM;
  1237. }
  1238. return line_list__add_line(&lr->line_list, lineno);
  1239. }
  1240. static int line_range_walk_cb(const char *fname, int lineno,
  1241. Dwarf_Addr addr __used,
  1242. void *data)
  1243. {
  1244. struct line_finder *lf = data;
  1245. if ((strtailcmp(fname, lf->fname) != 0) ||
  1246. (lf->lno_s > lineno || lf->lno_e < lineno))
  1247. return 0;
  1248. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1249. return -EINVAL;
  1250. return 0;
  1251. }
  1252. /* Find line range from its line number */
  1253. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1254. {
  1255. int ret;
  1256. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1257. /* Update status */
  1258. if (ret >= 0)
  1259. if (!list_empty(&lf->lr->line_list))
  1260. ret = lf->found = 1;
  1261. else
  1262. ret = 0; /* Lines are not found */
  1263. else {
  1264. free(lf->lr->path);
  1265. lf->lr->path = NULL;
  1266. }
  1267. return ret;
  1268. }
  1269. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1270. {
  1271. find_line_range_by_line(in_die, data);
  1272. /*
  1273. * We have to check all instances of inlined function, because
  1274. * some execution paths can be optimized out depends on the
  1275. * function argument of instances
  1276. */
  1277. return 0;
  1278. }
  1279. /* Search function from function name */
  1280. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1281. {
  1282. struct dwarf_callback_param *param = data;
  1283. struct line_finder *lf = param->data;
  1284. struct line_range *lr = lf->lr;
  1285. /* Check declared file */
  1286. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1287. return DWARF_CB_OK;
  1288. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1289. die_compare_name(sp_die, lr->function)) {
  1290. lf->fname = dwarf_decl_file(sp_die);
  1291. dwarf_decl_line(sp_die, &lr->offset);
  1292. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1293. lf->lno_s = lr->offset + lr->start;
  1294. if (lf->lno_s < 0) /* Overflow */
  1295. lf->lno_s = INT_MAX;
  1296. lf->lno_e = lr->offset + lr->end;
  1297. if (lf->lno_e < 0) /* Overflow */
  1298. lf->lno_e = INT_MAX;
  1299. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1300. lr->start = lf->lno_s;
  1301. lr->end = lf->lno_e;
  1302. if (dwarf_func_inline(sp_die))
  1303. param->retval = die_walk_instances(sp_die,
  1304. line_range_inline_cb, lf);
  1305. else
  1306. param->retval = find_line_range_by_line(sp_die, lf);
  1307. return DWARF_CB_ABORT;
  1308. }
  1309. return DWARF_CB_OK;
  1310. }
  1311. static int find_line_range_by_func(struct line_finder *lf)
  1312. {
  1313. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1314. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1315. return param.retval;
  1316. }
  1317. int debuginfo__find_line_range(struct debuginfo *self, struct line_range *lr)
  1318. {
  1319. struct line_finder lf = {.lr = lr, .found = 0};
  1320. int ret = 0;
  1321. Dwarf_Off off = 0, noff;
  1322. size_t cuhl;
  1323. Dwarf_Die *diep;
  1324. const char *comp_dir;
  1325. /* Fastpath: lookup by function name from .debug_pubnames section */
  1326. if (lr->function) {
  1327. struct pubname_callback_param pubname_param = {
  1328. .function = lr->function, .file = lr->file,
  1329. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1330. struct dwarf_callback_param line_range_param = {
  1331. .data = (void *)&lf, .retval = 0};
  1332. dwarf_getpubnames(self->dbg, pubname_search_cb,
  1333. &pubname_param, 0);
  1334. if (pubname_param.found) {
  1335. line_range_search_cb(&lf.sp_die, &line_range_param);
  1336. if (lf.found)
  1337. goto found;
  1338. }
  1339. }
  1340. /* Loop on CUs (Compilation Unit) */
  1341. while (!lf.found && ret >= 0) {
  1342. if (dwarf_nextcu(self->dbg, off, &noff, &cuhl,
  1343. NULL, NULL, NULL) != 0)
  1344. break;
  1345. /* Get the DIE(Debugging Information Entry) of this CU */
  1346. diep = dwarf_offdie(self->dbg, off + cuhl, &lf.cu_die);
  1347. if (!diep)
  1348. continue;
  1349. /* Check if target file is included. */
  1350. if (lr->file)
  1351. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1352. else
  1353. lf.fname = 0;
  1354. if (!lr->file || lf.fname) {
  1355. if (lr->function)
  1356. ret = find_line_range_by_func(&lf);
  1357. else {
  1358. lf.lno_s = lr->start;
  1359. lf.lno_e = lr->end;
  1360. ret = find_line_range_by_line(NULL, &lf);
  1361. }
  1362. }
  1363. off = noff;
  1364. }
  1365. found:
  1366. /* Store comp_dir */
  1367. if (lf.found) {
  1368. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1369. if (comp_dir) {
  1370. lr->comp_dir = strdup(comp_dir);
  1371. if (!lr->comp_dir)
  1372. ret = -ENOMEM;
  1373. }
  1374. }
  1375. pr_debug("path: %s\n", lr->path);
  1376. return (ret < 0) ? ret : lf.found;
  1377. }