auxtrace.c 47 KB

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