symbol.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064
  1. #include <dirent.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/param.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <inttypes.h>
  12. #include "build-id.h"
  13. #include "util.h"
  14. #include "debug.h"
  15. #include "symbol.h"
  16. #include "strlist.h"
  17. #include <libelf.h>
  18. #include <gelf.h>
  19. #include <elf.h>
  20. #include <limits.h>
  21. #include <sys/utsname.h>
  22. #ifndef KSYM_NAME_LEN
  23. #define KSYM_NAME_LEN 256
  24. #endif
  25. #ifndef NT_GNU_BUILD_ID
  26. #define NT_GNU_BUILD_ID 3
  27. #endif
  28. static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  29. static int elf_read_build_id(Elf *elf, void *bf, size_t size);
  30. static void dsos__add(struct list_head *head, struct dso *dso);
  31. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
  32. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  33. symbol_filter_t filter);
  34. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  35. symbol_filter_t filter);
  36. static int vmlinux_path__nr_entries;
  37. static char **vmlinux_path;
  38. struct symbol_conf symbol_conf = {
  39. .exclude_other = true,
  40. .use_modules = true,
  41. .try_vmlinux_path = true,
  42. .annotate_src = true,
  43. .symfs = "",
  44. };
  45. static enum dso_binary_type binary_type_symtab[] = {
  46. DSO_BINARY_TYPE__KALLSYMS,
  47. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  48. DSO_BINARY_TYPE__JAVA_JIT,
  49. DSO_BINARY_TYPE__DEBUGLINK,
  50. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  51. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  52. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  53. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  54. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  55. DSO_BINARY_TYPE__GUEST_KMODULE,
  56. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  57. DSO_BINARY_TYPE__NOT_FOUND,
  58. };
  59. #define DSO_BINARY_TYPE__SYMTAB_CNT sizeof(binary_type_symtab)
  60. static enum dso_binary_type binary_type_data[] = {
  61. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  62. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  63. DSO_BINARY_TYPE__NOT_FOUND,
  64. };
  65. #define DSO_BINARY_TYPE__DATA_CNT sizeof(binary_type_data)
  66. int dso__name_len(const struct dso *dso)
  67. {
  68. if (!dso)
  69. return strlen("[unknown]");
  70. if (verbose)
  71. return dso->long_name_len;
  72. return dso->short_name_len;
  73. }
  74. bool dso__loaded(const struct dso *dso, enum map_type type)
  75. {
  76. return dso->loaded & (1 << type);
  77. }
  78. bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
  79. {
  80. return dso->sorted_by_name & (1 << type);
  81. }
  82. static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
  83. {
  84. dso->sorted_by_name |= (1 << type);
  85. }
  86. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  87. {
  88. symbol_type = toupper(symbol_type);
  89. switch (map_type) {
  90. case MAP__FUNCTION:
  91. return symbol_type == 'T' || symbol_type == 'W';
  92. case MAP__VARIABLE:
  93. return symbol_type == 'D';
  94. default:
  95. return false;
  96. }
  97. }
  98. static int prefix_underscores_count(const char *str)
  99. {
  100. const char *tail = str;
  101. while (*tail == '_')
  102. tail++;
  103. return tail - str;
  104. }
  105. #define SYMBOL_A 0
  106. #define SYMBOL_B 1
  107. static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
  108. {
  109. s64 a;
  110. s64 b;
  111. /* Prefer a symbol with non zero length */
  112. a = syma->end - syma->start;
  113. b = symb->end - symb->start;
  114. if ((b == 0) && (a > 0))
  115. return SYMBOL_A;
  116. else if ((a == 0) && (b > 0))
  117. return SYMBOL_B;
  118. /* Prefer a non weak symbol over a weak one */
  119. a = syma->binding == STB_WEAK;
  120. b = symb->binding == STB_WEAK;
  121. if (b && !a)
  122. return SYMBOL_A;
  123. if (a && !b)
  124. return SYMBOL_B;
  125. /* Prefer a global symbol over a non global one */
  126. a = syma->binding == STB_GLOBAL;
  127. b = symb->binding == STB_GLOBAL;
  128. if (a && !b)
  129. return SYMBOL_A;
  130. if (b && !a)
  131. return SYMBOL_B;
  132. /* Prefer a symbol with less underscores */
  133. a = prefix_underscores_count(syma->name);
  134. b = prefix_underscores_count(symb->name);
  135. if (b > a)
  136. return SYMBOL_A;
  137. else if (a > b)
  138. return SYMBOL_B;
  139. /* If all else fails, choose the symbol with the longest name */
  140. if (strlen(syma->name) >= strlen(symb->name))
  141. return SYMBOL_A;
  142. else
  143. return SYMBOL_B;
  144. }
  145. static void symbols__fixup_duplicate(struct rb_root *symbols)
  146. {
  147. struct rb_node *nd;
  148. struct symbol *curr, *next;
  149. nd = rb_first(symbols);
  150. while (nd) {
  151. curr = rb_entry(nd, struct symbol, rb_node);
  152. again:
  153. nd = rb_next(&curr->rb_node);
  154. next = rb_entry(nd, struct symbol, rb_node);
  155. if (!nd)
  156. break;
  157. if (curr->start != next->start)
  158. continue;
  159. if (choose_best_symbol(curr, next) == SYMBOL_A) {
  160. rb_erase(&next->rb_node, symbols);
  161. goto again;
  162. } else {
  163. nd = rb_next(&curr->rb_node);
  164. rb_erase(&curr->rb_node, symbols);
  165. }
  166. }
  167. }
  168. static void symbols__fixup_end(struct rb_root *symbols)
  169. {
  170. struct rb_node *nd, *prevnd = rb_first(symbols);
  171. struct symbol *curr, *prev;
  172. if (prevnd == NULL)
  173. return;
  174. curr = rb_entry(prevnd, struct symbol, rb_node);
  175. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  176. prev = curr;
  177. curr = rb_entry(nd, struct symbol, rb_node);
  178. if (prev->end == prev->start && prev->end != curr->start)
  179. prev->end = curr->start - 1;
  180. }
  181. /* Last entry */
  182. if (curr->end == curr->start)
  183. curr->end = roundup(curr->start, 4096);
  184. }
  185. static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
  186. {
  187. struct map *prev, *curr;
  188. struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
  189. if (prevnd == NULL)
  190. return;
  191. curr = rb_entry(prevnd, struct map, rb_node);
  192. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  193. prev = curr;
  194. curr = rb_entry(nd, struct map, rb_node);
  195. prev->end = curr->start - 1;
  196. }
  197. /*
  198. * We still haven't the actual symbols, so guess the
  199. * last map final address.
  200. */
  201. curr->end = ~0ULL;
  202. }
  203. static void map_groups__fixup_end(struct map_groups *mg)
  204. {
  205. int i;
  206. for (i = 0; i < MAP__NR_TYPES; ++i)
  207. __map_groups__fixup_end(mg, i);
  208. }
  209. static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
  210. const char *name)
  211. {
  212. size_t namelen = strlen(name) + 1;
  213. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  214. sizeof(*sym) + namelen));
  215. if (sym == NULL)
  216. return NULL;
  217. if (symbol_conf.priv_size)
  218. sym = ((void *)sym) + symbol_conf.priv_size;
  219. sym->start = start;
  220. sym->end = len ? start + len - 1 : start;
  221. sym->binding = binding;
  222. sym->namelen = namelen - 1;
  223. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  224. __func__, name, start, sym->end);
  225. memcpy(sym->name, name, namelen);
  226. return sym;
  227. }
  228. void symbol__delete(struct symbol *sym)
  229. {
  230. free(((void *)sym) - symbol_conf.priv_size);
  231. }
  232. static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
  233. {
  234. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
  235. sym->start, sym->end,
  236. sym->binding == STB_GLOBAL ? 'g' :
  237. sym->binding == STB_LOCAL ? 'l' : 'w',
  238. sym->name);
  239. }
  240. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  241. const struct addr_location *al, FILE *fp)
  242. {
  243. unsigned long offset;
  244. size_t length;
  245. if (sym && sym->name) {
  246. length = fprintf(fp, "%s", sym->name);
  247. if (al) {
  248. offset = al->addr - sym->start;
  249. length += fprintf(fp, "+0x%lx", offset);
  250. }
  251. return length;
  252. } else
  253. return fprintf(fp, "[unknown]");
  254. }
  255. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
  256. {
  257. return symbol__fprintf_symname_offs(sym, NULL, fp);
  258. }
  259. void dso__set_long_name(struct dso *dso, char *name)
  260. {
  261. if (name == NULL)
  262. return;
  263. dso->long_name = name;
  264. dso->long_name_len = strlen(name);
  265. }
  266. static void dso__set_short_name(struct dso *dso, const char *name)
  267. {
  268. if (name == NULL)
  269. return;
  270. dso->short_name = name;
  271. dso->short_name_len = strlen(name);
  272. }
  273. static void dso__set_basename(struct dso *dso)
  274. {
  275. dso__set_short_name(dso, basename(dso->long_name));
  276. }
  277. struct dso *dso__new(const char *name)
  278. {
  279. struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
  280. if (dso != NULL) {
  281. int i;
  282. strcpy(dso->name, name);
  283. dso__set_long_name(dso, dso->name);
  284. dso__set_short_name(dso, dso->name);
  285. for (i = 0; i < MAP__NR_TYPES; ++i)
  286. dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
  287. dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  288. dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
  289. dso->loaded = 0;
  290. dso->sorted_by_name = 0;
  291. dso->has_build_id = 0;
  292. dso->kernel = DSO_TYPE_USER;
  293. dso->needs_swap = DSO_SWAP__UNSET;
  294. INIT_LIST_HEAD(&dso->node);
  295. }
  296. return dso;
  297. }
  298. static void symbols__delete(struct rb_root *symbols)
  299. {
  300. struct symbol *pos;
  301. struct rb_node *next = rb_first(symbols);
  302. while (next) {
  303. pos = rb_entry(next, struct symbol, rb_node);
  304. next = rb_next(&pos->rb_node);
  305. rb_erase(&pos->rb_node, symbols);
  306. symbol__delete(pos);
  307. }
  308. }
  309. void dso__delete(struct dso *dso)
  310. {
  311. int i;
  312. for (i = 0; i < MAP__NR_TYPES; ++i)
  313. symbols__delete(&dso->symbols[i]);
  314. if (dso->sname_alloc)
  315. free((char *)dso->short_name);
  316. if (dso->lname_alloc)
  317. free(dso->long_name);
  318. free(dso);
  319. }
  320. void dso__set_build_id(struct dso *dso, void *build_id)
  321. {
  322. memcpy(dso->build_id, build_id, sizeof(dso->build_id));
  323. dso->has_build_id = 1;
  324. }
  325. static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  326. {
  327. struct rb_node **p = &symbols->rb_node;
  328. struct rb_node *parent = NULL;
  329. const u64 ip = sym->start;
  330. struct symbol *s;
  331. while (*p != NULL) {
  332. parent = *p;
  333. s = rb_entry(parent, struct symbol, rb_node);
  334. if (ip < s->start)
  335. p = &(*p)->rb_left;
  336. else
  337. p = &(*p)->rb_right;
  338. }
  339. rb_link_node(&sym->rb_node, parent, p);
  340. rb_insert_color(&sym->rb_node, symbols);
  341. }
  342. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  343. {
  344. struct rb_node *n;
  345. if (symbols == NULL)
  346. return NULL;
  347. n = symbols->rb_node;
  348. while (n) {
  349. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  350. if (ip < s->start)
  351. n = n->rb_left;
  352. else if (ip > s->end)
  353. n = n->rb_right;
  354. else
  355. return s;
  356. }
  357. return NULL;
  358. }
  359. struct symbol_name_rb_node {
  360. struct rb_node rb_node;
  361. struct symbol sym;
  362. };
  363. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  364. {
  365. struct rb_node **p = &symbols->rb_node;
  366. struct rb_node *parent = NULL;
  367. struct symbol_name_rb_node *symn, *s;
  368. symn = container_of(sym, struct symbol_name_rb_node, sym);
  369. while (*p != NULL) {
  370. parent = *p;
  371. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  372. if (strcmp(sym->name, s->sym.name) < 0)
  373. p = &(*p)->rb_left;
  374. else
  375. p = &(*p)->rb_right;
  376. }
  377. rb_link_node(&symn->rb_node, parent, p);
  378. rb_insert_color(&symn->rb_node, symbols);
  379. }
  380. static void symbols__sort_by_name(struct rb_root *symbols,
  381. struct rb_root *source)
  382. {
  383. struct rb_node *nd;
  384. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  385. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  386. symbols__insert_by_name(symbols, pos);
  387. }
  388. }
  389. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  390. const char *name)
  391. {
  392. struct rb_node *n;
  393. if (symbols == NULL)
  394. return NULL;
  395. n = symbols->rb_node;
  396. while (n) {
  397. struct symbol_name_rb_node *s;
  398. int cmp;
  399. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  400. cmp = strcmp(name, s->sym.name);
  401. if (cmp < 0)
  402. n = n->rb_left;
  403. else if (cmp > 0)
  404. n = n->rb_right;
  405. else
  406. return &s->sym;
  407. }
  408. return NULL;
  409. }
  410. struct symbol *dso__find_symbol(struct dso *dso,
  411. enum map_type type, u64 addr)
  412. {
  413. return symbols__find(&dso->symbols[type], addr);
  414. }
  415. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  416. const char *name)
  417. {
  418. return symbols__find_by_name(&dso->symbol_names[type], name);
  419. }
  420. void dso__sort_by_name(struct dso *dso, enum map_type type)
  421. {
  422. dso__set_sorted_by_name(dso, type);
  423. return symbols__sort_by_name(&dso->symbol_names[type],
  424. &dso->symbols[type]);
  425. }
  426. int build_id__sprintf(const u8 *build_id, int len, char *bf)
  427. {
  428. char *bid = bf;
  429. const u8 *raw = build_id;
  430. int i;
  431. for (i = 0; i < len; ++i) {
  432. sprintf(bid, "%02x", *raw);
  433. ++raw;
  434. bid += 2;
  435. }
  436. return raw - build_id;
  437. }
  438. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
  439. {
  440. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  441. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  442. return fprintf(fp, "%s", sbuild_id);
  443. }
  444. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  445. enum map_type type, FILE *fp)
  446. {
  447. size_t ret = 0;
  448. struct rb_node *nd;
  449. struct symbol_name_rb_node *pos;
  450. for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
  451. pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  452. fprintf(fp, "%s\n", pos->sym.name);
  453. }
  454. return ret;
  455. }
  456. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
  457. {
  458. struct rb_node *nd;
  459. size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
  460. if (dso->short_name != dso->long_name)
  461. ret += fprintf(fp, "%s, ", dso->long_name);
  462. ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
  463. dso->loaded ? "" : "NOT ");
  464. ret += dso__fprintf_buildid(dso, fp);
  465. ret += fprintf(fp, ")\n");
  466. for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
  467. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  468. ret += symbol__fprintf(pos, fp);
  469. }
  470. return ret;
  471. }
  472. int kallsyms__parse(const char *filename, void *arg,
  473. int (*process_symbol)(void *arg, const char *name,
  474. char type, u64 start, u64 end))
  475. {
  476. char *line = NULL;
  477. size_t n;
  478. int err = -1;
  479. FILE *file = fopen(filename, "r");
  480. if (file == NULL)
  481. goto out_failure;
  482. err = 0;
  483. while (!feof(file)) {
  484. u64 start;
  485. int line_len, len;
  486. char symbol_type;
  487. char *symbol_name;
  488. line_len = getline(&line, &n, file);
  489. if (line_len < 0 || !line)
  490. break;
  491. line[--line_len] = '\0'; /* \n */
  492. len = hex2u64(line, &start);
  493. len++;
  494. if (len + 2 >= line_len)
  495. continue;
  496. symbol_type = line[len];
  497. len += 2;
  498. symbol_name = line + len;
  499. len = line_len - len;
  500. if (len >= KSYM_NAME_LEN) {
  501. err = -1;
  502. break;
  503. }
  504. /*
  505. * module symbols are not sorted so we add all
  506. * symbols with zero length and rely on
  507. * symbols__fixup_end() to fix it up.
  508. */
  509. err = process_symbol(arg, symbol_name,
  510. symbol_type, start, start);
  511. if (err)
  512. break;
  513. }
  514. free(line);
  515. fclose(file);
  516. return err;
  517. out_failure:
  518. return -1;
  519. }
  520. struct process_kallsyms_args {
  521. struct map *map;
  522. struct dso *dso;
  523. };
  524. static u8 kallsyms2elf_type(char type)
  525. {
  526. if (type == 'W')
  527. return STB_WEAK;
  528. return isupper(type) ? STB_GLOBAL : STB_LOCAL;
  529. }
  530. static int map__process_kallsym_symbol(void *arg, const char *name,
  531. char type, u64 start, u64 end)
  532. {
  533. struct symbol *sym;
  534. struct process_kallsyms_args *a = arg;
  535. struct rb_root *root = &a->dso->symbols[a->map->type];
  536. if (!symbol_type__is_a(type, a->map->type))
  537. return 0;
  538. sym = symbol__new(start, end - start + 1,
  539. kallsyms2elf_type(type), name);
  540. if (sym == NULL)
  541. return -ENOMEM;
  542. /*
  543. * We will pass the symbols to the filter later, in
  544. * map__split_kallsyms, when we have split the maps per module
  545. */
  546. symbols__insert(root, sym);
  547. return 0;
  548. }
  549. /*
  550. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  551. * so that we can in the next step set the symbol ->end address and then
  552. * call kernel_maps__split_kallsyms.
  553. */
  554. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  555. struct map *map)
  556. {
  557. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  558. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  559. }
  560. /*
  561. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  562. * kernel range is broken in several maps, named [kernel].N, as we don't have
  563. * the original ELF section names vmlinux have.
  564. */
  565. static int dso__split_kallsyms(struct dso *dso, struct map *map,
  566. symbol_filter_t filter)
  567. {
  568. struct map_groups *kmaps = map__kmap(map)->kmaps;
  569. struct machine *machine = kmaps->machine;
  570. struct map *curr_map = map;
  571. struct symbol *pos;
  572. int count = 0, moved = 0;
  573. struct rb_root *root = &dso->symbols[map->type];
  574. struct rb_node *next = rb_first(root);
  575. int kernel_range = 0;
  576. while (next) {
  577. char *module;
  578. pos = rb_entry(next, struct symbol, rb_node);
  579. next = rb_next(&pos->rb_node);
  580. module = strchr(pos->name, '\t');
  581. if (module) {
  582. if (!symbol_conf.use_modules)
  583. goto discard_symbol;
  584. *module++ = '\0';
  585. if (strcmp(curr_map->dso->short_name, module)) {
  586. if (curr_map != map &&
  587. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  588. machine__is_default_guest(machine)) {
  589. /*
  590. * We assume all symbols of a module are
  591. * continuous in * kallsyms, so curr_map
  592. * points to a module and all its
  593. * symbols are in its kmap. Mark it as
  594. * loaded.
  595. */
  596. dso__set_loaded(curr_map->dso,
  597. curr_map->type);
  598. }
  599. curr_map = map_groups__find_by_name(kmaps,
  600. map->type, module);
  601. if (curr_map == NULL) {
  602. pr_debug("%s/proc/{kallsyms,modules} "
  603. "inconsistency while looking "
  604. "for \"%s\" module!\n",
  605. machine->root_dir, module);
  606. curr_map = map;
  607. goto discard_symbol;
  608. }
  609. if (curr_map->dso->loaded &&
  610. !machine__is_default_guest(machine))
  611. goto discard_symbol;
  612. }
  613. /*
  614. * So that we look just like we get from .ko files,
  615. * i.e. not prelinked, relative to map->start.
  616. */
  617. pos->start = curr_map->map_ip(curr_map, pos->start);
  618. pos->end = curr_map->map_ip(curr_map, pos->end);
  619. } else if (curr_map != map) {
  620. char dso_name[PATH_MAX];
  621. struct dso *ndso;
  622. if (count == 0) {
  623. curr_map = map;
  624. goto filter_symbol;
  625. }
  626. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  627. snprintf(dso_name, sizeof(dso_name),
  628. "[guest.kernel].%d",
  629. kernel_range++);
  630. else
  631. snprintf(dso_name, sizeof(dso_name),
  632. "[kernel].%d",
  633. kernel_range++);
  634. ndso = dso__new(dso_name);
  635. if (ndso == NULL)
  636. return -1;
  637. ndso->kernel = dso->kernel;
  638. curr_map = map__new2(pos->start, ndso, map->type);
  639. if (curr_map == NULL) {
  640. dso__delete(ndso);
  641. return -1;
  642. }
  643. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  644. map_groups__insert(kmaps, curr_map);
  645. ++kernel_range;
  646. }
  647. filter_symbol:
  648. if (filter && filter(curr_map, pos)) {
  649. discard_symbol: rb_erase(&pos->rb_node, root);
  650. symbol__delete(pos);
  651. } else {
  652. if (curr_map != map) {
  653. rb_erase(&pos->rb_node, root);
  654. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  655. ++moved;
  656. } else
  657. ++count;
  658. }
  659. }
  660. if (curr_map != map &&
  661. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  662. machine__is_default_guest(kmaps->machine)) {
  663. dso__set_loaded(curr_map->dso, curr_map->type);
  664. }
  665. return count + moved;
  666. }
  667. static bool symbol__restricted_filename(const char *filename,
  668. const char *restricted_filename)
  669. {
  670. bool restricted = false;
  671. if (symbol_conf.kptr_restrict) {
  672. char *r = realpath(filename, NULL);
  673. if (r != NULL) {
  674. restricted = strcmp(r, restricted_filename) == 0;
  675. free(r);
  676. return restricted;
  677. }
  678. }
  679. return restricted;
  680. }
  681. int dso__load_kallsyms(struct dso *dso, const char *filename,
  682. struct map *map, symbol_filter_t filter)
  683. {
  684. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  685. return -1;
  686. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  687. return -1;
  688. symbols__fixup_duplicate(&dso->symbols[map->type]);
  689. symbols__fixup_end(&dso->symbols[map->type]);
  690. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  691. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  692. else
  693. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  694. return dso__split_kallsyms(dso, map, filter);
  695. }
  696. static int dso__load_perf_map(struct dso *dso, struct map *map,
  697. symbol_filter_t filter)
  698. {
  699. char *line = NULL;
  700. size_t n;
  701. FILE *file;
  702. int nr_syms = 0;
  703. file = fopen(dso->long_name, "r");
  704. if (file == NULL)
  705. goto out_failure;
  706. while (!feof(file)) {
  707. u64 start, size;
  708. struct symbol *sym;
  709. int line_len, len;
  710. line_len = getline(&line, &n, file);
  711. if (line_len < 0)
  712. break;
  713. if (!line)
  714. goto out_failure;
  715. line[--line_len] = '\0'; /* \n */
  716. len = hex2u64(line, &start);
  717. len++;
  718. if (len + 2 >= line_len)
  719. continue;
  720. len += hex2u64(line + len, &size);
  721. len++;
  722. if (len + 2 >= line_len)
  723. continue;
  724. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  725. if (sym == NULL)
  726. goto out_delete_line;
  727. if (filter && filter(map, sym))
  728. symbol__delete(sym);
  729. else {
  730. symbols__insert(&dso->symbols[map->type], sym);
  731. nr_syms++;
  732. }
  733. }
  734. free(line);
  735. fclose(file);
  736. return nr_syms;
  737. out_delete_line:
  738. free(line);
  739. out_failure:
  740. return -1;
  741. }
  742. /**
  743. * elf_symtab__for_each_symbol - iterate thru all the symbols
  744. *
  745. * @syms: struct elf_symtab instance to iterate
  746. * @idx: uint32_t idx
  747. * @sym: GElf_Sym iterator
  748. */
  749. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  750. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  751. idx < nr_syms; \
  752. idx++, gelf_getsym(syms, idx, &sym))
  753. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  754. {
  755. return GELF_ST_TYPE(sym->st_info);
  756. }
  757. static inline int elf_sym__is_function(const GElf_Sym *sym)
  758. {
  759. return elf_sym__type(sym) == STT_FUNC &&
  760. sym->st_name != 0 &&
  761. sym->st_shndx != SHN_UNDEF;
  762. }
  763. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  764. {
  765. return elf_sym__type(sym) == STT_OBJECT &&
  766. sym->st_name != 0 &&
  767. sym->st_shndx != SHN_UNDEF;
  768. }
  769. static inline int elf_sym__is_label(const GElf_Sym *sym)
  770. {
  771. return elf_sym__type(sym) == STT_NOTYPE &&
  772. sym->st_name != 0 &&
  773. sym->st_shndx != SHN_UNDEF &&
  774. sym->st_shndx != SHN_ABS;
  775. }
  776. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  777. const Elf_Data *secstrs)
  778. {
  779. return secstrs->d_buf + shdr->sh_name;
  780. }
  781. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  782. const Elf_Data *secstrs)
  783. {
  784. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  785. }
  786. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  787. const Elf_Data *secstrs)
  788. {
  789. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  790. }
  791. static inline const char *elf_sym__name(const GElf_Sym *sym,
  792. const Elf_Data *symstrs)
  793. {
  794. return symstrs->d_buf + sym->st_name;
  795. }
  796. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  797. GElf_Shdr *shp, const char *name,
  798. size_t *idx)
  799. {
  800. Elf_Scn *sec = NULL;
  801. size_t cnt = 1;
  802. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  803. char *str;
  804. gelf_getshdr(sec, shp);
  805. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  806. if (!strcmp(name, str)) {
  807. if (idx)
  808. *idx = cnt;
  809. break;
  810. }
  811. ++cnt;
  812. }
  813. return sec;
  814. }
  815. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  816. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  817. idx < nr_entries; \
  818. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  819. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  820. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  821. idx < nr_entries; \
  822. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  823. /*
  824. * We need to check if we have a .dynsym, so that we can handle the
  825. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  826. * .dynsym or .symtab).
  827. * And always look at the original dso, not at debuginfo packages, that
  828. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  829. */
  830. static int
  831. dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
  832. symbol_filter_t filter)
  833. {
  834. uint32_t nr_rel_entries, idx;
  835. GElf_Sym sym;
  836. u64 plt_offset;
  837. GElf_Shdr shdr_plt;
  838. struct symbol *f;
  839. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  840. Elf_Data *reldata, *syms, *symstrs;
  841. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  842. size_t dynsym_idx;
  843. GElf_Ehdr ehdr;
  844. char sympltname[1024];
  845. Elf *elf;
  846. int nr = 0, symidx, fd, err = 0;
  847. fd = open(name, O_RDONLY);
  848. if (fd < 0)
  849. goto out;
  850. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  851. if (elf == NULL)
  852. goto out_close;
  853. if (gelf_getehdr(elf, &ehdr) == NULL)
  854. goto out_elf_end;
  855. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  856. ".dynsym", &dynsym_idx);
  857. if (scn_dynsym == NULL)
  858. goto out_elf_end;
  859. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  860. ".rela.plt", NULL);
  861. if (scn_plt_rel == NULL) {
  862. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  863. ".rel.plt", NULL);
  864. if (scn_plt_rel == NULL)
  865. goto out_elf_end;
  866. }
  867. err = -1;
  868. if (shdr_rel_plt.sh_link != dynsym_idx)
  869. goto out_elf_end;
  870. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  871. goto out_elf_end;
  872. /*
  873. * Fetch the relocation section to find the idxes to the GOT
  874. * and the symbols in the .dynsym they refer to.
  875. */
  876. reldata = elf_getdata(scn_plt_rel, NULL);
  877. if (reldata == NULL)
  878. goto out_elf_end;
  879. syms = elf_getdata(scn_dynsym, NULL);
  880. if (syms == NULL)
  881. goto out_elf_end;
  882. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  883. if (scn_symstrs == NULL)
  884. goto out_elf_end;
  885. symstrs = elf_getdata(scn_symstrs, NULL);
  886. if (symstrs == NULL)
  887. goto out_elf_end;
  888. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  889. plt_offset = shdr_plt.sh_offset;
  890. if (shdr_rel_plt.sh_type == SHT_RELA) {
  891. GElf_Rela pos_mem, *pos;
  892. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  893. nr_rel_entries) {
  894. symidx = GELF_R_SYM(pos->r_info);
  895. plt_offset += shdr_plt.sh_entsize;
  896. gelf_getsym(syms, symidx, &sym);
  897. snprintf(sympltname, sizeof(sympltname),
  898. "%s@plt", elf_sym__name(&sym, symstrs));
  899. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  900. STB_GLOBAL, sympltname);
  901. if (!f)
  902. goto out_elf_end;
  903. if (filter && filter(map, f))
  904. symbol__delete(f);
  905. else {
  906. symbols__insert(&dso->symbols[map->type], f);
  907. ++nr;
  908. }
  909. }
  910. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  911. GElf_Rel pos_mem, *pos;
  912. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  913. nr_rel_entries) {
  914. symidx = GELF_R_SYM(pos->r_info);
  915. plt_offset += shdr_plt.sh_entsize;
  916. gelf_getsym(syms, symidx, &sym);
  917. snprintf(sympltname, sizeof(sympltname),
  918. "%s@plt", elf_sym__name(&sym, symstrs));
  919. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  920. STB_GLOBAL, sympltname);
  921. if (!f)
  922. goto out_elf_end;
  923. if (filter && filter(map, f))
  924. symbol__delete(f);
  925. else {
  926. symbols__insert(&dso->symbols[map->type], f);
  927. ++nr;
  928. }
  929. }
  930. }
  931. err = 0;
  932. out_elf_end:
  933. elf_end(elf);
  934. out_close:
  935. close(fd);
  936. if (err == 0)
  937. return nr;
  938. out:
  939. pr_debug("%s: problems reading %s PLT info.\n",
  940. __func__, dso->long_name);
  941. return 0;
  942. }
  943. static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
  944. {
  945. switch (type) {
  946. case MAP__FUNCTION:
  947. return elf_sym__is_function(sym);
  948. case MAP__VARIABLE:
  949. return elf_sym__is_object(sym);
  950. default:
  951. return false;
  952. }
  953. }
  954. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  955. enum map_type type)
  956. {
  957. switch (type) {
  958. case MAP__FUNCTION:
  959. return elf_sec__is_text(shdr, secstrs);
  960. case MAP__VARIABLE:
  961. return elf_sec__is_data(shdr, secstrs);
  962. default:
  963. return false;
  964. }
  965. }
  966. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  967. {
  968. Elf_Scn *sec = NULL;
  969. GElf_Shdr shdr;
  970. size_t cnt = 1;
  971. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  972. gelf_getshdr(sec, &shdr);
  973. if ((addr >= shdr.sh_addr) &&
  974. (addr < (shdr.sh_addr + shdr.sh_size)))
  975. return cnt;
  976. ++cnt;
  977. }
  978. return -1;
  979. }
  980. static int dso__swap_init(struct dso *dso, unsigned char eidata)
  981. {
  982. static unsigned int const endian = 1;
  983. dso->needs_swap = DSO_SWAP__NO;
  984. switch (eidata) {
  985. case ELFDATA2LSB:
  986. /* We are big endian, DSO is little endian. */
  987. if (*(unsigned char const *)&endian != 1)
  988. dso->needs_swap = DSO_SWAP__YES;
  989. break;
  990. case ELFDATA2MSB:
  991. /* We are little endian, DSO is big endian. */
  992. if (*(unsigned char const *)&endian != 0)
  993. dso->needs_swap = DSO_SWAP__YES;
  994. break;
  995. default:
  996. pr_err("unrecognized DSO data encoding %d\n", eidata);
  997. return -EINVAL;
  998. }
  999. return 0;
  1000. }
  1001. static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
  1002. int fd, symbol_filter_t filter, int kmodule,
  1003. int want_symtab)
  1004. {
  1005. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  1006. struct map *curr_map = map;
  1007. struct dso *curr_dso = dso;
  1008. Elf_Data *symstrs, *secstrs;
  1009. uint32_t nr_syms;
  1010. int err = -1;
  1011. uint32_t idx;
  1012. GElf_Ehdr ehdr;
  1013. GElf_Shdr shdr, opdshdr;
  1014. Elf_Data *syms, *opddata = NULL;
  1015. GElf_Sym sym;
  1016. Elf_Scn *sec, *sec_strndx, *opdsec;
  1017. Elf *elf;
  1018. int nr = 0;
  1019. size_t opdidx = 0;
  1020. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1021. if (elf == NULL) {
  1022. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  1023. goto out_close;
  1024. }
  1025. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1026. pr_debug("%s: cannot get elf header.\n", __func__);
  1027. goto out_elf_end;
  1028. }
  1029. if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
  1030. goto out_elf_end;
  1031. /* Always reject images with a mismatched build-id: */
  1032. if (dso->has_build_id) {
  1033. u8 build_id[BUILD_ID_SIZE];
  1034. if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
  1035. goto out_elf_end;
  1036. if (!dso__build_id_equal(dso, build_id))
  1037. goto out_elf_end;
  1038. }
  1039. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  1040. if (sec == NULL) {
  1041. if (want_symtab)
  1042. goto out_elf_end;
  1043. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  1044. if (sec == NULL)
  1045. goto out_elf_end;
  1046. }
  1047. opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
  1048. if (opdshdr.sh_type != SHT_PROGBITS)
  1049. opdsec = NULL;
  1050. if (opdsec)
  1051. opddata = elf_rawdata(opdsec, NULL);
  1052. syms = elf_getdata(sec, NULL);
  1053. if (syms == NULL)
  1054. goto out_elf_end;
  1055. sec = elf_getscn(elf, shdr.sh_link);
  1056. if (sec == NULL)
  1057. goto out_elf_end;
  1058. symstrs = elf_getdata(sec, NULL);
  1059. if (symstrs == NULL)
  1060. goto out_elf_end;
  1061. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  1062. if (sec_strndx == NULL)
  1063. goto out_elf_end;
  1064. secstrs = elf_getdata(sec_strndx, NULL);
  1065. if (secstrs == NULL)
  1066. goto out_elf_end;
  1067. nr_syms = shdr.sh_size / shdr.sh_entsize;
  1068. memset(&sym, 0, sizeof(sym));
  1069. if (dso->kernel == DSO_TYPE_USER) {
  1070. dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  1071. elf_section_by_name(elf, &ehdr, &shdr,
  1072. ".gnu.prelink_undo",
  1073. NULL) != NULL);
  1074. } else {
  1075. dso->adjust_symbols = 0;
  1076. }
  1077. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  1078. struct symbol *f;
  1079. const char *elf_name = elf_sym__name(&sym, symstrs);
  1080. char *demangled = NULL;
  1081. int is_label = elf_sym__is_label(&sym);
  1082. const char *section_name;
  1083. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  1084. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  1085. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  1086. if (!is_label && !elf_sym__is_a(&sym, map->type))
  1087. continue;
  1088. /* Reject ARM ELF "mapping symbols": these aren't unique and
  1089. * don't identify functions, so will confuse the profile
  1090. * output: */
  1091. if (ehdr.e_machine == EM_ARM) {
  1092. if (!strcmp(elf_name, "$a") ||
  1093. !strcmp(elf_name, "$d") ||
  1094. !strcmp(elf_name, "$t"))
  1095. continue;
  1096. }
  1097. if (opdsec && sym.st_shndx == opdidx) {
  1098. u32 offset = sym.st_value - opdshdr.sh_addr;
  1099. u64 *opd = opddata->d_buf + offset;
  1100. sym.st_value = DSO__SWAP(dso, u64, *opd);
  1101. sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
  1102. }
  1103. sec = elf_getscn(elf, sym.st_shndx);
  1104. if (!sec)
  1105. goto out_elf_end;
  1106. gelf_getshdr(sec, &shdr);
  1107. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  1108. continue;
  1109. section_name = elf_sec__name(&shdr, secstrs);
  1110. /* On ARM, symbols for thumb functions have 1 added to
  1111. * the symbol address as a flag - remove it */
  1112. if ((ehdr.e_machine == EM_ARM) &&
  1113. (map->type == MAP__FUNCTION) &&
  1114. (sym.st_value & 1))
  1115. --sym.st_value;
  1116. if (dso->kernel != DSO_TYPE_USER || kmodule) {
  1117. char dso_name[PATH_MAX];
  1118. if (strcmp(section_name,
  1119. (curr_dso->short_name +
  1120. dso->short_name_len)) == 0)
  1121. goto new_symbol;
  1122. if (strcmp(section_name, ".text") == 0) {
  1123. curr_map = map;
  1124. curr_dso = dso;
  1125. goto new_symbol;
  1126. }
  1127. snprintf(dso_name, sizeof(dso_name),
  1128. "%s%s", dso->short_name, section_name);
  1129. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  1130. if (curr_map == NULL) {
  1131. u64 start = sym.st_value;
  1132. if (kmodule)
  1133. start += map->start + shdr.sh_offset;
  1134. curr_dso = dso__new(dso_name);
  1135. if (curr_dso == NULL)
  1136. goto out_elf_end;
  1137. curr_dso->kernel = dso->kernel;
  1138. curr_dso->long_name = dso->long_name;
  1139. curr_dso->long_name_len = dso->long_name_len;
  1140. curr_map = map__new2(start, curr_dso,
  1141. map->type);
  1142. if (curr_map == NULL) {
  1143. dso__delete(curr_dso);
  1144. goto out_elf_end;
  1145. }
  1146. curr_map->map_ip = identity__map_ip;
  1147. curr_map->unmap_ip = identity__map_ip;
  1148. curr_dso->symtab_type = dso->symtab_type;
  1149. map_groups__insert(kmap->kmaps, curr_map);
  1150. dsos__add(&dso->node, curr_dso);
  1151. dso__set_loaded(curr_dso, map->type);
  1152. } else
  1153. curr_dso = curr_map->dso;
  1154. goto new_symbol;
  1155. }
  1156. if (curr_dso->adjust_symbols) {
  1157. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  1158. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  1159. (u64)sym.st_value, (u64)shdr.sh_addr,
  1160. (u64)shdr.sh_offset);
  1161. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  1162. }
  1163. /*
  1164. * We need to figure out if the object was created from C++ sources
  1165. * DWARF DW_compile_unit has this, but we don't always have access
  1166. * to it...
  1167. */
  1168. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  1169. if (demangled != NULL)
  1170. elf_name = demangled;
  1171. new_symbol:
  1172. f = symbol__new(sym.st_value, sym.st_size,
  1173. GELF_ST_BIND(sym.st_info), elf_name);
  1174. free(demangled);
  1175. if (!f)
  1176. goto out_elf_end;
  1177. if (filter && filter(curr_map, f))
  1178. symbol__delete(f);
  1179. else {
  1180. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  1181. nr++;
  1182. }
  1183. }
  1184. /*
  1185. * For misannotated, zeroed, ASM function sizes.
  1186. */
  1187. if (nr > 0) {
  1188. symbols__fixup_duplicate(&dso->symbols[map->type]);
  1189. symbols__fixup_end(&dso->symbols[map->type]);
  1190. if (kmap) {
  1191. /*
  1192. * We need to fixup this here too because we create new
  1193. * maps here, for things like vsyscall sections.
  1194. */
  1195. __map_groups__fixup_end(kmap->kmaps, map->type);
  1196. }
  1197. }
  1198. err = nr;
  1199. out_elf_end:
  1200. elf_end(elf);
  1201. out_close:
  1202. return err;
  1203. }
  1204. static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
  1205. {
  1206. return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
  1207. }
  1208. bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
  1209. {
  1210. bool have_build_id = false;
  1211. struct dso *pos;
  1212. list_for_each_entry(pos, head, node) {
  1213. if (with_hits && !pos->hit)
  1214. continue;
  1215. if (pos->has_build_id) {
  1216. have_build_id = true;
  1217. continue;
  1218. }
  1219. if (filename__read_build_id(pos->long_name, pos->build_id,
  1220. sizeof(pos->build_id)) > 0) {
  1221. have_build_id = true;
  1222. pos->has_build_id = true;
  1223. }
  1224. }
  1225. return have_build_id;
  1226. }
  1227. /*
  1228. * Align offset to 4 bytes as needed for note name and descriptor data.
  1229. */
  1230. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  1231. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  1232. {
  1233. int err = -1;
  1234. GElf_Ehdr ehdr;
  1235. GElf_Shdr shdr;
  1236. Elf_Data *data;
  1237. Elf_Scn *sec;
  1238. Elf_Kind ek;
  1239. void *ptr;
  1240. if (size < BUILD_ID_SIZE)
  1241. goto out;
  1242. ek = elf_kind(elf);
  1243. if (ek != ELF_K_ELF)
  1244. goto out;
  1245. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1246. pr_err("%s: cannot get elf header.\n", __func__);
  1247. goto out;
  1248. }
  1249. /*
  1250. * Check following sections for notes:
  1251. * '.note.gnu.build-id'
  1252. * '.notes'
  1253. * '.note' (VDSO specific)
  1254. */
  1255. do {
  1256. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1257. ".note.gnu.build-id", NULL);
  1258. if (sec)
  1259. break;
  1260. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1261. ".notes", NULL);
  1262. if (sec)
  1263. break;
  1264. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1265. ".note", NULL);
  1266. if (sec)
  1267. break;
  1268. return err;
  1269. } while (0);
  1270. data = elf_getdata(sec, NULL);
  1271. if (data == NULL)
  1272. goto out;
  1273. ptr = data->d_buf;
  1274. while (ptr < (data->d_buf + data->d_size)) {
  1275. GElf_Nhdr *nhdr = ptr;
  1276. size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
  1277. descsz = NOTE_ALIGN(nhdr->n_descsz);
  1278. const char *name;
  1279. ptr += sizeof(*nhdr);
  1280. name = ptr;
  1281. ptr += namesz;
  1282. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  1283. nhdr->n_namesz == sizeof("GNU")) {
  1284. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  1285. size_t sz = min(size, descsz);
  1286. memcpy(bf, ptr, sz);
  1287. memset(bf + sz, 0, size - sz);
  1288. err = descsz;
  1289. break;
  1290. }
  1291. }
  1292. ptr += descsz;
  1293. }
  1294. out:
  1295. return err;
  1296. }
  1297. int filename__read_build_id(const char *filename, void *bf, size_t size)
  1298. {
  1299. int fd, err = -1;
  1300. Elf *elf;
  1301. if (size < BUILD_ID_SIZE)
  1302. goto out;
  1303. fd = open(filename, O_RDONLY);
  1304. if (fd < 0)
  1305. goto out;
  1306. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1307. if (elf == NULL) {
  1308. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  1309. goto out_close;
  1310. }
  1311. err = elf_read_build_id(elf, bf, size);
  1312. elf_end(elf);
  1313. out_close:
  1314. close(fd);
  1315. out:
  1316. return err;
  1317. }
  1318. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  1319. {
  1320. int fd, err = -1;
  1321. if (size < BUILD_ID_SIZE)
  1322. goto out;
  1323. fd = open(filename, O_RDONLY);
  1324. if (fd < 0)
  1325. goto out;
  1326. while (1) {
  1327. char bf[BUFSIZ];
  1328. GElf_Nhdr nhdr;
  1329. size_t namesz, descsz;
  1330. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  1331. break;
  1332. namesz = NOTE_ALIGN(nhdr.n_namesz);
  1333. descsz = NOTE_ALIGN(nhdr.n_descsz);
  1334. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  1335. nhdr.n_namesz == sizeof("GNU")) {
  1336. if (read(fd, bf, namesz) != (ssize_t)namesz)
  1337. break;
  1338. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  1339. size_t sz = min(descsz, size);
  1340. if (read(fd, build_id, sz) == (ssize_t)sz) {
  1341. memset(build_id + sz, 0, size - sz);
  1342. err = 0;
  1343. break;
  1344. }
  1345. } else if (read(fd, bf, descsz) != (ssize_t)descsz)
  1346. break;
  1347. } else {
  1348. int n = namesz + descsz;
  1349. if (read(fd, bf, n) != n)
  1350. break;
  1351. }
  1352. }
  1353. close(fd);
  1354. out:
  1355. return err;
  1356. }
  1357. static int filename__read_debuglink(const char *filename,
  1358. char *debuglink, size_t size)
  1359. {
  1360. int fd, err = -1;
  1361. Elf *elf;
  1362. GElf_Ehdr ehdr;
  1363. GElf_Shdr shdr;
  1364. Elf_Data *data;
  1365. Elf_Scn *sec;
  1366. Elf_Kind ek;
  1367. fd = open(filename, O_RDONLY);
  1368. if (fd < 0)
  1369. goto out;
  1370. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1371. if (elf == NULL) {
  1372. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  1373. goto out_close;
  1374. }
  1375. ek = elf_kind(elf);
  1376. if (ek != ELF_K_ELF)
  1377. goto out_close;
  1378. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1379. pr_err("%s: cannot get elf header.\n", __func__);
  1380. goto out_close;
  1381. }
  1382. sec = elf_section_by_name(elf, &ehdr, &shdr,
  1383. ".gnu_debuglink", NULL);
  1384. if (sec == NULL)
  1385. goto out_close;
  1386. data = elf_getdata(sec, NULL);
  1387. if (data == NULL)
  1388. goto out_close;
  1389. /* the start of this section is a zero-terminated string */
  1390. strncpy(debuglink, data->d_buf, size);
  1391. elf_end(elf);
  1392. out_close:
  1393. close(fd);
  1394. out:
  1395. return err;
  1396. }
  1397. char dso__symtab_origin(const struct dso *dso)
  1398. {
  1399. static const char origin[] = {
  1400. [DSO_BINARY_TYPE__KALLSYMS] = 'k',
  1401. [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
  1402. [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
  1403. [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
  1404. [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
  1405. [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
  1406. [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
  1407. [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
  1408. [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
  1409. [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
  1410. [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
  1411. };
  1412. if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
  1413. return '!';
  1414. return origin[dso->symtab_type];
  1415. }
  1416. int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
  1417. char *root_dir, char *file, size_t size)
  1418. {
  1419. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  1420. int ret = 0;
  1421. switch (type) {
  1422. case DSO_BINARY_TYPE__DEBUGLINK: {
  1423. char *debuglink;
  1424. strncpy(file, dso->long_name, size);
  1425. debuglink = file + dso->long_name_len;
  1426. while (debuglink != file && *debuglink != '/')
  1427. debuglink--;
  1428. if (*debuglink == '/')
  1429. debuglink++;
  1430. filename__read_debuglink(dso->long_name, debuglink,
  1431. size - (debuglink - file));
  1432. }
  1433. break;
  1434. case DSO_BINARY_TYPE__BUILD_ID_CACHE:
  1435. /* skip the locally configured cache if a symfs is given */
  1436. if (symbol_conf.symfs[0] ||
  1437. (dso__build_id_filename(dso, file, size) == NULL))
  1438. ret = -1;
  1439. break;
  1440. case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
  1441. snprintf(file, size, "%s/usr/lib/debug%s.debug",
  1442. symbol_conf.symfs, dso->long_name);
  1443. break;
  1444. case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
  1445. snprintf(file, size, "%s/usr/lib/debug%s",
  1446. symbol_conf.symfs, dso->long_name);
  1447. break;
  1448. case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
  1449. if (!dso->has_build_id) {
  1450. ret = -1;
  1451. break;
  1452. }
  1453. build_id__sprintf(dso->build_id,
  1454. sizeof(dso->build_id),
  1455. build_id_hex);
  1456. snprintf(file, size,
  1457. "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
  1458. symbol_conf.symfs, build_id_hex, build_id_hex + 2);
  1459. break;
  1460. case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
  1461. snprintf(file, size, "%s%s",
  1462. symbol_conf.symfs, dso->long_name);
  1463. break;
  1464. case DSO_BINARY_TYPE__GUEST_KMODULE:
  1465. snprintf(file, size, "%s%s%s", symbol_conf.symfs,
  1466. root_dir, dso->long_name);
  1467. break;
  1468. case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
  1469. snprintf(file, size, "%s%s", symbol_conf.symfs,
  1470. dso->long_name);
  1471. break;
  1472. default:
  1473. case DSO_BINARY_TYPE__KALLSYMS:
  1474. case DSO_BINARY_TYPE__GUEST_KALLSYMS:
  1475. case DSO_BINARY_TYPE__JAVA_JIT:
  1476. case DSO_BINARY_TYPE__NOT_FOUND:
  1477. ret = -1;
  1478. break;
  1479. }
  1480. return ret;
  1481. }
  1482. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  1483. {
  1484. char *name;
  1485. int ret = -1;
  1486. int fd;
  1487. u_int i;
  1488. struct machine *machine;
  1489. char *root_dir = (char *) "";
  1490. int want_symtab;
  1491. dso__set_loaded(dso, map->type);
  1492. if (dso->kernel == DSO_TYPE_KERNEL)
  1493. return dso__load_kernel_sym(dso, map, filter);
  1494. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1495. return dso__load_guest_kernel_sym(dso, map, filter);
  1496. if (map->groups && map->groups->machine)
  1497. machine = map->groups->machine;
  1498. else
  1499. machine = NULL;
  1500. name = malloc(PATH_MAX);
  1501. if (!name)
  1502. return -1;
  1503. dso->adjust_symbols = 0;
  1504. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  1505. struct stat st;
  1506. if (lstat(dso->name, &st) < 0)
  1507. return -1;
  1508. if (st.st_uid && (st.st_uid != geteuid())) {
  1509. pr_warning("File %s not owned by current user or root, "
  1510. "ignoring it.\n", dso->name);
  1511. return -1;
  1512. }
  1513. ret = dso__load_perf_map(dso, map, filter);
  1514. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  1515. DSO_BINARY_TYPE__NOT_FOUND;
  1516. return ret;
  1517. }
  1518. if (machine)
  1519. root_dir = machine->root_dir;
  1520. /* Iterate over candidate debug images.
  1521. * On the first pass, only load images if they have a full symtab.
  1522. * Failing that, do a second pass where we accept .dynsym also
  1523. */
  1524. want_symtab = 1;
  1525. restart:
  1526. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  1527. dso->symtab_type = binary_type_symtab[i];
  1528. if (dso__binary_type_file(dso, dso->symtab_type,
  1529. root_dir, name, PATH_MAX))
  1530. continue;
  1531. /* Name is now the name of the next image to try */
  1532. fd = open(name, O_RDONLY);
  1533. if (fd < 0)
  1534. continue;
  1535. ret = dso__load_sym(dso, map, name, fd, filter, 0,
  1536. want_symtab);
  1537. close(fd);
  1538. /*
  1539. * Some people seem to have debuginfo files _WITHOUT_ debug
  1540. * info!?!?
  1541. */
  1542. if (!ret)
  1543. continue;
  1544. if (ret > 0) {
  1545. int nr_plt;
  1546. nr_plt = dso__synthesize_plt_symbols(dso, name, map, filter);
  1547. if (nr_plt > 0)
  1548. ret += nr_plt;
  1549. break;
  1550. }
  1551. }
  1552. /*
  1553. * If we wanted a full symtab but no image had one,
  1554. * relax our requirements and repeat the search.
  1555. */
  1556. if (ret <= 0 && want_symtab) {
  1557. want_symtab = 0;
  1558. goto restart;
  1559. }
  1560. free(name);
  1561. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1562. return 0;
  1563. return ret;
  1564. }
  1565. struct map *map_groups__find_by_name(struct map_groups *mg,
  1566. enum map_type type, const char *name)
  1567. {
  1568. struct rb_node *nd;
  1569. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  1570. struct map *map = rb_entry(nd, struct map, rb_node);
  1571. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1572. return map;
  1573. }
  1574. return NULL;
  1575. }
  1576. static int dso__kernel_module_get_build_id(struct dso *dso,
  1577. const char *root_dir)
  1578. {
  1579. char filename[PATH_MAX];
  1580. /*
  1581. * kernel module short names are of the form "[module]" and
  1582. * we need just "module" here.
  1583. */
  1584. const char *name = dso->short_name + 1;
  1585. snprintf(filename, sizeof(filename),
  1586. "%s/sys/module/%.*s/notes/.note.gnu.build-id",
  1587. root_dir, (int)strlen(name) - 1, name);
  1588. if (sysfs__read_build_id(filename, dso->build_id,
  1589. sizeof(dso->build_id)) == 0)
  1590. dso->has_build_id = true;
  1591. return 0;
  1592. }
  1593. static int map_groups__set_modules_path_dir(struct map_groups *mg,
  1594. const char *dir_name)
  1595. {
  1596. struct dirent *dent;
  1597. DIR *dir = opendir(dir_name);
  1598. int ret = 0;
  1599. if (!dir) {
  1600. pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
  1601. return -1;
  1602. }
  1603. while ((dent = readdir(dir)) != NULL) {
  1604. char path[PATH_MAX];
  1605. struct stat st;
  1606. /*sshfs might return bad dent->d_type, so we have to stat*/
  1607. snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
  1608. if (stat(path, &st))
  1609. continue;
  1610. if (S_ISDIR(st.st_mode)) {
  1611. if (!strcmp(dent->d_name, ".") ||
  1612. !strcmp(dent->d_name, ".."))
  1613. continue;
  1614. ret = map_groups__set_modules_path_dir(mg, path);
  1615. if (ret < 0)
  1616. goto out;
  1617. } else {
  1618. char *dot = strrchr(dent->d_name, '.'),
  1619. dso_name[PATH_MAX];
  1620. struct map *map;
  1621. char *long_name;
  1622. if (dot == NULL || strcmp(dot, ".ko"))
  1623. continue;
  1624. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1625. (int)(dot - dent->d_name), dent->d_name);
  1626. strxfrchar(dso_name, '-', '_');
  1627. map = map_groups__find_by_name(mg, MAP__FUNCTION,
  1628. dso_name);
  1629. if (map == NULL)
  1630. continue;
  1631. long_name = strdup(path);
  1632. if (long_name == NULL) {
  1633. ret = -1;
  1634. goto out;
  1635. }
  1636. dso__set_long_name(map->dso, long_name);
  1637. map->dso->lname_alloc = 1;
  1638. dso__kernel_module_get_build_id(map->dso, "");
  1639. }
  1640. }
  1641. out:
  1642. closedir(dir);
  1643. return ret;
  1644. }
  1645. static char *get_kernel_version(const char *root_dir)
  1646. {
  1647. char version[PATH_MAX];
  1648. FILE *file;
  1649. char *name, *tmp;
  1650. const char *prefix = "Linux version ";
  1651. sprintf(version, "%s/proc/version", root_dir);
  1652. file = fopen(version, "r");
  1653. if (!file)
  1654. return NULL;
  1655. version[0] = '\0';
  1656. tmp = fgets(version, sizeof(version), file);
  1657. fclose(file);
  1658. name = strstr(version, prefix);
  1659. if (!name)
  1660. return NULL;
  1661. name += strlen(prefix);
  1662. tmp = strchr(name, ' ');
  1663. if (tmp)
  1664. *tmp = '\0';
  1665. return strdup(name);
  1666. }
  1667. static int machine__set_modules_path(struct machine *machine)
  1668. {
  1669. char *version;
  1670. char modules_path[PATH_MAX];
  1671. version = get_kernel_version(machine->root_dir);
  1672. if (!version)
  1673. return -1;
  1674. snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
  1675. machine->root_dir, version);
  1676. free(version);
  1677. return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
  1678. }
  1679. /*
  1680. * Constructor variant for modules (where we know from /proc/modules where
  1681. * they are loaded) and for vmlinux, where only after we load all the
  1682. * symbols we'll know where it starts and ends.
  1683. */
  1684. static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  1685. {
  1686. struct map *map = calloc(1, (sizeof(*map) +
  1687. (dso->kernel ? sizeof(struct kmap) : 0)));
  1688. if (map != NULL) {
  1689. /*
  1690. * ->end will be filled after we load all the symbols
  1691. */
  1692. map__init(map, type, start, 0, 0, dso);
  1693. }
  1694. return map;
  1695. }
  1696. struct map *machine__new_module(struct machine *machine, u64 start,
  1697. const char *filename)
  1698. {
  1699. struct map *map;
  1700. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
  1701. if (dso == NULL)
  1702. return NULL;
  1703. map = map__new2(start, dso, MAP__FUNCTION);
  1704. if (map == NULL)
  1705. return NULL;
  1706. if (machine__is_host(machine))
  1707. dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
  1708. else
  1709. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
  1710. map_groups__insert(&machine->kmaps, map);
  1711. return map;
  1712. }
  1713. static int machine__create_modules(struct machine *machine)
  1714. {
  1715. char *line = NULL;
  1716. size_t n;
  1717. FILE *file;
  1718. struct map *map;
  1719. const char *modules;
  1720. char path[PATH_MAX];
  1721. if (machine__is_default_guest(machine))
  1722. modules = symbol_conf.default_guest_modules;
  1723. else {
  1724. sprintf(path, "%s/proc/modules", machine->root_dir);
  1725. modules = path;
  1726. }
  1727. if (symbol__restricted_filename(path, "/proc/modules"))
  1728. return -1;
  1729. file = fopen(modules, "r");
  1730. if (file == NULL)
  1731. return -1;
  1732. while (!feof(file)) {
  1733. char name[PATH_MAX];
  1734. u64 start;
  1735. char *sep;
  1736. int line_len;
  1737. line_len = getline(&line, &n, file);
  1738. if (line_len < 0)
  1739. break;
  1740. if (!line)
  1741. goto out_failure;
  1742. line[--line_len] = '\0'; /* \n */
  1743. sep = strrchr(line, 'x');
  1744. if (sep == NULL)
  1745. continue;
  1746. hex2u64(sep + 1, &start);
  1747. sep = strchr(line, ' ');
  1748. if (sep == NULL)
  1749. continue;
  1750. *sep = '\0';
  1751. snprintf(name, sizeof(name), "[%s]", line);
  1752. map = machine__new_module(machine, start, name);
  1753. if (map == NULL)
  1754. goto out_delete_line;
  1755. dso__kernel_module_get_build_id(map->dso, machine->root_dir);
  1756. }
  1757. free(line);
  1758. fclose(file);
  1759. return machine__set_modules_path(machine);
  1760. out_delete_line:
  1761. free(line);
  1762. out_failure:
  1763. return -1;
  1764. }
  1765. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1766. const char *vmlinux, symbol_filter_t filter)
  1767. {
  1768. int err = -1, fd;
  1769. char symfs_vmlinux[PATH_MAX];
  1770. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1771. symbol_conf.symfs, vmlinux);
  1772. fd = open(symfs_vmlinux, O_RDONLY);
  1773. if (fd < 0)
  1774. return -1;
  1775. dso__set_long_name(dso, (char *)vmlinux);
  1776. dso__set_loaded(dso, map->type);
  1777. err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
  1778. close(fd);
  1779. if (err > 0)
  1780. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1781. return err;
  1782. }
  1783. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1784. symbol_filter_t filter)
  1785. {
  1786. int i, err = 0;
  1787. char *filename;
  1788. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1789. vmlinux_path__nr_entries + 1);
  1790. filename = dso__build_id_filename(dso, NULL, 0);
  1791. if (filename != NULL) {
  1792. err = dso__load_vmlinux(dso, map, filename, filter);
  1793. if (err > 0) {
  1794. dso__set_long_name(dso, filename);
  1795. goto out;
  1796. }
  1797. free(filename);
  1798. }
  1799. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1800. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  1801. if (err > 0) {
  1802. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  1803. break;
  1804. }
  1805. }
  1806. out:
  1807. return err;
  1808. }
  1809. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1810. symbol_filter_t filter)
  1811. {
  1812. int err;
  1813. const char *kallsyms_filename = NULL;
  1814. char *kallsyms_allocated_filename = NULL;
  1815. /*
  1816. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1817. * it and only it, reporting errors to the user if it cannot be used.
  1818. *
  1819. * For instance, try to analyse an ARM perf.data file _without_ a
  1820. * build-id, or if the user specifies the wrong path to the right
  1821. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1822. * x86_86 one, on the machine where analysis is being performed, say),
  1823. * or worse, /proc/kallsyms.
  1824. *
  1825. * If the specified file _has_ a build-id and there is a build-id
  1826. * section in the perf.data file, we will still do the expected
  1827. * validation in dso__load_vmlinux and will bail out if they don't
  1828. * match.
  1829. */
  1830. if (symbol_conf.kallsyms_name != NULL) {
  1831. kallsyms_filename = symbol_conf.kallsyms_name;
  1832. goto do_kallsyms;
  1833. }
  1834. if (symbol_conf.vmlinux_name != NULL) {
  1835. err = dso__load_vmlinux(dso, map,
  1836. symbol_conf.vmlinux_name, filter);
  1837. if (err > 0) {
  1838. dso__set_long_name(dso,
  1839. strdup(symbol_conf.vmlinux_name));
  1840. goto out_fixup;
  1841. }
  1842. return err;
  1843. }
  1844. if (vmlinux_path != NULL) {
  1845. err = dso__load_vmlinux_path(dso, map, filter);
  1846. if (err > 0)
  1847. goto out_fixup;
  1848. }
  1849. /* do not try local files if a symfs was given */
  1850. if (symbol_conf.symfs[0] != 0)
  1851. return -1;
  1852. /*
  1853. * Say the kernel DSO was created when processing the build-id header table,
  1854. * we have a build-id, so check if it is the same as the running kernel,
  1855. * using it if it is.
  1856. */
  1857. if (dso->has_build_id) {
  1858. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1859. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1860. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1861. sizeof(kallsyms_build_id)) == 0) {
  1862. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  1863. kallsyms_filename = "/proc/kallsyms";
  1864. goto do_kallsyms;
  1865. }
  1866. }
  1867. /*
  1868. * Now look if we have it on the build-id cache in
  1869. * $HOME/.debug/[kernel.kallsyms].
  1870. */
  1871. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1872. sbuild_id);
  1873. if (asprintf(&kallsyms_allocated_filename,
  1874. "%s/.debug/[kernel.kallsyms]/%s",
  1875. getenv("HOME"), sbuild_id) == -1) {
  1876. pr_err("Not enough memory for kallsyms file lookup\n");
  1877. return -1;
  1878. }
  1879. kallsyms_filename = kallsyms_allocated_filename;
  1880. if (access(kallsyms_filename, F_OK)) {
  1881. pr_err("No kallsyms or vmlinux with build-id %s "
  1882. "was found\n", sbuild_id);
  1883. free(kallsyms_allocated_filename);
  1884. return -1;
  1885. }
  1886. } else {
  1887. /*
  1888. * Last resort, if we don't have a build-id and couldn't find
  1889. * any vmlinux file, try the running kernel kallsyms table.
  1890. */
  1891. kallsyms_filename = "/proc/kallsyms";
  1892. }
  1893. do_kallsyms:
  1894. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1895. if (err > 0)
  1896. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1897. free(kallsyms_allocated_filename);
  1898. if (err > 0) {
  1899. out_fixup:
  1900. if (kallsyms_filename != NULL)
  1901. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1902. map__fixup_start(map);
  1903. map__fixup_end(map);
  1904. }
  1905. return err;
  1906. }
  1907. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1908. symbol_filter_t filter)
  1909. {
  1910. int err;
  1911. const char *kallsyms_filename = NULL;
  1912. struct machine *machine;
  1913. char path[PATH_MAX];
  1914. if (!map->groups) {
  1915. pr_debug("Guest kernel map hasn't the point to groups\n");
  1916. return -1;
  1917. }
  1918. machine = map->groups->machine;
  1919. if (machine__is_default_guest(machine)) {
  1920. /*
  1921. * if the user specified a vmlinux filename, use it and only
  1922. * it, reporting errors to the user if it cannot be used.
  1923. * Or use file guest_kallsyms inputted by user on commandline
  1924. */
  1925. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1926. err = dso__load_vmlinux(dso, map,
  1927. symbol_conf.default_guest_vmlinux_name, filter);
  1928. goto out_try_fixup;
  1929. }
  1930. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1931. if (!kallsyms_filename)
  1932. return -1;
  1933. } else {
  1934. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1935. kallsyms_filename = path;
  1936. }
  1937. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1938. if (err > 0)
  1939. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1940. out_try_fixup:
  1941. if (err > 0) {
  1942. if (kallsyms_filename != NULL) {
  1943. machine__mmap_name(machine, path, sizeof(path));
  1944. dso__set_long_name(dso, strdup(path));
  1945. }
  1946. map__fixup_start(map);
  1947. map__fixup_end(map);
  1948. }
  1949. return err;
  1950. }
  1951. static void dsos__add(struct list_head *head, struct dso *dso)
  1952. {
  1953. list_add_tail(&dso->node, head);
  1954. }
  1955. static struct dso *dsos__find(struct list_head *head, const char *name)
  1956. {
  1957. struct dso *pos;
  1958. list_for_each_entry(pos, head, node)
  1959. if (strcmp(pos->long_name, name) == 0)
  1960. return pos;
  1961. return NULL;
  1962. }
  1963. struct dso *__dsos__findnew(struct list_head *head, const char *name)
  1964. {
  1965. struct dso *dso = dsos__find(head, name);
  1966. if (!dso) {
  1967. dso = dso__new(name);
  1968. if (dso != NULL) {
  1969. dsos__add(head, dso);
  1970. dso__set_basename(dso);
  1971. }
  1972. }
  1973. return dso;
  1974. }
  1975. size_t __dsos__fprintf(struct list_head *head, FILE *fp)
  1976. {
  1977. struct dso *pos;
  1978. size_t ret = 0;
  1979. list_for_each_entry(pos, head, node) {
  1980. int i;
  1981. for (i = 0; i < MAP__NR_TYPES; ++i)
  1982. ret += dso__fprintf(pos, i, fp);
  1983. }
  1984. return ret;
  1985. }
  1986. size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
  1987. {
  1988. struct rb_node *nd;
  1989. size_t ret = 0;
  1990. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  1991. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  1992. ret += __dsos__fprintf(&pos->kernel_dsos, fp);
  1993. ret += __dsos__fprintf(&pos->user_dsos, fp);
  1994. }
  1995. return ret;
  1996. }
  1997. static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  1998. bool with_hits)
  1999. {
  2000. struct dso *pos;
  2001. size_t ret = 0;
  2002. list_for_each_entry(pos, head, node) {
  2003. if (with_hits && !pos->hit)
  2004. continue;
  2005. ret += dso__fprintf_buildid(pos, fp);
  2006. ret += fprintf(fp, " %s\n", pos->long_name);
  2007. }
  2008. return ret;
  2009. }
  2010. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  2011. bool with_hits)
  2012. {
  2013. return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
  2014. __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
  2015. }
  2016. size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
  2017. FILE *fp, bool with_hits)
  2018. {
  2019. struct rb_node *nd;
  2020. size_t ret = 0;
  2021. for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
  2022. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  2023. ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
  2024. }
  2025. return ret;
  2026. }
  2027. static struct dso*
  2028. dso__kernel_findnew(struct machine *machine, const char *name,
  2029. const char *short_name, int dso_type)
  2030. {
  2031. /*
  2032. * The kernel dso could be created by build_id processing.
  2033. */
  2034. struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
  2035. /*
  2036. * We need to run this in all cases, since during the build_id
  2037. * processing we had no idea this was the kernel dso.
  2038. */
  2039. if (dso != NULL) {
  2040. dso__set_short_name(dso, short_name);
  2041. dso->kernel = dso_type;
  2042. }
  2043. return dso;
  2044. }
  2045. void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
  2046. {
  2047. char path[PATH_MAX];
  2048. if (machine__is_default_guest(machine))
  2049. return;
  2050. sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
  2051. if (sysfs__read_build_id(path, dso->build_id,
  2052. sizeof(dso->build_id)) == 0)
  2053. dso->has_build_id = true;
  2054. }
  2055. static struct dso *machine__get_kernel(struct machine *machine)
  2056. {
  2057. const char *vmlinux_name = NULL;
  2058. struct dso *kernel;
  2059. if (machine__is_host(machine)) {
  2060. vmlinux_name = symbol_conf.vmlinux_name;
  2061. if (!vmlinux_name)
  2062. vmlinux_name = "[kernel.kallsyms]";
  2063. kernel = dso__kernel_findnew(machine, vmlinux_name,
  2064. "[kernel]",
  2065. DSO_TYPE_KERNEL);
  2066. } else {
  2067. char bf[PATH_MAX];
  2068. if (machine__is_default_guest(machine))
  2069. vmlinux_name = symbol_conf.default_guest_vmlinux_name;
  2070. if (!vmlinux_name)
  2071. vmlinux_name = machine__mmap_name(machine, bf,
  2072. sizeof(bf));
  2073. kernel = dso__kernel_findnew(machine, vmlinux_name,
  2074. "[guest.kernel]",
  2075. DSO_TYPE_GUEST_KERNEL);
  2076. }
  2077. if (kernel != NULL && (!kernel->has_build_id))
  2078. dso__read_running_kernel_build_id(kernel, machine);
  2079. return kernel;
  2080. }
  2081. struct process_args {
  2082. u64 start;
  2083. };
  2084. static int symbol__in_kernel(void *arg, const char *name,
  2085. char type __used, u64 start, u64 end __used)
  2086. {
  2087. struct process_args *args = arg;
  2088. if (strchr(name, '['))
  2089. return 0;
  2090. args->start = start;
  2091. return 1;
  2092. }
  2093. /* Figure out the start address of kernel map from /proc/kallsyms */
  2094. static u64 machine__get_kernel_start_addr(struct machine *machine)
  2095. {
  2096. const char *filename;
  2097. char path[PATH_MAX];
  2098. struct process_args args;
  2099. if (machine__is_host(machine)) {
  2100. filename = "/proc/kallsyms";
  2101. } else {
  2102. if (machine__is_default_guest(machine))
  2103. filename = (char *)symbol_conf.default_guest_kallsyms;
  2104. else {
  2105. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  2106. filename = path;
  2107. }
  2108. }
  2109. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  2110. return 0;
  2111. if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
  2112. return 0;
  2113. return args.start;
  2114. }
  2115. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
  2116. {
  2117. enum map_type type;
  2118. u64 start = machine__get_kernel_start_addr(machine);
  2119. for (type = 0; type < MAP__NR_TYPES; ++type) {
  2120. struct kmap *kmap;
  2121. machine->vmlinux_maps[type] = map__new2(start, kernel, type);
  2122. if (machine->vmlinux_maps[type] == NULL)
  2123. return -1;
  2124. machine->vmlinux_maps[type]->map_ip =
  2125. machine->vmlinux_maps[type]->unmap_ip =
  2126. identity__map_ip;
  2127. kmap = map__kmap(machine->vmlinux_maps[type]);
  2128. kmap->kmaps = &machine->kmaps;
  2129. map_groups__insert(&machine->kmaps,
  2130. machine->vmlinux_maps[type]);
  2131. }
  2132. return 0;
  2133. }
  2134. void machine__destroy_kernel_maps(struct machine *machine)
  2135. {
  2136. enum map_type type;
  2137. for (type = 0; type < MAP__NR_TYPES; ++type) {
  2138. struct kmap *kmap;
  2139. if (machine->vmlinux_maps[type] == NULL)
  2140. continue;
  2141. kmap = map__kmap(machine->vmlinux_maps[type]);
  2142. map_groups__remove(&machine->kmaps,
  2143. machine->vmlinux_maps[type]);
  2144. if (kmap->ref_reloc_sym) {
  2145. /*
  2146. * ref_reloc_sym is shared among all maps, so free just
  2147. * on one of them.
  2148. */
  2149. if (type == MAP__FUNCTION) {
  2150. free((char *)kmap->ref_reloc_sym->name);
  2151. kmap->ref_reloc_sym->name = NULL;
  2152. free(kmap->ref_reloc_sym);
  2153. }
  2154. kmap->ref_reloc_sym = NULL;
  2155. }
  2156. map__delete(machine->vmlinux_maps[type]);
  2157. machine->vmlinux_maps[type] = NULL;
  2158. }
  2159. }
  2160. int machine__create_kernel_maps(struct machine *machine)
  2161. {
  2162. struct dso *kernel = machine__get_kernel(machine);
  2163. if (kernel == NULL ||
  2164. __machine__create_kernel_maps(machine, kernel) < 0)
  2165. return -1;
  2166. if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
  2167. if (machine__is_host(machine))
  2168. pr_debug("Problems creating module maps, "
  2169. "continuing anyway...\n");
  2170. else
  2171. pr_debug("Problems creating module maps for guest %d, "
  2172. "continuing anyway...\n", machine->pid);
  2173. }
  2174. /*
  2175. * Now that we have all the maps created, just set the ->end of them:
  2176. */
  2177. map_groups__fixup_end(&machine->kmaps);
  2178. return 0;
  2179. }
  2180. static void vmlinux_path__exit(void)
  2181. {
  2182. while (--vmlinux_path__nr_entries >= 0) {
  2183. free(vmlinux_path[vmlinux_path__nr_entries]);
  2184. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  2185. }
  2186. free(vmlinux_path);
  2187. vmlinux_path = NULL;
  2188. }
  2189. static int vmlinux_path__init(void)
  2190. {
  2191. struct utsname uts;
  2192. char bf[PATH_MAX];
  2193. vmlinux_path = malloc(sizeof(char *) * 5);
  2194. if (vmlinux_path == NULL)
  2195. return -1;
  2196. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  2197. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2198. goto out_fail;
  2199. ++vmlinux_path__nr_entries;
  2200. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  2201. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2202. goto out_fail;
  2203. ++vmlinux_path__nr_entries;
  2204. /* only try running kernel version if no symfs was given */
  2205. if (symbol_conf.symfs[0] != 0)
  2206. return 0;
  2207. if (uname(&uts) < 0)
  2208. return -1;
  2209. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  2210. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2211. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2212. goto out_fail;
  2213. ++vmlinux_path__nr_entries;
  2214. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  2215. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2216. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2217. goto out_fail;
  2218. ++vmlinux_path__nr_entries;
  2219. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  2220. uts.release);
  2221. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  2222. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  2223. goto out_fail;
  2224. ++vmlinux_path__nr_entries;
  2225. return 0;
  2226. out_fail:
  2227. vmlinux_path__exit();
  2228. return -1;
  2229. }
  2230. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
  2231. {
  2232. int i;
  2233. size_t printed = 0;
  2234. struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
  2235. if (kdso->has_build_id) {
  2236. char filename[PATH_MAX];
  2237. if (dso__build_id_filename(kdso, filename, sizeof(filename)))
  2238. printed += fprintf(fp, "[0] %s\n", filename);
  2239. }
  2240. for (i = 0; i < vmlinux_path__nr_entries; ++i)
  2241. printed += fprintf(fp, "[%d] %s\n",
  2242. i + kdso->has_build_id, vmlinux_path[i]);
  2243. return printed;
  2244. }
  2245. static int setup_list(struct strlist **list, const char *list_str,
  2246. const char *list_name)
  2247. {
  2248. if (list_str == NULL)
  2249. return 0;
  2250. *list = strlist__new(true, list_str);
  2251. if (!*list) {
  2252. pr_err("problems parsing %s list\n", list_name);
  2253. return -1;
  2254. }
  2255. return 0;
  2256. }
  2257. static bool symbol__read_kptr_restrict(void)
  2258. {
  2259. bool value = false;
  2260. if (geteuid() != 0) {
  2261. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  2262. if (fp != NULL) {
  2263. char line[8];
  2264. if (fgets(line, sizeof(line), fp) != NULL)
  2265. value = atoi(line) != 0;
  2266. fclose(fp);
  2267. }
  2268. }
  2269. return value;
  2270. }
  2271. int symbol__init(void)
  2272. {
  2273. const char *symfs;
  2274. if (symbol_conf.initialized)
  2275. return 0;
  2276. symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
  2277. elf_version(EV_CURRENT);
  2278. if (symbol_conf.sort_by_name)
  2279. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  2280. sizeof(struct symbol));
  2281. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  2282. return -1;
  2283. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  2284. pr_err("'.' is the only non valid --field-separator argument\n");
  2285. return -1;
  2286. }
  2287. if (setup_list(&symbol_conf.dso_list,
  2288. symbol_conf.dso_list_str, "dso") < 0)
  2289. return -1;
  2290. if (setup_list(&symbol_conf.comm_list,
  2291. symbol_conf.comm_list_str, "comm") < 0)
  2292. goto out_free_dso_list;
  2293. if (setup_list(&symbol_conf.sym_list,
  2294. symbol_conf.sym_list_str, "symbol") < 0)
  2295. goto out_free_comm_list;
  2296. /*
  2297. * A path to symbols of "/" is identical to ""
  2298. * reset here for simplicity.
  2299. */
  2300. symfs = realpath(symbol_conf.symfs, NULL);
  2301. if (symfs == NULL)
  2302. symfs = symbol_conf.symfs;
  2303. if (strcmp(symfs, "/") == 0)
  2304. symbol_conf.symfs = "";
  2305. if (symfs != symbol_conf.symfs)
  2306. free((void *)symfs);
  2307. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  2308. symbol_conf.initialized = true;
  2309. return 0;
  2310. out_free_comm_list:
  2311. strlist__delete(symbol_conf.comm_list);
  2312. out_free_dso_list:
  2313. strlist__delete(symbol_conf.dso_list);
  2314. return -1;
  2315. }
  2316. void symbol__exit(void)
  2317. {
  2318. if (!symbol_conf.initialized)
  2319. return;
  2320. strlist__delete(symbol_conf.sym_list);
  2321. strlist__delete(symbol_conf.dso_list);
  2322. strlist__delete(symbol_conf.comm_list);
  2323. vmlinux_path__exit();
  2324. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  2325. symbol_conf.initialized = false;
  2326. }
  2327. int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
  2328. {
  2329. struct machine *machine = machines__findnew(machines, pid);
  2330. if (machine == NULL)
  2331. return -1;
  2332. return machine__create_kernel_maps(machine);
  2333. }
  2334. static int hex(char ch)
  2335. {
  2336. if ((ch >= '0') && (ch <= '9'))
  2337. return ch - '0';
  2338. if ((ch >= 'a') && (ch <= 'f'))
  2339. return ch - 'a' + 10;
  2340. if ((ch >= 'A') && (ch <= 'F'))
  2341. return ch - 'A' + 10;
  2342. return -1;
  2343. }
  2344. /*
  2345. * While we find nice hex chars, build a long_val.
  2346. * Return number of chars processed.
  2347. */
  2348. int hex2u64(const char *ptr, u64 *long_val)
  2349. {
  2350. const char *p = ptr;
  2351. *long_val = 0;
  2352. while (*p) {
  2353. const int hex_val = hex(*p);
  2354. if (hex_val < 0)
  2355. break;
  2356. *long_val = (*long_val << 4) | hex_val;
  2357. p++;
  2358. }
  2359. return p - ptr;
  2360. }
  2361. char *strxfrchar(char *s, char from, char to)
  2362. {
  2363. char *p = s;
  2364. while ((p = strchr(p, from)) != NULL)
  2365. *p++ = to;
  2366. return s;
  2367. }
  2368. int machines__create_guest_kernel_maps(struct rb_root *machines)
  2369. {
  2370. int ret = 0;
  2371. struct dirent **namelist = NULL;
  2372. int i, items = 0;
  2373. char path[PATH_MAX];
  2374. pid_t pid;
  2375. if (symbol_conf.default_guest_vmlinux_name ||
  2376. symbol_conf.default_guest_modules ||
  2377. symbol_conf.default_guest_kallsyms) {
  2378. machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
  2379. }
  2380. if (symbol_conf.guestmount) {
  2381. items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
  2382. if (items <= 0)
  2383. return -ENOENT;
  2384. for (i = 0; i < items; i++) {
  2385. if (!isdigit(namelist[i]->d_name[0])) {
  2386. /* Filter out . and .. */
  2387. continue;
  2388. }
  2389. pid = atoi(namelist[i]->d_name);
  2390. sprintf(path, "%s/%s/proc/kallsyms",
  2391. symbol_conf.guestmount,
  2392. namelist[i]->d_name);
  2393. ret = access(path, R_OK);
  2394. if (ret) {
  2395. pr_debug("Can't access file %s\n", path);
  2396. goto failure;
  2397. }
  2398. machines__create_kernel_maps(machines, pid);
  2399. }
  2400. failure:
  2401. free(namelist);
  2402. }
  2403. return ret;
  2404. }
  2405. void machines__destroy_guest_kernel_maps(struct rb_root *machines)
  2406. {
  2407. struct rb_node *next = rb_first(machines);
  2408. while (next) {
  2409. struct machine *pos = rb_entry(next, struct machine, rb_node);
  2410. next = rb_next(&pos->rb_node);
  2411. rb_erase(&pos->rb_node, machines);
  2412. machine__delete(pos);
  2413. }
  2414. }
  2415. int machine__load_kallsyms(struct machine *machine, const char *filename,
  2416. enum map_type type, symbol_filter_t filter)
  2417. {
  2418. struct map *map = machine->vmlinux_maps[type];
  2419. int ret = dso__load_kallsyms(map->dso, filename, map, filter);
  2420. if (ret > 0) {
  2421. dso__set_loaded(map->dso, type);
  2422. /*
  2423. * Since /proc/kallsyms will have multiple sessions for the
  2424. * kernel, with modules between them, fixup the end of all
  2425. * sections.
  2426. */
  2427. __map_groups__fixup_end(&machine->kmaps, type);
  2428. }
  2429. return ret;
  2430. }
  2431. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  2432. symbol_filter_t filter)
  2433. {
  2434. struct map *map = machine->vmlinux_maps[type];
  2435. int ret = dso__load_vmlinux_path(map->dso, map, filter);
  2436. if (ret > 0) {
  2437. dso__set_loaded(map->dso, type);
  2438. map__reloc_vmlinux(map);
  2439. }
  2440. return ret;
  2441. }
  2442. struct map *dso__new_map(const char *name)
  2443. {
  2444. struct map *map = NULL;
  2445. struct dso *dso = dso__new(name);
  2446. if (dso)
  2447. map = map__new2(0, dso, MAP__FUNCTION);
  2448. return map;
  2449. }
  2450. static int open_dso(struct dso *dso, struct machine *machine)
  2451. {
  2452. char *root_dir = (char *) "";
  2453. char *name;
  2454. int fd;
  2455. name = malloc(PATH_MAX);
  2456. if (!name)
  2457. return -ENOMEM;
  2458. if (machine)
  2459. root_dir = machine->root_dir;
  2460. if (dso__binary_type_file(dso, dso->data_type,
  2461. root_dir, name, PATH_MAX)) {
  2462. free(name);
  2463. return -EINVAL;
  2464. }
  2465. fd = open(name, O_RDONLY);
  2466. free(name);
  2467. return fd;
  2468. }
  2469. int dso__data_fd(struct dso *dso, struct machine *machine)
  2470. {
  2471. int i = 0;
  2472. if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
  2473. return open_dso(dso, machine);
  2474. do {
  2475. int fd;
  2476. dso->data_type = binary_type_data[i++];
  2477. fd = open_dso(dso, machine);
  2478. if (fd >= 0)
  2479. return fd;
  2480. } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
  2481. return -EINVAL;
  2482. }
  2483. static ssize_t dso_cache_read(struct dso *dso __used, u64 offset __used,
  2484. u8 *data __used, ssize_t size __used)
  2485. {
  2486. return -EINVAL;
  2487. }
  2488. static int dso_cache_add(struct dso *dso __used, u64 offset __used,
  2489. u8 *data __used, ssize_t size __used)
  2490. {
  2491. return 0;
  2492. }
  2493. static ssize_t read_dso_data(struct dso *dso, struct machine *machine,
  2494. u64 offset, u8 *data, ssize_t size)
  2495. {
  2496. ssize_t rsize = -1;
  2497. int fd;
  2498. fd = dso__data_fd(dso, machine);
  2499. if (fd < 0)
  2500. return -1;
  2501. do {
  2502. if (-1 == lseek(fd, offset, SEEK_SET))
  2503. break;
  2504. rsize = read(fd, data, size);
  2505. if (-1 == rsize)
  2506. break;
  2507. if (dso_cache_add(dso, offset, data, size))
  2508. pr_err("Failed to add data int dso cache.");
  2509. } while (0);
  2510. close(fd);
  2511. return rsize;
  2512. }
  2513. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  2514. u64 offset, u8 *data, ssize_t size)
  2515. {
  2516. if (dso_cache_read(dso, offset, data, size))
  2517. return read_dso_data(dso, machine, offset, data, size);
  2518. return 0;
  2519. }
  2520. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  2521. struct machine *machine, u64 addr,
  2522. u8 *data, ssize_t size)
  2523. {
  2524. u64 offset = map->map_ip(map, addr);
  2525. return dso__data_read_offset(dso, machine, offset, data, size);
  2526. }