annotate-data.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Convert sample address to data type using DWARF debug info.
  4. *
  5. * Written by Namhyung Kim <namhyung@kernel.org>
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <inttypes.h>
  11. #include <linux/zalloc.h>
  12. #include "annotate.h"
  13. #include "annotate-data.h"
  14. #include "debuginfo.h"
  15. #include "debug.h"
  16. #include "dso.h"
  17. #include "dwarf-regs.h"
  18. #include "evsel.h"
  19. #include "evlist.h"
  20. #include "map.h"
  21. #include "map_symbol.h"
  22. #include "sort.h"
  23. #include "strbuf.h"
  24. #include "symbol.h"
  25. #include "symbol_conf.h"
  26. #include "thread.h"
  27. /* register number of the stack pointer */
  28. #define X86_REG_SP 7
  29. static void delete_var_types(struct die_var_type *var_types);
  30. #define pr_debug_dtp(fmt, ...) \
  31. do { \
  32. if (debug_type_profile) \
  33. pr_info(fmt, ##__VA_ARGS__); \
  34. else \
  35. pr_debug3(fmt, ##__VA_ARGS__); \
  36. } while (0)
  37. void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)
  38. {
  39. struct strbuf sb;
  40. char *str;
  41. Dwarf_Word size = 0;
  42. if (!debug_type_profile && verbose < 3)
  43. return;
  44. switch (kind) {
  45. case TSR_KIND_INVALID:
  46. pr_info("\n");
  47. return;
  48. case TSR_KIND_PERCPU_BASE:
  49. pr_info(" percpu base\n");
  50. return;
  51. case TSR_KIND_CONST:
  52. pr_info(" constant\n");
  53. return;
  54. case TSR_KIND_PERCPU_POINTER:
  55. pr_info(" percpu pointer");
  56. /* it also prints the type info */
  57. break;
  58. case TSR_KIND_POINTER:
  59. pr_info(" pointer");
  60. /* it also prints the type info */
  61. break;
  62. case TSR_KIND_CANARY:
  63. pr_info(" stack canary\n");
  64. return;
  65. case TSR_KIND_TYPE:
  66. default:
  67. break;
  68. }
  69. dwarf_aggregate_size(die, &size);
  70. strbuf_init(&sb, 32);
  71. die_get_typename_from_type(die, &sb);
  72. str = strbuf_detach(&sb, NULL);
  73. pr_info(" type='%s' size=%#lx (die:%#lx)\n",
  74. str, (long)size, (long)dwarf_dieoffset(die));
  75. free(str);
  76. }
  77. static void pr_debug_location(Dwarf_Die *die, u64 pc, int reg)
  78. {
  79. ptrdiff_t off = 0;
  80. Dwarf_Attribute attr;
  81. Dwarf_Addr base, start, end;
  82. Dwarf_Op *ops;
  83. size_t nops;
  84. if (!debug_type_profile && verbose < 3)
  85. return;
  86. if (dwarf_attr(die, DW_AT_location, &attr) == NULL)
  87. return;
  88. while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) {
  89. if (reg != DWARF_REG_PC && end <= pc)
  90. continue;
  91. if (reg != DWARF_REG_PC && start > pc)
  92. break;
  93. pr_info(" variable location: ");
  94. switch (ops->atom) {
  95. case DW_OP_reg0 ...DW_OP_reg31:
  96. pr_info("reg%d\n", ops->atom - DW_OP_reg0);
  97. break;
  98. case DW_OP_breg0 ...DW_OP_breg31:
  99. pr_info("base=reg%d, offset=%#lx\n",
  100. ops->atom - DW_OP_breg0, (long)ops->number);
  101. break;
  102. case DW_OP_regx:
  103. pr_info("reg%ld\n", (long)ops->number);
  104. break;
  105. case DW_OP_bregx:
  106. pr_info("base=reg%ld, offset=%#lx\n",
  107. (long)ops->number, (long)ops->number2);
  108. break;
  109. case DW_OP_fbreg:
  110. pr_info("use frame base, offset=%#lx\n", (long)ops->number);
  111. break;
  112. case DW_OP_addr:
  113. pr_info("address=%#lx\n", (long)ops->number);
  114. break;
  115. default:
  116. pr_info("unknown: code=%#x, number=%#lx\n",
  117. ops->atom, (long)ops->number);
  118. break;
  119. }
  120. break;
  121. }
  122. }
  123. static void pr_debug_scope(Dwarf_Die *scope_die)
  124. {
  125. int tag;
  126. if (!debug_type_profile && verbose < 3)
  127. return;
  128. pr_info("(die:%lx) ", (long)dwarf_dieoffset(scope_die));
  129. tag = dwarf_tag(scope_die);
  130. if (tag == DW_TAG_subprogram)
  131. pr_info("[function] %s\n", dwarf_diename(scope_die));
  132. else if (tag == DW_TAG_inlined_subroutine)
  133. pr_info("[inlined] %s\n", dwarf_diename(scope_die));
  134. else if (tag == DW_TAG_lexical_block)
  135. pr_info("[block]\n");
  136. else
  137. pr_info("[unknown] tag=%x\n", tag);
  138. }
  139. bool has_reg_type(struct type_state *state, int reg)
  140. {
  141. return (unsigned)reg < ARRAY_SIZE(state->regs);
  142. }
  143. static void init_type_state(struct type_state *state, const struct arch *arch)
  144. {
  145. memset(state, 0, sizeof(*state));
  146. INIT_LIST_HEAD(&state->stack_vars);
  147. if (arch__is_x86(arch)) {
  148. state->regs[0].caller_saved = true;
  149. state->regs[1].caller_saved = true;
  150. state->regs[2].caller_saved = true;
  151. state->regs[4].caller_saved = true;
  152. state->regs[5].caller_saved = true;
  153. state->regs[8].caller_saved = true;
  154. state->regs[9].caller_saved = true;
  155. state->regs[10].caller_saved = true;
  156. state->regs[11].caller_saved = true;
  157. state->ret_reg = 0;
  158. state->stack_reg = X86_REG_SP;
  159. }
  160. }
  161. static void exit_type_state(struct type_state *state)
  162. {
  163. struct type_state_stack *stack, *tmp;
  164. list_for_each_entry_safe(stack, tmp, &state->stack_vars, list) {
  165. list_del(&stack->list);
  166. free(stack);
  167. }
  168. }
  169. /*
  170. * Compare type name and size to maintain them in a tree.
  171. * I'm not sure if DWARF would have information of a single type in many
  172. * different places (compilation units). If not, it could compare the
  173. * offset of the type entry in the .debug_info section.
  174. */
  175. static int data_type_cmp(const void *_key, const struct rb_node *node)
  176. {
  177. const struct annotated_data_type *key = _key;
  178. struct annotated_data_type *type;
  179. type = rb_entry(node, struct annotated_data_type, node);
  180. if (key->self.size != type->self.size)
  181. return key->self.size - type->self.size;
  182. return strcmp(key->self.type_name, type->self.type_name);
  183. }
  184. static bool data_type_less(struct rb_node *node_a, const struct rb_node *node_b)
  185. {
  186. struct annotated_data_type *a, *b;
  187. a = rb_entry(node_a, struct annotated_data_type, node);
  188. b = rb_entry(node_b, struct annotated_data_type, node);
  189. if (a->self.size != b->self.size)
  190. return a->self.size < b->self.size;
  191. return strcmp(a->self.type_name, b->self.type_name) < 0;
  192. }
  193. /* Recursively add new members for struct/union */
  194. static int __add_member_cb(Dwarf_Die *die, void *arg)
  195. {
  196. struct annotated_member *parent = arg;
  197. struct annotated_member *member;
  198. Dwarf_Die member_type, die_mem;
  199. Dwarf_Word size, loc, bit_size = 0;
  200. Dwarf_Attribute attr;
  201. struct strbuf sb;
  202. int tag;
  203. if (dwarf_tag(die) != DW_TAG_member)
  204. return DIE_FIND_CB_SIBLING;
  205. member = zalloc(sizeof(*member));
  206. if (member == NULL)
  207. return DIE_FIND_CB_END;
  208. strbuf_init(&sb, 32);
  209. die_get_typename(die, &sb);
  210. __die_get_real_type(die, &member_type);
  211. if (dwarf_tag(&member_type) == DW_TAG_typedef)
  212. die_get_real_type(&member_type, &die_mem);
  213. else
  214. die_mem = member_type;
  215. if (dwarf_aggregate_size(&die_mem, &size) < 0)
  216. size = 0;
  217. if (dwarf_attr_integrate(die, DW_AT_data_member_location, &attr))
  218. dwarf_formudata(&attr, &loc);
  219. else {
  220. /* bitfield member */
  221. if (dwarf_attr_integrate(die, DW_AT_data_bit_offset, &attr) &&
  222. dwarf_formudata(&attr, &loc) == 0)
  223. loc /= 8;
  224. else
  225. loc = 0;
  226. if (dwarf_attr_integrate(die, DW_AT_bit_size, &attr) &&
  227. dwarf_formudata(&attr, &bit_size) == 0)
  228. size = (bit_size + 7) / 8;
  229. }
  230. member->type_name = strbuf_detach(&sb, NULL);
  231. /* member->var_name can be NULL */
  232. if (dwarf_diename(die)) {
  233. if (bit_size) {
  234. if (asprintf(&member->var_name, "%s:%ld",
  235. dwarf_diename(die), (long)bit_size) < 0)
  236. member->var_name = NULL;
  237. } else {
  238. member->var_name = strdup(dwarf_diename(die));
  239. }
  240. if (member->var_name == NULL) {
  241. free(member);
  242. return DIE_FIND_CB_END;
  243. }
  244. }
  245. member->size = size;
  246. member->offset = loc + parent->offset;
  247. INIT_LIST_HEAD(&member->children);
  248. list_add_tail(&member->node, &parent->children);
  249. tag = dwarf_tag(&die_mem);
  250. switch (tag) {
  251. case DW_TAG_structure_type:
  252. case DW_TAG_union_type:
  253. die_find_child(&die_mem, __add_member_cb, member, &die_mem);
  254. break;
  255. default:
  256. break;
  257. }
  258. return DIE_FIND_CB_SIBLING;
  259. }
  260. static void add_member_types(struct annotated_data_type *parent, Dwarf_Die *type)
  261. {
  262. Dwarf_Die die_mem;
  263. die_find_child(type, __add_member_cb, &parent->self, &die_mem);
  264. }
  265. static void delete_members(struct annotated_member *member)
  266. {
  267. struct annotated_member *child, *tmp;
  268. list_for_each_entry_safe(child, tmp, &member->children, node) {
  269. list_del(&child->node);
  270. delete_members(child);
  271. zfree(&child->type_name);
  272. zfree(&child->var_name);
  273. free(child);
  274. }
  275. }
  276. static int fill_member_name(char *buf, size_t sz, struct annotated_member *m,
  277. int offset, bool first)
  278. {
  279. struct annotated_member *child;
  280. if (list_empty(&m->children))
  281. return 0;
  282. list_for_each_entry(child, &m->children, node) {
  283. int len;
  284. if (offset < child->offset || offset >= child->offset + child->size)
  285. continue;
  286. /* It can have anonymous struct/union members */
  287. if (child->var_name) {
  288. len = scnprintf(buf, sz, "%s%s",
  289. first ? "" : ".", child->var_name);
  290. first = false;
  291. } else {
  292. len = 0;
  293. }
  294. return fill_member_name(buf + len, sz - len, child, offset, first) + len;
  295. }
  296. return 0;
  297. }
  298. int annotated_data_type__get_member_name(struct annotated_data_type *adt,
  299. char *buf, size_t sz, int member_offset)
  300. {
  301. return fill_member_name(buf, sz, &adt->self, member_offset, /*first=*/true);
  302. }
  303. static struct annotated_data_type *dso__findnew_data_type(struct dso *dso,
  304. Dwarf_Die *type_die)
  305. {
  306. struct annotated_data_type *result = NULL;
  307. struct annotated_data_type key;
  308. struct rb_node *node;
  309. struct strbuf sb;
  310. char *type_name;
  311. Dwarf_Word size;
  312. strbuf_init(&sb, 32);
  313. if (die_get_typename_from_type(type_die, &sb) < 0)
  314. strbuf_add(&sb, "(unknown type)", 14);
  315. type_name = strbuf_detach(&sb, NULL);
  316. if (dwarf_tag(type_die) == DW_TAG_typedef)
  317. die_get_real_type(type_die, type_die);
  318. dwarf_aggregate_size(type_die, &size);
  319. /* Check existing nodes in dso->data_types tree */
  320. key.self.type_name = type_name;
  321. key.self.size = size;
  322. node = rb_find(&key, dso__data_types(dso), data_type_cmp);
  323. if (node) {
  324. result = rb_entry(node, struct annotated_data_type, node);
  325. free(type_name);
  326. return result;
  327. }
  328. /* If not, add a new one */
  329. result = zalloc(sizeof(*result));
  330. if (result == NULL) {
  331. free(type_name);
  332. return NULL;
  333. }
  334. result->self.type_name = type_name;
  335. result->self.size = size;
  336. INIT_LIST_HEAD(&result->self.children);
  337. if (symbol_conf.annotate_data_member)
  338. add_member_types(result, type_die);
  339. rb_add(&result->node, dso__data_types(dso), data_type_less);
  340. return result;
  341. }
  342. static bool find_cu_die(struct debuginfo *di, u64 pc, Dwarf_Die *cu_die)
  343. {
  344. Dwarf_Off off, next_off;
  345. size_t header_size;
  346. if (dwarf_addrdie(di->dbg, pc, cu_die) != NULL)
  347. return cu_die;
  348. /*
  349. * There are some kernels don't have full aranges and contain only a few
  350. * aranges entries. Fallback to iterate all CU entries in .debug_info
  351. * in case it's missing.
  352. */
  353. off = 0;
  354. while (dwarf_nextcu(di->dbg, off, &next_off, &header_size,
  355. NULL, NULL, NULL) == 0) {
  356. if (dwarf_offdie(di->dbg, off + header_size, cu_die) &&
  357. dwarf_haspc(cu_die, pc))
  358. return true;
  359. off = next_off;
  360. }
  361. return false;
  362. }
  363. enum type_match_result {
  364. PERF_TMR_UNKNOWN = 0,
  365. PERF_TMR_OK,
  366. PERF_TMR_NO_TYPE,
  367. PERF_TMR_NO_POINTER,
  368. PERF_TMR_NO_SIZE,
  369. PERF_TMR_BAD_OFFSET,
  370. PERF_TMR_BAIL_OUT,
  371. };
  372. static const char *match_result_str(enum type_match_result tmr)
  373. {
  374. switch (tmr) {
  375. case PERF_TMR_OK:
  376. return "Good!";
  377. case PERF_TMR_NO_TYPE:
  378. return "no type information";
  379. case PERF_TMR_NO_POINTER:
  380. return "no/void pointer";
  381. case PERF_TMR_NO_SIZE:
  382. return "type size is unknown";
  383. case PERF_TMR_BAD_OFFSET:
  384. return "offset bigger than size";
  385. case PERF_TMR_UNKNOWN:
  386. case PERF_TMR_BAIL_OUT:
  387. default:
  388. return "invalid state";
  389. }
  390. }
  391. static bool is_pointer_type(Dwarf_Die *type_die)
  392. {
  393. int tag = dwarf_tag(type_die);
  394. return tag == DW_TAG_pointer_type || tag == DW_TAG_array_type;
  395. }
  396. static bool is_compound_type(Dwarf_Die *type_die)
  397. {
  398. int tag = dwarf_tag(type_die);
  399. return tag == DW_TAG_structure_type || tag == DW_TAG_union_type;
  400. }
  401. /* returns if Type B has better information than Type A */
  402. static bool is_better_type(Dwarf_Die *type_a, Dwarf_Die *type_b)
  403. {
  404. Dwarf_Word size_a, size_b;
  405. Dwarf_Die die_a, die_b;
  406. /* pointer type is preferred */
  407. if (is_pointer_type(type_a) != is_pointer_type(type_b))
  408. return is_pointer_type(type_b);
  409. if (is_pointer_type(type_b)) {
  410. /*
  411. * We want to compare the target type, but 'void *' can fail to
  412. * get the target type.
  413. */
  414. if (die_get_real_type(type_a, &die_a) == NULL)
  415. return true;
  416. if (die_get_real_type(type_b, &die_b) == NULL)
  417. return false;
  418. type_a = &die_a;
  419. type_b = &die_b;
  420. }
  421. /* bigger type is preferred */
  422. if (dwarf_aggregate_size(type_a, &size_a) < 0 ||
  423. dwarf_aggregate_size(type_b, &size_b) < 0)
  424. return false;
  425. if (size_a != size_b)
  426. return size_a < size_b;
  427. /* struct or union is preferred */
  428. if (is_compound_type(type_a) != is_compound_type(type_b))
  429. return is_compound_type(type_b);
  430. /* typedef is preferred */
  431. if (dwarf_tag(type_b) == DW_TAG_typedef)
  432. return true;
  433. return false;
  434. }
  435. /* The type info will be saved in @type_die */
  436. static enum type_match_result check_variable(struct data_loc_info *dloc,
  437. Dwarf_Die *var_die,
  438. Dwarf_Die *type_die, int reg,
  439. int offset, bool is_fbreg)
  440. {
  441. Dwarf_Word size;
  442. bool needs_pointer = true;
  443. Dwarf_Die sized_type;
  444. if (reg == DWARF_REG_PC)
  445. needs_pointer = false;
  446. else if (reg == dloc->fbreg || is_fbreg)
  447. needs_pointer = false;
  448. else if (arch__is_x86(dloc->arch) && reg == X86_REG_SP)
  449. needs_pointer = false;
  450. /* Get the type of the variable */
  451. if (__die_get_real_type(var_die, type_die) == NULL)
  452. return PERF_TMR_NO_TYPE;
  453. /*
  454. * Usually it expects a pointer type for a memory access.
  455. * Convert to a real type it points to. But global variables
  456. * and local variables are accessed directly without a pointer.
  457. */
  458. if (needs_pointer) {
  459. if (!is_pointer_type(type_die) ||
  460. __die_get_real_type(type_die, type_die) == NULL)
  461. return PERF_TMR_NO_POINTER;
  462. }
  463. if (dwarf_tag(type_die) == DW_TAG_typedef)
  464. die_get_real_type(type_die, &sized_type);
  465. else
  466. sized_type = *type_die;
  467. /* Get the size of the actual type */
  468. if (dwarf_aggregate_size(&sized_type, &size) < 0)
  469. return PERF_TMR_NO_SIZE;
  470. /* Minimal sanity check */
  471. if ((unsigned)offset >= size)
  472. return PERF_TMR_BAD_OFFSET;
  473. return PERF_TMR_OK;
  474. }
  475. struct type_state_stack *find_stack_state(struct type_state *state,
  476. int offset)
  477. {
  478. struct type_state_stack *stack;
  479. list_for_each_entry(stack, &state->stack_vars, list) {
  480. if (offset == stack->offset)
  481. return stack;
  482. if (stack->compound && stack->offset < offset &&
  483. offset < stack->offset + stack->size)
  484. return stack;
  485. }
  486. return NULL;
  487. }
  488. void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
  489. Dwarf_Die *type_die, int ptr_offset)
  490. {
  491. int tag;
  492. Dwarf_Word size;
  493. if (kind == TSR_KIND_POINTER) {
  494. /* TODO: arch-dependent pointer size */
  495. size = sizeof(void *);
  496. }
  497. else if (dwarf_aggregate_size(type_die, &size) < 0)
  498. size = 0;
  499. stack->type = *type_die;
  500. stack->size = size;
  501. stack->offset = offset;
  502. stack->ptr_offset = ptr_offset;
  503. stack->kind = kind;
  504. if (kind == TSR_KIND_POINTER) {
  505. stack->compound = false;
  506. return;
  507. }
  508. tag = dwarf_tag(type_die);
  509. switch (tag) {
  510. case DW_TAG_structure_type:
  511. case DW_TAG_union_type:
  512. stack->compound = (kind != TSR_KIND_PERCPU_POINTER);
  513. break;
  514. default:
  515. stack->compound = false;
  516. break;
  517. }
  518. }
  519. struct type_state_stack *findnew_stack_state(struct type_state *state,
  520. int offset, u8 kind,
  521. Dwarf_Die *type_die,
  522. int ptr_offset)
  523. {
  524. struct type_state_stack *stack = find_stack_state(state, offset);
  525. if (stack) {
  526. set_stack_state(stack, offset, kind, type_die, ptr_offset);
  527. return stack;
  528. }
  529. stack = malloc(sizeof(*stack));
  530. if (stack) {
  531. set_stack_state(stack, offset, kind, type_die, ptr_offset);
  532. list_add(&stack->list, &state->stack_vars);
  533. }
  534. return stack;
  535. }
  536. /* Maintain a cache for quick global variable lookup */
  537. struct global_var_entry {
  538. struct rb_node node;
  539. char *name;
  540. u64 start;
  541. u64 end;
  542. u64 die_offset;
  543. };
  544. static int global_var_cmp(const void *_key, const struct rb_node *node)
  545. {
  546. const u64 addr = (uintptr_t)_key;
  547. struct global_var_entry *gvar;
  548. gvar = rb_entry(node, struct global_var_entry, node);
  549. if (gvar->start <= addr && addr < gvar->end)
  550. return 0;
  551. return gvar->start > addr ? -1 : 1;
  552. }
  553. static bool global_var_less(struct rb_node *node_a, const struct rb_node *node_b)
  554. {
  555. struct global_var_entry *gvar_a, *gvar_b;
  556. gvar_a = rb_entry(node_a, struct global_var_entry, node);
  557. gvar_b = rb_entry(node_b, struct global_var_entry, node);
  558. return gvar_a->start < gvar_b->start;
  559. }
  560. static struct global_var_entry *global_var__find(struct data_loc_info *dloc, u64 addr)
  561. {
  562. struct dso *dso = map__dso(dloc->ms->map);
  563. struct rb_node *node;
  564. node = rb_find((void *)(uintptr_t)addr, dso__global_vars(dso), global_var_cmp);
  565. if (node == NULL)
  566. return NULL;
  567. return rb_entry(node, struct global_var_entry, node);
  568. }
  569. static bool global_var__add(struct data_loc_info *dloc, u64 addr,
  570. const char *name, Dwarf_Die *type_die)
  571. {
  572. struct dso *dso = map__dso(dloc->ms->map);
  573. struct global_var_entry *gvar;
  574. Dwarf_Word size;
  575. if (dwarf_aggregate_size(type_die, &size) < 0)
  576. return false;
  577. gvar = malloc(sizeof(*gvar));
  578. if (gvar == NULL)
  579. return false;
  580. gvar->name = name ? strdup(name) : NULL;
  581. if (name && gvar->name == NULL) {
  582. free(gvar);
  583. return false;
  584. }
  585. gvar->start = addr;
  586. gvar->end = addr + size;
  587. gvar->die_offset = dwarf_dieoffset(type_die);
  588. rb_add(&gvar->node, dso__global_vars(dso), global_var_less);
  589. return true;
  590. }
  591. void global_var_type__tree_delete(struct rb_root *root)
  592. {
  593. struct global_var_entry *gvar;
  594. while (!RB_EMPTY_ROOT(root)) {
  595. struct rb_node *node = rb_first(root);
  596. rb_erase(node, root);
  597. gvar = rb_entry(node, struct global_var_entry, node);
  598. zfree(&gvar->name);
  599. free(gvar);
  600. }
  601. }
  602. bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
  603. const char **var_name, int *var_offset)
  604. {
  605. struct addr_location al;
  606. struct symbol *sym;
  607. u64 mem_addr;
  608. /* Kernel symbols might be relocated */
  609. mem_addr = addr + map__reloc(dloc->ms->map);
  610. addr_location__init(&al);
  611. sym = thread__find_symbol_fb(dloc->thread, dloc->cpumode,
  612. mem_addr, &al);
  613. if (sym) {
  614. *var_name = sym->name;
  615. /* Calculate type offset from the start of variable */
  616. *var_offset = mem_addr - map__unmap_ip(al.map, sym->start);
  617. } else {
  618. *var_name = NULL;
  619. }
  620. addr_location__exit(&al);
  621. if (*var_name == NULL)
  622. return false;
  623. return true;
  624. }
  625. static void global_var__collect(struct data_loc_info *dloc)
  626. {
  627. Dwarf *dwarf = dloc->di->dbg;
  628. Dwarf_Off off, next_off;
  629. Dwarf_Die cu_die, type_die;
  630. size_t header_size;
  631. /* Iterate all CU and collect global variables that have no location in a register. */
  632. off = 0;
  633. while (dwarf_nextcu(dwarf, off, &next_off, &header_size,
  634. NULL, NULL, NULL) == 0) {
  635. struct die_var_type *var_types = NULL;
  636. struct die_var_type *pos;
  637. if (dwarf_offdie(dwarf, off + header_size, &cu_die) == NULL) {
  638. off = next_off;
  639. continue;
  640. }
  641. die_collect_global_vars(&cu_die, &var_types);
  642. for (pos = var_types; pos; pos = pos->next) {
  643. const char *var_name = NULL;
  644. int var_offset = 0;
  645. if (pos->reg != -1)
  646. continue;
  647. if (!dwarf_offdie(dwarf, pos->die_off, &type_die))
  648. continue;
  649. if (!get_global_var_info(dloc, pos->addr, &var_name,
  650. &var_offset))
  651. continue;
  652. if (var_offset != 0)
  653. continue;
  654. global_var__add(dloc, pos->addr, var_name, &type_die);
  655. }
  656. delete_var_types(var_types);
  657. off = next_off;
  658. }
  659. }
  660. bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,
  661. u64 ip, u64 var_addr, int *var_offset,
  662. Dwarf_Die *type_die)
  663. {
  664. u64 pc;
  665. int offset;
  666. const char *var_name = NULL;
  667. struct global_var_entry *gvar;
  668. struct dso *dso = map__dso(dloc->ms->map);
  669. Dwarf_Die var_die;
  670. if (RB_EMPTY_ROOT(dso__global_vars(dso)))
  671. global_var__collect(dloc);
  672. gvar = global_var__find(dloc, var_addr);
  673. if (gvar) {
  674. if (!dwarf_offdie(dloc->di->dbg, gvar->die_offset, type_die))
  675. return false;
  676. *var_offset = var_addr - gvar->start;
  677. return true;
  678. }
  679. /* Try to get the variable by address first */
  680. if (die_find_variable_by_addr(cu_die, var_addr, &var_die, &offset) &&
  681. check_variable(dloc, &var_die, type_die, DWARF_REG_PC, offset,
  682. /*is_fbreg=*/false) == PERF_TMR_OK) {
  683. var_name = dwarf_diename(&var_die);
  684. *var_offset = offset;
  685. goto ok;
  686. }
  687. if (!get_global_var_info(dloc, var_addr, &var_name, var_offset))
  688. return false;
  689. pc = map__rip_2objdump(dloc->ms->map, ip);
  690. /* Try to get the name of global variable */
  691. if (die_find_variable_at(cu_die, var_name, pc, &var_die) &&
  692. check_variable(dloc, &var_die, type_die, DWARF_REG_PC, *var_offset,
  693. /*is_fbreg=*/false) == PERF_TMR_OK)
  694. goto ok;
  695. return false;
  696. ok:
  697. /* The address should point to the start of the variable */
  698. global_var__add(dloc, var_addr - *var_offset, var_name, type_die);
  699. return true;
  700. }
  701. static bool die_is_same(Dwarf_Die *die_a, Dwarf_Die *die_b)
  702. {
  703. return (die_a->cu == die_b->cu) && (die_a->addr == die_b->addr);
  704. }
  705. /**
  706. * update_var_state - Update type state using given variables
  707. * @state: type state table
  708. * @dloc: data location info
  709. * @addr: instruction address to match with variable
  710. * @insn_offset: instruction offset (for debug)
  711. * @var_types: list of variables with type info
  712. *
  713. * This function fills the @state table using @var_types info. Each variable
  714. * is used only at the given location and updates an entry in the table.
  715. */
  716. static void update_var_state(struct type_state *state, struct data_loc_info *dloc,
  717. u64 addr, u64 insn_offset, struct die_var_type *var_types)
  718. {
  719. Dwarf_Die mem_die;
  720. struct die_var_type *var;
  721. int fbreg = dloc->fbreg;
  722. int fb_offset = 0;
  723. if (dloc->fb_cfa) {
  724. if (die_get_cfa(dloc->di->dbg, addr, &fbreg, &fb_offset) < 0)
  725. fbreg = -1;
  726. }
  727. for (var = var_types; var != NULL; var = var->next) {
  728. if (var->addr != addr)
  729. continue;
  730. /* Get the type DIE using the offset */
  731. if (!dwarf_offdie(dloc->di->dbg, var->die_off, &mem_die))
  732. continue;
  733. if (var->reg == DWARF_REG_FB || var->reg == fbreg || var->reg == state->stack_reg) {
  734. int offset = var->offset;
  735. struct type_state_stack *stack;
  736. /* If the reg location holds the pointer value, dereference the type */
  737. if (!var->is_reg_var_addr && is_pointer_type(&mem_die) &&
  738. __die_get_real_type(&mem_die, &mem_die) == NULL)
  739. continue;
  740. if (var->reg != DWARF_REG_FB)
  741. offset -= fb_offset;
  742. stack = find_stack_state(state, offset);
  743. if (stack && stack->kind == TSR_KIND_TYPE &&
  744. !is_better_type(&stack->type, &mem_die))
  745. continue;
  746. findnew_stack_state(state, offset, TSR_KIND_TYPE,
  747. &mem_die, /*ptr_offset=*/0);
  748. if (var->reg == state->stack_reg) {
  749. pr_debug_dtp("var [%"PRIx64"] %#x(reg%d)",
  750. insn_offset, offset, state->stack_reg);
  751. } else {
  752. pr_debug_dtp("var [%"PRIx64"] -%#x(stack)",
  753. insn_offset, -offset);
  754. }
  755. pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
  756. } else if (has_reg_type(state, var->reg)) {
  757. struct type_state_reg *reg;
  758. Dwarf_Die orig_type;
  759. reg = &state->regs[var->reg];
  760. if (reg->ok && reg->kind == TSR_KIND_TYPE &&
  761. (!is_better_type(&reg->type, &mem_die) || var->is_reg_var_addr))
  762. continue;
  763. /* Handle address registers with TSR_KIND_POINTER */
  764. if (var->is_reg_var_addr) {
  765. if (reg->ok && reg->kind == TSR_KIND_POINTER &&
  766. !is_better_type(&reg->type, &mem_die))
  767. continue;
  768. reg->offset = -var->offset;
  769. reg->type = mem_die;
  770. reg->kind = TSR_KIND_POINTER;
  771. reg->ok = true;
  772. pr_debug_dtp("var [%"PRIx64"] reg%d addr offset %x",
  773. insn_offset, var->reg, var->offset);
  774. pr_debug_type_name(&mem_die, TSR_KIND_POINTER);
  775. continue;
  776. }
  777. orig_type = reg->type;
  778. /*
  779. * var->offset + reg value is the beginning of the struct
  780. * reg->offset is the offset the reg points
  781. */
  782. reg->offset = -var->offset;
  783. reg->type = mem_die;
  784. reg->kind = TSR_KIND_TYPE;
  785. reg->ok = true;
  786. pr_debug_dtp("var [%"PRIx64"] reg%d offset %x",
  787. insn_offset, var->reg, var->offset);
  788. pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
  789. /*
  790. * If this register is directly copied from another and it gets a
  791. * better type, also update the type of the source register. This
  792. * is usually the case of container_of() macro with offset of 0.
  793. */
  794. if (has_reg_type(state, reg->copied_from)) {
  795. struct type_state_reg *copy_reg;
  796. copy_reg = &state->regs[reg->copied_from];
  797. /* TODO: check if type is compatible or embedded */
  798. if (!copy_reg->ok || (copy_reg->kind != TSR_KIND_TYPE) ||
  799. !die_is_same(&copy_reg->type, &orig_type) ||
  800. !is_better_type(&copy_reg->type, &mem_die))
  801. continue;
  802. copy_reg->type = mem_die;
  803. pr_debug_dtp("var [%"PRIx64"] copyback reg%d",
  804. insn_offset, reg->copied_from);
  805. pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
  806. }
  807. }
  808. }
  809. }
  810. /**
  811. * update_insn_state - Update type state for an instruction
  812. * @state: type state table
  813. * @dloc: data location info
  814. * @cu_die: compile unit debug entry
  815. * @dl: disasm line for the instruction
  816. *
  817. * This function updates the @state table for the target operand of the
  818. * instruction at @dl if it transfers the type like MOV on x86. Since it
  819. * tracks the type, it won't care about the values like in arithmetic
  820. * instructions like ADD/SUB/MUL/DIV and INC/DEC.
  821. *
  822. * Note that ops->reg2 is only available when both mem_ref and multi_regs
  823. * are true.
  824. */
  825. static void update_insn_state(struct type_state *state, struct data_loc_info *dloc,
  826. Dwarf_Die *cu_die, struct disasm_line *dl)
  827. {
  828. if (dloc->arch->update_insn_state)
  829. dloc->arch->update_insn_state(state, dloc, cu_die, dl);
  830. }
  831. /*
  832. * Prepend this_blocks (from the outer scope) to full_blocks, removing
  833. * duplicate disasm line.
  834. */
  835. static void prepend_basic_blocks(struct list_head *this_blocks,
  836. struct list_head *full_blocks)
  837. {
  838. struct annotated_basic_block *first_bb, *last_bb;
  839. last_bb = list_last_entry(this_blocks, typeof(*last_bb), list);
  840. first_bb = list_first_entry(full_blocks, typeof(*first_bb), list);
  841. if (list_empty(full_blocks))
  842. goto out;
  843. /* Last insn in this_blocks should be same as first insn in full_blocks */
  844. if (last_bb->end != first_bb->begin) {
  845. pr_debug("prepend basic blocks: mismatched disasm line %"PRIx64" -> %"PRIx64"\n",
  846. last_bb->end->al.offset, first_bb->begin->al.offset);
  847. goto out;
  848. }
  849. /* Is the basic block have only one disasm_line? */
  850. if (last_bb->begin == last_bb->end) {
  851. list_del(&last_bb->list);
  852. free(last_bb);
  853. goto out;
  854. }
  855. /* Point to the insn before the last when adding this block to full_blocks */
  856. last_bb->end = list_prev_entry(last_bb->end, al.node);
  857. out:
  858. list_splice(this_blocks, full_blocks);
  859. }
  860. static void delete_basic_blocks(struct list_head *basic_blocks)
  861. {
  862. struct annotated_basic_block *bb, *tmp;
  863. list_for_each_entry_safe(bb, tmp, basic_blocks, list) {
  864. list_del(&bb->list);
  865. free(bb);
  866. }
  867. }
  868. /* Make sure all variables have a valid start address */
  869. static void fixup_var_address(struct die_var_type *var_types, u64 addr)
  870. {
  871. while (var_types) {
  872. /*
  873. * Some variables have no address range meaning it's always
  874. * available in the whole scope. Let's adjust the start
  875. * address to the start of the scope.
  876. */
  877. if (var_types->addr == 0)
  878. var_types->addr = addr;
  879. var_types = var_types->next;
  880. }
  881. }
  882. static void delete_var_types(struct die_var_type *var_types)
  883. {
  884. while (var_types) {
  885. struct die_var_type *next = var_types->next;
  886. free(var_types);
  887. var_types = next;
  888. }
  889. }
  890. /* should match to is_stack_canary() in util/annotate.c */
  891. static void setup_stack_canary(struct data_loc_info *dloc)
  892. {
  893. if (arch__is_x86(dloc->arch)) {
  894. dloc->op->segment = INSN_SEG_X86_GS;
  895. dloc->op->imm = true;
  896. dloc->op->offset = 40;
  897. }
  898. }
  899. /*
  900. * It's at the target address, check if it has a matching type.
  901. * It returns PERF_TMR_BAIL_OUT when it looks up per-cpu variables which
  902. * are similar to global variables and no additional info is needed.
  903. */
  904. static enum type_match_result check_matching_type(struct type_state *state,
  905. struct data_loc_info *dloc,
  906. Dwarf_Die *cu_die,
  907. struct disasm_line *dl,
  908. Dwarf_Die *type_die)
  909. {
  910. Dwarf_Word size;
  911. u32 insn_offset = dl->al.offset;
  912. int reg = dloc->op->reg1;
  913. int offset = dloc->op->offset;
  914. const char *offset_sign = "";
  915. bool retry = true;
  916. if (offset < 0) {
  917. offset = -offset;
  918. offset_sign = "-";
  919. }
  920. again:
  921. pr_debug_dtp("chk [%x] reg%d offset=%s%#x ok=%d kind=%d ",
  922. insn_offset, reg, offset_sign, offset,
  923. state->regs[reg].ok, state->regs[reg].kind);
  924. if (!state->regs[reg].ok)
  925. goto check_non_register;
  926. if (state->regs[reg].kind == TSR_KIND_TYPE) {
  927. Dwarf_Die sized_type;
  928. struct strbuf sb;
  929. strbuf_init(&sb, 32);
  930. die_get_typename_from_type(&state->regs[reg].type, &sb);
  931. pr_debug_dtp("(%s)", sb.buf);
  932. strbuf_release(&sb);
  933. /*
  934. * Normal registers should hold a pointer (or array) to
  935. * dereference a memory location.
  936. */
  937. if (!is_pointer_type(&state->regs[reg].type)) {
  938. if (dloc->op->offset < 0 && reg != state->stack_reg)
  939. goto check_kernel;
  940. return PERF_TMR_NO_POINTER;
  941. }
  942. /* Remove the pointer and get the target type */
  943. if (__die_get_real_type(&state->regs[reg].type, type_die) == NULL)
  944. return PERF_TMR_NO_POINTER;
  945. dloc->type_offset = dloc->op->offset + state->regs[reg].offset;
  946. if (dwarf_tag(type_die) == DW_TAG_typedef)
  947. die_get_real_type(type_die, &sized_type);
  948. else
  949. sized_type = *type_die;
  950. /* Get the size of the actual type */
  951. if (dwarf_aggregate_size(&sized_type, &size) < 0 ||
  952. (unsigned)dloc->type_offset >= size)
  953. return PERF_TMR_BAD_OFFSET;
  954. return PERF_TMR_OK;
  955. }
  956. if (state->regs[reg].kind == TSR_KIND_POINTER) {
  957. struct strbuf sb;
  958. strbuf_init(&sb, 32);
  959. die_get_typename_from_type(&state->regs[reg].type, &sb);
  960. pr_debug_dtp("(ptr->%s)", sb.buf);
  961. strbuf_release(&sb);
  962. /*
  963. * Register holds a pointer (address) to the target variable.
  964. * The type is the type of the variable it points to.
  965. */
  966. *type_die = state->regs[reg].type;
  967. dloc->type_offset = dloc->op->offset + state->regs[reg].offset;
  968. /* Get the size of the actual type */
  969. if (dwarf_aggregate_size(type_die, &size) < 0 ||
  970. (unsigned)dloc->type_offset >= size)
  971. return PERF_TMR_BAD_OFFSET;
  972. return PERF_TMR_OK;
  973. }
  974. if (state->regs[reg].kind == TSR_KIND_PERCPU_POINTER) {
  975. pr_debug_dtp("percpu ptr");
  976. /*
  977. * It's actaully pointer but the address was calculated using
  978. * some arithmetic. So it points to the actual type already.
  979. */
  980. *type_die = state->regs[reg].type;
  981. dloc->type_offset = dloc->op->offset;
  982. /* Get the size of the actual type */
  983. if (dwarf_aggregate_size(type_die, &size) < 0 ||
  984. (unsigned)dloc->type_offset >= size)
  985. return PERF_TMR_BAIL_OUT;
  986. return PERF_TMR_OK;
  987. }
  988. if (state->regs[reg].kind == TSR_KIND_CANARY) {
  989. pr_debug_dtp("stack canary");
  990. /*
  991. * This is a saved value of the stack canary which will be handled
  992. * in the outer logic when it returns failure here. Pretend it's
  993. * from the stack canary directly.
  994. */
  995. setup_stack_canary(dloc);
  996. return PERF_TMR_BAIL_OUT;
  997. }
  998. if (state->regs[reg].kind == TSR_KIND_PERCPU_BASE) {
  999. u64 var_addr = dloc->op->offset;
  1000. int var_offset;
  1001. pr_debug_dtp("percpu var");
  1002. if (dloc->op->multi_regs) {
  1003. int reg2 = dloc->op->reg2;
  1004. if (dloc->op->reg2 == reg)
  1005. reg2 = dloc->op->reg1;
  1006. if (has_reg_type(state, reg2) && state->regs[reg2].ok &&
  1007. state->regs[reg2].kind == TSR_KIND_CONST)
  1008. var_addr += state->regs[reg2].imm_value;
  1009. }
  1010. if (get_global_var_type(cu_die, dloc, dloc->ip, var_addr,
  1011. &var_offset, type_die)) {
  1012. dloc->type_offset = var_offset;
  1013. return PERF_TMR_OK;
  1014. }
  1015. /* No need to retry per-cpu (global) variables */
  1016. return PERF_TMR_BAIL_OUT;
  1017. }
  1018. check_non_register:
  1019. if (reg == dloc->fbreg || reg == state->stack_reg) {
  1020. struct type_state_stack *stack;
  1021. pr_debug_dtp("%s", reg == dloc->fbreg ? "fbreg" : "stack");
  1022. stack = find_stack_state(state, dloc->type_offset);
  1023. if (stack == NULL) {
  1024. if (retry) {
  1025. pr_debug_dtp(" : retry\n");
  1026. retry = false;
  1027. /* update type info it's the first store to the stack */
  1028. update_insn_state(state, dloc, cu_die, dl);
  1029. goto again;
  1030. }
  1031. return PERF_TMR_NO_TYPE;
  1032. }
  1033. if (stack->kind == TSR_KIND_CANARY) {
  1034. setup_stack_canary(dloc);
  1035. return PERF_TMR_BAIL_OUT;
  1036. }
  1037. if (stack->kind != TSR_KIND_TYPE)
  1038. return PERF_TMR_NO_TYPE;
  1039. *type_die = stack->type;
  1040. /* Update the type offset from the start of slot */
  1041. dloc->type_offset -= stack->offset;
  1042. return PERF_TMR_OK;
  1043. }
  1044. if (dloc->fb_cfa) {
  1045. struct type_state_stack *stack;
  1046. u64 pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
  1047. int fbreg, fboff;
  1048. pr_debug_dtp("cfa");
  1049. if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
  1050. fbreg = -1;
  1051. if (reg != fbreg)
  1052. return PERF_TMR_NO_TYPE;
  1053. stack = find_stack_state(state, dloc->type_offset - fboff);
  1054. if (stack == NULL) {
  1055. if (retry) {
  1056. pr_debug_dtp(" : retry\n");
  1057. retry = false;
  1058. /* update type info it's the first store to the stack */
  1059. update_insn_state(state, dloc, cu_die, dl);
  1060. goto again;
  1061. }
  1062. return PERF_TMR_NO_TYPE;
  1063. }
  1064. if (stack->kind == TSR_KIND_CANARY) {
  1065. setup_stack_canary(dloc);
  1066. return PERF_TMR_BAIL_OUT;
  1067. }
  1068. if (stack->kind != TSR_KIND_TYPE)
  1069. return PERF_TMR_NO_TYPE;
  1070. *type_die = stack->type;
  1071. /* Update the type offset from the start of slot */
  1072. dloc->type_offset -= fboff + stack->offset;
  1073. return PERF_TMR_OK;
  1074. }
  1075. check_kernel:
  1076. if (dso__kernel(map__dso(dloc->ms->map))) {
  1077. u64 addr;
  1078. /* Direct this-cpu access like "%gs:0x34740" */
  1079. if (dloc->op->segment == INSN_SEG_X86_GS && dloc->op->imm &&
  1080. arch__is_x86(dloc->arch)) {
  1081. pr_debug_dtp("this-cpu var");
  1082. addr = dloc->op->offset;
  1083. if (get_global_var_type(cu_die, dloc, dloc->ip, addr,
  1084. &offset, type_die)) {
  1085. dloc->type_offset = offset;
  1086. return PERF_TMR_OK;
  1087. }
  1088. return PERF_TMR_BAIL_OUT;
  1089. }
  1090. /* Access to global variable like "-0x7dcf0500(,%rdx,8)" */
  1091. if (dloc->op->offset < 0 && reg != state->stack_reg) {
  1092. addr = (s64) dloc->op->offset;
  1093. if (get_global_var_type(cu_die, dloc, dloc->ip, addr,
  1094. &offset, type_die)) {
  1095. pr_debug_dtp("global var");
  1096. dloc->type_offset = offset;
  1097. return PERF_TMR_OK;
  1098. }
  1099. return PERF_TMR_BAIL_OUT;
  1100. }
  1101. }
  1102. return PERF_TMR_UNKNOWN;
  1103. }
  1104. /* Iterate instructions in basic blocks and update type table */
  1105. static enum type_match_result find_data_type_insn(struct data_loc_info *dloc,
  1106. struct list_head *basic_blocks,
  1107. struct die_var_type *var_types,
  1108. Dwarf_Die *cu_die,
  1109. Dwarf_Die *type_die)
  1110. {
  1111. struct type_state state;
  1112. struct symbol *sym = dloc->ms->sym;
  1113. struct annotation *notes = symbol__annotation(sym);
  1114. struct annotated_basic_block *bb;
  1115. enum type_match_result ret = PERF_TMR_UNKNOWN;
  1116. init_type_state(&state, dloc->arch);
  1117. list_for_each_entry(bb, basic_blocks, list) {
  1118. struct disasm_line *dl = bb->begin;
  1119. BUG_ON(bb->begin->al.offset == -1 || bb->end->al.offset == -1);
  1120. pr_debug_dtp("bb: [%"PRIx64" - %"PRIx64"]\n",
  1121. bb->begin->al.offset, bb->end->al.offset);
  1122. list_for_each_entry_from(dl, &notes->src->source, al.node) {
  1123. u64 this_ip = sym->start + dl->al.offset;
  1124. u64 addr = map__rip_2objdump(dloc->ms->map, this_ip);
  1125. /* Skip comment or debug info lines */
  1126. if (dl->al.offset == -1)
  1127. continue;
  1128. /* Update variable type at this address */
  1129. update_var_state(&state, dloc, addr, dl->al.offset, var_types);
  1130. if (this_ip == dloc->ip) {
  1131. ret = check_matching_type(&state, dloc,
  1132. cu_die, dl, type_die);
  1133. pr_debug_dtp(" : %s\n", match_result_str(ret));
  1134. goto out;
  1135. }
  1136. /* Update type table after processing the instruction */
  1137. update_insn_state(&state, dloc, cu_die, dl);
  1138. if (dl == bb->end)
  1139. break;
  1140. }
  1141. }
  1142. out:
  1143. exit_type_state(&state);
  1144. return ret;
  1145. }
  1146. static int arch_supports_insn_tracking(struct data_loc_info *dloc)
  1147. {
  1148. if ((arch__is_x86(dloc->arch)) || (arch__is_powerpc(dloc->arch)))
  1149. return 1;
  1150. return 0;
  1151. }
  1152. /*
  1153. * Construct a list of basic blocks for each scope with variables and try to find
  1154. * the data type by updating a type state table through instructions.
  1155. */
  1156. static enum type_match_result find_data_type_block(struct data_loc_info *dloc,
  1157. Dwarf_Die *cu_die,
  1158. Dwarf_Die *scopes,
  1159. int nr_scopes,
  1160. Dwarf_Die *type_die)
  1161. {
  1162. LIST_HEAD(basic_blocks);
  1163. struct die_var_type *var_types = NULL;
  1164. u64 src_ip, dst_ip, prev_dst_ip;
  1165. enum type_match_result ret = PERF_TMR_UNKNOWN;
  1166. /* TODO: other architecture support */
  1167. if (!arch_supports_insn_tracking(dloc))
  1168. return PERF_TMR_BAIL_OUT;
  1169. prev_dst_ip = dst_ip = dloc->ip;
  1170. for (int i = nr_scopes - 1; i >= 0; i--) {
  1171. Dwarf_Addr base, start, end;
  1172. LIST_HEAD(this_blocks);
  1173. if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0)
  1174. break;
  1175. pr_debug_dtp("scope: [%d/%d] ", i + 1, nr_scopes);
  1176. pr_debug_scope(&scopes[i]);
  1177. src_ip = map__objdump_2rip(dloc->ms->map, start);
  1178. again:
  1179. /* Get basic blocks for this scope */
  1180. if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip,
  1181. &this_blocks) < 0) {
  1182. /* Try previous block if they are not connected */
  1183. if (prev_dst_ip != dst_ip) {
  1184. dst_ip = prev_dst_ip;
  1185. goto again;
  1186. }
  1187. pr_debug_dtp("cannot find a basic block from %"PRIx64" to %"PRIx64"\n",
  1188. src_ip - dloc->ms->sym->start,
  1189. dst_ip - dloc->ms->sym->start);
  1190. continue;
  1191. }
  1192. prepend_basic_blocks(&this_blocks, &basic_blocks);
  1193. /* Get variable info for this scope and add to var_types list */
  1194. die_collect_vars(&scopes[i], &var_types);
  1195. fixup_var_address(var_types, start);
  1196. /* Find from start of this scope to the target instruction */
  1197. ret = find_data_type_insn(dloc, &basic_blocks, var_types,
  1198. cu_die, type_die);
  1199. if (ret == PERF_TMR_OK) {
  1200. char buf[64];
  1201. int offset = dloc->op->offset;
  1202. const char *offset_sign = "";
  1203. if (offset < 0) {
  1204. offset = -offset;
  1205. offset_sign = "-";
  1206. }
  1207. if (dloc->op->multi_regs)
  1208. snprintf(buf, sizeof(buf), "reg%d, reg%d",
  1209. dloc->op->reg1, dloc->op->reg2);
  1210. else
  1211. snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
  1212. pr_debug_dtp("found by insn track: %s%#x(%s) type-offset=%#x\n",
  1213. offset_sign, offset, buf, dloc->type_offset);
  1214. break;
  1215. }
  1216. if (ret == PERF_TMR_BAIL_OUT)
  1217. break;
  1218. /* Go up to the next scope and find blocks to the start */
  1219. prev_dst_ip = dst_ip;
  1220. dst_ip = src_ip;
  1221. }
  1222. delete_basic_blocks(&basic_blocks);
  1223. delete_var_types(var_types);
  1224. return ret;
  1225. }
  1226. /* The result will be saved in @type_die */
  1227. static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die)
  1228. {
  1229. struct annotated_op_loc *loc = dloc->op;
  1230. Dwarf_Die cu_die, var_die;
  1231. Dwarf_Die *scopes = NULL;
  1232. int reg, offset = loc->offset;
  1233. int ret = -1;
  1234. int i, nr_scopes;
  1235. int fbreg = -1;
  1236. int fb_offset = 0;
  1237. bool is_fbreg = false;
  1238. bool found = false;
  1239. u64 pc;
  1240. char buf[64];
  1241. enum type_match_result result = PERF_TMR_UNKNOWN;
  1242. const char *offset_sign = "";
  1243. if (dloc->op->multi_regs)
  1244. snprintf(buf, sizeof(buf), "reg%d, reg%d", dloc->op->reg1, dloc->op->reg2);
  1245. else if (dloc->op->reg1 == DWARF_REG_PC)
  1246. snprintf(buf, sizeof(buf), "PC");
  1247. else
  1248. snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1);
  1249. if (offset < 0) {
  1250. offset = -offset;
  1251. offset_sign = "-";
  1252. }
  1253. pr_debug_dtp("-----------------------------------------------------------\n");
  1254. pr_debug_dtp("find data type for %s%#x(%s) at %s+%#"PRIx64"\n",
  1255. offset_sign, offset, buf,
  1256. dloc->ms->sym->name, dloc->ip - dloc->ms->sym->start);
  1257. /*
  1258. * IP is a relative instruction address from the start of the map, as
  1259. * it can be randomized/relocated, it needs to translate to PC which is
  1260. * a file address for DWARF processing.
  1261. */
  1262. pc = map__rip_2objdump(dloc->ms->map, dloc->ip);
  1263. /* Get a compile_unit for this address */
  1264. if (!find_cu_die(dloc->di, pc, &cu_die)) {
  1265. pr_debug_dtp("cannot find CU for address %"PRIx64"\n", pc);
  1266. ann_data_stat.no_cuinfo++;
  1267. return -1;
  1268. }
  1269. reg = loc->reg1;
  1270. offset = loc->offset;
  1271. pr_debug_dtp("CU for %s (die:%#lx)\n",
  1272. dwarf_diename(&cu_die), (long)dwarf_dieoffset(&cu_die));
  1273. if (reg == DWARF_REG_PC) {
  1274. if (get_global_var_type(&cu_die, dloc, dloc->ip, dloc->var_addr,
  1275. &offset, type_die)) {
  1276. dloc->type_offset = offset;
  1277. pr_debug_dtp("found by addr=%#"PRIx64" type_offset=%#x\n",
  1278. dloc->var_addr, offset);
  1279. pr_debug_type_name(type_die, TSR_KIND_TYPE);
  1280. found = true;
  1281. goto out;
  1282. }
  1283. }
  1284. /* Get a list of nested scopes - i.e. (inlined) functions and blocks. */
  1285. nr_scopes = die_get_scopes(&cu_die, pc, &scopes);
  1286. if (reg != DWARF_REG_PC && dwarf_hasattr(&scopes[0], DW_AT_frame_base)) {
  1287. Dwarf_Attribute attr;
  1288. Dwarf_Block block;
  1289. /* Check if the 'reg' is assigned as frame base register */
  1290. if (dwarf_attr(&scopes[0], DW_AT_frame_base, &attr) != NULL &&
  1291. dwarf_formblock(&attr, &block) == 0 && block.length == 1) {
  1292. switch (*block.data) {
  1293. case DW_OP_reg0 ... DW_OP_reg31:
  1294. fbreg = dloc->fbreg = *block.data - DW_OP_reg0;
  1295. break;
  1296. case DW_OP_call_frame_cfa:
  1297. dloc->fb_cfa = true;
  1298. if (die_get_cfa(dloc->di->dbg, pc, &fbreg,
  1299. &fb_offset) < 0)
  1300. fbreg = -1;
  1301. break;
  1302. default:
  1303. break;
  1304. }
  1305. pr_debug_dtp("frame base: cfa=%d fbreg=%d\n",
  1306. dloc->fb_cfa, fbreg);
  1307. }
  1308. }
  1309. retry:
  1310. is_fbreg = (reg == fbreg);
  1311. if (is_fbreg)
  1312. offset = loc->offset - fb_offset;
  1313. /* Search from the inner-most scope to the outer */
  1314. for (i = nr_scopes - 1; i >= 0; i--) {
  1315. Dwarf_Die mem_die;
  1316. int type_offset = offset;
  1317. if (reg == DWARF_REG_PC) {
  1318. if (!die_find_variable_by_addr(&scopes[i], dloc->var_addr,
  1319. &var_die, &type_offset))
  1320. continue;
  1321. } else {
  1322. /* Look up variables/parameters in this scope */
  1323. if (!die_find_variable_by_reg(&scopes[i], pc, reg,
  1324. &type_offset, is_fbreg, &var_die))
  1325. continue;
  1326. }
  1327. pr_debug_dtp("found \"%s\" (die: %#lx) in scope=%d/%d (die: %#lx) ",
  1328. dwarf_diename(&var_die), (long)dwarf_dieoffset(&var_die),
  1329. i+1, nr_scopes, (long)dwarf_dieoffset(&scopes[i]));
  1330. /* Found a variable, see if it's correct */
  1331. result = check_variable(dloc, &var_die, &mem_die, reg, type_offset, is_fbreg);
  1332. if (result == PERF_TMR_OK) {
  1333. if (reg == DWARF_REG_PC) {
  1334. pr_debug_dtp("addr=%#"PRIx64" type_offset=%#x\n",
  1335. dloc->var_addr, type_offset);
  1336. } else if (reg == DWARF_REG_FB || is_fbreg) {
  1337. pr_debug_dtp("stack_offset=%#x type_offset=%#x\n",
  1338. fb_offset, type_offset);
  1339. } else {
  1340. pr_debug_dtp("type_offset=%#x\n", type_offset);
  1341. }
  1342. if (!found || is_better_type(type_die, &mem_die)) {
  1343. *type_die = mem_die;
  1344. dloc->type_offset = type_offset;
  1345. found = true;
  1346. }
  1347. } else {
  1348. pr_debug_dtp("failed: %s\n", match_result_str(result));
  1349. }
  1350. pr_debug_location(&var_die, pc, reg);
  1351. pr_debug_type_name(&mem_die, TSR_KIND_TYPE);
  1352. }
  1353. if (!found && loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) {
  1354. reg = loc->reg2;
  1355. goto retry;
  1356. }
  1357. if (!found && reg != DWARF_REG_PC) {
  1358. result = find_data_type_block(dloc, &cu_die, scopes,
  1359. nr_scopes, type_die);
  1360. if (result == PERF_TMR_OK) {
  1361. ann_data_stat.insn_track++;
  1362. found = true;
  1363. }
  1364. }
  1365. out:
  1366. pr_debug_dtp("final result: ");
  1367. if (found) {
  1368. pr_debug_type_name(type_die, TSR_KIND_TYPE);
  1369. ret = 0;
  1370. } else {
  1371. switch (result) {
  1372. case PERF_TMR_NO_TYPE:
  1373. case PERF_TMR_NO_POINTER:
  1374. pr_debug_dtp("%s\n", match_result_str(result));
  1375. ann_data_stat.no_typeinfo++;
  1376. break;
  1377. case PERF_TMR_NO_SIZE:
  1378. pr_debug_dtp("%s\n", match_result_str(result));
  1379. ann_data_stat.invalid_size++;
  1380. break;
  1381. case PERF_TMR_BAD_OFFSET:
  1382. pr_debug_dtp("%s\n", match_result_str(result));
  1383. ann_data_stat.bad_offset++;
  1384. break;
  1385. case PERF_TMR_UNKNOWN:
  1386. case PERF_TMR_BAIL_OUT:
  1387. case PERF_TMR_OK: /* should not reach here */
  1388. default:
  1389. pr_debug_dtp("no variable found\n");
  1390. ann_data_stat.no_var++;
  1391. break;
  1392. }
  1393. ret = -1;
  1394. }
  1395. free(scopes);
  1396. return ret;
  1397. }
  1398. /**
  1399. * find_data_type - Return a data type at the location
  1400. * @dloc: data location
  1401. *
  1402. * This functions searches the debug information of the binary to get the data
  1403. * type it accesses. The exact location is expressed by (ip, reg, offset)
  1404. * for pointer variables or (ip, addr) for global variables. Note that global
  1405. * variables might update the @dloc->type_offset after finding the start of the
  1406. * variable. If it cannot find a global variable by address, it tried to find
  1407. * a declaration of the variable using var_name. In that case, @dloc->offset
  1408. * won't be updated.
  1409. *
  1410. * It return %NULL if not found.
  1411. */
  1412. struct annotated_data_type *find_data_type(struct data_loc_info *dloc)
  1413. {
  1414. struct dso *dso = map__dso(dloc->ms->map);
  1415. Dwarf_Die type_die;
  1416. /*
  1417. * The type offset is the same as instruction offset by default.
  1418. * But when finding a global variable, the offset won't be valid.
  1419. */
  1420. dloc->type_offset = dloc->op->offset;
  1421. dloc->fbreg = -1;
  1422. if (find_data_type_die(dloc, &type_die) < 0)
  1423. return NULL;
  1424. return dso__findnew_data_type(dso, &type_die);
  1425. }
  1426. static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries)
  1427. {
  1428. int i;
  1429. size_t sz = sizeof(struct type_hist);
  1430. sz += sizeof(struct type_hist_entry) * adt->self.size;
  1431. /* Allocate a table of pointers for each event */
  1432. adt->histograms = calloc(nr_entries, sizeof(*adt->histograms));
  1433. if (adt->histograms == NULL)
  1434. return -ENOMEM;
  1435. /*
  1436. * Each histogram is allocated for the whole size of the type.
  1437. * TODO: Probably we can move the histogram to members.
  1438. */
  1439. for (i = 0; i < nr_entries; i++) {
  1440. adt->histograms[i] = zalloc(sz);
  1441. if (adt->histograms[i] == NULL)
  1442. goto err;
  1443. }
  1444. adt->nr_histograms = nr_entries;
  1445. return 0;
  1446. err:
  1447. while (--i >= 0)
  1448. zfree(&(adt->histograms[i]));
  1449. zfree(&adt->histograms);
  1450. return -ENOMEM;
  1451. }
  1452. static void delete_data_type_histograms(struct annotated_data_type *adt)
  1453. {
  1454. for (int i = 0; i < adt->nr_histograms; i++)
  1455. zfree(&(adt->histograms[i]));
  1456. zfree(&adt->histograms);
  1457. adt->nr_histograms = 0;
  1458. }
  1459. void annotated_data_type__tree_delete(struct rb_root *root)
  1460. {
  1461. struct annotated_data_type *pos;
  1462. while (!RB_EMPTY_ROOT(root)) {
  1463. struct rb_node *node = rb_first(root);
  1464. rb_erase(node, root);
  1465. pos = rb_entry(node, struct annotated_data_type, node);
  1466. delete_members(&pos->self);
  1467. delete_data_type_histograms(pos);
  1468. zfree(&pos->self.type_name);
  1469. free(pos);
  1470. }
  1471. }
  1472. /**
  1473. * annotated_data_type__update_samples - Update histogram
  1474. * @adt: Data type to update
  1475. * @evsel: Event to update
  1476. * @offset: Offset in the type
  1477. * @nr_samples: Number of samples at this offset
  1478. * @period: Event count at this offset
  1479. *
  1480. * This function updates type histogram at @ofs for @evsel. Samples are
  1481. * aggregated before calling this function so it can be called with more
  1482. * than one samples at a certain offset.
  1483. */
  1484. int annotated_data_type__update_samples(struct annotated_data_type *adt,
  1485. struct evsel *evsel, int offset,
  1486. int nr_samples, u64 period)
  1487. {
  1488. struct type_hist *h;
  1489. if (adt == NULL)
  1490. return 0;
  1491. if (adt->histograms == NULL) {
  1492. int nr = evsel->evlist->core.nr_entries;
  1493. if (alloc_data_type_histograms(adt, nr) < 0)
  1494. return -1;
  1495. }
  1496. if (offset < 0 || offset >= adt->self.size)
  1497. return -1;
  1498. h = adt->histograms[evsel->core.idx];
  1499. h->nr_samples += nr_samples;
  1500. h->addr[offset].nr_samples += nr_samples;
  1501. h->period += period;
  1502. h->addr[offset].period += period;
  1503. return 0;
  1504. }
  1505. static void print_annotated_data_header(struct hist_entry *he, struct evsel *evsel)
  1506. {
  1507. struct dso *dso = map__dso(he->ms.map);
  1508. int nr_members = 1;
  1509. int nr_samples = he->stat.nr_events;
  1510. int width = 7;
  1511. const char *val_hdr = "Percent";
  1512. if (evsel__is_group_event(evsel)) {
  1513. struct hist_entry *pair;
  1514. list_for_each_entry(pair, &he->pairs.head, pairs.node)
  1515. nr_samples += pair->stat.nr_events;
  1516. }
  1517. printf("Annotate type: '%s' in %s (%d samples):\n",
  1518. he->mem_type->self.type_name, dso__name(dso), nr_samples);
  1519. if (evsel__is_group_event(evsel)) {
  1520. struct evsel *pos;
  1521. int i = 0;
  1522. nr_members = 0;
  1523. for_each_group_evsel(pos, evsel) {
  1524. if (symbol_conf.skip_empty &&
  1525. evsel__hists(pos)->stats.nr_samples == 0)
  1526. continue;
  1527. printf(" event[%d] = %s\n", i++, pos->name);
  1528. nr_members++;
  1529. }
  1530. }
  1531. if (symbol_conf.show_total_period) {
  1532. width = 11;
  1533. val_hdr = "Period";
  1534. } else if (symbol_conf.show_nr_samples) {
  1535. width = 7;
  1536. val_hdr = "Samples";
  1537. }
  1538. printf("============================================================================\n");
  1539. printf("%*s %10s %10s %s\n", (width + 1) * nr_members, val_hdr,
  1540. "offset", "size", "field");
  1541. }
  1542. static void print_annotated_data_value(struct type_hist *h, u64 period, int nr_samples)
  1543. {
  1544. double percent = h->period ? (100.0 * period / h->period) : 0;
  1545. const char *color = get_percent_color(percent);
  1546. if (symbol_conf.show_total_period)
  1547. color_fprintf(stdout, color, " %11" PRIu64, period);
  1548. else if (symbol_conf.show_nr_samples)
  1549. color_fprintf(stdout, color, " %7d", nr_samples);
  1550. else
  1551. color_fprintf(stdout, color, " %7.2f", percent);
  1552. }
  1553. static void print_annotated_data_type(struct annotated_data_type *mem_type,
  1554. struct annotated_member *member,
  1555. struct evsel *evsel, int indent)
  1556. {
  1557. struct annotated_member *child;
  1558. struct type_hist *h = mem_type->histograms[evsel->core.idx];
  1559. int i, nr_events = 0, samples = 0;
  1560. u64 period = 0;
  1561. int width = symbol_conf.show_total_period ? 11 : 7;
  1562. struct evsel *pos;
  1563. for_each_group_evsel(pos, evsel) {
  1564. h = mem_type->histograms[pos->core.idx];
  1565. if (symbol_conf.skip_empty &&
  1566. evsel__hists(pos)->stats.nr_samples == 0)
  1567. continue;
  1568. samples = 0;
  1569. period = 0;
  1570. for (i = 0; i < member->size; i++) {
  1571. samples += h->addr[member->offset + i].nr_samples;
  1572. period += h->addr[member->offset + i].period;
  1573. }
  1574. print_annotated_data_value(h, period, samples);
  1575. nr_events++;
  1576. }
  1577. printf(" %#10x %#10x %*s%s\t%s",
  1578. member->offset, member->size, indent, "", member->type_name,
  1579. member->var_name ?: "");
  1580. if (!list_empty(&member->children))
  1581. printf(" {\n");
  1582. list_for_each_entry(child, &member->children, node)
  1583. print_annotated_data_type(mem_type, child, evsel, indent + 4);
  1584. if (!list_empty(&member->children))
  1585. printf("%*s}", (width + 1) * nr_events + 24 + indent, "");
  1586. printf(";\n");
  1587. }
  1588. int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel)
  1589. {
  1590. print_annotated_data_header(he, evsel);
  1591. print_annotated_data_type(he->mem_type, &he->mem_type->self, evsel, 0);
  1592. printf("\n");
  1593. /* move to the next entry */
  1594. return '>';
  1595. }