intel-pt.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631
  1. /*
  2. * intel_pt.c: Intel Processor 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 <stdio.h>
  17. #include <stdbool.h>
  18. #include <errno.h>
  19. #include <linux/kernel.h>
  20. #include <linux/types.h>
  21. #include "../perf.h"
  22. #include "session.h"
  23. #include "machine.h"
  24. #include "memswap.h"
  25. #include "sort.h"
  26. #include "tool.h"
  27. #include "event.h"
  28. #include "evlist.h"
  29. #include "evsel.h"
  30. #include "map.h"
  31. #include "color.h"
  32. #include "util.h"
  33. #include "thread.h"
  34. #include "thread-stack.h"
  35. #include "symbol.h"
  36. #include "callchain.h"
  37. #include "dso.h"
  38. #include "debug.h"
  39. #include "auxtrace.h"
  40. #include "tsc.h"
  41. #include "intel-pt.h"
  42. #include "config.h"
  43. #include "intel-pt-decoder/intel-pt-log.h"
  44. #include "intel-pt-decoder/intel-pt-decoder.h"
  45. #include "intel-pt-decoder/intel-pt-insn-decoder.h"
  46. #include "intel-pt-decoder/intel-pt-pkt-decoder.h"
  47. #define MAX_TIMESTAMP (~0ULL)
  48. struct intel_pt {
  49. struct auxtrace auxtrace;
  50. struct auxtrace_queues queues;
  51. struct auxtrace_heap heap;
  52. u32 auxtrace_type;
  53. struct perf_session *session;
  54. struct machine *machine;
  55. struct perf_evsel *switch_evsel;
  56. struct thread *unknown_thread;
  57. bool timeless_decoding;
  58. bool sampling_mode;
  59. bool snapshot_mode;
  60. bool per_cpu_mmaps;
  61. bool have_tsc;
  62. bool data_queued;
  63. bool est_tsc;
  64. bool sync_switch;
  65. bool mispred_all;
  66. int have_sched_switch;
  67. u32 pmu_type;
  68. u64 kernel_start;
  69. u64 switch_ip;
  70. u64 ptss_ip;
  71. struct perf_tsc_conversion tc;
  72. bool cap_user_time_zero;
  73. struct itrace_synth_opts synth_opts;
  74. bool sample_instructions;
  75. u64 instructions_sample_type;
  76. u64 instructions_id;
  77. bool sample_branches;
  78. u32 branches_filter;
  79. u64 branches_sample_type;
  80. u64 branches_id;
  81. bool sample_transactions;
  82. u64 transactions_sample_type;
  83. u64 transactions_id;
  84. bool sample_ptwrites;
  85. u64 ptwrites_sample_type;
  86. u64 ptwrites_id;
  87. bool sample_pwr_events;
  88. u64 pwr_events_sample_type;
  89. u64 mwait_id;
  90. u64 pwre_id;
  91. u64 exstop_id;
  92. u64 pwrx_id;
  93. u64 cbr_id;
  94. u64 tsc_bit;
  95. u64 mtc_bit;
  96. u64 mtc_freq_bits;
  97. u32 tsc_ctc_ratio_n;
  98. u32 tsc_ctc_ratio_d;
  99. u64 cyc_bit;
  100. u64 noretcomp_bit;
  101. unsigned max_non_turbo_ratio;
  102. unsigned cbr2khz;
  103. unsigned long num_events;
  104. char *filter;
  105. struct addr_filters filts;
  106. };
  107. enum switch_state {
  108. INTEL_PT_SS_NOT_TRACING,
  109. INTEL_PT_SS_UNKNOWN,
  110. INTEL_PT_SS_TRACING,
  111. INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
  112. INTEL_PT_SS_EXPECTING_SWITCH_IP,
  113. };
  114. struct intel_pt_queue {
  115. struct intel_pt *pt;
  116. unsigned int queue_nr;
  117. struct auxtrace_buffer *buffer;
  118. struct auxtrace_buffer *old_buffer;
  119. void *decoder;
  120. const struct intel_pt_state *state;
  121. struct ip_callchain *chain;
  122. struct branch_stack *last_branch;
  123. struct branch_stack *last_branch_rb;
  124. size_t last_branch_pos;
  125. union perf_event *event_buf;
  126. bool on_heap;
  127. bool stop;
  128. bool step_through_buffers;
  129. bool use_buffer_pid_tid;
  130. bool sync_switch;
  131. pid_t pid, tid;
  132. int cpu;
  133. int switch_state;
  134. pid_t next_tid;
  135. struct thread *thread;
  136. bool exclude_kernel;
  137. bool have_sample;
  138. u64 time;
  139. u64 timestamp;
  140. u32 flags;
  141. u16 insn_len;
  142. u64 last_insn_cnt;
  143. char insn[INTEL_PT_INSN_BUF_SZ];
  144. };
  145. static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
  146. unsigned char *buf, size_t len)
  147. {
  148. struct intel_pt_pkt packet;
  149. size_t pos = 0;
  150. int ret, pkt_len, i;
  151. char desc[INTEL_PT_PKT_DESC_MAX];
  152. const char *color = PERF_COLOR_BLUE;
  153. color_fprintf(stdout, color,
  154. ". ... Intel Processor Trace data: size %zu bytes\n",
  155. len);
  156. while (len) {
  157. ret = intel_pt_get_packet(buf, len, &packet);
  158. if (ret > 0)
  159. pkt_len = ret;
  160. else
  161. pkt_len = 1;
  162. printf(".");
  163. color_fprintf(stdout, color, " %08x: ", pos);
  164. for (i = 0; i < pkt_len; i++)
  165. color_fprintf(stdout, color, " %02x", buf[i]);
  166. for (; i < 16; i++)
  167. color_fprintf(stdout, color, " ");
  168. if (ret > 0) {
  169. ret = intel_pt_pkt_desc(&packet, desc,
  170. INTEL_PT_PKT_DESC_MAX);
  171. if (ret > 0)
  172. color_fprintf(stdout, color, " %s\n", desc);
  173. } else {
  174. color_fprintf(stdout, color, " Bad packet!\n");
  175. }
  176. pos += pkt_len;
  177. buf += pkt_len;
  178. len -= pkt_len;
  179. }
  180. }
  181. static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
  182. size_t len)
  183. {
  184. printf(".\n");
  185. intel_pt_dump(pt, buf, len);
  186. }
  187. static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
  188. struct auxtrace_buffer *b)
  189. {
  190. bool consecutive = false;
  191. void *start;
  192. start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
  193. pt->have_tsc, &consecutive);
  194. if (!start)
  195. return -EINVAL;
  196. b->use_size = b->data + b->size - start;
  197. b->use_data = start;
  198. if (b->use_size && consecutive)
  199. b->consecutive = true;
  200. return 0;
  201. }
  202. /* This function assumes data is processed sequentially only */
  203. static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
  204. {
  205. struct intel_pt_queue *ptq = data;
  206. struct auxtrace_buffer *buffer = ptq->buffer;
  207. struct auxtrace_buffer *old_buffer = ptq->old_buffer;
  208. struct auxtrace_queue *queue;
  209. bool might_overlap;
  210. if (ptq->stop) {
  211. b->len = 0;
  212. return 0;
  213. }
  214. queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
  215. buffer = auxtrace_buffer__next(queue, buffer);
  216. if (!buffer) {
  217. if (old_buffer)
  218. auxtrace_buffer__drop_data(old_buffer);
  219. b->len = 0;
  220. return 0;
  221. }
  222. ptq->buffer = buffer;
  223. if (!buffer->data) {
  224. int fd = perf_data__fd(ptq->pt->session->data);
  225. buffer->data = auxtrace_buffer__get_data(buffer, fd);
  226. if (!buffer->data)
  227. return -ENOMEM;
  228. }
  229. might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
  230. if (might_overlap && !buffer->consecutive && old_buffer &&
  231. intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
  232. return -ENOMEM;
  233. if (buffer->use_data) {
  234. b->len = buffer->use_size;
  235. b->buf = buffer->use_data;
  236. } else {
  237. b->len = buffer->size;
  238. b->buf = buffer->data;
  239. }
  240. b->ref_timestamp = buffer->reference;
  241. if (!old_buffer || (might_overlap && !buffer->consecutive)) {
  242. b->consecutive = false;
  243. b->trace_nr = buffer->buffer_nr + 1;
  244. } else {
  245. b->consecutive = true;
  246. }
  247. if (ptq->step_through_buffers)
  248. ptq->stop = true;
  249. if (b->len) {
  250. if (old_buffer)
  251. auxtrace_buffer__drop_data(old_buffer);
  252. ptq->old_buffer = buffer;
  253. } else {
  254. auxtrace_buffer__drop_data(buffer);
  255. return intel_pt_get_trace(b, data);
  256. }
  257. return 0;
  258. }
  259. struct intel_pt_cache_entry {
  260. struct auxtrace_cache_entry entry;
  261. u64 insn_cnt;
  262. u64 byte_cnt;
  263. enum intel_pt_insn_op op;
  264. enum intel_pt_insn_branch branch;
  265. int length;
  266. int32_t rel;
  267. char insn[INTEL_PT_INSN_BUF_SZ];
  268. };
  269. static int intel_pt_config_div(const char *var, const char *value, void *data)
  270. {
  271. int *d = data;
  272. long val;
  273. if (!strcmp(var, "intel-pt.cache-divisor")) {
  274. val = strtol(value, NULL, 0);
  275. if (val > 0 && val <= INT_MAX)
  276. *d = val;
  277. }
  278. return 0;
  279. }
  280. static int intel_pt_cache_divisor(void)
  281. {
  282. static int d;
  283. if (d)
  284. return d;
  285. perf_config(intel_pt_config_div, &d);
  286. if (!d)
  287. d = 64;
  288. return d;
  289. }
  290. static unsigned int intel_pt_cache_size(struct dso *dso,
  291. struct machine *machine)
  292. {
  293. off_t size;
  294. size = dso__data_size(dso, machine);
  295. size /= intel_pt_cache_divisor();
  296. if (size < 1000)
  297. return 10;
  298. if (size > (1 << 21))
  299. return 21;
  300. return 32 - __builtin_clz(size);
  301. }
  302. static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
  303. struct machine *machine)
  304. {
  305. struct auxtrace_cache *c;
  306. unsigned int bits;
  307. if (dso->auxtrace_cache)
  308. return dso->auxtrace_cache;
  309. bits = intel_pt_cache_size(dso, machine);
  310. /* Ignoring cache creation failure */
  311. c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
  312. dso->auxtrace_cache = c;
  313. return c;
  314. }
  315. static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
  316. u64 offset, u64 insn_cnt, u64 byte_cnt,
  317. struct intel_pt_insn *intel_pt_insn)
  318. {
  319. struct auxtrace_cache *c = intel_pt_cache(dso, machine);
  320. struct intel_pt_cache_entry *e;
  321. int err;
  322. if (!c)
  323. return -ENOMEM;
  324. e = auxtrace_cache__alloc_entry(c);
  325. if (!e)
  326. return -ENOMEM;
  327. e->insn_cnt = insn_cnt;
  328. e->byte_cnt = byte_cnt;
  329. e->op = intel_pt_insn->op;
  330. e->branch = intel_pt_insn->branch;
  331. e->length = intel_pt_insn->length;
  332. e->rel = intel_pt_insn->rel;
  333. memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
  334. err = auxtrace_cache__add(c, offset, &e->entry);
  335. if (err)
  336. auxtrace_cache__free_entry(c, e);
  337. return err;
  338. }
  339. static struct intel_pt_cache_entry *
  340. intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
  341. {
  342. struct auxtrace_cache *c = intel_pt_cache(dso, machine);
  343. if (!c)
  344. return NULL;
  345. return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
  346. }
  347. static inline u8 intel_pt_cpumode(struct intel_pt *pt, uint64_t ip)
  348. {
  349. return ip >= pt->kernel_start ?
  350. PERF_RECORD_MISC_KERNEL :
  351. PERF_RECORD_MISC_USER;
  352. }
  353. static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
  354. uint64_t *insn_cnt_ptr, uint64_t *ip,
  355. uint64_t to_ip, uint64_t max_insn_cnt,
  356. void *data)
  357. {
  358. struct intel_pt_queue *ptq = data;
  359. struct machine *machine = ptq->pt->machine;
  360. struct thread *thread;
  361. struct addr_location al;
  362. unsigned char buf[INTEL_PT_INSN_BUF_SZ];
  363. ssize_t len;
  364. int x86_64;
  365. u8 cpumode;
  366. u64 offset, start_offset, start_ip;
  367. u64 insn_cnt = 0;
  368. bool one_map = true;
  369. intel_pt_insn->length = 0;
  370. if (to_ip && *ip == to_ip)
  371. goto out_no_cache;
  372. cpumode = intel_pt_cpumode(ptq->pt, *ip);
  373. thread = ptq->thread;
  374. if (!thread) {
  375. if (cpumode != PERF_RECORD_MISC_KERNEL)
  376. return -EINVAL;
  377. thread = ptq->pt->unknown_thread;
  378. }
  379. while (1) {
  380. if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso)
  381. return -EINVAL;
  382. if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
  383. dso__data_status_seen(al.map->dso,
  384. DSO_DATA_STATUS_SEEN_ITRACE))
  385. return -ENOENT;
  386. offset = al.map->map_ip(al.map, *ip);
  387. if (!to_ip && one_map) {
  388. struct intel_pt_cache_entry *e;
  389. e = intel_pt_cache_lookup(al.map->dso, machine, offset);
  390. if (e &&
  391. (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
  392. *insn_cnt_ptr = e->insn_cnt;
  393. *ip += e->byte_cnt;
  394. intel_pt_insn->op = e->op;
  395. intel_pt_insn->branch = e->branch;
  396. intel_pt_insn->length = e->length;
  397. intel_pt_insn->rel = e->rel;
  398. memcpy(intel_pt_insn->buf, e->insn,
  399. INTEL_PT_INSN_BUF_SZ);
  400. intel_pt_log_insn_no_data(intel_pt_insn, *ip);
  401. return 0;
  402. }
  403. }
  404. start_offset = offset;
  405. start_ip = *ip;
  406. /* Load maps to ensure dso->is_64_bit has been updated */
  407. map__load(al.map);
  408. x86_64 = al.map->dso->is_64_bit;
  409. while (1) {
  410. len = dso__data_read_offset(al.map->dso, machine,
  411. offset, buf,
  412. INTEL_PT_INSN_BUF_SZ);
  413. if (len <= 0)
  414. return -EINVAL;
  415. if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
  416. return -EINVAL;
  417. intel_pt_log_insn(intel_pt_insn, *ip);
  418. insn_cnt += 1;
  419. if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
  420. goto out;
  421. if (max_insn_cnt && insn_cnt >= max_insn_cnt)
  422. goto out_no_cache;
  423. *ip += intel_pt_insn->length;
  424. if (to_ip && *ip == to_ip)
  425. goto out_no_cache;
  426. if (*ip >= al.map->end)
  427. break;
  428. offset += intel_pt_insn->length;
  429. }
  430. one_map = false;
  431. }
  432. out:
  433. *insn_cnt_ptr = insn_cnt;
  434. if (!one_map)
  435. goto out_no_cache;
  436. /*
  437. * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
  438. * entries.
  439. */
  440. if (to_ip) {
  441. struct intel_pt_cache_entry *e;
  442. e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
  443. if (e)
  444. return 0;
  445. }
  446. /* Ignore cache errors */
  447. intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
  448. *ip - start_ip, intel_pt_insn);
  449. return 0;
  450. out_no_cache:
  451. *insn_cnt_ptr = insn_cnt;
  452. return 0;
  453. }
  454. static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
  455. uint64_t offset, const char *filename)
  456. {
  457. struct addr_filter *filt;
  458. bool have_filter = false;
  459. bool hit_tracestop = false;
  460. bool hit_filter = false;
  461. list_for_each_entry(filt, &pt->filts.head, list) {
  462. if (filt->start)
  463. have_filter = true;
  464. if ((filename && !filt->filename) ||
  465. (!filename && filt->filename) ||
  466. (filename && strcmp(filename, filt->filename)))
  467. continue;
  468. if (!(offset >= filt->addr && offset < filt->addr + filt->size))
  469. continue;
  470. intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
  471. ip, offset, filename ? filename : "[kernel]",
  472. filt->start ? "filter" : "stop",
  473. filt->addr, filt->size);
  474. if (filt->start)
  475. hit_filter = true;
  476. else
  477. hit_tracestop = true;
  478. }
  479. if (!hit_tracestop && !hit_filter)
  480. intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
  481. ip, offset, filename ? filename : "[kernel]");
  482. return hit_tracestop || (have_filter && !hit_filter);
  483. }
  484. static int __intel_pt_pgd_ip(uint64_t ip, void *data)
  485. {
  486. struct intel_pt_queue *ptq = data;
  487. struct thread *thread;
  488. struct addr_location al;
  489. u8 cpumode;
  490. u64 offset;
  491. if (ip >= ptq->pt->kernel_start)
  492. return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
  493. cpumode = PERF_RECORD_MISC_USER;
  494. thread = ptq->thread;
  495. if (!thread)
  496. return -EINVAL;
  497. if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
  498. return -EINVAL;
  499. offset = al.map->map_ip(al.map, ip);
  500. return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
  501. al.map->dso->long_name);
  502. }
  503. static bool intel_pt_pgd_ip(uint64_t ip, void *data)
  504. {
  505. return __intel_pt_pgd_ip(ip, data) > 0;
  506. }
  507. static bool intel_pt_get_config(struct intel_pt *pt,
  508. struct perf_event_attr *attr, u64 *config)
  509. {
  510. if (attr->type == pt->pmu_type) {
  511. if (config)
  512. *config = attr->config;
  513. return true;
  514. }
  515. return false;
  516. }
  517. static bool intel_pt_exclude_kernel(struct intel_pt *pt)
  518. {
  519. struct perf_evsel *evsel;
  520. evlist__for_each_entry(pt->session->evlist, evsel) {
  521. if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
  522. !evsel->attr.exclude_kernel)
  523. return false;
  524. }
  525. return true;
  526. }
  527. static bool intel_pt_return_compression(struct intel_pt *pt)
  528. {
  529. struct perf_evsel *evsel;
  530. u64 config;
  531. if (!pt->noretcomp_bit)
  532. return true;
  533. evlist__for_each_entry(pt->session->evlist, evsel) {
  534. if (intel_pt_get_config(pt, &evsel->attr, &config) &&
  535. (config & pt->noretcomp_bit))
  536. return false;
  537. }
  538. return true;
  539. }
  540. static bool intel_pt_branch_enable(struct intel_pt *pt)
  541. {
  542. struct perf_evsel *evsel;
  543. u64 config;
  544. evlist__for_each_entry(pt->session->evlist, evsel) {
  545. if (intel_pt_get_config(pt, &evsel->attr, &config) &&
  546. (config & 1) && !(config & 0x2000))
  547. return false;
  548. }
  549. return true;
  550. }
  551. static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
  552. {
  553. struct perf_evsel *evsel;
  554. unsigned int shift;
  555. u64 config;
  556. if (!pt->mtc_freq_bits)
  557. return 0;
  558. for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
  559. config >>= 1;
  560. evlist__for_each_entry(pt->session->evlist, evsel) {
  561. if (intel_pt_get_config(pt, &evsel->attr, &config))
  562. return (config & pt->mtc_freq_bits) >> shift;
  563. }
  564. return 0;
  565. }
  566. static bool intel_pt_timeless_decoding(struct intel_pt *pt)
  567. {
  568. struct perf_evsel *evsel;
  569. bool timeless_decoding = true;
  570. u64 config;
  571. if (!pt->tsc_bit || !pt->cap_user_time_zero)
  572. return true;
  573. evlist__for_each_entry(pt->session->evlist, evsel) {
  574. if (!(evsel->attr.sample_type & PERF_SAMPLE_TIME))
  575. return true;
  576. if (intel_pt_get_config(pt, &evsel->attr, &config)) {
  577. if (config & pt->tsc_bit)
  578. timeless_decoding = false;
  579. else
  580. return true;
  581. }
  582. }
  583. return timeless_decoding;
  584. }
  585. static bool intel_pt_tracing_kernel(struct intel_pt *pt)
  586. {
  587. struct perf_evsel *evsel;
  588. evlist__for_each_entry(pt->session->evlist, evsel) {
  589. if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
  590. !evsel->attr.exclude_kernel)
  591. return true;
  592. }
  593. return false;
  594. }
  595. static bool intel_pt_have_tsc(struct intel_pt *pt)
  596. {
  597. struct perf_evsel *evsel;
  598. bool have_tsc = false;
  599. u64 config;
  600. if (!pt->tsc_bit)
  601. return false;
  602. evlist__for_each_entry(pt->session->evlist, evsel) {
  603. if (intel_pt_get_config(pt, &evsel->attr, &config)) {
  604. if (config & pt->tsc_bit)
  605. have_tsc = true;
  606. else
  607. return false;
  608. }
  609. }
  610. return have_tsc;
  611. }
  612. static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
  613. {
  614. u64 quot, rem;
  615. quot = ns / pt->tc.time_mult;
  616. rem = ns % pt->tc.time_mult;
  617. return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
  618. pt->tc.time_mult;
  619. }
  620. static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
  621. unsigned int queue_nr)
  622. {
  623. struct intel_pt_params params = { .get_trace = 0, };
  624. struct perf_env *env = pt->machine->env;
  625. struct intel_pt_queue *ptq;
  626. ptq = zalloc(sizeof(struct intel_pt_queue));
  627. if (!ptq)
  628. return NULL;
  629. if (pt->synth_opts.callchain) {
  630. size_t sz = sizeof(struct ip_callchain);
  631. /* Add 1 to callchain_sz for callchain context */
  632. sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64);
  633. ptq->chain = zalloc(sz);
  634. if (!ptq->chain)
  635. goto out_free;
  636. }
  637. if (pt->synth_opts.last_branch) {
  638. size_t sz = sizeof(struct branch_stack);
  639. sz += pt->synth_opts.last_branch_sz *
  640. sizeof(struct branch_entry);
  641. ptq->last_branch = zalloc(sz);
  642. if (!ptq->last_branch)
  643. goto out_free;
  644. ptq->last_branch_rb = zalloc(sz);
  645. if (!ptq->last_branch_rb)
  646. goto out_free;
  647. }
  648. ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
  649. if (!ptq->event_buf)
  650. goto out_free;
  651. ptq->pt = pt;
  652. ptq->queue_nr = queue_nr;
  653. ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
  654. ptq->pid = -1;
  655. ptq->tid = -1;
  656. ptq->cpu = -1;
  657. ptq->next_tid = -1;
  658. params.get_trace = intel_pt_get_trace;
  659. params.walk_insn = intel_pt_walk_next_insn;
  660. params.data = ptq;
  661. params.return_compression = intel_pt_return_compression(pt);
  662. params.branch_enable = intel_pt_branch_enable(pt);
  663. params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
  664. params.mtc_period = intel_pt_mtc_period(pt);
  665. params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
  666. params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
  667. if (pt->filts.cnt > 0)
  668. params.pgd_ip = intel_pt_pgd_ip;
  669. if (pt->synth_opts.instructions) {
  670. if (pt->synth_opts.period) {
  671. switch (pt->synth_opts.period_type) {
  672. case PERF_ITRACE_PERIOD_INSTRUCTIONS:
  673. params.period_type =
  674. INTEL_PT_PERIOD_INSTRUCTIONS;
  675. params.period = pt->synth_opts.period;
  676. break;
  677. case PERF_ITRACE_PERIOD_TICKS:
  678. params.period_type = INTEL_PT_PERIOD_TICKS;
  679. params.period = pt->synth_opts.period;
  680. break;
  681. case PERF_ITRACE_PERIOD_NANOSECS:
  682. params.period_type = INTEL_PT_PERIOD_TICKS;
  683. params.period = intel_pt_ns_to_ticks(pt,
  684. pt->synth_opts.period);
  685. break;
  686. default:
  687. break;
  688. }
  689. }
  690. if (!params.period) {
  691. params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
  692. params.period = 1;
  693. }
  694. }
  695. if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
  696. params.flags |= INTEL_PT_FUP_WITH_NLIP;
  697. ptq->decoder = intel_pt_decoder_new(&params);
  698. if (!ptq->decoder)
  699. goto out_free;
  700. return ptq;
  701. out_free:
  702. zfree(&ptq->event_buf);
  703. zfree(&ptq->last_branch);
  704. zfree(&ptq->last_branch_rb);
  705. zfree(&ptq->chain);
  706. free(ptq);
  707. return NULL;
  708. }
  709. static void intel_pt_free_queue(void *priv)
  710. {
  711. struct intel_pt_queue *ptq = priv;
  712. if (!ptq)
  713. return;
  714. thread__zput(ptq->thread);
  715. intel_pt_decoder_free(ptq->decoder);
  716. zfree(&ptq->event_buf);
  717. zfree(&ptq->last_branch);
  718. zfree(&ptq->last_branch_rb);
  719. zfree(&ptq->chain);
  720. free(ptq);
  721. }
  722. static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
  723. struct auxtrace_queue *queue)
  724. {
  725. struct intel_pt_queue *ptq = queue->priv;
  726. if (queue->tid == -1 || pt->have_sched_switch) {
  727. ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
  728. thread__zput(ptq->thread);
  729. }
  730. if (!ptq->thread && ptq->tid != -1)
  731. ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
  732. if (ptq->thread) {
  733. ptq->pid = ptq->thread->pid_;
  734. if (queue->cpu == -1)
  735. ptq->cpu = ptq->thread->cpu;
  736. }
  737. }
  738. static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
  739. {
  740. if (ptq->state->flags & INTEL_PT_ABORT_TX) {
  741. ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
  742. } else if (ptq->state->flags & INTEL_PT_ASYNC) {
  743. if (ptq->state->to_ip)
  744. ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
  745. PERF_IP_FLAG_ASYNC |
  746. PERF_IP_FLAG_INTERRUPT;
  747. else
  748. ptq->flags = PERF_IP_FLAG_BRANCH |
  749. PERF_IP_FLAG_TRACE_END;
  750. ptq->insn_len = 0;
  751. } else {
  752. if (ptq->state->from_ip)
  753. ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
  754. else
  755. ptq->flags = PERF_IP_FLAG_BRANCH |
  756. PERF_IP_FLAG_TRACE_BEGIN;
  757. if (ptq->state->flags & INTEL_PT_IN_TX)
  758. ptq->flags |= PERF_IP_FLAG_IN_TX;
  759. ptq->insn_len = ptq->state->insn_len;
  760. memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
  761. }
  762. }
  763. static int intel_pt_setup_queue(struct intel_pt *pt,
  764. struct auxtrace_queue *queue,
  765. unsigned int queue_nr)
  766. {
  767. struct intel_pt_queue *ptq = queue->priv;
  768. if (list_empty(&queue->head))
  769. return 0;
  770. if (!ptq) {
  771. ptq = intel_pt_alloc_queue(pt, queue_nr);
  772. if (!ptq)
  773. return -ENOMEM;
  774. queue->priv = ptq;
  775. if (queue->cpu != -1)
  776. ptq->cpu = queue->cpu;
  777. ptq->tid = queue->tid;
  778. if (pt->sampling_mode && !pt->snapshot_mode &&
  779. pt->timeless_decoding)
  780. ptq->step_through_buffers = true;
  781. ptq->sync_switch = pt->sync_switch;
  782. }
  783. if (!ptq->on_heap &&
  784. (!ptq->sync_switch ||
  785. ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
  786. const struct intel_pt_state *state;
  787. int ret;
  788. if (pt->timeless_decoding)
  789. return 0;
  790. intel_pt_log("queue %u getting timestamp\n", queue_nr);
  791. intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
  792. queue_nr, ptq->cpu, ptq->pid, ptq->tid);
  793. while (1) {
  794. state = intel_pt_decode(ptq->decoder);
  795. if (state->err) {
  796. if (state->err == INTEL_PT_ERR_NODATA) {
  797. intel_pt_log("queue %u has no timestamp\n",
  798. queue_nr);
  799. return 0;
  800. }
  801. continue;
  802. }
  803. if (state->timestamp)
  804. break;
  805. }
  806. ptq->timestamp = state->timestamp;
  807. intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
  808. queue_nr, ptq->timestamp);
  809. ptq->state = state;
  810. ptq->have_sample = true;
  811. intel_pt_sample_flags(ptq);
  812. ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
  813. if (ret)
  814. return ret;
  815. ptq->on_heap = true;
  816. }
  817. return 0;
  818. }
  819. static int intel_pt_setup_queues(struct intel_pt *pt)
  820. {
  821. unsigned int i;
  822. int ret;
  823. for (i = 0; i < pt->queues.nr_queues; i++) {
  824. ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
  825. if (ret)
  826. return ret;
  827. }
  828. return 0;
  829. }
  830. static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
  831. {
  832. struct branch_stack *bs_src = ptq->last_branch_rb;
  833. struct branch_stack *bs_dst = ptq->last_branch;
  834. size_t nr = 0;
  835. bs_dst->nr = bs_src->nr;
  836. if (!bs_src->nr)
  837. return;
  838. nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
  839. memcpy(&bs_dst->entries[0],
  840. &bs_src->entries[ptq->last_branch_pos],
  841. sizeof(struct branch_entry) * nr);
  842. if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
  843. memcpy(&bs_dst->entries[nr],
  844. &bs_src->entries[0],
  845. sizeof(struct branch_entry) * ptq->last_branch_pos);
  846. }
  847. }
  848. static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
  849. {
  850. ptq->last_branch_pos = 0;
  851. ptq->last_branch_rb->nr = 0;
  852. }
  853. static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
  854. {
  855. const struct intel_pt_state *state = ptq->state;
  856. struct branch_stack *bs = ptq->last_branch_rb;
  857. struct branch_entry *be;
  858. if (!ptq->last_branch_pos)
  859. ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
  860. ptq->last_branch_pos -= 1;
  861. be = &bs->entries[ptq->last_branch_pos];
  862. be->from = state->from_ip;
  863. be->to = state->to_ip;
  864. be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
  865. be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
  866. /* No support for mispredict */
  867. be->flags.mispred = ptq->pt->mispred_all;
  868. if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
  869. bs->nr += 1;
  870. }
  871. static inline bool intel_pt_skip_event(struct intel_pt *pt)
  872. {
  873. return pt->synth_opts.initial_skip &&
  874. pt->num_events++ < pt->synth_opts.initial_skip;
  875. }
  876. static void intel_pt_prep_b_sample(struct intel_pt *pt,
  877. struct intel_pt_queue *ptq,
  878. union perf_event *event,
  879. struct perf_sample *sample)
  880. {
  881. if (!pt->timeless_decoding)
  882. sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
  883. sample->ip = ptq->state->from_ip;
  884. sample->cpumode = intel_pt_cpumode(pt, sample->ip);
  885. sample->pid = ptq->pid;
  886. sample->tid = ptq->tid;
  887. sample->addr = ptq->state->to_ip;
  888. sample->period = 1;
  889. sample->cpu = ptq->cpu;
  890. sample->flags = ptq->flags;
  891. sample->insn_len = ptq->insn_len;
  892. memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
  893. event->sample.header.type = PERF_RECORD_SAMPLE;
  894. event->sample.header.misc = sample->cpumode;
  895. event->sample.header.size = sizeof(struct perf_event_header);
  896. }
  897. static int intel_pt_inject_event(union perf_event *event,
  898. struct perf_sample *sample, u64 type)
  899. {
  900. event->header.size = perf_event__sample_event_size(sample, type, 0);
  901. return perf_event__synthesize_sample(event, type, 0, sample);
  902. }
  903. static inline int intel_pt_opt_inject(struct intel_pt *pt,
  904. union perf_event *event,
  905. struct perf_sample *sample, u64 type)
  906. {
  907. if (!pt->synth_opts.inject)
  908. return 0;
  909. return intel_pt_inject_event(event, sample, type);
  910. }
  911. static int intel_pt_deliver_synth_b_event(struct intel_pt *pt,
  912. union perf_event *event,
  913. struct perf_sample *sample, u64 type)
  914. {
  915. int ret;
  916. ret = intel_pt_opt_inject(pt, event, sample, type);
  917. if (ret)
  918. return ret;
  919. ret = perf_session__deliver_synth_event(pt->session, event, sample);
  920. if (ret)
  921. pr_err("Intel PT: failed to deliver event, error %d\n", ret);
  922. return ret;
  923. }
  924. static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
  925. {
  926. struct intel_pt *pt = ptq->pt;
  927. union perf_event *event = ptq->event_buf;
  928. struct perf_sample sample = { .ip = 0, };
  929. struct dummy_branch_stack {
  930. u64 nr;
  931. struct branch_entry entries;
  932. } dummy_bs;
  933. if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
  934. return 0;
  935. if (intel_pt_skip_event(pt))
  936. return 0;
  937. intel_pt_prep_b_sample(pt, ptq, event, &sample);
  938. sample.id = ptq->pt->branches_id;
  939. sample.stream_id = ptq->pt->branches_id;
  940. /*
  941. * perf report cannot handle events without a branch stack when using
  942. * SORT_MODE__BRANCH so make a dummy one.
  943. */
  944. if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
  945. dummy_bs = (struct dummy_branch_stack){
  946. .nr = 1,
  947. .entries = {
  948. .from = sample.ip,
  949. .to = sample.addr,
  950. },
  951. };
  952. sample.branch_stack = (struct branch_stack *)&dummy_bs;
  953. }
  954. return intel_pt_deliver_synth_b_event(pt, event, &sample,
  955. pt->branches_sample_type);
  956. }
  957. static void intel_pt_prep_sample(struct intel_pt *pt,
  958. struct intel_pt_queue *ptq,
  959. union perf_event *event,
  960. struct perf_sample *sample)
  961. {
  962. intel_pt_prep_b_sample(pt, ptq, event, sample);
  963. if (pt->synth_opts.callchain) {
  964. thread_stack__sample(ptq->thread, ptq->chain,
  965. pt->synth_opts.callchain_sz + 1,
  966. sample->ip, pt->kernel_start);
  967. sample->callchain = ptq->chain;
  968. }
  969. if (pt->synth_opts.last_branch) {
  970. intel_pt_copy_last_branch_rb(ptq);
  971. sample->branch_stack = ptq->last_branch;
  972. }
  973. }
  974. static inline int intel_pt_deliver_synth_event(struct intel_pt *pt,
  975. struct intel_pt_queue *ptq,
  976. union perf_event *event,
  977. struct perf_sample *sample,
  978. u64 type)
  979. {
  980. int ret;
  981. ret = intel_pt_deliver_synth_b_event(pt, event, sample, type);
  982. if (pt->synth_opts.last_branch)
  983. intel_pt_reset_last_branch_rb(ptq);
  984. return ret;
  985. }
  986. static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
  987. {
  988. struct intel_pt *pt = ptq->pt;
  989. union perf_event *event = ptq->event_buf;
  990. struct perf_sample sample = { .ip = 0, };
  991. if (intel_pt_skip_event(pt))
  992. return 0;
  993. intel_pt_prep_sample(pt, ptq, event, &sample);
  994. sample.id = ptq->pt->instructions_id;
  995. sample.stream_id = ptq->pt->instructions_id;
  996. sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
  997. ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
  998. return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
  999. pt->instructions_sample_type);
  1000. }
  1001. static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
  1002. {
  1003. struct intel_pt *pt = ptq->pt;
  1004. union perf_event *event = ptq->event_buf;
  1005. struct perf_sample sample = { .ip = 0, };
  1006. if (intel_pt_skip_event(pt))
  1007. return 0;
  1008. intel_pt_prep_sample(pt, ptq, event, &sample);
  1009. sample.id = ptq->pt->transactions_id;
  1010. sample.stream_id = ptq->pt->transactions_id;
  1011. return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
  1012. pt->transactions_sample_type);
  1013. }
  1014. static void intel_pt_prep_p_sample(struct intel_pt *pt,
  1015. struct intel_pt_queue *ptq,
  1016. union perf_event *event,
  1017. struct perf_sample *sample)
  1018. {
  1019. intel_pt_prep_sample(pt, ptq, event, sample);
  1020. /*
  1021. * Zero IP is used to mean "trace start" but that is not the case for
  1022. * power or PTWRITE events with no IP, so clear the flags.
  1023. */
  1024. if (!sample->ip)
  1025. sample->flags = 0;
  1026. }
  1027. static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
  1028. {
  1029. struct intel_pt *pt = ptq->pt;
  1030. union perf_event *event = ptq->event_buf;
  1031. struct perf_sample sample = { .ip = 0, };
  1032. struct perf_synth_intel_ptwrite raw;
  1033. if (intel_pt_skip_event(pt))
  1034. return 0;
  1035. intel_pt_prep_p_sample(pt, ptq, event, &sample);
  1036. sample.id = ptq->pt->ptwrites_id;
  1037. sample.stream_id = ptq->pt->ptwrites_id;
  1038. raw.flags = 0;
  1039. raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
  1040. raw.payload = cpu_to_le64(ptq->state->ptw_payload);
  1041. sample.raw_size = perf_synth__raw_size(raw);
  1042. sample.raw_data = perf_synth__raw_data(&raw);
  1043. return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
  1044. pt->ptwrites_sample_type);
  1045. }
  1046. static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
  1047. {
  1048. struct intel_pt *pt = ptq->pt;
  1049. union perf_event *event = ptq->event_buf;
  1050. struct perf_sample sample = { .ip = 0, };
  1051. struct perf_synth_intel_cbr raw;
  1052. u32 flags;
  1053. if (intel_pt_skip_event(pt))
  1054. return 0;
  1055. intel_pt_prep_p_sample(pt, ptq, event, &sample);
  1056. sample.id = ptq->pt->cbr_id;
  1057. sample.stream_id = ptq->pt->cbr_id;
  1058. flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
  1059. raw.flags = cpu_to_le32(flags);
  1060. raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
  1061. raw.reserved3 = 0;
  1062. sample.raw_size = perf_synth__raw_size(raw);
  1063. sample.raw_data = perf_synth__raw_data(&raw);
  1064. return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
  1065. pt->pwr_events_sample_type);
  1066. }
  1067. static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
  1068. {
  1069. struct intel_pt *pt = ptq->pt;
  1070. union perf_event *event = ptq->event_buf;
  1071. struct perf_sample sample = { .ip = 0, };
  1072. struct perf_synth_intel_mwait raw;
  1073. if (intel_pt_skip_event(pt))
  1074. return 0;
  1075. intel_pt_prep_p_sample(pt, ptq, event, &sample);
  1076. sample.id = ptq->pt->mwait_id;
  1077. sample.stream_id = ptq->pt->mwait_id;
  1078. raw.reserved = 0;
  1079. raw.payload = cpu_to_le64(ptq->state->mwait_payload);
  1080. sample.raw_size = perf_synth__raw_size(raw);
  1081. sample.raw_data = perf_synth__raw_data(&raw);
  1082. return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
  1083. pt->pwr_events_sample_type);
  1084. }
  1085. static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
  1086. {
  1087. struct intel_pt *pt = ptq->pt;
  1088. union perf_event *event = ptq->event_buf;
  1089. struct perf_sample sample = { .ip = 0, };
  1090. struct perf_synth_intel_pwre raw;
  1091. if (intel_pt_skip_event(pt))
  1092. return 0;
  1093. intel_pt_prep_p_sample(pt, ptq, event, &sample);
  1094. sample.id = ptq->pt->pwre_id;
  1095. sample.stream_id = ptq->pt->pwre_id;
  1096. raw.reserved = 0;
  1097. raw.payload = cpu_to_le64(ptq->state->pwre_payload);
  1098. sample.raw_size = perf_synth__raw_size(raw);
  1099. sample.raw_data = perf_synth__raw_data(&raw);
  1100. return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
  1101. pt->pwr_events_sample_type);
  1102. }
  1103. static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
  1104. {
  1105. struct intel_pt *pt = ptq->pt;
  1106. union perf_event *event = ptq->event_buf;
  1107. struct perf_sample sample = { .ip = 0, };
  1108. struct perf_synth_intel_exstop raw;
  1109. if (intel_pt_skip_event(pt))
  1110. return 0;
  1111. intel_pt_prep_p_sample(pt, ptq, event, &sample);
  1112. sample.id = ptq->pt->exstop_id;
  1113. sample.stream_id = ptq->pt->exstop_id;
  1114. raw.flags = 0;
  1115. raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
  1116. sample.raw_size = perf_synth__raw_size(raw);
  1117. sample.raw_data = perf_synth__raw_data(&raw);
  1118. return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
  1119. pt->pwr_events_sample_type);
  1120. }
  1121. static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
  1122. {
  1123. struct intel_pt *pt = ptq->pt;
  1124. union perf_event *event = ptq->event_buf;
  1125. struct perf_sample sample = { .ip = 0, };
  1126. struct perf_synth_intel_pwrx raw;
  1127. if (intel_pt_skip_event(pt))
  1128. return 0;
  1129. intel_pt_prep_p_sample(pt, ptq, event, &sample);
  1130. sample.id = ptq->pt->pwrx_id;
  1131. sample.stream_id = ptq->pt->pwrx_id;
  1132. raw.reserved = 0;
  1133. raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
  1134. sample.raw_size = perf_synth__raw_size(raw);
  1135. sample.raw_data = perf_synth__raw_data(&raw);
  1136. return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
  1137. pt->pwr_events_sample_type);
  1138. }
  1139. static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
  1140. pid_t pid, pid_t tid, u64 ip)
  1141. {
  1142. union perf_event event;
  1143. char msg[MAX_AUXTRACE_ERROR_MSG];
  1144. int err;
  1145. intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
  1146. auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
  1147. code, cpu, pid, tid, ip, msg);
  1148. err = perf_session__deliver_synth_event(pt->session, &event, NULL);
  1149. if (err)
  1150. pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
  1151. err);
  1152. return err;
  1153. }
  1154. static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
  1155. {
  1156. struct auxtrace_queue *queue;
  1157. pid_t tid = ptq->next_tid;
  1158. int err;
  1159. if (tid == -1)
  1160. return 0;
  1161. intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
  1162. err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
  1163. queue = &pt->queues.queue_array[ptq->queue_nr];
  1164. intel_pt_set_pid_tid_cpu(pt, queue);
  1165. ptq->next_tid = -1;
  1166. return err;
  1167. }
  1168. static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
  1169. {
  1170. struct intel_pt *pt = ptq->pt;
  1171. return ip == pt->switch_ip &&
  1172. (ptq->flags & PERF_IP_FLAG_BRANCH) &&
  1173. !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
  1174. PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
  1175. }
  1176. #define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
  1177. INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT | \
  1178. INTEL_PT_CBR_CHG)
  1179. static int intel_pt_sample(struct intel_pt_queue *ptq)
  1180. {
  1181. const struct intel_pt_state *state = ptq->state;
  1182. struct intel_pt *pt = ptq->pt;
  1183. int err;
  1184. if (!ptq->have_sample)
  1185. return 0;
  1186. ptq->have_sample = false;
  1187. if (pt->sample_pwr_events && (state->type & INTEL_PT_PWR_EVT)) {
  1188. if (state->type & INTEL_PT_CBR_CHG) {
  1189. err = intel_pt_synth_cbr_sample(ptq);
  1190. if (err)
  1191. return err;
  1192. }
  1193. if (state->type & INTEL_PT_MWAIT_OP) {
  1194. err = intel_pt_synth_mwait_sample(ptq);
  1195. if (err)
  1196. return err;
  1197. }
  1198. if (state->type & INTEL_PT_PWR_ENTRY) {
  1199. err = intel_pt_synth_pwre_sample(ptq);
  1200. if (err)
  1201. return err;
  1202. }
  1203. if (state->type & INTEL_PT_EX_STOP) {
  1204. err = intel_pt_synth_exstop_sample(ptq);
  1205. if (err)
  1206. return err;
  1207. }
  1208. if (state->type & INTEL_PT_PWR_EXIT) {
  1209. err = intel_pt_synth_pwrx_sample(ptq);
  1210. if (err)
  1211. return err;
  1212. }
  1213. }
  1214. if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
  1215. err = intel_pt_synth_instruction_sample(ptq);
  1216. if (err)
  1217. return err;
  1218. }
  1219. if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
  1220. err = intel_pt_synth_transaction_sample(ptq);
  1221. if (err)
  1222. return err;
  1223. }
  1224. if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
  1225. err = intel_pt_synth_ptwrite_sample(ptq);
  1226. if (err)
  1227. return err;
  1228. }
  1229. if (!(state->type & INTEL_PT_BRANCH))
  1230. return 0;
  1231. if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
  1232. thread_stack__event(ptq->thread, ptq->flags, state->from_ip,
  1233. state->to_ip, ptq->insn_len,
  1234. state->trace_nr);
  1235. else
  1236. thread_stack__set_trace_nr(ptq->thread, state->trace_nr);
  1237. if (pt->sample_branches) {
  1238. err = intel_pt_synth_branch_sample(ptq);
  1239. if (err)
  1240. return err;
  1241. }
  1242. if (pt->synth_opts.last_branch)
  1243. intel_pt_update_last_branch_rb(ptq);
  1244. if (!ptq->sync_switch)
  1245. return 0;
  1246. if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
  1247. switch (ptq->switch_state) {
  1248. case INTEL_PT_SS_NOT_TRACING:
  1249. case INTEL_PT_SS_UNKNOWN:
  1250. case INTEL_PT_SS_EXPECTING_SWITCH_IP:
  1251. err = intel_pt_next_tid(pt, ptq);
  1252. if (err)
  1253. return err;
  1254. ptq->switch_state = INTEL_PT_SS_TRACING;
  1255. break;
  1256. default:
  1257. ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
  1258. return 1;
  1259. }
  1260. } else if (!state->to_ip) {
  1261. ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
  1262. } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
  1263. ptq->switch_state = INTEL_PT_SS_UNKNOWN;
  1264. } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
  1265. state->to_ip == pt->ptss_ip &&
  1266. (ptq->flags & PERF_IP_FLAG_CALL)) {
  1267. ptq->switch_state = INTEL_PT_SS_TRACING;
  1268. }
  1269. return 0;
  1270. }
  1271. static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
  1272. {
  1273. struct machine *machine = pt->machine;
  1274. struct map *map;
  1275. struct symbol *sym, *start;
  1276. u64 ip, switch_ip = 0;
  1277. const char *ptss;
  1278. if (ptss_ip)
  1279. *ptss_ip = 0;
  1280. map = machine__kernel_map(machine);
  1281. if (!map)
  1282. return 0;
  1283. if (map__load(map))
  1284. return 0;
  1285. start = dso__first_symbol(map->dso);
  1286. for (sym = start; sym; sym = dso__next_symbol(sym)) {
  1287. if (sym->binding == STB_GLOBAL &&
  1288. !strcmp(sym->name, "__switch_to")) {
  1289. ip = map->unmap_ip(map, sym->start);
  1290. if (ip >= map->start && ip < map->end) {
  1291. switch_ip = ip;
  1292. break;
  1293. }
  1294. }
  1295. }
  1296. if (!switch_ip || !ptss_ip)
  1297. return 0;
  1298. if (pt->have_sched_switch == 1)
  1299. ptss = "perf_trace_sched_switch";
  1300. else
  1301. ptss = "__perf_event_task_sched_out";
  1302. for (sym = start; sym; sym = dso__next_symbol(sym)) {
  1303. if (!strcmp(sym->name, ptss)) {
  1304. ip = map->unmap_ip(map, sym->start);
  1305. if (ip >= map->start && ip < map->end) {
  1306. *ptss_ip = ip;
  1307. break;
  1308. }
  1309. }
  1310. }
  1311. return switch_ip;
  1312. }
  1313. static void intel_pt_enable_sync_switch(struct intel_pt *pt)
  1314. {
  1315. unsigned int i;
  1316. pt->sync_switch = true;
  1317. for (i = 0; i < pt->queues.nr_queues; i++) {
  1318. struct auxtrace_queue *queue = &pt->queues.queue_array[i];
  1319. struct intel_pt_queue *ptq = queue->priv;
  1320. if (ptq)
  1321. ptq->sync_switch = true;
  1322. }
  1323. }
  1324. static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
  1325. {
  1326. const struct intel_pt_state *state = ptq->state;
  1327. struct intel_pt *pt = ptq->pt;
  1328. int err;
  1329. if (!pt->kernel_start) {
  1330. pt->kernel_start = machine__kernel_start(pt->machine);
  1331. if (pt->per_cpu_mmaps &&
  1332. (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
  1333. !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
  1334. !pt->sampling_mode) {
  1335. pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
  1336. if (pt->switch_ip) {
  1337. intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
  1338. pt->switch_ip, pt->ptss_ip);
  1339. intel_pt_enable_sync_switch(pt);
  1340. }
  1341. }
  1342. }
  1343. intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
  1344. ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
  1345. while (1) {
  1346. err = intel_pt_sample(ptq);
  1347. if (err)
  1348. return err;
  1349. state = intel_pt_decode(ptq->decoder);
  1350. if (state->err) {
  1351. if (state->err == INTEL_PT_ERR_NODATA)
  1352. return 1;
  1353. if (ptq->sync_switch &&
  1354. state->from_ip >= pt->kernel_start) {
  1355. ptq->sync_switch = false;
  1356. intel_pt_next_tid(pt, ptq);
  1357. }
  1358. if (pt->synth_opts.errors) {
  1359. err = intel_pt_synth_error(pt, state->err,
  1360. ptq->cpu, ptq->pid,
  1361. ptq->tid,
  1362. state->from_ip);
  1363. if (err)
  1364. return err;
  1365. }
  1366. continue;
  1367. }
  1368. ptq->state = state;
  1369. ptq->have_sample = true;
  1370. intel_pt_sample_flags(ptq);
  1371. /* Use estimated TSC upon return to user space */
  1372. if (pt->est_tsc &&
  1373. (state->from_ip >= pt->kernel_start || !state->from_ip) &&
  1374. state->to_ip && state->to_ip < pt->kernel_start) {
  1375. intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
  1376. state->timestamp, state->est_timestamp);
  1377. ptq->timestamp = state->est_timestamp;
  1378. /* Use estimated TSC in unknown switch state */
  1379. } else if (ptq->sync_switch &&
  1380. ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
  1381. intel_pt_is_switch_ip(ptq, state->to_ip) &&
  1382. ptq->next_tid == -1) {
  1383. intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
  1384. state->timestamp, state->est_timestamp);
  1385. ptq->timestamp = state->est_timestamp;
  1386. } else if (state->timestamp > ptq->timestamp) {
  1387. ptq->timestamp = state->timestamp;
  1388. }
  1389. if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
  1390. *timestamp = ptq->timestamp;
  1391. return 0;
  1392. }
  1393. }
  1394. return 0;
  1395. }
  1396. static inline int intel_pt_update_queues(struct intel_pt *pt)
  1397. {
  1398. if (pt->queues.new_data) {
  1399. pt->queues.new_data = false;
  1400. return intel_pt_setup_queues(pt);
  1401. }
  1402. return 0;
  1403. }
  1404. static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
  1405. {
  1406. unsigned int queue_nr;
  1407. u64 ts;
  1408. int ret;
  1409. while (1) {
  1410. struct auxtrace_queue *queue;
  1411. struct intel_pt_queue *ptq;
  1412. if (!pt->heap.heap_cnt)
  1413. return 0;
  1414. if (pt->heap.heap_array[0].ordinal >= timestamp)
  1415. return 0;
  1416. queue_nr = pt->heap.heap_array[0].queue_nr;
  1417. queue = &pt->queues.queue_array[queue_nr];
  1418. ptq = queue->priv;
  1419. intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
  1420. queue_nr, pt->heap.heap_array[0].ordinal,
  1421. timestamp);
  1422. auxtrace_heap__pop(&pt->heap);
  1423. if (pt->heap.heap_cnt) {
  1424. ts = pt->heap.heap_array[0].ordinal + 1;
  1425. if (ts > timestamp)
  1426. ts = timestamp;
  1427. } else {
  1428. ts = timestamp;
  1429. }
  1430. intel_pt_set_pid_tid_cpu(pt, queue);
  1431. ret = intel_pt_run_decoder(ptq, &ts);
  1432. if (ret < 0) {
  1433. auxtrace_heap__add(&pt->heap, queue_nr, ts);
  1434. return ret;
  1435. }
  1436. if (!ret) {
  1437. ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
  1438. if (ret < 0)
  1439. return ret;
  1440. } else {
  1441. ptq->on_heap = false;
  1442. }
  1443. }
  1444. return 0;
  1445. }
  1446. static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
  1447. u64 time_)
  1448. {
  1449. struct auxtrace_queues *queues = &pt->queues;
  1450. unsigned int i;
  1451. u64 ts = 0;
  1452. for (i = 0; i < queues->nr_queues; i++) {
  1453. struct auxtrace_queue *queue = &pt->queues.queue_array[i];
  1454. struct intel_pt_queue *ptq = queue->priv;
  1455. if (ptq && (tid == -1 || ptq->tid == tid)) {
  1456. ptq->time = time_;
  1457. intel_pt_set_pid_tid_cpu(pt, queue);
  1458. intel_pt_run_decoder(ptq, &ts);
  1459. }
  1460. }
  1461. return 0;
  1462. }
  1463. static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
  1464. {
  1465. return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
  1466. sample->pid, sample->tid, 0);
  1467. }
  1468. static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
  1469. {
  1470. unsigned i, j;
  1471. if (cpu < 0 || !pt->queues.nr_queues)
  1472. return NULL;
  1473. if ((unsigned)cpu >= pt->queues.nr_queues)
  1474. i = pt->queues.nr_queues - 1;
  1475. else
  1476. i = cpu;
  1477. if (pt->queues.queue_array[i].cpu == cpu)
  1478. return pt->queues.queue_array[i].priv;
  1479. for (j = 0; i > 0; j++) {
  1480. if (pt->queues.queue_array[--i].cpu == cpu)
  1481. return pt->queues.queue_array[i].priv;
  1482. }
  1483. for (; j < pt->queues.nr_queues; j++) {
  1484. if (pt->queues.queue_array[j].cpu == cpu)
  1485. return pt->queues.queue_array[j].priv;
  1486. }
  1487. return NULL;
  1488. }
  1489. static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
  1490. u64 timestamp)
  1491. {
  1492. struct intel_pt_queue *ptq;
  1493. int err;
  1494. if (!pt->sync_switch)
  1495. return 1;
  1496. ptq = intel_pt_cpu_to_ptq(pt, cpu);
  1497. if (!ptq || !ptq->sync_switch)
  1498. return 1;
  1499. switch (ptq->switch_state) {
  1500. case INTEL_PT_SS_NOT_TRACING:
  1501. ptq->next_tid = -1;
  1502. break;
  1503. case INTEL_PT_SS_UNKNOWN:
  1504. case INTEL_PT_SS_TRACING:
  1505. ptq->next_tid = tid;
  1506. ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
  1507. return 0;
  1508. case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
  1509. if (!ptq->on_heap) {
  1510. ptq->timestamp = perf_time_to_tsc(timestamp,
  1511. &pt->tc);
  1512. err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
  1513. ptq->timestamp);
  1514. if (err)
  1515. return err;
  1516. ptq->on_heap = true;
  1517. }
  1518. ptq->switch_state = INTEL_PT_SS_TRACING;
  1519. break;
  1520. case INTEL_PT_SS_EXPECTING_SWITCH_IP:
  1521. ptq->next_tid = tid;
  1522. intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
  1523. break;
  1524. default:
  1525. break;
  1526. }
  1527. return 1;
  1528. }
  1529. static int intel_pt_process_switch(struct intel_pt *pt,
  1530. struct perf_sample *sample)
  1531. {
  1532. struct perf_evsel *evsel;
  1533. pid_t tid;
  1534. int cpu, ret;
  1535. evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
  1536. if (evsel != pt->switch_evsel)
  1537. return 0;
  1538. tid = perf_evsel__intval(evsel, sample, "next_pid");
  1539. cpu = sample->cpu;
  1540. intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
  1541. cpu, tid, sample->time, perf_time_to_tsc(sample->time,
  1542. &pt->tc));
  1543. ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
  1544. if (ret <= 0)
  1545. return ret;
  1546. return machine__set_current_tid(pt->machine, cpu, -1, tid);
  1547. }
  1548. static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
  1549. struct perf_sample *sample)
  1550. {
  1551. bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
  1552. pid_t pid, tid;
  1553. int cpu, ret;
  1554. cpu = sample->cpu;
  1555. if (pt->have_sched_switch == 3) {
  1556. if (!out)
  1557. return 0;
  1558. if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
  1559. pr_err("Expecting CPU-wide context switch event\n");
  1560. return -EINVAL;
  1561. }
  1562. pid = event->context_switch.next_prev_pid;
  1563. tid = event->context_switch.next_prev_tid;
  1564. } else {
  1565. if (out)
  1566. return 0;
  1567. pid = sample->pid;
  1568. tid = sample->tid;
  1569. }
  1570. if (tid == -1) {
  1571. pr_err("context_switch event has no tid\n");
  1572. return -EINVAL;
  1573. }
  1574. intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
  1575. cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
  1576. &pt->tc));
  1577. ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
  1578. if (ret <= 0)
  1579. return ret;
  1580. return machine__set_current_tid(pt->machine, cpu, pid, tid);
  1581. }
  1582. static int intel_pt_process_itrace_start(struct intel_pt *pt,
  1583. union perf_event *event,
  1584. struct perf_sample *sample)
  1585. {
  1586. if (!pt->per_cpu_mmaps)
  1587. return 0;
  1588. intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
  1589. sample->cpu, event->itrace_start.pid,
  1590. event->itrace_start.tid, sample->time,
  1591. perf_time_to_tsc(sample->time, &pt->tc));
  1592. return machine__set_current_tid(pt->machine, sample->cpu,
  1593. event->itrace_start.pid,
  1594. event->itrace_start.tid);
  1595. }
  1596. static int intel_pt_process_event(struct perf_session *session,
  1597. union perf_event *event,
  1598. struct perf_sample *sample,
  1599. struct perf_tool *tool)
  1600. {
  1601. struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
  1602. auxtrace);
  1603. u64 timestamp;
  1604. int err = 0;
  1605. if (dump_trace)
  1606. return 0;
  1607. if (!tool->ordered_events) {
  1608. pr_err("Intel Processor Trace requires ordered events\n");
  1609. return -EINVAL;
  1610. }
  1611. if (sample->time && sample->time != (u64)-1)
  1612. timestamp = perf_time_to_tsc(sample->time, &pt->tc);
  1613. else
  1614. timestamp = 0;
  1615. if (timestamp || pt->timeless_decoding) {
  1616. err = intel_pt_update_queues(pt);
  1617. if (err)
  1618. return err;
  1619. }
  1620. if (pt->timeless_decoding) {
  1621. if (event->header.type == PERF_RECORD_EXIT) {
  1622. err = intel_pt_process_timeless_queues(pt,
  1623. event->fork.tid,
  1624. sample->time);
  1625. }
  1626. } else if (timestamp) {
  1627. err = intel_pt_process_queues(pt, timestamp);
  1628. }
  1629. if (err)
  1630. return err;
  1631. if (event->header.type == PERF_RECORD_AUX &&
  1632. (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
  1633. pt->synth_opts.errors) {
  1634. err = intel_pt_lost(pt, sample);
  1635. if (err)
  1636. return err;
  1637. }
  1638. if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
  1639. err = intel_pt_process_switch(pt, sample);
  1640. else if (event->header.type == PERF_RECORD_ITRACE_START)
  1641. err = intel_pt_process_itrace_start(pt, event, sample);
  1642. else if (event->header.type == PERF_RECORD_SWITCH ||
  1643. event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
  1644. err = intel_pt_context_switch(pt, event, sample);
  1645. intel_pt_log("event %s (%u): cpu %d time %"PRIu64" tsc %#"PRIx64"\n",
  1646. perf_event__name(event->header.type), event->header.type,
  1647. sample->cpu, sample->time, timestamp);
  1648. return err;
  1649. }
  1650. static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
  1651. {
  1652. struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
  1653. auxtrace);
  1654. int ret;
  1655. if (dump_trace)
  1656. return 0;
  1657. if (!tool->ordered_events)
  1658. return -EINVAL;
  1659. ret = intel_pt_update_queues(pt);
  1660. if (ret < 0)
  1661. return ret;
  1662. if (pt->timeless_decoding)
  1663. return intel_pt_process_timeless_queues(pt, -1,
  1664. MAX_TIMESTAMP - 1);
  1665. return intel_pt_process_queues(pt, MAX_TIMESTAMP);
  1666. }
  1667. static void intel_pt_free_events(struct perf_session *session)
  1668. {
  1669. struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
  1670. auxtrace);
  1671. struct auxtrace_queues *queues = &pt->queues;
  1672. unsigned int i;
  1673. for (i = 0; i < queues->nr_queues; i++) {
  1674. intel_pt_free_queue(queues->queue_array[i].priv);
  1675. queues->queue_array[i].priv = NULL;
  1676. }
  1677. intel_pt_log_disable();
  1678. auxtrace_queues__free(queues);
  1679. }
  1680. static void intel_pt_free(struct perf_session *session)
  1681. {
  1682. struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
  1683. auxtrace);
  1684. auxtrace_heap__free(&pt->heap);
  1685. intel_pt_free_events(session);
  1686. session->auxtrace = NULL;
  1687. thread__put(pt->unknown_thread);
  1688. addr_filters__exit(&pt->filts);
  1689. zfree(&pt->filter);
  1690. free(pt);
  1691. }
  1692. static int intel_pt_process_auxtrace_event(struct perf_session *session,
  1693. union perf_event *event,
  1694. struct perf_tool *tool __maybe_unused)
  1695. {
  1696. struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
  1697. auxtrace);
  1698. if (!pt->data_queued) {
  1699. struct auxtrace_buffer *buffer;
  1700. off_t data_offset;
  1701. int fd = perf_data__fd(session->data);
  1702. int err;
  1703. if (perf_data__is_pipe(session->data)) {
  1704. data_offset = 0;
  1705. } else {
  1706. data_offset = lseek(fd, 0, SEEK_CUR);
  1707. if (data_offset == -1)
  1708. return -errno;
  1709. }
  1710. err = auxtrace_queues__add_event(&pt->queues, session, event,
  1711. data_offset, &buffer);
  1712. if (err)
  1713. return err;
  1714. /* Dump here now we have copied a piped trace out of the pipe */
  1715. if (dump_trace) {
  1716. if (auxtrace_buffer__get_data(buffer, fd)) {
  1717. intel_pt_dump_event(pt, buffer->data,
  1718. buffer->size);
  1719. auxtrace_buffer__put_data(buffer);
  1720. }
  1721. }
  1722. }
  1723. return 0;
  1724. }
  1725. struct intel_pt_synth {
  1726. struct perf_tool dummy_tool;
  1727. struct perf_session *session;
  1728. };
  1729. static int intel_pt_event_synth(struct perf_tool *tool,
  1730. union perf_event *event,
  1731. struct perf_sample *sample __maybe_unused,
  1732. struct machine *machine __maybe_unused)
  1733. {
  1734. struct intel_pt_synth *intel_pt_synth =
  1735. container_of(tool, struct intel_pt_synth, dummy_tool);
  1736. return perf_session__deliver_synth_event(intel_pt_synth->session, event,
  1737. NULL);
  1738. }
  1739. static int intel_pt_synth_event(struct perf_session *session, const char *name,
  1740. struct perf_event_attr *attr, u64 id)
  1741. {
  1742. struct intel_pt_synth intel_pt_synth;
  1743. int err;
  1744. pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
  1745. name, id, (u64)attr->sample_type);
  1746. memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
  1747. intel_pt_synth.session = session;
  1748. err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
  1749. &id, intel_pt_event_synth);
  1750. if (err)
  1751. pr_err("%s: failed to synthesize '%s' event type\n",
  1752. __func__, name);
  1753. return err;
  1754. }
  1755. static void intel_pt_set_event_name(struct perf_evlist *evlist, u64 id,
  1756. const char *name)
  1757. {
  1758. struct perf_evsel *evsel;
  1759. evlist__for_each_entry(evlist, evsel) {
  1760. if (evsel->id && evsel->id[0] == id) {
  1761. if (evsel->name)
  1762. zfree(&evsel->name);
  1763. evsel->name = strdup(name);
  1764. break;
  1765. }
  1766. }
  1767. }
  1768. static struct perf_evsel *intel_pt_evsel(struct intel_pt *pt,
  1769. struct perf_evlist *evlist)
  1770. {
  1771. struct perf_evsel *evsel;
  1772. evlist__for_each_entry(evlist, evsel) {
  1773. if (evsel->attr.type == pt->pmu_type && evsel->ids)
  1774. return evsel;
  1775. }
  1776. return NULL;
  1777. }
  1778. static int intel_pt_synth_events(struct intel_pt *pt,
  1779. struct perf_session *session)
  1780. {
  1781. struct perf_evlist *evlist = session->evlist;
  1782. struct perf_evsel *evsel = intel_pt_evsel(pt, evlist);
  1783. struct perf_event_attr attr;
  1784. u64 id;
  1785. int err;
  1786. if (!evsel) {
  1787. pr_debug("There are no selected events with Intel Processor Trace data\n");
  1788. return 0;
  1789. }
  1790. memset(&attr, 0, sizeof(struct perf_event_attr));
  1791. attr.size = sizeof(struct perf_event_attr);
  1792. attr.type = PERF_TYPE_HARDWARE;
  1793. attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
  1794. attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
  1795. PERF_SAMPLE_PERIOD;
  1796. if (pt->timeless_decoding)
  1797. attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
  1798. else
  1799. attr.sample_type |= PERF_SAMPLE_TIME;
  1800. if (!pt->per_cpu_mmaps)
  1801. attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
  1802. attr.exclude_user = evsel->attr.exclude_user;
  1803. attr.exclude_kernel = evsel->attr.exclude_kernel;
  1804. attr.exclude_hv = evsel->attr.exclude_hv;
  1805. attr.exclude_host = evsel->attr.exclude_host;
  1806. attr.exclude_guest = evsel->attr.exclude_guest;
  1807. attr.sample_id_all = evsel->attr.sample_id_all;
  1808. attr.read_format = evsel->attr.read_format;
  1809. id = evsel->id[0] + 1000000000;
  1810. if (!id)
  1811. id = 1;
  1812. if (pt->synth_opts.branches) {
  1813. attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
  1814. attr.sample_period = 1;
  1815. attr.sample_type |= PERF_SAMPLE_ADDR;
  1816. err = intel_pt_synth_event(session, "branches", &attr, id);
  1817. if (err)
  1818. return err;
  1819. pt->sample_branches = true;
  1820. pt->branches_sample_type = attr.sample_type;
  1821. pt->branches_id = id;
  1822. id += 1;
  1823. attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
  1824. }
  1825. if (pt->synth_opts.callchain)
  1826. attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
  1827. if (pt->synth_opts.last_branch)
  1828. attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
  1829. if (pt->synth_opts.instructions) {
  1830. attr.config = PERF_COUNT_HW_INSTRUCTIONS;
  1831. if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
  1832. attr.sample_period =
  1833. intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
  1834. else
  1835. attr.sample_period = pt->synth_opts.period;
  1836. err = intel_pt_synth_event(session, "instructions", &attr, id);
  1837. if (err)
  1838. return err;
  1839. pt->sample_instructions = true;
  1840. pt->instructions_sample_type = attr.sample_type;
  1841. pt->instructions_id = id;
  1842. id += 1;
  1843. }
  1844. attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
  1845. attr.sample_period = 1;
  1846. if (pt->synth_opts.transactions) {
  1847. attr.config = PERF_COUNT_HW_INSTRUCTIONS;
  1848. err = intel_pt_synth_event(session, "transactions", &attr, id);
  1849. if (err)
  1850. return err;
  1851. pt->sample_transactions = true;
  1852. pt->transactions_sample_type = attr.sample_type;
  1853. pt->transactions_id = id;
  1854. intel_pt_set_event_name(evlist, id, "transactions");
  1855. id += 1;
  1856. }
  1857. attr.type = PERF_TYPE_SYNTH;
  1858. attr.sample_type |= PERF_SAMPLE_RAW;
  1859. if (pt->synth_opts.ptwrites) {
  1860. attr.config = PERF_SYNTH_INTEL_PTWRITE;
  1861. err = intel_pt_synth_event(session, "ptwrite", &attr, id);
  1862. if (err)
  1863. return err;
  1864. pt->sample_ptwrites = true;
  1865. pt->ptwrites_sample_type = attr.sample_type;
  1866. pt->ptwrites_id = id;
  1867. intel_pt_set_event_name(evlist, id, "ptwrite");
  1868. id += 1;
  1869. }
  1870. if (pt->synth_opts.pwr_events) {
  1871. pt->sample_pwr_events = true;
  1872. pt->pwr_events_sample_type = attr.sample_type;
  1873. attr.config = PERF_SYNTH_INTEL_CBR;
  1874. err = intel_pt_synth_event(session, "cbr", &attr, id);
  1875. if (err)
  1876. return err;
  1877. pt->cbr_id = id;
  1878. intel_pt_set_event_name(evlist, id, "cbr");
  1879. id += 1;
  1880. }
  1881. if (pt->synth_opts.pwr_events && (evsel->attr.config & 0x10)) {
  1882. attr.config = PERF_SYNTH_INTEL_MWAIT;
  1883. err = intel_pt_synth_event(session, "mwait", &attr, id);
  1884. if (err)
  1885. return err;
  1886. pt->mwait_id = id;
  1887. intel_pt_set_event_name(evlist, id, "mwait");
  1888. id += 1;
  1889. attr.config = PERF_SYNTH_INTEL_PWRE;
  1890. err = intel_pt_synth_event(session, "pwre", &attr, id);
  1891. if (err)
  1892. return err;
  1893. pt->pwre_id = id;
  1894. intel_pt_set_event_name(evlist, id, "pwre");
  1895. id += 1;
  1896. attr.config = PERF_SYNTH_INTEL_EXSTOP;
  1897. err = intel_pt_synth_event(session, "exstop", &attr, id);
  1898. if (err)
  1899. return err;
  1900. pt->exstop_id = id;
  1901. intel_pt_set_event_name(evlist, id, "exstop");
  1902. id += 1;
  1903. attr.config = PERF_SYNTH_INTEL_PWRX;
  1904. err = intel_pt_synth_event(session, "pwrx", &attr, id);
  1905. if (err)
  1906. return err;
  1907. pt->pwrx_id = id;
  1908. intel_pt_set_event_name(evlist, id, "pwrx");
  1909. id += 1;
  1910. }
  1911. return 0;
  1912. }
  1913. static struct perf_evsel *intel_pt_find_sched_switch(struct perf_evlist *evlist)
  1914. {
  1915. struct perf_evsel *evsel;
  1916. evlist__for_each_entry_reverse(evlist, evsel) {
  1917. const char *name = perf_evsel__name(evsel);
  1918. if (!strcmp(name, "sched:sched_switch"))
  1919. return evsel;
  1920. }
  1921. return NULL;
  1922. }
  1923. static bool intel_pt_find_switch(struct perf_evlist *evlist)
  1924. {
  1925. struct perf_evsel *evsel;
  1926. evlist__for_each_entry(evlist, evsel) {
  1927. if (evsel->attr.context_switch)
  1928. return true;
  1929. }
  1930. return false;
  1931. }
  1932. static int intel_pt_perf_config(const char *var, const char *value, void *data)
  1933. {
  1934. struct intel_pt *pt = data;
  1935. if (!strcmp(var, "intel-pt.mispred-all"))
  1936. pt->mispred_all = perf_config_bool(var, value);
  1937. return 0;
  1938. }
  1939. static const char * const intel_pt_info_fmts[] = {
  1940. [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
  1941. [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
  1942. [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
  1943. [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
  1944. [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
  1945. [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
  1946. [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
  1947. [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
  1948. [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
  1949. [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
  1950. [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
  1951. [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
  1952. [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
  1953. [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
  1954. [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
  1955. [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
  1956. };
  1957. static void intel_pt_print_info(u64 *arr, int start, int finish)
  1958. {
  1959. int i;
  1960. if (!dump_trace)
  1961. return;
  1962. for (i = start; i <= finish; i++)
  1963. fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
  1964. }
  1965. static void intel_pt_print_info_str(const char *name, const char *str)
  1966. {
  1967. if (!dump_trace)
  1968. return;
  1969. fprintf(stdout, " %-20s%s\n", name, str ? str : "");
  1970. }
  1971. static bool intel_pt_has(struct auxtrace_info_event *auxtrace_info, int pos)
  1972. {
  1973. return auxtrace_info->header.size >=
  1974. sizeof(struct auxtrace_info_event) + (sizeof(u64) * (pos + 1));
  1975. }
  1976. int intel_pt_process_auxtrace_info(union perf_event *event,
  1977. struct perf_session *session)
  1978. {
  1979. struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
  1980. size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
  1981. struct intel_pt *pt;
  1982. void *info_end;
  1983. u64 *info;
  1984. int err;
  1985. if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
  1986. min_sz)
  1987. return -EINVAL;
  1988. pt = zalloc(sizeof(struct intel_pt));
  1989. if (!pt)
  1990. return -ENOMEM;
  1991. addr_filters__init(&pt->filts);
  1992. err = perf_config(intel_pt_perf_config, pt);
  1993. if (err)
  1994. goto err_free;
  1995. err = auxtrace_queues__init(&pt->queues);
  1996. if (err)
  1997. goto err_free;
  1998. intel_pt_log_set_name(INTEL_PT_PMU_NAME);
  1999. pt->session = session;
  2000. pt->machine = &session->machines.host; /* No kvm support */
  2001. pt->auxtrace_type = auxtrace_info->type;
  2002. pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
  2003. pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
  2004. pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
  2005. pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
  2006. pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
  2007. pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
  2008. pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
  2009. pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
  2010. pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
  2011. pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
  2012. intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
  2013. INTEL_PT_PER_CPU_MMAPS);
  2014. if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
  2015. pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
  2016. pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
  2017. pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
  2018. pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
  2019. pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
  2020. intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
  2021. INTEL_PT_CYC_BIT);
  2022. }
  2023. if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
  2024. pt->max_non_turbo_ratio =
  2025. auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
  2026. intel_pt_print_info(&auxtrace_info->priv[0],
  2027. INTEL_PT_MAX_NONTURBO_RATIO,
  2028. INTEL_PT_MAX_NONTURBO_RATIO);
  2029. }
  2030. info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
  2031. info_end = (void *)info + auxtrace_info->header.size;
  2032. if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
  2033. size_t len;
  2034. len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
  2035. intel_pt_print_info(&auxtrace_info->priv[0],
  2036. INTEL_PT_FILTER_STR_LEN,
  2037. INTEL_PT_FILTER_STR_LEN);
  2038. if (len) {
  2039. const char *filter = (const char *)info;
  2040. len = roundup(len + 1, 8);
  2041. info += len >> 3;
  2042. if ((void *)info > info_end) {
  2043. pr_err("%s: bad filter string length\n", __func__);
  2044. err = -EINVAL;
  2045. goto err_free_queues;
  2046. }
  2047. pt->filter = memdup(filter, len);
  2048. if (!pt->filter) {
  2049. err = -ENOMEM;
  2050. goto err_free_queues;
  2051. }
  2052. if (session->header.needs_swap)
  2053. mem_bswap_64(pt->filter, len);
  2054. if (pt->filter[len - 1]) {
  2055. pr_err("%s: filter string not null terminated\n", __func__);
  2056. err = -EINVAL;
  2057. goto err_free_queues;
  2058. }
  2059. err = addr_filters__parse_bare_filter(&pt->filts,
  2060. filter);
  2061. if (err)
  2062. goto err_free_queues;
  2063. }
  2064. intel_pt_print_info_str("Filter string", pt->filter);
  2065. }
  2066. pt->timeless_decoding = intel_pt_timeless_decoding(pt);
  2067. pt->have_tsc = intel_pt_have_tsc(pt);
  2068. pt->sampling_mode = false;
  2069. pt->est_tsc = !pt->timeless_decoding;
  2070. pt->unknown_thread = thread__new(999999999, 999999999);
  2071. if (!pt->unknown_thread) {
  2072. err = -ENOMEM;
  2073. goto err_free_queues;
  2074. }
  2075. /*
  2076. * Since this thread will not be kept in any rbtree not in a
  2077. * list, initialize its list node so that at thread__put() the
  2078. * current thread lifetime assuption is kept and we don't segfault
  2079. * at list_del_init().
  2080. */
  2081. INIT_LIST_HEAD(&pt->unknown_thread->node);
  2082. err = thread__set_comm(pt->unknown_thread, "unknown", 0);
  2083. if (err)
  2084. goto err_delete_thread;
  2085. if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
  2086. err = -ENOMEM;
  2087. goto err_delete_thread;
  2088. }
  2089. pt->auxtrace.process_event = intel_pt_process_event;
  2090. pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
  2091. pt->auxtrace.flush_events = intel_pt_flush;
  2092. pt->auxtrace.free_events = intel_pt_free_events;
  2093. pt->auxtrace.free = intel_pt_free;
  2094. session->auxtrace = &pt->auxtrace;
  2095. if (dump_trace)
  2096. return 0;
  2097. if (pt->have_sched_switch == 1) {
  2098. pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
  2099. if (!pt->switch_evsel) {
  2100. pr_err("%s: missing sched_switch event\n", __func__);
  2101. err = -EINVAL;
  2102. goto err_delete_thread;
  2103. }
  2104. } else if (pt->have_sched_switch == 2 &&
  2105. !intel_pt_find_switch(session->evlist)) {
  2106. pr_err("%s: missing context_switch attribute flag\n", __func__);
  2107. err = -EINVAL;
  2108. goto err_delete_thread;
  2109. }
  2110. if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
  2111. pt->synth_opts = *session->itrace_synth_opts;
  2112. } else {
  2113. itrace_synth_opts__set_default(&pt->synth_opts);
  2114. if (use_browser != -1) {
  2115. pt->synth_opts.branches = false;
  2116. pt->synth_opts.callchain = true;
  2117. }
  2118. if (session->itrace_synth_opts)
  2119. pt->synth_opts.thread_stack =
  2120. session->itrace_synth_opts->thread_stack;
  2121. }
  2122. if (pt->synth_opts.log)
  2123. intel_pt_log_enable();
  2124. /* Maximum non-turbo ratio is TSC freq / 100 MHz */
  2125. if (pt->tc.time_mult) {
  2126. u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
  2127. if (!pt->max_non_turbo_ratio)
  2128. pt->max_non_turbo_ratio =
  2129. (tsc_freq + 50000000) / 100000000;
  2130. intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
  2131. intel_pt_log("Maximum non-turbo ratio %u\n",
  2132. pt->max_non_turbo_ratio);
  2133. pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
  2134. }
  2135. if (pt->synth_opts.calls)
  2136. pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  2137. PERF_IP_FLAG_TRACE_END;
  2138. if (pt->synth_opts.returns)
  2139. pt->branches_filter |= PERF_IP_FLAG_RETURN |
  2140. PERF_IP_FLAG_TRACE_BEGIN;
  2141. if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
  2142. symbol_conf.use_callchain = true;
  2143. if (callchain_register_param(&callchain_param) < 0) {
  2144. symbol_conf.use_callchain = false;
  2145. pt->synth_opts.callchain = false;
  2146. }
  2147. }
  2148. err = intel_pt_synth_events(pt, session);
  2149. if (err)
  2150. goto err_delete_thread;
  2151. err = auxtrace_queues__process_index(&pt->queues, session);
  2152. if (err)
  2153. goto err_delete_thread;
  2154. if (pt->queues.populated)
  2155. pt->data_queued = true;
  2156. if (pt->timeless_decoding)
  2157. pr_debug2("Intel PT decoding without timestamps\n");
  2158. return 0;
  2159. err_delete_thread:
  2160. thread__zput(pt->unknown_thread);
  2161. err_free_queues:
  2162. intel_pt_log_disable();
  2163. auxtrace_queues__free(&pt->queues);
  2164. session->auxtrace = NULL;
  2165. err_free:
  2166. addr_filters__exit(&pt->filts);
  2167. zfree(&pt->filter);
  2168. free(pt);
  2169. return err;
  2170. }