probe-finder.c 43 KB

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