auxtrace.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. /*
  2. * auxtrace.c: AUX area trace support
  3. * Copyright (c) 2013-2015, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <sys/types.h>
  16. #include <sys/mman.h>
  17. #include <stdbool.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include <errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/perf_event.h>
  24. #include <linux/types.h>
  25. #include <linux/bitops.h>
  26. #include <linux/log2.h>
  27. #include <linux/string.h>
  28. #include <sys/param.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <limits.h>
  33. #include <errno.h>
  34. #include <linux/list.h>
  35. #include "../perf.h"
  36. #include "util.h"
  37. #include "evlist.h"
  38. #include "dso.h"
  39. #include "map.h"
  40. #include "pmu.h"
  41. #include "evsel.h"
  42. #include "cpumap.h"
  43. #include "thread_map.h"
  44. #include "asm/bug.h"
  45. #include "symbol/kallsyms.h"
  46. #include "auxtrace.h"
  47. #include <linux/hash.h>
  48. #include "event.h"
  49. #include "session.h"
  50. #include "debug.h"
  51. #include <subcmd/parse-options.h>
  52. #include "intel-pt.h"
  53. #include "intel-bts.h"
  54. int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
  55. struct auxtrace_mmap_params *mp,
  56. void *userpg, int fd)
  57. {
  58. struct perf_event_mmap_page *pc = userpg;
  59. WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
  60. mm->userpg = userpg;
  61. mm->mask = mp->mask;
  62. mm->len = mp->len;
  63. mm->prev = 0;
  64. mm->idx = mp->idx;
  65. mm->tid = mp->tid;
  66. mm->cpu = mp->cpu;
  67. if (!mp->len) {
  68. mm->base = NULL;
  69. return 0;
  70. }
  71. #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
  72. pr_err("Cannot use AUX area tracing mmaps\n");
  73. return -1;
  74. #endif
  75. pc->aux_offset = mp->offset;
  76. pc->aux_size = mp->len;
  77. mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
  78. if (mm->base == MAP_FAILED) {
  79. pr_debug2("failed to mmap AUX area\n");
  80. mm->base = NULL;
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
  86. {
  87. if (mm->base) {
  88. munmap(mm->base, mm->len);
  89. mm->base = NULL;
  90. }
  91. }
  92. void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
  93. off_t auxtrace_offset,
  94. unsigned int auxtrace_pages,
  95. bool auxtrace_overwrite)
  96. {
  97. if (auxtrace_pages) {
  98. mp->offset = auxtrace_offset;
  99. mp->len = auxtrace_pages * (size_t)page_size;
  100. mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
  101. mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
  102. pr_debug2("AUX area mmap length %zu\n", mp->len);
  103. } else {
  104. mp->len = 0;
  105. }
  106. }
  107. void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
  108. struct perf_evlist *evlist, int idx,
  109. bool per_cpu)
  110. {
  111. mp->idx = idx;
  112. if (per_cpu) {
  113. mp->cpu = evlist->cpus->map[idx];
  114. if (evlist->threads)
  115. mp->tid = thread_map__pid(evlist->threads, 0);
  116. else
  117. mp->tid = -1;
  118. } else {
  119. mp->cpu = -1;
  120. mp->tid = thread_map__pid(evlist->threads, idx);
  121. }
  122. }
  123. #define AUXTRACE_INIT_NR_QUEUES 32
  124. static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
  125. {
  126. struct auxtrace_queue *queue_array;
  127. unsigned int max_nr_queues, i;
  128. max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
  129. if (nr_queues > max_nr_queues)
  130. return NULL;
  131. queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
  132. if (!queue_array)
  133. return NULL;
  134. for (i = 0; i < nr_queues; i++) {
  135. INIT_LIST_HEAD(&queue_array[i].head);
  136. queue_array[i].priv = NULL;
  137. }
  138. return queue_array;
  139. }
  140. int auxtrace_queues__init(struct auxtrace_queues *queues)
  141. {
  142. queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
  143. queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
  144. if (!queues->queue_array)
  145. return -ENOMEM;
  146. return 0;
  147. }
  148. static int auxtrace_queues__grow(struct auxtrace_queues *queues,
  149. unsigned int new_nr_queues)
  150. {
  151. unsigned int nr_queues = queues->nr_queues;
  152. struct auxtrace_queue *queue_array;
  153. unsigned int i;
  154. if (!nr_queues)
  155. nr_queues = AUXTRACE_INIT_NR_QUEUES;
  156. while (nr_queues && nr_queues < new_nr_queues)
  157. nr_queues <<= 1;
  158. if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
  159. return -EINVAL;
  160. queue_array = auxtrace_alloc_queue_array(nr_queues);
  161. if (!queue_array)
  162. return -ENOMEM;
  163. for (i = 0; i < queues->nr_queues; i++) {
  164. list_splice_tail(&queues->queue_array[i].head,
  165. &queue_array[i].head);
  166. queue_array[i].priv = queues->queue_array[i].priv;
  167. }
  168. queues->nr_queues = nr_queues;
  169. queues->queue_array = queue_array;
  170. return 0;
  171. }
  172. static void *auxtrace_copy_data(u64 size, struct perf_session *session)
  173. {
  174. int fd = perf_data_file__fd(session->file);
  175. void *p;
  176. ssize_t ret;
  177. if (size > SSIZE_MAX)
  178. return NULL;
  179. p = malloc(size);
  180. if (!p)
  181. return NULL;
  182. ret = readn(fd, p, size);
  183. if (ret != (ssize_t)size) {
  184. free(p);
  185. return NULL;
  186. }
  187. return p;
  188. }
  189. static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
  190. unsigned int idx,
  191. struct auxtrace_buffer *buffer)
  192. {
  193. struct auxtrace_queue *queue;
  194. int err;
  195. if (idx >= queues->nr_queues) {
  196. err = auxtrace_queues__grow(queues, idx + 1);
  197. if (err)
  198. return err;
  199. }
  200. queue = &queues->queue_array[idx];
  201. if (!queue->set) {
  202. queue->set = true;
  203. queue->tid = buffer->tid;
  204. queue->cpu = buffer->cpu;
  205. } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
  206. pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
  207. queue->cpu, queue->tid, buffer->cpu, buffer->tid);
  208. return -EINVAL;
  209. }
  210. buffer->buffer_nr = queues->next_buffer_nr++;
  211. list_add_tail(&buffer->list, &queue->head);
  212. queues->new_data = true;
  213. queues->populated = true;
  214. return 0;
  215. }
  216. /* Limit buffers to 32MiB on 32-bit */
  217. #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
  218. static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
  219. unsigned int idx,
  220. struct auxtrace_buffer *buffer)
  221. {
  222. u64 sz = buffer->size;
  223. bool consecutive = false;
  224. struct auxtrace_buffer *b;
  225. int err;
  226. while (sz > BUFFER_LIMIT_FOR_32_BIT) {
  227. b = memdup(buffer, sizeof(struct auxtrace_buffer));
  228. if (!b)
  229. return -ENOMEM;
  230. b->size = BUFFER_LIMIT_FOR_32_BIT;
  231. b->consecutive = consecutive;
  232. err = auxtrace_queues__add_buffer(queues, idx, b);
  233. if (err) {
  234. auxtrace_buffer__free(b);
  235. return err;
  236. }
  237. buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
  238. sz -= BUFFER_LIMIT_FOR_32_BIT;
  239. consecutive = true;
  240. }
  241. buffer->size = sz;
  242. buffer->consecutive = consecutive;
  243. return 0;
  244. }
  245. static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
  246. struct perf_session *session,
  247. unsigned int idx,
  248. struct auxtrace_buffer *buffer)
  249. {
  250. if (session->one_mmap) {
  251. buffer->data = buffer->data_offset - session->one_mmap_offset +
  252. session->one_mmap_addr;
  253. } else if (perf_data_file__is_pipe(session->file)) {
  254. buffer->data = auxtrace_copy_data(buffer->size, session);
  255. if (!buffer->data)
  256. return -ENOMEM;
  257. buffer->data_needs_freeing = true;
  258. } else if (BITS_PER_LONG == 32 &&
  259. buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
  260. int err;
  261. err = auxtrace_queues__split_buffer(queues, idx, buffer);
  262. if (err)
  263. return err;
  264. }
  265. return auxtrace_queues__add_buffer(queues, idx, buffer);
  266. }
  267. int auxtrace_queues__add_event(struct auxtrace_queues *queues,
  268. struct perf_session *session,
  269. union perf_event *event, off_t data_offset,
  270. struct auxtrace_buffer **buffer_ptr)
  271. {
  272. struct auxtrace_buffer *buffer;
  273. unsigned int idx;
  274. int err;
  275. buffer = zalloc(sizeof(struct auxtrace_buffer));
  276. if (!buffer)
  277. return -ENOMEM;
  278. buffer->pid = -1;
  279. buffer->tid = event->auxtrace.tid;
  280. buffer->cpu = event->auxtrace.cpu;
  281. buffer->data_offset = data_offset;
  282. buffer->offset = event->auxtrace.offset;
  283. buffer->reference = event->auxtrace.reference;
  284. buffer->size = event->auxtrace.size;
  285. idx = event->auxtrace.idx;
  286. err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
  287. if (err)
  288. goto out_err;
  289. if (buffer_ptr)
  290. *buffer_ptr = buffer;
  291. return 0;
  292. out_err:
  293. auxtrace_buffer__free(buffer);
  294. return err;
  295. }
  296. static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
  297. struct perf_session *session,
  298. off_t file_offset, size_t sz)
  299. {
  300. union perf_event *event;
  301. int err;
  302. char buf[PERF_SAMPLE_MAX_SIZE];
  303. err = perf_session__peek_event(session, file_offset, buf,
  304. PERF_SAMPLE_MAX_SIZE, &event, NULL);
  305. if (err)
  306. return err;
  307. if (event->header.type == PERF_RECORD_AUXTRACE) {
  308. if (event->header.size < sizeof(struct auxtrace_event) ||
  309. event->header.size != sz) {
  310. err = -EINVAL;
  311. goto out;
  312. }
  313. file_offset += event->header.size;
  314. err = auxtrace_queues__add_event(queues, session, event,
  315. file_offset, NULL);
  316. }
  317. out:
  318. return err;
  319. }
  320. void auxtrace_queues__free(struct auxtrace_queues *queues)
  321. {
  322. unsigned int i;
  323. for (i = 0; i < queues->nr_queues; i++) {
  324. while (!list_empty(&queues->queue_array[i].head)) {
  325. struct auxtrace_buffer *buffer;
  326. buffer = list_entry(queues->queue_array[i].head.next,
  327. struct auxtrace_buffer, list);
  328. list_del(&buffer->list);
  329. auxtrace_buffer__free(buffer);
  330. }
  331. }
  332. zfree(&queues->queue_array);
  333. queues->nr_queues = 0;
  334. }
  335. static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
  336. unsigned int pos, unsigned int queue_nr,
  337. u64 ordinal)
  338. {
  339. unsigned int parent;
  340. while (pos) {
  341. parent = (pos - 1) >> 1;
  342. if (heap_array[parent].ordinal <= ordinal)
  343. break;
  344. heap_array[pos] = heap_array[parent];
  345. pos = parent;
  346. }
  347. heap_array[pos].queue_nr = queue_nr;
  348. heap_array[pos].ordinal = ordinal;
  349. }
  350. int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
  351. u64 ordinal)
  352. {
  353. struct auxtrace_heap_item *heap_array;
  354. if (queue_nr >= heap->heap_sz) {
  355. unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
  356. while (heap_sz <= queue_nr)
  357. heap_sz <<= 1;
  358. heap_array = realloc(heap->heap_array,
  359. heap_sz * sizeof(struct auxtrace_heap_item));
  360. if (!heap_array)
  361. return -ENOMEM;
  362. heap->heap_array = heap_array;
  363. heap->heap_sz = heap_sz;
  364. }
  365. auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
  366. return 0;
  367. }
  368. void auxtrace_heap__free(struct auxtrace_heap *heap)
  369. {
  370. zfree(&heap->heap_array);
  371. heap->heap_cnt = 0;
  372. heap->heap_sz = 0;
  373. }
  374. void auxtrace_heap__pop(struct auxtrace_heap *heap)
  375. {
  376. unsigned int pos, last, heap_cnt = heap->heap_cnt;
  377. struct auxtrace_heap_item *heap_array;
  378. if (!heap_cnt)
  379. return;
  380. heap->heap_cnt -= 1;
  381. heap_array = heap->heap_array;
  382. pos = 0;
  383. while (1) {
  384. unsigned int left, right;
  385. left = (pos << 1) + 1;
  386. if (left >= heap_cnt)
  387. break;
  388. right = left + 1;
  389. if (right >= heap_cnt) {
  390. heap_array[pos] = heap_array[left];
  391. return;
  392. }
  393. if (heap_array[left].ordinal < heap_array[right].ordinal) {
  394. heap_array[pos] = heap_array[left];
  395. pos = left;
  396. } else {
  397. heap_array[pos] = heap_array[right];
  398. pos = right;
  399. }
  400. }
  401. last = heap_cnt - 1;
  402. auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
  403. heap_array[last].ordinal);
  404. }
  405. size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
  406. struct perf_evlist *evlist)
  407. {
  408. if (itr)
  409. return itr->info_priv_size(itr, evlist);
  410. return 0;
  411. }
  412. static int auxtrace_not_supported(void)
  413. {
  414. pr_err("AUX area tracing is not supported on this architecture\n");
  415. return -EINVAL;
  416. }
  417. int auxtrace_record__info_fill(struct auxtrace_record *itr,
  418. struct perf_session *session,
  419. struct auxtrace_info_event *auxtrace_info,
  420. size_t priv_size)
  421. {
  422. if (itr)
  423. return itr->info_fill(itr, session, auxtrace_info, priv_size);
  424. return auxtrace_not_supported();
  425. }
  426. void auxtrace_record__free(struct auxtrace_record *itr)
  427. {
  428. if (itr)
  429. itr->free(itr);
  430. }
  431. int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
  432. {
  433. if (itr && itr->snapshot_start)
  434. return itr->snapshot_start(itr);
  435. return 0;
  436. }
  437. int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
  438. {
  439. if (itr && itr->snapshot_finish)
  440. return itr->snapshot_finish(itr);
  441. return 0;
  442. }
  443. int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
  444. struct auxtrace_mmap *mm,
  445. unsigned char *data, u64 *head, u64 *old)
  446. {
  447. if (itr && itr->find_snapshot)
  448. return itr->find_snapshot(itr, idx, mm, data, head, old);
  449. return 0;
  450. }
  451. int auxtrace_record__options(struct auxtrace_record *itr,
  452. struct perf_evlist *evlist,
  453. struct record_opts *opts)
  454. {
  455. if (itr)
  456. return itr->recording_options(itr, evlist, opts);
  457. return 0;
  458. }
  459. u64 auxtrace_record__reference(struct auxtrace_record *itr)
  460. {
  461. if (itr)
  462. return itr->reference(itr);
  463. return 0;
  464. }
  465. int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
  466. struct record_opts *opts, const char *str)
  467. {
  468. if (!str)
  469. return 0;
  470. if (itr)
  471. return itr->parse_snapshot_options(itr, opts, str);
  472. pr_err("No AUX area tracing to snapshot\n");
  473. return -EINVAL;
  474. }
  475. struct auxtrace_record *__weak
  476. auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
  477. {
  478. *err = 0;
  479. return NULL;
  480. }
  481. static int auxtrace_index__alloc(struct list_head *head)
  482. {
  483. struct auxtrace_index *auxtrace_index;
  484. auxtrace_index = malloc(sizeof(struct auxtrace_index));
  485. if (!auxtrace_index)
  486. return -ENOMEM;
  487. auxtrace_index->nr = 0;
  488. INIT_LIST_HEAD(&auxtrace_index->list);
  489. list_add_tail(&auxtrace_index->list, head);
  490. return 0;
  491. }
  492. void auxtrace_index__free(struct list_head *head)
  493. {
  494. struct auxtrace_index *auxtrace_index, *n;
  495. list_for_each_entry_safe(auxtrace_index, n, head, list) {
  496. list_del(&auxtrace_index->list);
  497. free(auxtrace_index);
  498. }
  499. }
  500. static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
  501. {
  502. struct auxtrace_index *auxtrace_index;
  503. int err;
  504. if (list_empty(head)) {
  505. err = auxtrace_index__alloc(head);
  506. if (err)
  507. return NULL;
  508. }
  509. auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
  510. if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
  511. err = auxtrace_index__alloc(head);
  512. if (err)
  513. return NULL;
  514. auxtrace_index = list_entry(head->prev, struct auxtrace_index,
  515. list);
  516. }
  517. return auxtrace_index;
  518. }
  519. int auxtrace_index__auxtrace_event(struct list_head *head,
  520. union perf_event *event, off_t file_offset)
  521. {
  522. struct auxtrace_index *auxtrace_index;
  523. size_t nr;
  524. auxtrace_index = auxtrace_index__last(head);
  525. if (!auxtrace_index)
  526. return -ENOMEM;
  527. nr = auxtrace_index->nr;
  528. auxtrace_index->entries[nr].file_offset = file_offset;
  529. auxtrace_index->entries[nr].sz = event->header.size;
  530. auxtrace_index->nr += 1;
  531. return 0;
  532. }
  533. static int auxtrace_index__do_write(int fd,
  534. struct auxtrace_index *auxtrace_index)
  535. {
  536. struct auxtrace_index_entry ent;
  537. size_t i;
  538. for (i = 0; i < auxtrace_index->nr; i++) {
  539. ent.file_offset = auxtrace_index->entries[i].file_offset;
  540. ent.sz = auxtrace_index->entries[i].sz;
  541. if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
  542. return -errno;
  543. }
  544. return 0;
  545. }
  546. int auxtrace_index__write(int fd, struct list_head *head)
  547. {
  548. struct auxtrace_index *auxtrace_index;
  549. u64 total = 0;
  550. int err;
  551. list_for_each_entry(auxtrace_index, head, list)
  552. total += auxtrace_index->nr;
  553. if (writen(fd, &total, sizeof(total)) != sizeof(total))
  554. return -errno;
  555. list_for_each_entry(auxtrace_index, head, list) {
  556. err = auxtrace_index__do_write(fd, auxtrace_index);
  557. if (err)
  558. return err;
  559. }
  560. return 0;
  561. }
  562. static int auxtrace_index__process_entry(int fd, struct list_head *head,
  563. bool needs_swap)
  564. {
  565. struct auxtrace_index *auxtrace_index;
  566. struct auxtrace_index_entry ent;
  567. size_t nr;
  568. if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
  569. return -1;
  570. auxtrace_index = auxtrace_index__last(head);
  571. if (!auxtrace_index)
  572. return -1;
  573. nr = auxtrace_index->nr;
  574. if (needs_swap) {
  575. auxtrace_index->entries[nr].file_offset =
  576. bswap_64(ent.file_offset);
  577. auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
  578. } else {
  579. auxtrace_index->entries[nr].file_offset = ent.file_offset;
  580. auxtrace_index->entries[nr].sz = ent.sz;
  581. }
  582. auxtrace_index->nr = nr + 1;
  583. return 0;
  584. }
  585. int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
  586. bool needs_swap)
  587. {
  588. struct list_head *head = &session->auxtrace_index;
  589. u64 nr;
  590. if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
  591. return -1;
  592. if (needs_swap)
  593. nr = bswap_64(nr);
  594. if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
  595. return -1;
  596. while (nr--) {
  597. int err;
  598. err = auxtrace_index__process_entry(fd, head, needs_swap);
  599. if (err)
  600. return -1;
  601. }
  602. return 0;
  603. }
  604. static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
  605. struct perf_session *session,
  606. struct auxtrace_index_entry *ent)
  607. {
  608. return auxtrace_queues__add_indexed_event(queues, session,
  609. ent->file_offset, ent->sz);
  610. }
  611. int auxtrace_queues__process_index(struct auxtrace_queues *queues,
  612. struct perf_session *session)
  613. {
  614. struct auxtrace_index *auxtrace_index;
  615. struct auxtrace_index_entry *ent;
  616. size_t i;
  617. int err;
  618. list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
  619. for (i = 0; i < auxtrace_index->nr; i++) {
  620. ent = &auxtrace_index->entries[i];
  621. err = auxtrace_queues__process_index_entry(queues,
  622. session,
  623. ent);
  624. if (err)
  625. return err;
  626. }
  627. }
  628. return 0;
  629. }
  630. struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
  631. struct auxtrace_buffer *buffer)
  632. {
  633. if (buffer) {
  634. if (list_is_last(&buffer->list, &queue->head))
  635. return NULL;
  636. return list_entry(buffer->list.next, struct auxtrace_buffer,
  637. list);
  638. } else {
  639. if (list_empty(&queue->head))
  640. return NULL;
  641. return list_entry(queue->head.next, struct auxtrace_buffer,
  642. list);
  643. }
  644. }
  645. void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
  646. {
  647. size_t adj = buffer->data_offset & (page_size - 1);
  648. size_t size = buffer->size + adj;
  649. off_t file_offset = buffer->data_offset - adj;
  650. void *addr;
  651. if (buffer->data)
  652. return buffer->data;
  653. addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
  654. if (addr == MAP_FAILED)
  655. return NULL;
  656. buffer->mmap_addr = addr;
  657. buffer->mmap_size = size;
  658. buffer->data = addr + adj;
  659. return buffer->data;
  660. }
  661. void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
  662. {
  663. if (!buffer->data || !buffer->mmap_addr)
  664. return;
  665. munmap(buffer->mmap_addr, buffer->mmap_size);
  666. buffer->mmap_addr = NULL;
  667. buffer->mmap_size = 0;
  668. buffer->data = NULL;
  669. buffer->use_data = NULL;
  670. }
  671. void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
  672. {
  673. auxtrace_buffer__put_data(buffer);
  674. if (buffer->data_needs_freeing) {
  675. buffer->data_needs_freeing = false;
  676. zfree(&buffer->data);
  677. buffer->use_data = NULL;
  678. buffer->size = 0;
  679. }
  680. }
  681. void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
  682. {
  683. auxtrace_buffer__drop_data(buffer);
  684. free(buffer);
  685. }
  686. void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
  687. int code, int cpu, pid_t pid, pid_t tid, u64 ip,
  688. const char *msg)
  689. {
  690. size_t size;
  691. memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
  692. auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
  693. auxtrace_error->type = type;
  694. auxtrace_error->code = code;
  695. auxtrace_error->cpu = cpu;
  696. auxtrace_error->pid = pid;
  697. auxtrace_error->tid = tid;
  698. auxtrace_error->ip = ip;
  699. strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
  700. size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
  701. strlen(auxtrace_error->msg) + 1;
  702. auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
  703. }
  704. int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
  705. struct perf_tool *tool,
  706. struct perf_session *session,
  707. perf_event__handler_t process)
  708. {
  709. union perf_event *ev;
  710. size_t priv_size;
  711. int err;
  712. pr_debug2("Synthesizing auxtrace information\n");
  713. priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
  714. ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
  715. if (!ev)
  716. return -ENOMEM;
  717. ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
  718. ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
  719. priv_size;
  720. err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
  721. priv_size);
  722. if (err)
  723. goto out_free;
  724. err = process(tool, ev, NULL, NULL);
  725. out_free:
  726. free(ev);
  727. return err;
  728. }
  729. static bool auxtrace__dont_decode(struct perf_session *session)
  730. {
  731. return !session->itrace_synth_opts ||
  732. session->itrace_synth_opts->dont_decode;
  733. }
  734. int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
  735. union perf_event *event,
  736. struct perf_session *session)
  737. {
  738. enum auxtrace_type type = event->auxtrace_info.type;
  739. if (dump_trace)
  740. fprintf(stdout, " type: %u\n", type);
  741. switch (type) {
  742. case PERF_AUXTRACE_INTEL_PT:
  743. return intel_pt_process_auxtrace_info(event, session);
  744. case PERF_AUXTRACE_INTEL_BTS:
  745. return intel_bts_process_auxtrace_info(event, session);
  746. case PERF_AUXTRACE_CS_ETM:
  747. case PERF_AUXTRACE_UNKNOWN:
  748. default:
  749. return -EINVAL;
  750. }
  751. }
  752. s64 perf_event__process_auxtrace(struct perf_tool *tool,
  753. union perf_event *event,
  754. struct perf_session *session)
  755. {
  756. s64 err;
  757. if (dump_trace)
  758. fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n",
  759. event->auxtrace.size, event->auxtrace.offset,
  760. event->auxtrace.reference, event->auxtrace.idx,
  761. event->auxtrace.tid, event->auxtrace.cpu);
  762. if (auxtrace__dont_decode(session))
  763. return event->auxtrace.size;
  764. if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
  765. return -EINVAL;
  766. err = session->auxtrace->process_auxtrace_event(session, event, tool);
  767. if (err < 0)
  768. return err;
  769. return event->auxtrace.size;
  770. }
  771. #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
  772. #define PERF_ITRACE_DEFAULT_PERIOD 100000
  773. #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
  774. #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
  775. #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
  776. #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
  777. void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
  778. {
  779. synth_opts->instructions = true;
  780. synth_opts->branches = true;
  781. synth_opts->transactions = true;
  782. synth_opts->errors = true;
  783. synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
  784. synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
  785. synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
  786. synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
  787. synth_opts->initial_skip = 0;
  788. }
  789. /*
  790. * Please check tools/perf/Documentation/perf-script.txt for information
  791. * about the options parsed here, which is introduced after this cset,
  792. * when support in 'perf script' for these options is introduced.
  793. */
  794. int itrace_parse_synth_opts(const struct option *opt, const char *str,
  795. int unset)
  796. {
  797. struct itrace_synth_opts *synth_opts = opt->value;
  798. const char *p;
  799. char *endptr;
  800. bool period_type_set = false;
  801. bool period_set = false;
  802. synth_opts->set = true;
  803. if (unset) {
  804. synth_opts->dont_decode = true;
  805. return 0;
  806. }
  807. if (!str) {
  808. itrace_synth_opts__set_default(synth_opts);
  809. return 0;
  810. }
  811. for (p = str; *p;) {
  812. switch (*p++) {
  813. case 'i':
  814. synth_opts->instructions = true;
  815. while (*p == ' ' || *p == ',')
  816. p += 1;
  817. if (isdigit(*p)) {
  818. synth_opts->period = strtoull(p, &endptr, 10);
  819. period_set = true;
  820. p = endptr;
  821. while (*p == ' ' || *p == ',')
  822. p += 1;
  823. switch (*p++) {
  824. case 'i':
  825. synth_opts->period_type =
  826. PERF_ITRACE_PERIOD_INSTRUCTIONS;
  827. period_type_set = true;
  828. break;
  829. case 't':
  830. synth_opts->period_type =
  831. PERF_ITRACE_PERIOD_TICKS;
  832. period_type_set = true;
  833. break;
  834. case 'm':
  835. synth_opts->period *= 1000;
  836. /* Fall through */
  837. case 'u':
  838. synth_opts->period *= 1000;
  839. /* Fall through */
  840. case 'n':
  841. if (*p++ != 's')
  842. goto out_err;
  843. synth_opts->period_type =
  844. PERF_ITRACE_PERIOD_NANOSECS;
  845. period_type_set = true;
  846. break;
  847. case '\0':
  848. goto out;
  849. default:
  850. goto out_err;
  851. }
  852. }
  853. break;
  854. case 'b':
  855. synth_opts->branches = true;
  856. break;
  857. case 'x':
  858. synth_opts->transactions = true;
  859. break;
  860. case 'e':
  861. synth_opts->errors = true;
  862. break;
  863. case 'd':
  864. synth_opts->log = true;
  865. break;
  866. case 'c':
  867. synth_opts->branches = true;
  868. synth_opts->calls = true;
  869. break;
  870. case 'r':
  871. synth_opts->branches = true;
  872. synth_opts->returns = true;
  873. break;
  874. case 'g':
  875. synth_opts->callchain = true;
  876. synth_opts->callchain_sz =
  877. PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
  878. while (*p == ' ' || *p == ',')
  879. p += 1;
  880. if (isdigit(*p)) {
  881. unsigned int val;
  882. val = strtoul(p, &endptr, 10);
  883. p = endptr;
  884. if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
  885. goto out_err;
  886. synth_opts->callchain_sz = val;
  887. }
  888. break;
  889. case 'l':
  890. synth_opts->last_branch = true;
  891. synth_opts->last_branch_sz =
  892. PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
  893. while (*p == ' ' || *p == ',')
  894. p += 1;
  895. if (isdigit(*p)) {
  896. unsigned int val;
  897. val = strtoul(p, &endptr, 10);
  898. p = endptr;
  899. if (!val ||
  900. val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
  901. goto out_err;
  902. synth_opts->last_branch_sz = val;
  903. }
  904. break;
  905. case 's':
  906. synth_opts->initial_skip = strtoul(p, &endptr, 10);
  907. if (p == endptr)
  908. goto out_err;
  909. p = endptr;
  910. break;
  911. case ' ':
  912. case ',':
  913. break;
  914. default:
  915. goto out_err;
  916. }
  917. }
  918. out:
  919. if (synth_opts->instructions) {
  920. if (!period_type_set)
  921. synth_opts->period_type =
  922. PERF_ITRACE_DEFAULT_PERIOD_TYPE;
  923. if (!period_set)
  924. synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
  925. }
  926. return 0;
  927. out_err:
  928. pr_err("Bad Instruction Tracing options '%s'\n", str);
  929. return -EINVAL;
  930. }
  931. static const char * const auxtrace_error_type_name[] = {
  932. [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
  933. };
  934. static const char *auxtrace_error_name(int type)
  935. {
  936. const char *error_type_name = NULL;
  937. if (type < PERF_AUXTRACE_ERROR_MAX)
  938. error_type_name = auxtrace_error_type_name[type];
  939. if (!error_type_name)
  940. error_type_name = "unknown AUX";
  941. return error_type_name;
  942. }
  943. size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
  944. {
  945. struct auxtrace_error_event *e = &event->auxtrace_error;
  946. int ret;
  947. ret = fprintf(fp, " %s error type %u",
  948. auxtrace_error_name(e->type), e->type);
  949. ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
  950. e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
  951. return ret;
  952. }
  953. void perf_session__auxtrace_error_inc(struct perf_session *session,
  954. union perf_event *event)
  955. {
  956. struct auxtrace_error_event *e = &event->auxtrace_error;
  957. if (e->type < PERF_AUXTRACE_ERROR_MAX)
  958. session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
  959. }
  960. void events_stats__auxtrace_error_warn(const struct events_stats *stats)
  961. {
  962. int i;
  963. for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
  964. if (!stats->nr_auxtrace_errors[i])
  965. continue;
  966. ui__warning("%u %s errors\n",
  967. stats->nr_auxtrace_errors[i],
  968. auxtrace_error_name(i));
  969. }
  970. }
  971. int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
  972. union perf_event *event,
  973. struct perf_session *session)
  974. {
  975. if (auxtrace__dont_decode(session))
  976. return 0;
  977. perf_event__fprintf_auxtrace_error(event, stdout);
  978. return 0;
  979. }
  980. static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
  981. struct auxtrace_record *itr,
  982. struct perf_tool *tool, process_auxtrace_t fn,
  983. bool snapshot, size_t snapshot_size)
  984. {
  985. u64 head, old = mm->prev, offset, ref;
  986. unsigned char *data = mm->base;
  987. size_t size, head_off, old_off, len1, len2, padding;
  988. union perf_event ev;
  989. void *data1, *data2;
  990. if (snapshot) {
  991. head = auxtrace_mmap__read_snapshot_head(mm);
  992. if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
  993. &head, &old))
  994. return -1;
  995. } else {
  996. head = auxtrace_mmap__read_head(mm);
  997. }
  998. if (old == head)
  999. return 0;
  1000. pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
  1001. mm->idx, old, head, head - old);
  1002. if (mm->mask) {
  1003. head_off = head & mm->mask;
  1004. old_off = old & mm->mask;
  1005. } else {
  1006. head_off = head % mm->len;
  1007. old_off = old % mm->len;
  1008. }
  1009. if (head_off > old_off)
  1010. size = head_off - old_off;
  1011. else
  1012. size = mm->len - (old_off - head_off);
  1013. if (snapshot && size > snapshot_size)
  1014. size = snapshot_size;
  1015. ref = auxtrace_record__reference(itr);
  1016. if (head > old || size <= head || mm->mask) {
  1017. offset = head - size;
  1018. } else {
  1019. /*
  1020. * When the buffer size is not a power of 2, 'head' wraps at the
  1021. * highest multiple of the buffer size, so we have to subtract
  1022. * the remainder here.
  1023. */
  1024. u64 rem = (0ULL - mm->len) % mm->len;
  1025. offset = head - size - rem;
  1026. }
  1027. if (size > head_off) {
  1028. len1 = size - head_off;
  1029. data1 = &data[mm->len - len1];
  1030. len2 = head_off;
  1031. data2 = &data[0];
  1032. } else {
  1033. len1 = size;
  1034. data1 = &data[head_off - len1];
  1035. len2 = 0;
  1036. data2 = NULL;
  1037. }
  1038. if (itr->alignment) {
  1039. unsigned int unwanted = len1 % itr->alignment;
  1040. len1 -= unwanted;
  1041. size -= unwanted;
  1042. }
  1043. /* padding must be written by fn() e.g. record__process_auxtrace() */
  1044. padding = size & 7;
  1045. if (padding)
  1046. padding = 8 - padding;
  1047. memset(&ev, 0, sizeof(ev));
  1048. ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
  1049. ev.auxtrace.header.size = sizeof(ev.auxtrace);
  1050. ev.auxtrace.size = size + padding;
  1051. ev.auxtrace.offset = offset;
  1052. ev.auxtrace.reference = ref;
  1053. ev.auxtrace.idx = mm->idx;
  1054. ev.auxtrace.tid = mm->tid;
  1055. ev.auxtrace.cpu = mm->cpu;
  1056. if (fn(tool, &ev, data1, len1, data2, len2))
  1057. return -1;
  1058. mm->prev = head;
  1059. if (!snapshot) {
  1060. auxtrace_mmap__write_tail(mm, head);
  1061. if (itr->read_finish) {
  1062. int err;
  1063. err = itr->read_finish(itr, mm->idx);
  1064. if (err < 0)
  1065. return err;
  1066. }
  1067. }
  1068. return 1;
  1069. }
  1070. int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
  1071. struct perf_tool *tool, process_auxtrace_t fn)
  1072. {
  1073. return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
  1074. }
  1075. int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
  1076. struct auxtrace_record *itr,
  1077. struct perf_tool *tool, process_auxtrace_t fn,
  1078. size_t snapshot_size)
  1079. {
  1080. return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
  1081. }
  1082. /**
  1083. * struct auxtrace_cache - hash table to implement a cache
  1084. * @hashtable: the hashtable
  1085. * @sz: hashtable size (number of hlists)
  1086. * @entry_size: size of an entry
  1087. * @limit: limit the number of entries to this maximum, when reached the cache
  1088. * is dropped and caching begins again with an empty cache
  1089. * @cnt: current number of entries
  1090. * @bits: hashtable size (@sz = 2^@bits)
  1091. */
  1092. struct auxtrace_cache {
  1093. struct hlist_head *hashtable;
  1094. size_t sz;
  1095. size_t entry_size;
  1096. size_t limit;
  1097. size_t cnt;
  1098. unsigned int bits;
  1099. };
  1100. struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
  1101. unsigned int limit_percent)
  1102. {
  1103. struct auxtrace_cache *c;
  1104. struct hlist_head *ht;
  1105. size_t sz, i;
  1106. c = zalloc(sizeof(struct auxtrace_cache));
  1107. if (!c)
  1108. return NULL;
  1109. sz = 1UL << bits;
  1110. ht = calloc(sz, sizeof(struct hlist_head));
  1111. if (!ht)
  1112. goto out_free;
  1113. for (i = 0; i < sz; i++)
  1114. INIT_HLIST_HEAD(&ht[i]);
  1115. c->hashtable = ht;
  1116. c->sz = sz;
  1117. c->entry_size = entry_size;
  1118. c->limit = (c->sz * limit_percent) / 100;
  1119. c->bits = bits;
  1120. return c;
  1121. out_free:
  1122. free(c);
  1123. return NULL;
  1124. }
  1125. static void auxtrace_cache__drop(struct auxtrace_cache *c)
  1126. {
  1127. struct auxtrace_cache_entry *entry;
  1128. struct hlist_node *tmp;
  1129. size_t i;
  1130. if (!c)
  1131. return;
  1132. for (i = 0; i < c->sz; i++) {
  1133. hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
  1134. hlist_del(&entry->hash);
  1135. auxtrace_cache__free_entry(c, entry);
  1136. }
  1137. }
  1138. c->cnt = 0;
  1139. }
  1140. void auxtrace_cache__free(struct auxtrace_cache *c)
  1141. {
  1142. if (!c)
  1143. return;
  1144. auxtrace_cache__drop(c);
  1145. free(c->hashtable);
  1146. free(c);
  1147. }
  1148. void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
  1149. {
  1150. return malloc(c->entry_size);
  1151. }
  1152. void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
  1153. void *entry)
  1154. {
  1155. free(entry);
  1156. }
  1157. int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
  1158. struct auxtrace_cache_entry *entry)
  1159. {
  1160. if (c->limit && ++c->cnt > c->limit)
  1161. auxtrace_cache__drop(c);
  1162. entry->key = key;
  1163. hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
  1164. return 0;
  1165. }
  1166. void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
  1167. {
  1168. struct auxtrace_cache_entry *entry;
  1169. struct hlist_head *hlist;
  1170. if (!c)
  1171. return NULL;
  1172. hlist = &c->hashtable[hash_32(key, c->bits)];
  1173. hlist_for_each_entry(entry, hlist, hash) {
  1174. if (entry->key == key)
  1175. return entry;
  1176. }
  1177. return NULL;
  1178. }
  1179. static void addr_filter__free_str(struct addr_filter *filt)
  1180. {
  1181. free(filt->str);
  1182. filt->action = NULL;
  1183. filt->sym_from = NULL;
  1184. filt->sym_to = NULL;
  1185. filt->filename = NULL;
  1186. filt->str = NULL;
  1187. }
  1188. static struct addr_filter *addr_filter__new(void)
  1189. {
  1190. struct addr_filter *filt = zalloc(sizeof(*filt));
  1191. if (filt)
  1192. INIT_LIST_HEAD(&filt->list);
  1193. return filt;
  1194. }
  1195. static void addr_filter__free(struct addr_filter *filt)
  1196. {
  1197. if (filt)
  1198. addr_filter__free_str(filt);
  1199. free(filt);
  1200. }
  1201. static void addr_filters__add(struct addr_filters *filts,
  1202. struct addr_filter *filt)
  1203. {
  1204. list_add_tail(&filt->list, &filts->head);
  1205. filts->cnt += 1;
  1206. }
  1207. static void addr_filters__del(struct addr_filters *filts,
  1208. struct addr_filter *filt)
  1209. {
  1210. list_del_init(&filt->list);
  1211. filts->cnt -= 1;
  1212. }
  1213. void addr_filters__init(struct addr_filters *filts)
  1214. {
  1215. INIT_LIST_HEAD(&filts->head);
  1216. filts->cnt = 0;
  1217. }
  1218. void addr_filters__exit(struct addr_filters *filts)
  1219. {
  1220. struct addr_filter *filt, *n;
  1221. list_for_each_entry_safe(filt, n, &filts->head, list) {
  1222. addr_filters__del(filts, filt);
  1223. addr_filter__free(filt);
  1224. }
  1225. }
  1226. static int parse_num_or_str(char **inp, u64 *num, const char **str,
  1227. const char *str_delim)
  1228. {
  1229. *inp += strspn(*inp, " ");
  1230. if (isdigit(**inp)) {
  1231. char *endptr;
  1232. if (!num)
  1233. return -EINVAL;
  1234. errno = 0;
  1235. *num = strtoull(*inp, &endptr, 0);
  1236. if (errno)
  1237. return -errno;
  1238. if (endptr == *inp)
  1239. return -EINVAL;
  1240. *inp = endptr;
  1241. } else {
  1242. size_t n;
  1243. if (!str)
  1244. return -EINVAL;
  1245. *inp += strspn(*inp, " ");
  1246. *str = *inp;
  1247. n = strcspn(*inp, str_delim);
  1248. if (!n)
  1249. return -EINVAL;
  1250. *inp += n;
  1251. if (**inp) {
  1252. **inp = '\0';
  1253. *inp += 1;
  1254. }
  1255. }
  1256. return 0;
  1257. }
  1258. static int parse_action(struct addr_filter *filt)
  1259. {
  1260. if (!strcmp(filt->action, "filter")) {
  1261. filt->start = true;
  1262. filt->range = true;
  1263. } else if (!strcmp(filt->action, "start")) {
  1264. filt->start = true;
  1265. } else if (!strcmp(filt->action, "stop")) {
  1266. filt->start = false;
  1267. } else if (!strcmp(filt->action, "tracestop")) {
  1268. filt->start = false;
  1269. filt->range = true;
  1270. filt->action += 5; /* Change 'tracestop' to 'stop' */
  1271. } else {
  1272. return -EINVAL;
  1273. }
  1274. return 0;
  1275. }
  1276. static int parse_sym_idx(char **inp, int *idx)
  1277. {
  1278. *idx = -1;
  1279. *inp += strspn(*inp, " ");
  1280. if (**inp != '#')
  1281. return 0;
  1282. *inp += 1;
  1283. if (**inp == 'g' || **inp == 'G') {
  1284. *inp += 1;
  1285. *idx = 0;
  1286. } else {
  1287. unsigned long num;
  1288. char *endptr;
  1289. errno = 0;
  1290. num = strtoul(*inp, &endptr, 0);
  1291. if (errno)
  1292. return -errno;
  1293. if (endptr == *inp || num > INT_MAX)
  1294. return -EINVAL;
  1295. *inp = endptr;
  1296. *idx = num;
  1297. }
  1298. return 0;
  1299. }
  1300. static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
  1301. {
  1302. int err = parse_num_or_str(inp, num, str, " ");
  1303. if (!err && *str)
  1304. err = parse_sym_idx(inp, idx);
  1305. return err;
  1306. }
  1307. static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
  1308. {
  1309. char *fstr;
  1310. int err;
  1311. filt->str = fstr = strdup(*filter_inp);
  1312. if (!fstr)
  1313. return -ENOMEM;
  1314. err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
  1315. if (err)
  1316. goto out_err;
  1317. err = parse_action(filt);
  1318. if (err)
  1319. goto out_err;
  1320. err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
  1321. &filt->sym_from_idx);
  1322. if (err)
  1323. goto out_err;
  1324. fstr += strspn(fstr, " ");
  1325. if (*fstr == '/') {
  1326. fstr += 1;
  1327. err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
  1328. &filt->sym_to_idx);
  1329. if (err)
  1330. goto out_err;
  1331. filt->range = true;
  1332. }
  1333. fstr += strspn(fstr, " ");
  1334. if (*fstr == '@') {
  1335. fstr += 1;
  1336. err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
  1337. if (err)
  1338. goto out_err;
  1339. }
  1340. fstr += strspn(fstr, " ,");
  1341. *filter_inp += fstr - filt->str;
  1342. return 0;
  1343. out_err:
  1344. addr_filter__free_str(filt);
  1345. return err;
  1346. }
  1347. int addr_filters__parse_bare_filter(struct addr_filters *filts,
  1348. const char *filter)
  1349. {
  1350. struct addr_filter *filt;
  1351. const char *fstr = filter;
  1352. int err;
  1353. while (*fstr) {
  1354. filt = addr_filter__new();
  1355. err = parse_one_filter(filt, &fstr);
  1356. if (err) {
  1357. addr_filter__free(filt);
  1358. addr_filters__exit(filts);
  1359. return err;
  1360. }
  1361. addr_filters__add(filts, filt);
  1362. }
  1363. return 0;
  1364. }
  1365. struct sym_args {
  1366. const char *name;
  1367. u64 start;
  1368. u64 size;
  1369. int idx;
  1370. int cnt;
  1371. bool started;
  1372. bool global;
  1373. bool selected;
  1374. bool duplicate;
  1375. bool near;
  1376. };
  1377. static bool kern_sym_match(struct sym_args *args, const char *name, char type)
  1378. {
  1379. /* A function with the same name, and global or the n'th found or any */
  1380. return symbol_type__is_a(type, MAP__FUNCTION) &&
  1381. !strcmp(name, args->name) &&
  1382. ((args->global && isupper(type)) ||
  1383. (args->selected && ++(args->cnt) == args->idx) ||
  1384. (!args->global && !args->selected));
  1385. }
  1386. static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
  1387. {
  1388. struct sym_args *args = arg;
  1389. if (args->started) {
  1390. if (!args->size)
  1391. args->size = start - args->start;
  1392. if (args->selected) {
  1393. if (args->size)
  1394. return 1;
  1395. } else if (kern_sym_match(args, name, type)) {
  1396. args->duplicate = true;
  1397. return 1;
  1398. }
  1399. } else if (kern_sym_match(args, name, type)) {
  1400. args->started = true;
  1401. args->start = start;
  1402. }
  1403. return 0;
  1404. }
  1405. static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
  1406. {
  1407. struct sym_args *args = arg;
  1408. if (kern_sym_match(args, name, type)) {
  1409. pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
  1410. ++args->cnt, start, type, name);
  1411. args->near = true;
  1412. } else if (args->near) {
  1413. args->near = false;
  1414. pr_err("\t\twhich is near\t\t%s\n", name);
  1415. }
  1416. return 0;
  1417. }
  1418. static int sym_not_found_error(const char *sym_name, int idx)
  1419. {
  1420. if (idx > 0) {
  1421. pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
  1422. idx, sym_name);
  1423. } else if (!idx) {
  1424. pr_err("Global symbol '%s' not found.\n", sym_name);
  1425. } else {
  1426. pr_err("Symbol '%s' not found.\n", sym_name);
  1427. }
  1428. pr_err("Note that symbols must be functions.\n");
  1429. return -EINVAL;
  1430. }
  1431. static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
  1432. {
  1433. struct sym_args args = {
  1434. .name = sym_name,
  1435. .idx = idx,
  1436. .global = !idx,
  1437. .selected = idx > 0,
  1438. };
  1439. int err;
  1440. *start = 0;
  1441. *size = 0;
  1442. err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
  1443. if (err < 0) {
  1444. pr_err("Failed to parse /proc/kallsyms\n");
  1445. return err;
  1446. }
  1447. if (args.duplicate) {
  1448. pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
  1449. args.cnt = 0;
  1450. kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
  1451. pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
  1452. sym_name);
  1453. pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
  1454. return -EINVAL;
  1455. }
  1456. if (!args.started) {
  1457. pr_err("Kernel symbol lookup: ");
  1458. return sym_not_found_error(sym_name, idx);
  1459. }
  1460. *start = args.start;
  1461. *size = args.size;
  1462. return 0;
  1463. }
  1464. static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
  1465. char type, u64 start)
  1466. {
  1467. struct sym_args *args = arg;
  1468. if (!symbol_type__is_a(type, MAP__FUNCTION))
  1469. return 0;
  1470. if (!args->started) {
  1471. args->started = true;
  1472. args->start = start;
  1473. }
  1474. /* Don't know exactly where the kernel ends, so we add a page */
  1475. args->size = round_up(start, page_size) + page_size - args->start;
  1476. return 0;
  1477. }
  1478. static int addr_filter__entire_kernel(struct addr_filter *filt)
  1479. {
  1480. struct sym_args args = { .started = false };
  1481. int err;
  1482. err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
  1483. if (err < 0 || !args.started) {
  1484. pr_err("Failed to parse /proc/kallsyms\n");
  1485. return err;
  1486. }
  1487. filt->addr = args.start;
  1488. filt->size = args.size;
  1489. return 0;
  1490. }
  1491. static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
  1492. {
  1493. if (start + size >= filt->addr)
  1494. return 0;
  1495. if (filt->sym_from) {
  1496. pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
  1497. filt->sym_to, start, filt->sym_from, filt->addr);
  1498. } else {
  1499. pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
  1500. filt->sym_to, start, filt->addr);
  1501. }
  1502. return -EINVAL;
  1503. }
  1504. static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
  1505. {
  1506. bool no_size = false;
  1507. u64 start, size;
  1508. int err;
  1509. if (symbol_conf.kptr_restrict) {
  1510. pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
  1511. return -EINVAL;
  1512. }
  1513. if (filt->sym_from && !strcmp(filt->sym_from, "*"))
  1514. return addr_filter__entire_kernel(filt);
  1515. if (filt->sym_from) {
  1516. err = find_kern_sym(filt->sym_from, &start, &size,
  1517. filt->sym_from_idx);
  1518. if (err)
  1519. return err;
  1520. filt->addr = start;
  1521. if (filt->range && !filt->size && !filt->sym_to) {
  1522. filt->size = size;
  1523. no_size = !size;
  1524. }
  1525. }
  1526. if (filt->sym_to) {
  1527. err = find_kern_sym(filt->sym_to, &start, &size,
  1528. filt->sym_to_idx);
  1529. if (err)
  1530. return err;
  1531. err = check_end_after_start(filt, start, size);
  1532. if (err)
  1533. return err;
  1534. filt->size = start + size - filt->addr;
  1535. no_size = !size;
  1536. }
  1537. /* The very last symbol in kallsyms does not imply a particular size */
  1538. if (no_size) {
  1539. pr_err("Cannot determine size of symbol '%s'\n",
  1540. filt->sym_to ? filt->sym_to : filt->sym_from);
  1541. return -EINVAL;
  1542. }
  1543. return 0;
  1544. }
  1545. static struct dso *load_dso(const char *name)
  1546. {
  1547. struct map *map;
  1548. struct dso *dso;
  1549. map = dso__new_map(name);
  1550. if (!map)
  1551. return NULL;
  1552. map__load(map);
  1553. dso = dso__get(map->dso);
  1554. map__put(map);
  1555. return dso;
  1556. }
  1557. static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
  1558. int idx)
  1559. {
  1560. /* Same name, and global or the n'th found or any */
  1561. return !arch__compare_symbol_names(name, sym->name) &&
  1562. ((!idx && sym->binding == STB_GLOBAL) ||
  1563. (idx > 0 && ++*cnt == idx) ||
  1564. idx < 0);
  1565. }
  1566. static void print_duplicate_syms(struct dso *dso, const char *sym_name)
  1567. {
  1568. struct symbol *sym;
  1569. bool near = false;
  1570. int cnt = 0;
  1571. pr_err("Multiple symbols with name '%s'\n", sym_name);
  1572. sym = dso__first_symbol(dso, MAP__FUNCTION);
  1573. while (sym) {
  1574. if (dso_sym_match(sym, sym_name, &cnt, -1)) {
  1575. pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
  1576. ++cnt, sym->start,
  1577. sym->binding == STB_GLOBAL ? 'g' :
  1578. sym->binding == STB_LOCAL ? 'l' : 'w',
  1579. sym->name);
  1580. near = true;
  1581. } else if (near) {
  1582. near = false;
  1583. pr_err("\t\twhich is near\t\t%s\n", sym->name);
  1584. }
  1585. sym = dso__next_symbol(sym);
  1586. }
  1587. pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
  1588. sym_name);
  1589. pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
  1590. }
  1591. static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
  1592. u64 *size, int idx)
  1593. {
  1594. struct symbol *sym;
  1595. int cnt = 0;
  1596. *start = 0;
  1597. *size = 0;
  1598. sym = dso__first_symbol(dso, MAP__FUNCTION);
  1599. while (sym) {
  1600. if (*start) {
  1601. if (!*size)
  1602. *size = sym->start - *start;
  1603. if (idx > 0) {
  1604. if (*size)
  1605. return 1;
  1606. } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
  1607. print_duplicate_syms(dso, sym_name);
  1608. return -EINVAL;
  1609. }
  1610. } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
  1611. *start = sym->start;
  1612. *size = sym->end - sym->start;
  1613. }
  1614. sym = dso__next_symbol(sym);
  1615. }
  1616. if (!*start)
  1617. return sym_not_found_error(sym_name, idx);
  1618. return 0;
  1619. }
  1620. static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
  1621. {
  1622. struct symbol *first_sym = dso__first_symbol(dso, MAP__FUNCTION);
  1623. struct symbol *last_sym = dso__last_symbol(dso, MAP__FUNCTION);
  1624. if (!first_sym || !last_sym) {
  1625. pr_err("Failed to determine filter for %s\nNo symbols found.\n",
  1626. filt->filename);
  1627. return -EINVAL;
  1628. }
  1629. filt->addr = first_sym->start;
  1630. filt->size = last_sym->end - first_sym->start;
  1631. return 0;
  1632. }
  1633. static int addr_filter__resolve_syms(struct addr_filter *filt)
  1634. {
  1635. u64 start, size;
  1636. struct dso *dso;
  1637. int err = 0;
  1638. if (!filt->sym_from && !filt->sym_to)
  1639. return 0;
  1640. if (!filt->filename)
  1641. return addr_filter__resolve_kernel_syms(filt);
  1642. dso = load_dso(filt->filename);
  1643. if (!dso) {
  1644. pr_err("Failed to load symbols from: %s\n", filt->filename);
  1645. return -EINVAL;
  1646. }
  1647. if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
  1648. err = addr_filter__entire_dso(filt, dso);
  1649. goto put_dso;
  1650. }
  1651. if (filt->sym_from) {
  1652. err = find_dso_sym(dso, filt->sym_from, &start, &size,
  1653. filt->sym_from_idx);
  1654. if (err)
  1655. goto put_dso;
  1656. filt->addr = start;
  1657. if (filt->range && !filt->size && !filt->sym_to)
  1658. filt->size = size;
  1659. }
  1660. if (filt->sym_to) {
  1661. err = find_dso_sym(dso, filt->sym_to, &start, &size,
  1662. filt->sym_to_idx);
  1663. if (err)
  1664. goto put_dso;
  1665. err = check_end_after_start(filt, start, size);
  1666. if (err)
  1667. return err;
  1668. filt->size = start + size - filt->addr;
  1669. }
  1670. put_dso:
  1671. dso__put(dso);
  1672. return err;
  1673. }
  1674. static char *addr_filter__to_str(struct addr_filter *filt)
  1675. {
  1676. char filename_buf[PATH_MAX];
  1677. const char *at = "";
  1678. const char *fn = "";
  1679. char *filter;
  1680. int err;
  1681. if (filt->filename) {
  1682. at = "@";
  1683. fn = realpath(filt->filename, filename_buf);
  1684. if (!fn)
  1685. return NULL;
  1686. }
  1687. if (filt->range) {
  1688. err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
  1689. filt->action, filt->addr, filt->size, at, fn);
  1690. } else {
  1691. err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
  1692. filt->action, filt->addr, at, fn);
  1693. }
  1694. return err < 0 ? NULL : filter;
  1695. }
  1696. static int parse_addr_filter(struct perf_evsel *evsel, const char *filter,
  1697. int max_nr)
  1698. {
  1699. struct addr_filters filts;
  1700. struct addr_filter *filt;
  1701. int err;
  1702. addr_filters__init(&filts);
  1703. err = addr_filters__parse_bare_filter(&filts, filter);
  1704. if (err)
  1705. goto out_exit;
  1706. if (filts.cnt > max_nr) {
  1707. pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
  1708. filts.cnt, max_nr);
  1709. err = -EINVAL;
  1710. goto out_exit;
  1711. }
  1712. list_for_each_entry(filt, &filts.head, list) {
  1713. char *new_filter;
  1714. err = addr_filter__resolve_syms(filt);
  1715. if (err)
  1716. goto out_exit;
  1717. new_filter = addr_filter__to_str(filt);
  1718. if (!new_filter) {
  1719. err = -ENOMEM;
  1720. goto out_exit;
  1721. }
  1722. if (perf_evsel__append_addr_filter(evsel, new_filter)) {
  1723. err = -ENOMEM;
  1724. goto out_exit;
  1725. }
  1726. }
  1727. out_exit:
  1728. addr_filters__exit(&filts);
  1729. if (err) {
  1730. pr_err("Failed to parse address filter: '%s'\n", filter);
  1731. pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
  1732. pr_err("Where multiple filters are separated by space or comma.\n");
  1733. }
  1734. return err;
  1735. }
  1736. static struct perf_pmu *perf_evsel__find_pmu(struct perf_evsel *evsel)
  1737. {
  1738. struct perf_pmu *pmu = NULL;
  1739. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  1740. if (pmu->type == evsel->attr.type)
  1741. break;
  1742. }
  1743. return pmu;
  1744. }
  1745. static int perf_evsel__nr_addr_filter(struct perf_evsel *evsel)
  1746. {
  1747. struct perf_pmu *pmu = perf_evsel__find_pmu(evsel);
  1748. int nr_addr_filters = 0;
  1749. if (!pmu)
  1750. return 0;
  1751. perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
  1752. return nr_addr_filters;
  1753. }
  1754. int auxtrace_parse_filters(struct perf_evlist *evlist)
  1755. {
  1756. struct perf_evsel *evsel;
  1757. char *filter;
  1758. int err, max_nr;
  1759. evlist__for_each_entry(evlist, evsel) {
  1760. filter = evsel->filter;
  1761. max_nr = perf_evsel__nr_addr_filter(evsel);
  1762. if (!filter || !max_nr)
  1763. continue;
  1764. evsel->filter = NULL;
  1765. err = parse_addr_filter(evsel, filter, max_nr);
  1766. free(filter);
  1767. if (err)
  1768. return err;
  1769. pr_debug("Address filter: %s\n", evsel->filter);
  1770. }
  1771. return 0;
  1772. }