dso.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <asm/bug.h>
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <linux/zalloc.h>
  6. #include <sys/time.h>
  7. #include <sys/resource.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <stdlib.h>
  14. #ifdef HAVE_LIBBPF_SUPPORT
  15. #include <bpf/libbpf.h>
  16. #include "bpf-event.h"
  17. #include "bpf-utils.h"
  18. #endif
  19. #include "compress.h"
  20. #include "env.h"
  21. #include "namespaces.h"
  22. #include "path.h"
  23. #include "map.h"
  24. #include "symbol.h"
  25. #include "srcline.h"
  26. #include "dso.h"
  27. #include "dsos.h"
  28. #include "machine.h"
  29. #include "auxtrace.h"
  30. #include "util.h" /* O_CLOEXEC for older systems */
  31. #include "debug.h"
  32. #include "string2.h"
  33. #include "vdso.h"
  34. #include "annotate-data.h"
  35. #include "libdw.h"
  36. static const char * const debuglink_paths[] = {
  37. "%.0s%s",
  38. "%s/%s",
  39. "%s/.debug/%s",
  40. "/usr/lib/debug%s/%s"
  41. };
  42. void dso__set_nsinfo(struct dso *dso, struct nsinfo *nsi)
  43. {
  44. nsinfo__put(RC_CHK_ACCESS(dso)->nsinfo);
  45. RC_CHK_ACCESS(dso)->nsinfo = nsi;
  46. }
  47. char dso__symtab_origin(const struct dso *dso)
  48. {
  49. static const char origin[] = {
  50. [DSO_BINARY_TYPE__KALLSYMS] = 'k',
  51. [DSO_BINARY_TYPE__VMLINUX] = 'v',
  52. [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
  53. [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
  54. [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
  55. [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
  56. [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
  57. [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
  58. [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
  59. [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
  60. [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
  61. [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
  62. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
  63. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
  64. [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
  65. [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
  66. [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
  67. [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
  68. [DSO_BINARY_TYPE__GNU_DEBUGDATA] = 'n',
  69. };
  70. if (dso == NULL || dso__symtab_type(dso) == DSO_BINARY_TYPE__NOT_FOUND)
  71. return '!';
  72. return origin[dso__symtab_type(dso)];
  73. }
  74. bool dso__is_object_file(const struct dso *dso)
  75. {
  76. switch (dso__binary_type(dso)) {
  77. case DSO_BINARY_TYPE__KALLSYMS:
  78. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  79. case DSO_BINARY_TYPE__JAVA_JIT:
  80. case DSO_BINARY_TYPE__BPF_PROG_INFO:
  81. case DSO_BINARY_TYPE__BPF_IMAGE:
  82. case DSO_BINARY_TYPE__OOL:
  83. return false;
  84. case DSO_BINARY_TYPE__VMLINUX:
  85. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  86. case DSO_BINARY_TYPE__DEBUGLINK:
  87. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  88. case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
  89. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  90. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  91. case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
  92. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  93. case DSO_BINARY_TYPE__GNU_DEBUGDATA:
  94. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  95. case DSO_BINARY_TYPE__GUEST_KMODULE:
  96. case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
  97. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  98. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
  99. case DSO_BINARY_TYPE__KCORE:
  100. case DSO_BINARY_TYPE__GUEST_KCORE:
  101. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  102. case DSO_BINARY_TYPE__NOT_FOUND:
  103. default:
  104. return true;
  105. }
  106. }
  107. int dso__read_binary_type_filename(const struct dso *dso,
  108. enum dso_binary_type type,
  109. const char *root_dir, char *filename, size_t size)
  110. {
  111. char build_id_hex[SBUILD_ID_SIZE];
  112. int ret = 0;
  113. size_t len;
  114. switch (type) {
  115. case DSO_BINARY_TYPE__DEBUGLINK:
  116. {
  117. const char *last_slash;
  118. char dso_dir[PATH_MAX];
  119. char symfile[PATH_MAX];
  120. unsigned int i;
  121. len = __symbol__join_symfs(filename, size, dso__long_name(dso));
  122. last_slash = filename + len;
  123. while (last_slash != filename && *last_slash != '/')
  124. last_slash--;
  125. strncpy(dso_dir, filename, last_slash - filename);
  126. dso_dir[last_slash-filename] = '\0';
  127. if (!is_regular_file(filename)) {
  128. ret = -1;
  129. break;
  130. }
  131. ret = filename__read_debuglink(filename, symfile, PATH_MAX);
  132. if (ret)
  133. break;
  134. /* Check predefined locations where debug file might reside */
  135. ret = -1;
  136. for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
  137. snprintf(filename, size,
  138. debuglink_paths[i], dso_dir, symfile);
  139. if (is_regular_file(filename)) {
  140. ret = 0;
  141. break;
  142. }
  143. }
  144. break;
  145. }
  146. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  147. if (dso__build_id_filename(dso, filename, size, false) == NULL)
  148. ret = -1;
  149. break;
  150. case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
  151. if (dso__build_id_filename(dso, filename, size, true) == NULL)
  152. ret = -1;
  153. break;
  154. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  155. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  156. snprintf(filename + len, size - len, "%s.debug", dso__long_name(dso));
  157. break;
  158. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  159. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  160. snprintf(filename + len, size - len, "%s", dso__long_name(dso));
  161. break;
  162. case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
  163. /*
  164. * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
  165. * /usr/lib/debug/lib when it is expected to be in
  166. * /usr/lib/debug/usr/lib
  167. */
  168. if (strlen(dso__long_name(dso)) < 9 ||
  169. strncmp(dso__long_name(dso), "/usr/lib/", 9)) {
  170. ret = -1;
  171. break;
  172. }
  173. len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
  174. snprintf(filename + len, size - len, "%s", dso__long_name(dso) + 4);
  175. break;
  176. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  177. {
  178. const char *last_slash;
  179. size_t dir_size;
  180. last_slash = dso__long_name(dso) + dso__long_name_len(dso);
  181. while (last_slash != dso__long_name(dso) && *last_slash != '/')
  182. last_slash--;
  183. len = __symbol__join_symfs(filename, size, "");
  184. dir_size = last_slash - dso__long_name(dso) + 2;
  185. if (dir_size > (size - len)) {
  186. ret = -1;
  187. break;
  188. }
  189. len += scnprintf(filename + len, dir_size, "%s", dso__long_name(dso));
  190. len += scnprintf(filename + len , size - len, ".debug%s",
  191. last_slash);
  192. break;
  193. }
  194. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  195. if (!dso__has_build_id(dso)) {
  196. ret = -1;
  197. break;
  198. }
  199. build_id__snprintf(dso__bid(dso), build_id_hex, sizeof(build_id_hex));
  200. len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
  201. snprintf(filename + len, size - len, "%.2s/%s.debug",
  202. build_id_hex, build_id_hex + 2);
  203. break;
  204. case DSO_BINARY_TYPE__VMLINUX:
  205. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  206. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  207. case DSO_BINARY_TYPE__GNU_DEBUGDATA:
  208. __symbol__join_symfs(filename, size, dso__long_name(dso));
  209. break;
  210. case DSO_BINARY_TYPE__GUEST_KMODULE:
  211. case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
  212. path__join3(filename, size, symbol_conf.symfs,
  213. root_dir, dso__long_name(dso));
  214. break;
  215. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  216. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
  217. __symbol__join_symfs(filename, size, dso__long_name(dso));
  218. break;
  219. case DSO_BINARY_TYPE__KCORE:
  220. case DSO_BINARY_TYPE__GUEST_KCORE:
  221. snprintf(filename, size, "%s", dso__long_name(dso));
  222. break;
  223. default:
  224. case DSO_BINARY_TYPE__KALLSYMS:
  225. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  226. case DSO_BINARY_TYPE__JAVA_JIT:
  227. case DSO_BINARY_TYPE__BPF_PROG_INFO:
  228. case DSO_BINARY_TYPE__BPF_IMAGE:
  229. case DSO_BINARY_TYPE__OOL:
  230. case DSO_BINARY_TYPE__NOT_FOUND:
  231. ret = -1;
  232. break;
  233. }
  234. return ret;
  235. }
  236. enum {
  237. COMP_ID__NONE = 0,
  238. };
  239. static const struct {
  240. const char *fmt;
  241. int (*decompress)(const char *input, int output);
  242. bool (*is_compressed)(const char *input);
  243. } compressions[] = {
  244. [COMP_ID__NONE] = { .fmt = NULL, },
  245. #ifdef HAVE_ZLIB_SUPPORT
  246. { "gz", gzip_decompress_to_file, gzip_is_compressed },
  247. #endif
  248. #ifdef HAVE_LZMA_SUPPORT
  249. { "xz", lzma_decompress_to_file, lzma_is_compressed },
  250. #endif
  251. { NULL, NULL, NULL },
  252. };
  253. static int is_supported_compression(const char *ext)
  254. {
  255. unsigned i;
  256. for (i = 1; compressions[i].fmt; i++) {
  257. if (!strcmp(ext, compressions[i].fmt))
  258. return i;
  259. }
  260. return COMP_ID__NONE;
  261. }
  262. bool is_kernel_module(const char *pathname, int cpumode)
  263. {
  264. struct kmod_path m;
  265. int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
  266. WARN_ONCE(mode != cpumode,
  267. "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
  268. cpumode);
  269. switch (mode) {
  270. case PERF_RECORD_MISC_USER:
  271. case PERF_RECORD_MISC_HYPERVISOR:
  272. case PERF_RECORD_MISC_GUEST_USER:
  273. return false;
  274. /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
  275. default:
  276. if (kmod_path__parse(&m, pathname)) {
  277. pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
  278. pathname);
  279. return true;
  280. }
  281. }
  282. return m.kmod;
  283. }
  284. bool dso__needs_decompress(struct dso *dso)
  285. {
  286. return dso__symtab_type(dso) == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
  287. dso__symtab_type(dso) == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
  288. }
  289. int filename__decompress(const char *name, char *pathname,
  290. size_t len, int comp, int *err)
  291. {
  292. char tmpbuf[] = KMOD_DECOMP_NAME;
  293. int fd = -1;
  294. /*
  295. * We have proper compression id for DSO and yet the file
  296. * behind the 'name' can still be plain uncompressed object.
  297. *
  298. * The reason is behind the logic we open the DSO object files,
  299. * when we try all possible 'debug' objects until we find the
  300. * data. So even if the DSO is represented by 'krava.xz' module,
  301. * we can end up here opening ~/.debug/....23432432/debug' file
  302. * which is not compressed.
  303. *
  304. * To keep this transparent, we detect this and return the file
  305. * descriptor to the uncompressed file.
  306. */
  307. if (!compressions[comp].is_compressed(name))
  308. return open(name, O_RDONLY);
  309. fd = mkstemp(tmpbuf);
  310. if (fd < 0) {
  311. *err = errno;
  312. return -1;
  313. }
  314. if (compressions[comp].decompress(name, fd)) {
  315. *err = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
  316. close(fd);
  317. fd = -1;
  318. }
  319. if (!pathname || (fd < 0))
  320. unlink(tmpbuf);
  321. if (pathname && (fd >= 0))
  322. strlcpy(pathname, tmpbuf, len);
  323. return fd;
  324. }
  325. static int decompress_kmodule(struct dso *dso, const char *name,
  326. char *pathname, size_t len)
  327. {
  328. if (!dso__needs_decompress(dso))
  329. return -1;
  330. if (dso__comp(dso) == COMP_ID__NONE)
  331. return -1;
  332. return filename__decompress(name, pathname, len, dso__comp(dso), dso__load_errno(dso));
  333. }
  334. int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
  335. {
  336. return decompress_kmodule(dso, name, NULL, 0);
  337. }
  338. int dso__decompress_kmodule_path(struct dso *dso, const char *name,
  339. char *pathname, size_t len)
  340. {
  341. int fd = decompress_kmodule(dso, name, pathname, len);
  342. close(fd);
  343. return fd >= 0 ? 0 : -1;
  344. }
  345. /*
  346. * Parses kernel module specified in @path and updates
  347. * @m argument like:
  348. *
  349. * @comp - true if @path contains supported compression suffix,
  350. * false otherwise
  351. * @kmod - true if @path contains '.ko' suffix in right position,
  352. * false otherwise
  353. * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
  354. * of the kernel module without suffixes, otherwise strudup-ed
  355. * base name of @path
  356. * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
  357. * the compression suffix
  358. *
  359. * Returns 0 if there's no strdup error, -ENOMEM otherwise.
  360. */
  361. int __kmod_path__parse(struct kmod_path *m, const char *path,
  362. bool alloc_name)
  363. {
  364. const char *name = strrchr(path, '/');
  365. const char *ext = strrchr(path, '.');
  366. bool is_simple_name = false;
  367. memset(m, 0x0, sizeof(*m));
  368. name = name ? name + 1 : path;
  369. /*
  370. * '.' is also a valid character for module name. For example:
  371. * [aaa.bbb] is a valid module name. '[' should have higher
  372. * priority than '.ko' suffix.
  373. *
  374. * The kernel names are from machine__mmap_name. Such
  375. * name should belong to kernel itself, not kernel module.
  376. */
  377. if (name[0] == '[') {
  378. is_simple_name = true;
  379. if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
  380. (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
  381. (strncmp(name, "[vdso]", 6) == 0) ||
  382. (strncmp(name, "[vdso32]", 8) == 0) ||
  383. (strncmp(name, "[vdsox32]", 9) == 0) ||
  384. (strncmp(name, "[vsyscall]", 10) == 0)) {
  385. m->kmod = false;
  386. } else
  387. m->kmod = true;
  388. }
  389. /* No extension, just return name. */
  390. if ((ext == NULL) || is_simple_name) {
  391. if (alloc_name) {
  392. m->name = strdup(name);
  393. return m->name ? 0 : -ENOMEM;
  394. }
  395. return 0;
  396. }
  397. m->comp = is_supported_compression(ext + 1);
  398. if (m->comp > COMP_ID__NONE)
  399. ext -= 3;
  400. /* Check .ko extension only if there's enough name left. */
  401. if (ext > name)
  402. m->kmod = !strncmp(ext, ".ko", 3);
  403. if (alloc_name) {
  404. if (m->kmod) {
  405. if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
  406. return -ENOMEM;
  407. } else {
  408. if (asprintf(&m->name, "%s", name) == -1)
  409. return -ENOMEM;
  410. }
  411. strreplace(m->name, '-', '_');
  412. }
  413. return 0;
  414. }
  415. void dso__set_module_info(struct dso *dso, struct kmod_path *m,
  416. struct machine *machine)
  417. {
  418. if (machine__is_host(machine))
  419. dso__set_symtab_type(dso, DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE);
  420. else
  421. dso__set_symtab_type(dso, DSO_BINARY_TYPE__GUEST_KMODULE);
  422. /* _KMODULE_COMP should be next to _KMODULE */
  423. if (m->kmod && m->comp) {
  424. dso__set_symtab_type(dso, dso__symtab_type(dso) + 1);
  425. dso__set_comp(dso, m->comp);
  426. }
  427. dso__set_is_kmod(dso);
  428. dso__set_short_name(dso, strdup(m->name), true);
  429. }
  430. /*
  431. * Global list of open DSOs and the counter.
  432. */
  433. struct mutex _dso__data_open_lock;
  434. static LIST_HEAD(dso__data_open);
  435. static long dso__data_open_cnt GUARDED_BY(_dso__data_open_lock);
  436. static void dso__data_open_lock_init(void)
  437. {
  438. mutex_init(&_dso__data_open_lock);
  439. }
  440. static struct mutex *dso__data_open_lock(void) LOCK_RETURNED(_dso__data_open_lock)
  441. {
  442. static pthread_once_t data_open_lock_once = PTHREAD_ONCE_INIT;
  443. pthread_once(&data_open_lock_once, dso__data_open_lock_init);
  444. return &_dso__data_open_lock;
  445. }
  446. static void dso__list_add(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  447. {
  448. list_add_tail(&dso__data(dso)->open_entry, &dso__data_open);
  449. #ifdef REFCNT_CHECKING
  450. dso__data(dso)->dso = dso__get(dso);
  451. #endif
  452. /* Assume the dso is part of dsos, hence the optional reference count above. */
  453. assert(dso__dsos(dso));
  454. dso__data_open_cnt++;
  455. }
  456. static void dso__list_del(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  457. {
  458. list_del_init(&dso__data(dso)->open_entry);
  459. #ifdef REFCNT_CHECKING
  460. mutex_unlock(dso__data_open_lock());
  461. dso__put(dso__data(dso)->dso);
  462. mutex_lock(dso__data_open_lock());
  463. #endif
  464. WARN_ONCE(dso__data_open_cnt <= 0,
  465. "DSO data fd counter out of bounds.");
  466. dso__data_open_cnt--;
  467. }
  468. static void close_first_dso(void);
  469. static int do_open(char *name) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  470. {
  471. do {
  472. int fd = open(name, O_RDONLY|O_CLOEXEC);
  473. if (fd >= 0)
  474. return fd;
  475. pr_debug("dso open failed: %m\n");
  476. if (!dso__data_open_cnt || errno != EMFILE)
  477. break;
  478. close_first_dso();
  479. } while (1);
  480. return -1;
  481. }
  482. char *dso__filename_with_chroot(const struct dso *dso, const char *filename)
  483. {
  484. return filename_with_chroot(nsinfo__pid(dso__nsinfo_const(dso)), filename);
  485. }
  486. static char *dso__get_filename(struct dso *dso, const char *root_dir,
  487. bool *decomp)
  488. {
  489. char *name = malloc(PATH_MAX);
  490. *decomp = false;
  491. if (name == NULL)
  492. return NULL;
  493. if (dso__read_binary_type_filename(dso, dso__binary_type(dso),
  494. root_dir, name, PATH_MAX))
  495. goto out;
  496. if (!is_regular_file(name)) {
  497. char *new_name;
  498. if (errno != ENOENT || dso__nsinfo(dso) == NULL)
  499. goto out;
  500. new_name = dso__filename_with_chroot(dso, name);
  501. if (!new_name)
  502. goto out;
  503. free(name);
  504. name = new_name;
  505. }
  506. if (dso__needs_decompress(dso)) {
  507. char newpath[KMOD_DECOMP_LEN];
  508. size_t len = sizeof(newpath);
  509. if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
  510. errno = *dso__load_errno(dso);
  511. goto out;
  512. }
  513. *decomp = true;
  514. strcpy(name, newpath);
  515. }
  516. return name;
  517. out:
  518. free(name);
  519. return NULL;
  520. }
  521. static int __open_dso(struct dso *dso, struct machine *machine)
  522. EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  523. {
  524. int fd = -EINVAL;
  525. char *name;
  526. bool decomp = false;
  527. mutex_lock(dso__lock(dso));
  528. name = dso__get_filename(dso, machine ? machine->root_dir : "", &decomp);
  529. if (name)
  530. fd = do_open(name);
  531. else
  532. fd = -errno;
  533. if (decomp)
  534. unlink(name);
  535. mutex_unlock(dso__lock(dso));
  536. free(name);
  537. return fd;
  538. }
  539. static void check_data_close(void);
  540. /**
  541. * dso_close - Open DSO data file
  542. * @dso: dso object
  543. *
  544. * Open @dso's data file descriptor and updates
  545. * list/count of open DSO objects.
  546. */
  547. static int open_dso(struct dso *dso, struct machine *machine)
  548. EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  549. {
  550. int fd;
  551. struct nscookie nsc;
  552. if (dso__binary_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE) {
  553. mutex_lock(dso__lock(dso));
  554. nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
  555. mutex_unlock(dso__lock(dso));
  556. }
  557. fd = __open_dso(dso, machine);
  558. if (dso__binary_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE)
  559. nsinfo__mountns_exit(&nsc);
  560. if (fd >= 0) {
  561. dso__list_add(dso);
  562. /*
  563. * Check if we crossed the allowed number
  564. * of opened DSOs and close one if needed.
  565. */
  566. check_data_close();
  567. }
  568. return fd;
  569. }
  570. static void close_data_fd(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  571. {
  572. if (dso__data(dso)->fd >= 0) {
  573. close(dso__data(dso)->fd);
  574. dso__data(dso)->fd = -1;
  575. dso__data(dso)->file_size = 0;
  576. dso__list_del(dso);
  577. }
  578. }
  579. /**
  580. * dso_close - Close DSO data file
  581. * @dso: dso object
  582. *
  583. * Close @dso's data file descriptor and updates
  584. * list/count of open DSO objects.
  585. */
  586. static void close_dso(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  587. {
  588. close_data_fd(dso);
  589. }
  590. static void close_first_dso(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  591. {
  592. struct dso_data *dso_data;
  593. struct dso *dso;
  594. dso_data = list_first_entry(&dso__data_open, struct dso_data, open_entry);
  595. #ifdef REFCNT_CHECKING
  596. dso = dso_data->dso;
  597. #else
  598. dso = container_of(dso_data, struct dso, data);
  599. #endif
  600. close_dso(dso);
  601. }
  602. static rlim_t get_fd_limit(void)
  603. {
  604. struct rlimit l;
  605. rlim_t limit = 0;
  606. /* Allow half of the current open fd limit. */
  607. if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
  608. if (l.rlim_cur == RLIM_INFINITY)
  609. limit = l.rlim_cur;
  610. else
  611. limit = l.rlim_cur / 2;
  612. } else {
  613. pr_err("failed to get fd limit\n");
  614. limit = 1;
  615. }
  616. return limit;
  617. }
  618. static rlim_t fd_limit;
  619. /*
  620. * Used only by tests/dso-data.c to reset the environment
  621. * for tests. I dont expect we should change this during
  622. * standard runtime.
  623. */
  624. void reset_fd_limit(void)
  625. {
  626. fd_limit = 0;
  627. }
  628. static bool may_cache_fd(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  629. {
  630. if (!fd_limit)
  631. fd_limit = get_fd_limit();
  632. if (fd_limit == RLIM_INFINITY)
  633. return true;
  634. return fd_limit > (rlim_t) dso__data_open_cnt;
  635. }
  636. /*
  637. * Check and close LRU dso if we crossed allowed limit
  638. * for opened dso file descriptors. The limit is half
  639. * of the RLIMIT_NOFILE files opened.
  640. */
  641. static void check_data_close(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  642. {
  643. bool cache_fd = may_cache_fd();
  644. if (!cache_fd)
  645. close_first_dso();
  646. }
  647. /**
  648. * dso__data_close - Close DSO data file
  649. * @dso: dso object
  650. *
  651. * External interface to close @dso's data file descriptor.
  652. */
  653. void dso__data_close(struct dso *dso)
  654. {
  655. mutex_lock(dso__data_open_lock());
  656. close_dso(dso);
  657. mutex_unlock(dso__data_open_lock());
  658. }
  659. static void try_to_open_dso(struct dso *dso, struct machine *machine)
  660. EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock)
  661. {
  662. enum dso_binary_type binary_type_data[] = {
  663. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  664. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  665. DSO_BINARY_TYPE__NOT_FOUND,
  666. };
  667. int i = 0;
  668. struct dso_data *dso_data = dso__data(dso);
  669. if (dso_data->fd >= 0)
  670. return;
  671. if (dso__binary_type(dso) != DSO_BINARY_TYPE__NOT_FOUND) {
  672. dso_data->fd = open_dso(dso, machine);
  673. goto out;
  674. }
  675. do {
  676. dso__set_binary_type(dso, binary_type_data[i++]);
  677. dso_data->fd = open_dso(dso, machine);
  678. if (dso_data->fd >= 0)
  679. goto out;
  680. } while (dso__binary_type(dso) != DSO_BINARY_TYPE__NOT_FOUND);
  681. out:
  682. if (dso_data->fd >= 0)
  683. dso_data->status = DSO_DATA_STATUS_OK;
  684. else
  685. dso_data->status = DSO_DATA_STATUS_ERROR;
  686. }
  687. /**
  688. * dso__data_get_fd - Get dso's data file descriptor
  689. * @dso: dso object
  690. * @machine: machine object
  691. *
  692. * External interface to find dso's file, open it and
  693. * returns file descriptor. It should be paired with
  694. * dso__data_put_fd() if it returns non-negative value.
  695. */
  696. bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd)
  697. {
  698. *fd = -1;
  699. if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR)
  700. return false;
  701. mutex_lock(dso__data_open_lock());
  702. try_to_open_dso(dso, machine);
  703. *fd = dso__data(dso)->fd;
  704. if (*fd >= 0)
  705. return true;
  706. mutex_unlock(dso__data_open_lock());
  707. return false;
  708. }
  709. void dso__data_put_fd(struct dso *dso __maybe_unused)
  710. {
  711. mutex_unlock(dso__data_open_lock());
  712. }
  713. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
  714. {
  715. u32 flag = 1 << by;
  716. if (dso__data(dso)->status_seen & flag)
  717. return true;
  718. dso__data(dso)->status_seen |= flag;
  719. return false;
  720. }
  721. #ifdef HAVE_LIBBPF_SUPPORT
  722. static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
  723. {
  724. struct bpf_prog_info_node *node;
  725. ssize_t size = DSO__DATA_CACHE_SIZE;
  726. struct dso_bpf_prog *dso_bpf_prog = dso__bpf_prog(dso);
  727. u64 len;
  728. u8 *buf;
  729. node = perf_env__find_bpf_prog_info(dso_bpf_prog->env, dso_bpf_prog->id);
  730. if (!node || !node->info_linear) {
  731. dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
  732. return -1;
  733. }
  734. len = node->info_linear->info.jited_prog_len;
  735. buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
  736. if (offset >= len)
  737. return -1;
  738. size = (ssize_t)min(len - offset, (u64)size);
  739. memcpy(data, buf + offset, size);
  740. return size;
  741. }
  742. static int bpf_size(struct dso *dso)
  743. {
  744. struct bpf_prog_info_node *node;
  745. struct dso_bpf_prog *dso_bpf_prog = dso__bpf_prog(dso);
  746. node = perf_env__find_bpf_prog_info(dso_bpf_prog->env, dso_bpf_prog->id);
  747. if (!node || !node->info_linear) {
  748. dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
  749. return -1;
  750. }
  751. dso__data(dso)->file_size = node->info_linear->info.jited_prog_len;
  752. return 0;
  753. }
  754. #endif // HAVE_LIBBPF_SUPPORT
  755. static void
  756. dso_cache__free(struct dso *dso)
  757. {
  758. struct rb_root *root = &dso__data(dso)->cache;
  759. struct rb_node *next = rb_first(root);
  760. mutex_lock(dso__lock(dso));
  761. while (next) {
  762. struct dso_cache *cache;
  763. cache = rb_entry(next, struct dso_cache, rb_node);
  764. next = rb_next(&cache->rb_node);
  765. rb_erase(&cache->rb_node, root);
  766. free(cache);
  767. }
  768. mutex_unlock(dso__lock(dso));
  769. }
  770. static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
  771. {
  772. const struct rb_root *root = &dso__data(dso)->cache;
  773. struct rb_node * const *p = &root->rb_node;
  774. const struct rb_node *parent = NULL;
  775. struct dso_cache *cache;
  776. while (*p != NULL) {
  777. u64 end;
  778. parent = *p;
  779. cache = rb_entry(parent, struct dso_cache, rb_node);
  780. end = cache->offset + DSO__DATA_CACHE_SIZE;
  781. if (offset < cache->offset)
  782. p = &(*p)->rb_left;
  783. else if (offset >= end)
  784. p = &(*p)->rb_right;
  785. else
  786. return cache;
  787. }
  788. return NULL;
  789. }
  790. static struct dso_cache *
  791. dso_cache__insert(struct dso *dso, struct dso_cache *new)
  792. {
  793. struct rb_root *root = &dso__data(dso)->cache;
  794. struct rb_node **p = &root->rb_node;
  795. struct rb_node *parent = NULL;
  796. struct dso_cache *cache;
  797. u64 offset = new->offset;
  798. mutex_lock(dso__lock(dso));
  799. while (*p != NULL) {
  800. u64 end;
  801. parent = *p;
  802. cache = rb_entry(parent, struct dso_cache, rb_node);
  803. end = cache->offset + DSO__DATA_CACHE_SIZE;
  804. if (offset < cache->offset)
  805. p = &(*p)->rb_left;
  806. else if (offset >= end)
  807. p = &(*p)->rb_right;
  808. else
  809. goto out;
  810. }
  811. rb_link_node(&new->rb_node, parent, p);
  812. rb_insert_color(&new->rb_node, root);
  813. cache = NULL;
  814. out:
  815. mutex_unlock(dso__lock(dso));
  816. return cache;
  817. }
  818. static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
  819. u64 size, bool out)
  820. {
  821. u64 cache_offset = offset - cache->offset;
  822. u64 cache_size = min(cache->size - cache_offset, size);
  823. if (out)
  824. memcpy(data, cache->data + cache_offset, cache_size);
  825. else
  826. memcpy(cache->data + cache_offset, data, cache_size);
  827. return cache_size;
  828. }
  829. static ssize_t file_read(struct dso *dso, struct machine *machine,
  830. u64 offset, char *data)
  831. {
  832. ssize_t ret;
  833. mutex_lock(dso__data_open_lock());
  834. /*
  835. * dso__data(dso)->fd might be closed if other thread opened another
  836. * file (dso) due to open file limit (RLIMIT_NOFILE).
  837. */
  838. try_to_open_dso(dso, machine);
  839. if (dso__data(dso)->fd < 0) {
  840. dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
  841. ret = -errno;
  842. goto out;
  843. }
  844. ret = pread(dso__data(dso)->fd, data, DSO__DATA_CACHE_SIZE, offset);
  845. out:
  846. mutex_unlock(dso__data_open_lock());
  847. return ret;
  848. }
  849. static struct dso_cache *dso_cache__populate(struct dso *dso,
  850. struct machine *machine,
  851. u64 offset, ssize_t *ret)
  852. {
  853. u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
  854. struct dso_cache *cache;
  855. struct dso_cache *old;
  856. cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
  857. if (!cache) {
  858. *ret = -ENOMEM;
  859. return NULL;
  860. }
  861. #ifdef HAVE_LIBBPF_SUPPORT
  862. if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO)
  863. *ret = bpf_read(dso, cache_offset, cache->data);
  864. else
  865. #endif
  866. if (dso__binary_type(dso) == DSO_BINARY_TYPE__OOL)
  867. *ret = DSO__DATA_CACHE_SIZE;
  868. else
  869. *ret = file_read(dso, machine, cache_offset, cache->data);
  870. if (*ret <= 0) {
  871. free(cache);
  872. return NULL;
  873. }
  874. cache->offset = cache_offset;
  875. cache->size = *ret;
  876. old = dso_cache__insert(dso, cache);
  877. if (old) {
  878. /* we lose the race */
  879. free(cache);
  880. cache = old;
  881. }
  882. return cache;
  883. }
  884. static struct dso_cache *dso_cache__find(struct dso *dso,
  885. struct machine *machine,
  886. u64 offset,
  887. ssize_t *ret)
  888. {
  889. struct dso_cache *cache = __dso_cache__find(dso, offset);
  890. return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
  891. }
  892. static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
  893. u64 offset, u8 *data, ssize_t size, bool out)
  894. {
  895. struct dso_cache *cache;
  896. ssize_t ret = 0;
  897. cache = dso_cache__find(dso, machine, offset, &ret);
  898. if (!cache)
  899. return ret;
  900. return dso_cache__memcpy(cache, offset, data, size, out);
  901. }
  902. /*
  903. * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
  904. * in the rb_tree. Any read to already cached data is served
  905. * by cached data. Writes update the cache only, not the backing file.
  906. */
  907. static ssize_t cached_io(struct dso *dso, struct machine *machine,
  908. u64 offset, u8 *data, ssize_t size, bool out)
  909. {
  910. ssize_t r = 0;
  911. u8 *p = data;
  912. do {
  913. ssize_t ret;
  914. ret = dso_cache_io(dso, machine, offset, p, size, out);
  915. if (ret < 0)
  916. return ret;
  917. /* Reached EOF, return what we have. */
  918. if (!ret)
  919. break;
  920. BUG_ON(ret > size);
  921. r += ret;
  922. p += ret;
  923. offset += ret;
  924. size -= ret;
  925. } while (size);
  926. return r;
  927. }
  928. static int file_size(struct dso *dso, struct machine *machine)
  929. {
  930. int ret = 0;
  931. struct stat st;
  932. mutex_lock(dso__data_open_lock());
  933. /*
  934. * dso__data(dso)->fd might be closed if other thread opened another
  935. * file (dso) due to open file limit (RLIMIT_NOFILE).
  936. */
  937. try_to_open_dso(dso, machine);
  938. if (dso__data(dso)->fd < 0) {
  939. ret = -errno;
  940. dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
  941. goto out;
  942. }
  943. if (fstat(dso__data(dso)->fd, &st) < 0) {
  944. ret = -errno;
  945. pr_err("dso cache fstat failed: %m\n");
  946. dso__data(dso)->status = DSO_DATA_STATUS_ERROR;
  947. goto out;
  948. }
  949. dso__data(dso)->file_size = st.st_size;
  950. out:
  951. mutex_unlock(dso__data_open_lock());
  952. return ret;
  953. }
  954. int dso__data_file_size(struct dso *dso, struct machine *machine)
  955. {
  956. if (dso__data(dso)->file_size)
  957. return 0;
  958. if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR)
  959. return -1;
  960. #ifdef HAVE_LIBBPF_SUPPORT
  961. if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO)
  962. return bpf_size(dso);
  963. #endif
  964. return file_size(dso, machine);
  965. }
  966. /**
  967. * dso__data_size - Return dso data size
  968. * @dso: dso object
  969. * @machine: machine object
  970. *
  971. * Return: dso data size
  972. */
  973. off_t dso__data_size(struct dso *dso, struct machine *machine)
  974. {
  975. if (dso__data_file_size(dso, machine))
  976. return -1;
  977. /* For now just estimate dso data size is close to file size */
  978. return dso__data(dso)->file_size;
  979. }
  980. static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
  981. u64 offset, u8 *data, ssize_t size,
  982. bool out)
  983. {
  984. if (dso__data_file_size(dso, machine))
  985. return -1;
  986. /* Check the offset sanity. */
  987. if (offset > dso__data(dso)->file_size)
  988. return -1;
  989. if (offset + size < offset)
  990. return -1;
  991. return cached_io(dso, machine, offset, data, size, out);
  992. }
  993. /**
  994. * dso__data_read_offset - Read data from dso file offset
  995. * @dso: dso object
  996. * @machine: machine object
  997. * @offset: file offset
  998. * @data: buffer to store data
  999. * @size: size of the @data buffer
  1000. *
  1001. * External interface to read data from dso file offset. Open
  1002. * dso data file and use cached_read to get the data.
  1003. */
  1004. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  1005. u64 offset, u8 *data, ssize_t size)
  1006. {
  1007. if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR)
  1008. return -1;
  1009. return data_read_write_offset(dso, machine, offset, data, size, true);
  1010. }
  1011. static enum dso_swap_type dso_swap_type__from_elf_data(unsigned char eidata)
  1012. {
  1013. static const unsigned int endian = 1;
  1014. switch (eidata) {
  1015. case ELFDATA2LSB:
  1016. /* We are big endian, DSO is little endian. */
  1017. return (*(unsigned char const *)&endian != 1) ? DSO_SWAP__YES : DSO_SWAP__NO;
  1018. case ELFDATA2MSB:
  1019. /* We are little endian, DSO is big endian. */
  1020. return (*(unsigned char const *)&endian != 0) ? DSO_SWAP__YES : DSO_SWAP__NO;
  1021. default:
  1022. return DSO_SWAP__UNSET;
  1023. }
  1024. }
  1025. /* Reads e_machine from fd, optionally caching data in dso. */
  1026. uint16_t dso__read_e_machine(struct dso *optional_dso, int fd, uint32_t *e_flags)
  1027. {
  1028. uint16_t e_machine = EM_NONE;
  1029. unsigned char e_ident[EI_NIDENT];
  1030. enum dso_swap_type swap_type;
  1031. bool need_e_flags;
  1032. if (e_flags)
  1033. *e_flags = 0;
  1034. {
  1035. _Static_assert(offsetof(Elf32_Ehdr, e_ident) == 0, "Unexpected offset");
  1036. _Static_assert(offsetof(Elf64_Ehdr, e_ident) == 0, "Unexpected offset");
  1037. }
  1038. if (pread(fd, &e_ident, sizeof(e_ident), 0) != sizeof(e_ident))
  1039. return EM_NONE; // Read failed.
  1040. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0)
  1041. return EM_NONE; // Not an ELF file.
  1042. if (e_ident[EI_CLASS] == ELFCLASSNONE || e_ident[EI_CLASS] >= ELFCLASSNUM)
  1043. return EM_NONE; // Bad ELF class (32 or 64-bit objects).
  1044. if (e_ident[EI_VERSION] != EV_CURRENT)
  1045. return EM_NONE; // Bad ELF version.
  1046. swap_type = dso_swap_type__from_elf_data(e_ident[EI_DATA]);
  1047. if (swap_type == DSO_SWAP__UNSET)
  1048. return EM_NONE; // Bad ELF data encoding.
  1049. /* Cache the need for swapping. */
  1050. if (optional_dso) {
  1051. assert(dso__needs_swap(optional_dso) == DSO_SWAP__UNSET ||
  1052. dso__needs_swap(optional_dso) == swap_type);
  1053. dso__set_needs_swap(optional_dso, swap_type);
  1054. }
  1055. {
  1056. _Static_assert(offsetof(Elf32_Ehdr, e_machine) == 18, "Unexpected offset");
  1057. _Static_assert(offsetof(Elf64_Ehdr, e_machine) == 18, "Unexpected offset");
  1058. }
  1059. if (pread(fd, &e_machine, sizeof(e_machine), 18) != sizeof(e_machine))
  1060. return EM_NONE; // e_machine read failed.
  1061. e_machine = DSO_SWAP_TYPE__SWAP(swap_type, uint16_t, e_machine);
  1062. if (e_machine >= EM_NUM)
  1063. return EM_NONE; // Bad ELF machine number.
  1064. #ifdef NDEBUG
  1065. /* In production code the e_flags are only needed on CSKY. */
  1066. need_e_flags = e_flags && e_machine == EM_CSKY;
  1067. #else
  1068. /* Debug code will always read the e_flags. */
  1069. need_e_flags = e_flags != NULL;
  1070. #endif
  1071. if (need_e_flags) {
  1072. off_t offset = e_ident[EI_CLASS] == ELFCLASS32
  1073. ? offsetof(Elf32_Ehdr, e_flags)
  1074. : offsetof(Elf64_Ehdr, e_flags);
  1075. if (pread(fd, e_flags, sizeof(*e_flags), offset) != sizeof(*e_flags)) {
  1076. *e_flags = 0;
  1077. return EM_NONE; // e_flags read failed.
  1078. }
  1079. }
  1080. return e_machine;
  1081. }
  1082. uint16_t dso__e_machine(struct dso *dso, struct machine *machine, uint32_t *e_flags)
  1083. {
  1084. uint16_t e_machine = EM_NONE;
  1085. int fd;
  1086. switch (dso__binary_type(dso)) {
  1087. case DSO_BINARY_TYPE__KALLSYMS:
  1088. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  1089. case DSO_BINARY_TYPE__VMLINUX:
  1090. case DSO_BINARY_TYPE__GUEST_VMLINUX:
  1091. case DSO_BINARY_TYPE__GUEST_KMODULE:
  1092. case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
  1093. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  1094. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
  1095. case DSO_BINARY_TYPE__KCORE:
  1096. case DSO_BINARY_TYPE__GUEST_KCORE:
  1097. case DSO_BINARY_TYPE__BPF_PROG_INFO:
  1098. case DSO_BINARY_TYPE__BPF_IMAGE:
  1099. case DSO_BINARY_TYPE__OOL:
  1100. case DSO_BINARY_TYPE__JAVA_JIT:
  1101. if (e_flags)
  1102. *e_flags = EF_HOST;
  1103. return EM_HOST;
  1104. case DSO_BINARY_TYPE__DEBUGLINK:
  1105. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  1106. case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
  1107. case DSO_BINARY_TYPE__GNU_DEBUGDATA:
  1108. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  1109. case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
  1110. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  1111. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  1112. case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
  1113. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  1114. break;
  1115. case DSO_BINARY_TYPE__NOT_FOUND:
  1116. default:
  1117. if (e_flags)
  1118. *e_flags = 0;
  1119. return EM_NONE;
  1120. }
  1121. mutex_lock(dso__data_open_lock());
  1122. /*
  1123. * dso__data(dso)->fd might be closed if other thread opened another
  1124. * file (dso) due to open file limit (RLIMIT_NOFILE).
  1125. */
  1126. try_to_open_dso(dso, machine);
  1127. fd = dso__data(dso)->fd;
  1128. if (fd >= 0)
  1129. e_machine = dso__read_e_machine(dso, fd, e_flags);
  1130. else if (e_flags)
  1131. *e_flags = 0;
  1132. mutex_unlock(dso__data_open_lock());
  1133. return e_machine;
  1134. }
  1135. /**
  1136. * dso__data_read_addr - Read data from dso address
  1137. * @dso: dso object
  1138. * @machine: machine object
  1139. * @add: virtual memory address
  1140. * @data: buffer to store data
  1141. * @size: size of the @data buffer
  1142. *
  1143. * External interface to read data from dso address.
  1144. */
  1145. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  1146. struct machine *machine, u64 addr,
  1147. u8 *data, ssize_t size)
  1148. {
  1149. u64 offset = map__map_ip(map, addr);
  1150. return dso__data_read_offset(dso, machine, offset, data, size);
  1151. }
  1152. /**
  1153. * dso__data_write_cache_offs - Write data to dso data cache at file offset
  1154. * @dso: dso object
  1155. * @machine: machine object
  1156. * @offset: file offset
  1157. * @data: buffer to write
  1158. * @size: size of the @data buffer
  1159. *
  1160. * Write into the dso file data cache, but do not change the file itself.
  1161. */
  1162. ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
  1163. u64 offset, const u8 *data_in, ssize_t size)
  1164. {
  1165. u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
  1166. if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR)
  1167. return -1;
  1168. return data_read_write_offset(dso, machine, offset, data, size, false);
  1169. }
  1170. /**
  1171. * dso__data_write_cache_addr - Write data to dso data cache at dso address
  1172. * @dso: dso object
  1173. * @machine: machine object
  1174. * @add: virtual memory address
  1175. * @data: buffer to write
  1176. * @size: size of the @data buffer
  1177. *
  1178. * External interface to write into the dso file data cache, but do not change
  1179. * the file itself.
  1180. */
  1181. ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
  1182. struct machine *machine, u64 addr,
  1183. const u8 *data, ssize_t size)
  1184. {
  1185. u64 offset = map__map_ip(map, addr);
  1186. return dso__data_write_cache_offs(dso, machine, offset, data, size);
  1187. }
  1188. struct map *dso__new_map(const char *name)
  1189. {
  1190. struct map *map = NULL;
  1191. struct dso *dso = dso__new(name);
  1192. if (dso) {
  1193. map = map__new2(0, dso);
  1194. dso__put(dso);
  1195. }
  1196. return map;
  1197. }
  1198. struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
  1199. const char *short_name, int dso_type)
  1200. {
  1201. /*
  1202. * The kernel dso could be created by build_id processing.
  1203. */
  1204. struct dso *dso = machine__findnew_dso(machine, name);
  1205. /*
  1206. * We need to run this in all cases, since during the build_id
  1207. * processing we had no idea this was the kernel dso.
  1208. */
  1209. if (dso != NULL) {
  1210. dso__set_short_name(dso, short_name, false);
  1211. dso__set_kernel(dso, dso_type);
  1212. }
  1213. return dso;
  1214. }
  1215. static void __dso__set_long_name_id(struct dso *dso, const char *name, bool name_allocated)
  1216. {
  1217. if (dso__long_name_allocated(dso))
  1218. free((char *)dso__long_name(dso));
  1219. RC_CHK_ACCESS(dso)->long_name = name;
  1220. RC_CHK_ACCESS(dso)->long_name_len = strlen(name);
  1221. dso__set_long_name_allocated(dso, name_allocated);
  1222. }
  1223. static void dso__set_long_name_id(struct dso *dso, const char *name, bool name_allocated)
  1224. {
  1225. struct dsos *dsos = dso__dsos(dso);
  1226. if (name == NULL)
  1227. return;
  1228. if (dsos) {
  1229. /*
  1230. * Need to avoid re-sorting the dsos breaking by non-atomically
  1231. * renaming the dso.
  1232. */
  1233. down_write(&dsos->lock);
  1234. __dso__set_long_name_id(dso, name, name_allocated);
  1235. dsos->sorted = false;
  1236. up_write(&dsos->lock);
  1237. } else {
  1238. __dso__set_long_name_id(dso, name, name_allocated);
  1239. }
  1240. }
  1241. static int __dso_id__cmp(const struct dso_id *a, const struct dso_id *b)
  1242. {
  1243. if (a->mmap2_valid && b->mmap2_valid) {
  1244. if (a->maj > b->maj) return -1;
  1245. if (a->maj < b->maj) return 1;
  1246. if (a->min > b->min) return -1;
  1247. if (a->min < b->min) return 1;
  1248. if (a->ino > b->ino) return -1;
  1249. if (a->ino < b->ino) return 1;
  1250. }
  1251. if (a->mmap2_ino_generation_valid && b->mmap2_ino_generation_valid) {
  1252. if (a->ino_generation > b->ino_generation) return -1;
  1253. if (a->ino_generation < b->ino_generation) return 1;
  1254. }
  1255. if (build_id__is_defined(&a->build_id) && build_id__is_defined(&b->build_id)) {
  1256. if (a->build_id.size != b->build_id.size)
  1257. return a->build_id.size < b->build_id.size ? -1 : 1;
  1258. return memcmp(a->build_id.data, b->build_id.data, a->build_id.size);
  1259. }
  1260. return 0;
  1261. }
  1262. const struct dso_id dso_id_empty = {
  1263. {
  1264. .maj = 0,
  1265. .min = 0,
  1266. .ino = 0,
  1267. .ino_generation = 0,
  1268. },
  1269. .mmap2_valid = false,
  1270. .mmap2_ino_generation_valid = false,
  1271. {
  1272. .size = 0,
  1273. }
  1274. };
  1275. void __dso__improve_id(struct dso *dso, const struct dso_id *id)
  1276. {
  1277. struct dsos *dsos = dso__dsos(dso);
  1278. struct dso_id *dso_id = dso__id(dso);
  1279. bool changed = false;
  1280. /* dsos write lock held by caller. */
  1281. if (id->mmap2_valid && !dso_id->mmap2_valid) {
  1282. dso_id->maj = id->maj;
  1283. dso_id->min = id->min;
  1284. dso_id->ino = id->ino;
  1285. dso_id->mmap2_valid = true;
  1286. changed = true;
  1287. }
  1288. if (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_valid) {
  1289. dso_id->ino_generation = id->ino_generation;
  1290. dso_id->mmap2_ino_generation_valid = true;
  1291. changed = true;
  1292. }
  1293. if (build_id__is_defined(&id->build_id) && !build_id__is_defined(&dso_id->build_id)) {
  1294. dso_id->build_id = id->build_id;
  1295. changed = true;
  1296. }
  1297. if (changed && dsos)
  1298. dsos->sorted = false;
  1299. }
  1300. int dso_id__cmp(const struct dso_id *a, const struct dso_id *b)
  1301. {
  1302. if (a == &dso_id_empty || b == &dso_id_empty) {
  1303. /* There is no valid data to compare so the comparison always returns identical. */
  1304. return 0;
  1305. }
  1306. return __dso_id__cmp(a, b);
  1307. }
  1308. int dso__cmp_id(struct dso *a, struct dso *b)
  1309. {
  1310. return __dso_id__cmp(dso__id(a), dso__id(b));
  1311. }
  1312. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
  1313. {
  1314. dso__set_long_name_id(dso, name, name_allocated);
  1315. }
  1316. static void __dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  1317. {
  1318. if (dso__short_name_allocated(dso))
  1319. free((char *)dso__short_name(dso));
  1320. RC_CHK_ACCESS(dso)->short_name = name;
  1321. RC_CHK_ACCESS(dso)->short_name_len = strlen(name);
  1322. dso__set_short_name_allocated(dso, name_allocated);
  1323. }
  1324. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
  1325. {
  1326. struct dsos *dsos = dso__dsos(dso);
  1327. if (name == NULL)
  1328. return;
  1329. if (dsos) {
  1330. /*
  1331. * Need to avoid re-sorting the dsos breaking by non-atomically
  1332. * renaming the dso.
  1333. */
  1334. down_write(&dsos->lock);
  1335. __dso__set_short_name(dso, name, name_allocated);
  1336. dsos->sorted = false;
  1337. up_write(&dsos->lock);
  1338. } else {
  1339. __dso__set_short_name(dso, name, name_allocated);
  1340. }
  1341. }
  1342. int dso__name_len(const struct dso *dso)
  1343. {
  1344. if (!dso)
  1345. return strlen("[unknown]");
  1346. if (verbose > 0)
  1347. return dso__long_name_len(dso);
  1348. return dso__short_name_len(dso);
  1349. }
  1350. bool dso__loaded(const struct dso *dso)
  1351. {
  1352. return RC_CHK_ACCESS(dso)->loaded;
  1353. }
  1354. bool dso__sorted_by_name(const struct dso *dso)
  1355. {
  1356. return RC_CHK_ACCESS(dso)->sorted_by_name;
  1357. }
  1358. void dso__set_sorted_by_name(struct dso *dso)
  1359. {
  1360. RC_CHK_ACCESS(dso)->sorted_by_name = true;
  1361. }
  1362. struct dso *dso__new_id(const char *name, const struct dso_id *id)
  1363. {
  1364. RC_STRUCT(dso) *dso = zalloc(sizeof(*dso) + strlen(name) + 1);
  1365. struct dso *res;
  1366. struct dso_data *data;
  1367. if (!dso)
  1368. return NULL;
  1369. if (ADD_RC_CHK(res, dso)) {
  1370. strcpy(dso->name, name);
  1371. if (id)
  1372. dso->id = *id;
  1373. dso__set_long_name_id(res, dso->name, false);
  1374. dso__set_short_name(res, dso->name, false);
  1375. dso->symbols = RB_ROOT_CACHED;
  1376. dso->symbol_names = NULL;
  1377. dso->symbol_names_len = 0;
  1378. dso->inlined_nodes = RB_ROOT_CACHED;
  1379. dso->srclines = RB_ROOT_CACHED;
  1380. dso->data_types = RB_ROOT;
  1381. dso->global_vars = RB_ROOT;
  1382. dso->data.fd = -1;
  1383. dso->data.status = DSO_DATA_STATUS_UNKNOWN;
  1384. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  1385. dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
  1386. dso->is_64_bit = (sizeof(void *) == 8);
  1387. dso->loaded = 0;
  1388. dso->rel = 0;
  1389. dso->sorted_by_name = 0;
  1390. dso->has_srcline = 1;
  1391. dso->a2l_fails = 1;
  1392. dso->kernel = DSO_SPACE__USER;
  1393. dso->is_kmod = 0;
  1394. dso->needs_swap = DSO_SWAP__UNSET;
  1395. dso->comp = COMP_ID__NONE;
  1396. mutex_init(&dso->lock);
  1397. refcount_set(&dso->refcnt, 1);
  1398. data = &dso->data;
  1399. data->cache = RB_ROOT;
  1400. data->fd = -1;
  1401. data->status = DSO_DATA_STATUS_UNKNOWN;
  1402. INIT_LIST_HEAD(&data->open_entry);
  1403. #ifdef REFCNT_CHECKING
  1404. data->dso = NULL; /* Set when on the open_entry list. */
  1405. #endif
  1406. }
  1407. return res;
  1408. }
  1409. struct dso *dso__new(const char *name)
  1410. {
  1411. return dso__new_id(name, NULL);
  1412. }
  1413. void dso__delete(struct dso *dso)
  1414. {
  1415. if (dso__dsos(dso))
  1416. pr_err("DSO %s is still in rbtree when being deleted!\n", dso__long_name(dso));
  1417. /* free inlines first, as they reference symbols */
  1418. inlines__tree_delete(&RC_CHK_ACCESS(dso)->inlined_nodes);
  1419. srcline__tree_delete(&RC_CHK_ACCESS(dso)->srclines);
  1420. symbols__delete(&RC_CHK_ACCESS(dso)->symbols);
  1421. RC_CHK_ACCESS(dso)->symbol_names_len = 0;
  1422. zfree(&RC_CHK_ACCESS(dso)->symbol_names);
  1423. annotated_data_type__tree_delete(dso__data_types(dso));
  1424. global_var_type__tree_delete(dso__global_vars(dso));
  1425. if (RC_CHK_ACCESS(dso)->short_name_allocated) {
  1426. zfree((char **)&RC_CHK_ACCESS(dso)->short_name);
  1427. RC_CHK_ACCESS(dso)->short_name_allocated = false;
  1428. }
  1429. if (RC_CHK_ACCESS(dso)->long_name_allocated) {
  1430. zfree((char **)&RC_CHK_ACCESS(dso)->long_name);
  1431. RC_CHK_ACCESS(dso)->long_name_allocated = false;
  1432. }
  1433. dso__data_close(dso);
  1434. auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache);
  1435. dso_cache__free(dso);
  1436. dso__free_a2l(dso);
  1437. dso__free_libdw(dso);
  1438. dso__free_symsrc_filename(dso);
  1439. nsinfo__zput(RC_CHK_ACCESS(dso)->nsinfo);
  1440. mutex_destroy(dso__lock(dso));
  1441. RC_CHK_FREE(dso);
  1442. }
  1443. struct dso *dso__get(struct dso *dso)
  1444. {
  1445. struct dso *result;
  1446. if (RC_CHK_GET(result, dso))
  1447. refcount_inc(&RC_CHK_ACCESS(dso)->refcnt);
  1448. return result;
  1449. }
  1450. void dso__put(struct dso *dso)
  1451. {
  1452. #ifdef REFCNT_CHECKING
  1453. if (dso && dso__data(dso) && refcount_read(&RC_CHK_ACCESS(dso)->refcnt) == 2)
  1454. dso__data_close(dso);
  1455. #endif
  1456. if (dso && refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt))
  1457. dso__delete(dso);
  1458. else
  1459. RC_CHK_PUT(dso);
  1460. }
  1461. int dso__swap_init(struct dso *dso, unsigned char eidata)
  1462. {
  1463. enum dso_swap_type type = dso_swap_type__from_elf_data(eidata);
  1464. dso__set_needs_swap(dso, type);
  1465. if (type == DSO_SWAP__UNSET) {
  1466. pr_err("unrecognized DSO data encoding %d\n", eidata);
  1467. return -EINVAL;
  1468. }
  1469. return 0;
  1470. }
  1471. void dso__set_build_id(struct dso *dso, const struct build_id *bid)
  1472. {
  1473. dso__id(dso)->build_id = *bid;
  1474. }
  1475. bool dso__build_id_equal(const struct dso *dso, const struct build_id *bid)
  1476. {
  1477. const struct build_id *dso_bid = dso__bid(dso);
  1478. if (dso_bid->size > bid->size && dso_bid->size == BUILD_ID_SIZE) {
  1479. /*
  1480. * For the backward compatibility, it allows a build-id has
  1481. * trailing zeros.
  1482. */
  1483. return !memcmp(dso_bid->data, bid->data, bid->size) &&
  1484. !memchr_inv(&dso_bid->data[bid->size], 0,
  1485. dso_bid->size - bid->size);
  1486. }
  1487. return dso_bid->size == bid->size &&
  1488. memcmp(dso_bid->data, bid->data, dso_bid->size) == 0;
  1489. }
  1490. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  1491. {
  1492. char path[PATH_MAX];
  1493. struct build_id bid = { .size = 0, };
  1494. if (machine__is_default_guest(machine))
  1495. return;
  1496. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  1497. sysfs__read_build_id(path, &bid);
  1498. dso__set_build_id(dso, &bid);
  1499. }
  1500. int dso__kernel_module_get_build_id(struct dso *dso,
  1501. const char *root_dir)
  1502. {
  1503. char filename[PATH_MAX];
  1504. struct build_id bid = { .size = 0, };
  1505. /*
  1506. * kernel module short names are of the form "[module]" and
  1507. * we need just "module" here.
  1508. */
  1509. const char *name = dso__short_name(dso) + 1;
  1510. snprintf(filename, sizeof(filename),
  1511. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1512. root_dir, (int)strlen(name) - 1, name);
  1513. sysfs__read_build_id(filename, &bid);
  1514. dso__set_build_id(dso, &bid);
  1515. return 0;
  1516. }
  1517. static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  1518. {
  1519. char sbuild_id[SBUILD_ID_SIZE];
  1520. build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id));
  1521. return fprintf(fp, "%s", sbuild_id);
  1522. }
  1523. size_t dso__fprintf(struct dso *dso, FILE *fp)
  1524. {
  1525. struct rb_node *nd;
  1526. size_t ret = fprintf(fp, "dso: %s (", dso__short_name(dso));
  1527. if (dso__short_name(dso) != dso__long_name(dso))
  1528. ret += fprintf(fp, "%s, ", dso__long_name(dso));
  1529. ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
  1530. ret += dso__fprintf_buildid(dso, fp);
  1531. ret += fprintf(fp, ")\n");
  1532. for (nd = rb_first_cached(dso__symbols(dso)); nd; nd = rb_next(nd)) {
  1533. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  1534. ret += symbol__fprintf(pos, fp);
  1535. }
  1536. return ret;
  1537. }
  1538. enum dso_type dso__type(struct dso *dso, struct machine *machine)
  1539. {
  1540. int fd = -1;
  1541. enum dso_type type = DSO__TYPE_UNKNOWN;
  1542. if (dso__data_get_fd(dso, machine, &fd)) {
  1543. type = dso__type_fd(fd);
  1544. dso__data_put_fd(dso);
  1545. }
  1546. return type;
  1547. }
  1548. int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
  1549. {
  1550. int idx, errnum = *dso__load_errno(dso);
  1551. /*
  1552. * This must have a same ordering as the enum dso_load_errno.
  1553. */
  1554. static const char *dso_load__error_str[] = {
  1555. "Internal tools/perf/ library error",
  1556. "Invalid ELF file",
  1557. "Can not read build id",
  1558. "Mismatching build id",
  1559. "Decompression failure",
  1560. };
  1561. BUG_ON(buflen == 0);
  1562. if (errnum >= 0) {
  1563. errno = errnum;
  1564. scnprintf(buf, buflen, "%m");
  1565. return 0;
  1566. }
  1567. if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
  1568. return -1;
  1569. idx = errnum - __DSO_LOAD_ERRNO__START;
  1570. scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
  1571. return 0;
  1572. }
  1573. bool perf_pid_map_tid(const char *dso_name, int *tid)
  1574. {
  1575. return sscanf(dso_name, "/tmp/perf-%d.map", tid) == 1;
  1576. }
  1577. bool is_perf_pid_map_name(const char *dso_name)
  1578. {
  1579. int tid;
  1580. return perf_pid_map_tid(dso_name, &tid);
  1581. }
  1582. struct find_file_offset_data {
  1583. u64 ip;
  1584. u64 offset;
  1585. };
  1586. /* This will be called for each PHDR in an ELF binary */
  1587. static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
  1588. {
  1589. struct find_file_offset_data *data = arg;
  1590. if (start <= data->ip && data->ip < start + len) {
  1591. data->offset = pgoff + data->ip - start;
  1592. return 1;
  1593. }
  1594. return 0;
  1595. }
  1596. static const u8 *__dso__read_symbol(struct dso *dso, const char *symfs_filename,
  1597. u64 start, size_t len,
  1598. u8 **out_buf, u64 *out_buf_len, bool *is_64bit)
  1599. {
  1600. struct nscookie nsc;
  1601. int fd;
  1602. ssize_t count;
  1603. struct find_file_offset_data data = {
  1604. .ip = start,
  1605. };
  1606. u8 *code_buf = NULL;
  1607. int saved_errno;
  1608. nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
  1609. fd = open(symfs_filename, O_RDONLY);
  1610. saved_errno = errno;
  1611. nsinfo__mountns_exit(&nsc);
  1612. if (fd < 0) {
  1613. errno = saved_errno;
  1614. return NULL;
  1615. }
  1616. if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) <= 0) {
  1617. close(fd);
  1618. errno = ENOENT;
  1619. return NULL;
  1620. }
  1621. code_buf = malloc(len);
  1622. if (code_buf == NULL) {
  1623. close(fd);
  1624. errno = ENOMEM;
  1625. return NULL;
  1626. }
  1627. count = pread(fd, code_buf, len, data.offset);
  1628. saved_errno = errno;
  1629. close(fd);
  1630. if ((u64)count != len) {
  1631. free(code_buf);
  1632. errno = saved_errno;
  1633. return NULL;
  1634. }
  1635. *out_buf = code_buf;
  1636. *out_buf_len = len;
  1637. return code_buf;
  1638. }
  1639. /*
  1640. * Read a symbol into memory for disassembly by a library like capstone of
  1641. * libLLVM. If memory is allocated out_buf holds it.
  1642. */
  1643. const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
  1644. const struct map *map, const struct symbol *sym,
  1645. u8 **out_buf, u64 *out_buf_len, bool *is_64bit)
  1646. {
  1647. u64 start = map__rip_2objdump(map, sym->start);
  1648. u64 end = map__rip_2objdump(map, sym->end);
  1649. size_t len = end - start;
  1650. *out_buf = NULL;
  1651. *out_buf_len = 0;
  1652. *is_64bit = false;
  1653. if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
  1654. /*
  1655. * Note, there is fallback BPF image disassembly in the objdump
  1656. * version but it currently does nothing.
  1657. */
  1658. errno = EOPNOTSUPP;
  1659. return NULL;
  1660. }
  1661. if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
  1662. #ifdef HAVE_LIBBPF_SUPPORT
  1663. struct bpf_prog_info_node *info_node;
  1664. struct perf_bpil *info_linear;
  1665. *is_64bit = sizeof(void *) == sizeof(u64);
  1666. info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env,
  1667. dso__bpf_prog(dso)->id);
  1668. if (!info_node) {
  1669. errno = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
  1670. return NULL;
  1671. }
  1672. info_linear = info_node->info_linear;
  1673. assert(len <= info_linear->info.jited_prog_len);
  1674. *out_buf_len = len;
  1675. return (const u8 *)(uintptr_t)(info_linear->info.jited_prog_insns);
  1676. #else
  1677. pr_debug("No BPF program disassembly support\n");
  1678. errno = EOPNOTSUPP;
  1679. return NULL;
  1680. #endif
  1681. }
  1682. return __dso__read_symbol(dso, symfs_filename, start, len,
  1683. out_buf, out_buf_len, is_64bit);
  1684. }
  1685. struct debuginfo *dso__debuginfo(struct dso *dso)
  1686. {
  1687. char *name;
  1688. bool decomp = false;
  1689. struct debuginfo *dinfo = NULL;
  1690. mutex_lock(dso__lock(dso));
  1691. name = dso__get_filename(dso, "", &decomp);
  1692. if (name)
  1693. dinfo = debuginfo__new(name);
  1694. if (decomp)
  1695. unlink(name);
  1696. mutex_unlock(dso__lock(dso));
  1697. free(name);
  1698. return dinfo;
  1699. }