event.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <inttypes.h>
  4. #include <linux/compiler.h>
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <perf/cpumap.h>
  8. #include <perf/event.h>
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
  14. #include <linux/perf_event.h>
  15. #include <linux/zalloc.h>
  16. #include "cpumap.h"
  17. #include "dso.h"
  18. #include "event.h"
  19. #include "debug.h"
  20. #include "hist.h"
  21. #include "machine.h"
  22. #include "sort.h"
  23. #include "string2.h"
  24. #include "strlist.h"
  25. #include "thread.h"
  26. #include "thread_map.h"
  27. #include "time-utils.h"
  28. #include <linux/ctype.h>
  29. #include "map.h"
  30. #include "util/namespaces.h"
  31. #include "symbol.h"
  32. #include "symbol/kallsyms.h"
  33. #include "asm/bug.h"
  34. #include "stat.h"
  35. #include "session.h"
  36. #include "bpf-event.h"
  37. #include "print_binary.h"
  38. #include "tool.h"
  39. #include "util.h"
  40. static const char *perf_event__names[] = {
  41. [0] = "TOTAL",
  42. [PERF_RECORD_MMAP] = "MMAP",
  43. [PERF_RECORD_MMAP2] = "MMAP2",
  44. [PERF_RECORD_LOST] = "LOST",
  45. [PERF_RECORD_COMM] = "COMM",
  46. [PERF_RECORD_EXIT] = "EXIT",
  47. [PERF_RECORD_THROTTLE] = "THROTTLE",
  48. [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
  49. [PERF_RECORD_FORK] = "FORK",
  50. [PERF_RECORD_READ] = "READ",
  51. [PERF_RECORD_SAMPLE] = "SAMPLE",
  52. [PERF_RECORD_AUX] = "AUX",
  53. [PERF_RECORD_ITRACE_START] = "ITRACE_START",
  54. [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
  55. [PERF_RECORD_SWITCH] = "SWITCH",
  56. [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
  57. [PERF_RECORD_NAMESPACES] = "NAMESPACES",
  58. [PERF_RECORD_KSYMBOL] = "KSYMBOL",
  59. [PERF_RECORD_BPF_EVENT] = "BPF_EVENT",
  60. [PERF_RECORD_CGROUP] = "CGROUP",
  61. [PERF_RECORD_TEXT_POKE] = "TEXT_POKE",
  62. [PERF_RECORD_AUX_OUTPUT_HW_ID] = "AUX_OUTPUT_HW_ID",
  63. [PERF_RECORD_CALLCHAIN_DEFERRED] = "CALLCHAIN_DEFERRED",
  64. [PERF_RECORD_HEADER_ATTR] = "ATTR",
  65. [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
  66. [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
  67. [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
  68. [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
  69. [PERF_RECORD_ID_INDEX] = "ID_INDEX",
  70. [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
  71. [PERF_RECORD_AUXTRACE] = "AUXTRACE",
  72. [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
  73. [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
  74. [PERF_RECORD_CPU_MAP] = "CPU_MAP",
  75. [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
  76. [PERF_RECORD_STAT] = "STAT",
  77. [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
  78. [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
  79. [PERF_RECORD_TIME_CONV] = "TIME_CONV",
  80. [PERF_RECORD_HEADER_FEATURE] = "FEATURE",
  81. [PERF_RECORD_COMPRESSED] = "COMPRESSED",
  82. [PERF_RECORD_FINISHED_INIT] = "FINISHED_INIT",
  83. [PERF_RECORD_COMPRESSED2] = "COMPRESSED2",
  84. [PERF_RECORD_BPF_METADATA] = "BPF_METADATA",
  85. [PERF_RECORD_SCHEDSTAT_CPU] = "SCHEDSTAT_CPU",
  86. [PERF_RECORD_SCHEDSTAT_DOMAIN] = "SCHEDSTAT_DOMAIN",
  87. };
  88. const char *perf_event__name(unsigned int id)
  89. {
  90. if (id >= ARRAY_SIZE(perf_event__names))
  91. return "INVALID";
  92. if (!perf_event__names[id])
  93. return "UNKNOWN";
  94. return perf_event__names[id];
  95. }
  96. struct process_symbol_args {
  97. const char *name;
  98. u64 start;
  99. };
  100. static int find_func_symbol_cb(void *arg, const char *name, char type,
  101. u64 start)
  102. {
  103. struct process_symbol_args *args = arg;
  104. /*
  105. * Must be a function or at least an alias, as in PARISC64, where "_text" is
  106. * an 'A' to the same address as "_stext".
  107. */
  108. if (!(kallsyms__is_function(type) ||
  109. type == 'A') || strcmp(name, args->name))
  110. return 0;
  111. args->start = start;
  112. return 1;
  113. }
  114. static int find_any_symbol_cb(void *arg, const char *name,
  115. char type __maybe_unused, u64 start)
  116. {
  117. struct process_symbol_args *args = arg;
  118. if (strcmp(name, args->name))
  119. return 0;
  120. args->start = start;
  121. return 1;
  122. }
  123. int kallsyms__get_function_start(const char *kallsyms_filename,
  124. const char *symbol_name, u64 *addr)
  125. {
  126. struct process_symbol_args args = { .name = symbol_name, };
  127. if (kallsyms__parse(kallsyms_filename, &args, find_func_symbol_cb) <= 0)
  128. return -1;
  129. *addr = args.start;
  130. return 0;
  131. }
  132. int kallsyms__get_symbol_start(const char *kallsyms_filename,
  133. const char *symbol_name, u64 *addr)
  134. {
  135. struct process_symbol_args args = { .name = symbol_name, };
  136. if (kallsyms__parse(kallsyms_filename, &args, find_any_symbol_cb) <= 0)
  137. return -1;
  138. *addr = args.start;
  139. return 0;
  140. }
  141. void perf_event__read_stat_config(struct perf_stat_config *config,
  142. struct perf_record_stat_config *event)
  143. {
  144. unsigned i;
  145. for (i = 0; i < event->nr; i++) {
  146. switch (event->data[i].tag) {
  147. #define CASE(__term, __val) \
  148. case PERF_STAT_CONFIG_TERM__##__term: \
  149. config->__val = event->data[i].val; \
  150. break;
  151. CASE(AGGR_MODE, aggr_mode)
  152. CASE(SCALE, scale)
  153. CASE(INTERVAL, interval)
  154. CASE(AGGR_LEVEL, aggr_level)
  155. #undef CASE
  156. default:
  157. pr_warning("unknown stat config term %" PRI_lu64 "\n",
  158. event->data[i].tag);
  159. }
  160. }
  161. }
  162. size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
  163. {
  164. const char *s;
  165. if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
  166. s = " exec";
  167. else
  168. s = "";
  169. return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
  170. }
  171. size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
  172. {
  173. size_t ret = 0;
  174. struct perf_ns_link_info *ns_link_info;
  175. u32 nr_namespaces, idx;
  176. ns_link_info = event->namespaces.link_info;
  177. nr_namespaces = event->namespaces.nr_namespaces;
  178. ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
  179. event->namespaces.pid,
  180. event->namespaces.tid,
  181. nr_namespaces);
  182. for (idx = 0; idx < nr_namespaces; idx++) {
  183. if (idx && (idx % 4 == 0))
  184. ret += fprintf(fp, "\n\t\t ");
  185. ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
  186. perf_ns__name(idx), (u64)ns_link_info[idx].dev,
  187. (u64)ns_link_info[idx].ino,
  188. ((idx + 1) != nr_namespaces) ? ", " : "]\n");
  189. }
  190. return ret;
  191. }
  192. size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp)
  193. {
  194. return fprintf(fp, " cgroup: %" PRI_lu64 " %s\n",
  195. event->cgroup.id, event->cgroup.path);
  196. }
  197. int perf_event__process_comm(const struct perf_tool *tool __maybe_unused,
  198. union perf_event *event,
  199. struct perf_sample *sample,
  200. struct machine *machine)
  201. {
  202. return machine__process_comm_event(machine, event, sample);
  203. }
  204. int perf_event__process_namespaces(const struct perf_tool *tool __maybe_unused,
  205. union perf_event *event,
  206. struct perf_sample *sample,
  207. struct machine *machine)
  208. {
  209. return machine__process_namespaces_event(machine, event, sample);
  210. }
  211. int perf_event__process_cgroup(const struct perf_tool *tool __maybe_unused,
  212. union perf_event *event,
  213. struct perf_sample *sample,
  214. struct machine *machine)
  215. {
  216. return machine__process_cgroup_event(machine, event, sample);
  217. }
  218. int perf_event__process_lost(const struct perf_tool *tool __maybe_unused,
  219. union perf_event *event,
  220. struct perf_sample *sample,
  221. struct machine *machine)
  222. {
  223. return machine__process_lost_event(machine, event, sample);
  224. }
  225. int perf_event__process_aux(const struct perf_tool *tool __maybe_unused,
  226. union perf_event *event,
  227. struct perf_sample *sample __maybe_unused,
  228. struct machine *machine)
  229. {
  230. return machine__process_aux_event(machine, event);
  231. }
  232. int perf_event__process_itrace_start(const struct perf_tool *tool __maybe_unused,
  233. union perf_event *event,
  234. struct perf_sample *sample __maybe_unused,
  235. struct machine *machine)
  236. {
  237. return machine__process_itrace_start_event(machine, event);
  238. }
  239. int perf_event__process_aux_output_hw_id(const struct perf_tool *tool __maybe_unused,
  240. union perf_event *event,
  241. struct perf_sample *sample __maybe_unused,
  242. struct machine *machine)
  243. {
  244. return machine__process_aux_output_hw_id_event(machine, event);
  245. }
  246. int perf_event__process_lost_samples(const struct perf_tool *tool __maybe_unused,
  247. union perf_event *event,
  248. struct perf_sample *sample,
  249. struct machine *machine)
  250. {
  251. return machine__process_lost_samples_event(machine, event, sample);
  252. }
  253. int perf_event__process_switch(const struct perf_tool *tool __maybe_unused,
  254. union perf_event *event,
  255. struct perf_sample *sample __maybe_unused,
  256. struct machine *machine)
  257. {
  258. return machine__process_switch_event(machine, event);
  259. }
  260. int perf_event__process_ksymbol(const struct perf_tool *tool __maybe_unused,
  261. union perf_event *event,
  262. struct perf_sample *sample __maybe_unused,
  263. struct machine *machine)
  264. {
  265. return machine__process_ksymbol(machine, event, sample);
  266. }
  267. int perf_event__process_bpf(const struct perf_tool *tool __maybe_unused,
  268. union perf_event *event,
  269. struct perf_sample *sample,
  270. struct machine *machine)
  271. {
  272. return machine__process_bpf(machine, event, sample);
  273. }
  274. int perf_event__process_text_poke(const struct perf_tool *tool __maybe_unused,
  275. union perf_event *event,
  276. struct perf_sample *sample,
  277. struct machine *machine)
  278. {
  279. return machine__process_text_poke(machine, event, sample);
  280. }
  281. size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
  282. {
  283. return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 "]: %c %s\n",
  284. event->mmap.pid, event->mmap.tid, event->mmap.start,
  285. event->mmap.len, event->mmap.pgoff,
  286. (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
  287. event->mmap.filename);
  288. }
  289. size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
  290. {
  291. if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
  292. char sbuild_id[SBUILD_ID_SIZE];
  293. struct build_id bid;
  294. build_id__init(&bid, event->mmap2.build_id,
  295. event->mmap2.build_id_size);
  296. build_id__snprintf(&bid, sbuild_id, sizeof(sbuild_id));
  297. return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
  298. " <%s>]: %c%c%c%c %s\n",
  299. event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
  300. event->mmap2.len, event->mmap2.pgoff, sbuild_id,
  301. (event->mmap2.prot & PROT_READ) ? 'r' : '-',
  302. (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
  303. (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
  304. (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
  305. event->mmap2.filename);
  306. } else {
  307. return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
  308. " %02x:%02x %"PRI_lu64" %"PRI_lu64"]: %c%c%c%c %s\n",
  309. event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
  310. event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
  311. event->mmap2.min, event->mmap2.ino,
  312. event->mmap2.ino_generation,
  313. (event->mmap2.prot & PROT_READ) ? 'r' : '-',
  314. (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
  315. (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
  316. (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
  317. event->mmap2.filename);
  318. }
  319. }
  320. size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
  321. {
  322. struct perf_thread_map *threads = thread_map__new_event(&event->thread_map);
  323. size_t ret;
  324. ret = fprintf(fp, " nr: ");
  325. if (threads)
  326. ret += thread_map__fprintf(threads, fp);
  327. else
  328. ret += fprintf(fp, "failed to get threads from event\n");
  329. perf_thread_map__put(threads);
  330. return ret;
  331. }
  332. size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
  333. {
  334. struct perf_cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
  335. size_t ret;
  336. ret = fprintf(fp, ": ");
  337. if (cpus)
  338. ret += cpu_map__fprintf(cpus, fp);
  339. else
  340. ret += fprintf(fp, "failed to get cpumap from event\n");
  341. perf_cpu_map__put(cpus);
  342. return ret;
  343. }
  344. int perf_event__process_mmap(const struct perf_tool *tool __maybe_unused,
  345. union perf_event *event,
  346. struct perf_sample *sample,
  347. struct machine *machine)
  348. {
  349. return machine__process_mmap_event(machine, event, sample);
  350. }
  351. int perf_event__process_mmap2(const struct perf_tool *tool __maybe_unused,
  352. union perf_event *event,
  353. struct perf_sample *sample,
  354. struct machine *machine)
  355. {
  356. return machine__process_mmap2_event(machine, event, sample);
  357. }
  358. size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
  359. {
  360. return fprintf(fp, "(%d:%d):(%d:%d)\n",
  361. event->fork.pid, event->fork.tid,
  362. event->fork.ppid, event->fork.ptid);
  363. }
  364. int perf_event__process_fork(const struct perf_tool *tool __maybe_unused,
  365. union perf_event *event,
  366. struct perf_sample *sample,
  367. struct machine *machine)
  368. {
  369. return machine__process_fork_event(machine, event, sample);
  370. }
  371. int perf_event__process_exit(const struct perf_tool *tool __maybe_unused,
  372. union perf_event *event,
  373. struct perf_sample *sample,
  374. struct machine *machine)
  375. {
  376. return machine__process_exit_event(machine, event, sample);
  377. }
  378. int perf_event__exit_del_thread(const struct perf_tool *tool __maybe_unused,
  379. union perf_event *event,
  380. struct perf_sample *sample __maybe_unused,
  381. struct machine *machine)
  382. {
  383. struct thread *thread = machine__findnew_thread(machine,
  384. event->fork.pid,
  385. event->fork.tid);
  386. dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
  387. event->fork.ppid, event->fork.ptid);
  388. if (thread) {
  389. machine__remove_thread(machine, thread);
  390. thread__put(thread);
  391. }
  392. return 0;
  393. }
  394. size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
  395. {
  396. return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s%s]\n",
  397. event->aux.aux_offset, event->aux.aux_size,
  398. event->aux.flags,
  399. event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
  400. event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
  401. event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "",
  402. event->aux.flags & PERF_AUX_FLAG_COLLISION ? "C" : "");
  403. }
  404. size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
  405. {
  406. return fprintf(fp, " pid: %u tid: %u\n",
  407. event->itrace_start.pid, event->itrace_start.tid);
  408. }
  409. size_t perf_event__fprintf_aux_output_hw_id(union perf_event *event, FILE *fp)
  410. {
  411. return fprintf(fp, " hw_id: %#"PRI_lx64"\n",
  412. event->aux_output_hw_id.hw_id);
  413. }
  414. size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
  415. {
  416. bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
  417. const char *in_out = !out ? "IN " :
  418. !(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) ?
  419. "OUT " : "OUT preempt";
  420. if (event->header.type == PERF_RECORD_SWITCH)
  421. return fprintf(fp, " %s\n", in_out);
  422. return fprintf(fp, " %s %s pid/tid: %5d/%-5d\n",
  423. in_out, out ? "next" : "prev",
  424. event->context_switch.next_prev_pid,
  425. event->context_switch.next_prev_tid);
  426. }
  427. static size_t perf_event__fprintf_lost(union perf_event *event, FILE *fp)
  428. {
  429. return fprintf(fp, " lost %" PRI_lu64 "\n", event->lost.lost);
  430. }
  431. size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp)
  432. {
  433. return fprintf(fp, " addr %" PRI_lx64 " len %u type %u flags 0x%x name %s\n",
  434. event->ksymbol.addr, event->ksymbol.len,
  435. event->ksymbol.ksym_type,
  436. event->ksymbol.flags, event->ksymbol.name);
  437. }
  438. size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp)
  439. {
  440. return fprintf(fp, " type %u, flags %u, id %u\n",
  441. event->bpf.type, event->bpf.flags, event->bpf.id);
  442. }
  443. size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp)
  444. {
  445. struct perf_record_bpf_metadata *metadata = &event->bpf_metadata;
  446. size_t ret;
  447. ret = fprintf(fp, " prog %s\n", metadata->prog_name);
  448. for (__u32 i = 0; i < metadata->nr_entries; i++) {
  449. ret += fprintf(fp, " entry %d: %20s = %s\n", i,
  450. metadata->entries[i].key,
  451. metadata->entries[i].value);
  452. }
  453. return ret;
  454. }
  455. static int text_poke_printer(enum binary_printer_ops op, unsigned int val,
  456. void *extra, FILE *fp)
  457. {
  458. bool old = *(bool *)extra;
  459. switch ((int)op) {
  460. case BINARY_PRINT_LINE_BEGIN:
  461. return fprintf(fp, " %s bytes:", old ? "Old" : "New");
  462. case BINARY_PRINT_NUM_DATA:
  463. return fprintf(fp, " %02x", val);
  464. case BINARY_PRINT_LINE_END:
  465. return fprintf(fp, "\n");
  466. default:
  467. return 0;
  468. }
  469. }
  470. size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine, FILE *fp)
  471. {
  472. struct perf_record_text_poke_event *tp = &event->text_poke;
  473. size_t ret;
  474. bool old;
  475. ret = fprintf(fp, " %" PRI_lx64 " ", tp->addr);
  476. if (machine) {
  477. struct addr_location al;
  478. addr_location__init(&al);
  479. al.map = maps__find(machine__kernel_maps(machine), tp->addr);
  480. if (al.map && map__load(al.map) >= 0) {
  481. al.addr = map__map_ip(al.map, tp->addr);
  482. al.sym = map__find_symbol(al.map, al.addr);
  483. if (al.sym)
  484. ret += symbol__fprintf_symname_offs(al.sym, &al, fp);
  485. }
  486. addr_location__exit(&al);
  487. }
  488. ret += fprintf(fp, " old len %u new len %u\n", tp->old_len, tp->new_len);
  489. old = true;
  490. ret += binary__fprintf(tp->bytes, tp->old_len, 16, text_poke_printer,
  491. &old, fp);
  492. old = false;
  493. ret += binary__fprintf(tp->bytes + tp->old_len, tp->new_len, 16,
  494. text_poke_printer, &old, fp);
  495. return ret;
  496. }
  497. size_t perf_event__fprintf_schedstat_cpu(union perf_event *event, FILE *fp)
  498. {
  499. struct perf_record_schedstat_cpu *cs = &event->schedstat_cpu;
  500. size_t size = fprintf(fp, "\ncpu%u ", cs->cpu);
  501. __u16 version = cs->version;
  502. #define CPU_FIELD(_type, _name, _desc, _format, _is_pct, _pct_of, _ver) \
  503. size += fprintf(fp, "%" PRIu64 " ", (uint64_t)cs->_ver._name)
  504. if (version == 15) {
  505. #include <perf/schedstat-v15.h>
  506. return size;
  507. } else if (version == 16) {
  508. #include <perf/schedstat-v16.h>
  509. return size;
  510. } else if (version == 17) {
  511. #include <perf/schedstat-v17.h>
  512. return size;
  513. }
  514. #undef CPU_FIELD
  515. return fprintf(fp, "Unsupported /proc/schedstat version %d.\n",
  516. event->schedstat_cpu.version);
  517. }
  518. size_t perf_event__fprintf_schedstat_domain(union perf_event *event, FILE *fp)
  519. {
  520. struct perf_record_schedstat_domain *ds = &event->schedstat_domain;
  521. __u16 version = ds->version;
  522. size_t size = fprintf(fp, "\ndomain%u ", ds->domain);
  523. #define DOMAIN_FIELD(_type, _name, _desc, _format, _is_jiffies, _ver) \
  524. size += fprintf(fp, "%" PRIu64 " ", (uint64_t)ds->_ver._name)
  525. if (version == 15) {
  526. #include <perf/schedstat-v15.h>
  527. return size;
  528. } else if (version == 16) {
  529. #include <perf/schedstat-v16.h>
  530. return size;
  531. } else if (version == 17) {
  532. #include <perf/schedstat-v17.h>
  533. return size;
  534. }
  535. #undef DOMAIN_FIELD
  536. return fprintf(fp, "Unsupported /proc/schedstat version %d.\n",
  537. event->schedstat_domain.version);
  538. }
  539. size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp)
  540. {
  541. size_t ret = fprintf(fp, "PERF_RECORD_%s",
  542. perf_event__name(event->header.type));
  543. switch (event->header.type) {
  544. case PERF_RECORD_COMM:
  545. ret += perf_event__fprintf_comm(event, fp);
  546. break;
  547. case PERF_RECORD_FORK:
  548. case PERF_RECORD_EXIT:
  549. ret += perf_event__fprintf_task(event, fp);
  550. break;
  551. case PERF_RECORD_MMAP:
  552. ret += perf_event__fprintf_mmap(event, fp);
  553. break;
  554. case PERF_RECORD_NAMESPACES:
  555. ret += perf_event__fprintf_namespaces(event, fp);
  556. break;
  557. case PERF_RECORD_CGROUP:
  558. ret += perf_event__fprintf_cgroup(event, fp);
  559. break;
  560. case PERF_RECORD_MMAP2:
  561. ret += perf_event__fprintf_mmap2(event, fp);
  562. break;
  563. case PERF_RECORD_AUX:
  564. ret += perf_event__fprintf_aux(event, fp);
  565. break;
  566. case PERF_RECORD_ITRACE_START:
  567. ret += perf_event__fprintf_itrace_start(event, fp);
  568. break;
  569. case PERF_RECORD_SWITCH:
  570. case PERF_RECORD_SWITCH_CPU_WIDE:
  571. ret += perf_event__fprintf_switch(event, fp);
  572. break;
  573. case PERF_RECORD_LOST:
  574. ret += perf_event__fprintf_lost(event, fp);
  575. break;
  576. case PERF_RECORD_KSYMBOL:
  577. ret += perf_event__fprintf_ksymbol(event, fp);
  578. break;
  579. case PERF_RECORD_BPF_EVENT:
  580. ret += perf_event__fprintf_bpf(event, fp);
  581. break;
  582. case PERF_RECORD_TEXT_POKE:
  583. ret += perf_event__fprintf_text_poke(event, machine, fp);
  584. break;
  585. case PERF_RECORD_AUX_OUTPUT_HW_ID:
  586. ret += perf_event__fprintf_aux_output_hw_id(event, fp);
  587. break;
  588. case PERF_RECORD_BPF_METADATA:
  589. ret += perf_event__fprintf_bpf_metadata(event, fp);
  590. break;
  591. default:
  592. ret += fprintf(fp, "\n");
  593. }
  594. return ret;
  595. }
  596. int perf_event__process(const struct perf_tool *tool __maybe_unused,
  597. union perf_event *event,
  598. struct perf_sample *sample,
  599. struct machine *machine)
  600. {
  601. return machine__process_event(machine, event, sample);
  602. }
  603. struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
  604. struct addr_location *al)
  605. {
  606. struct maps *maps = thread__maps(thread);
  607. struct machine *machine = maps__machine(maps);
  608. bool load_map = false;
  609. map__zput(al->map);
  610. thread__zput(al->thread);
  611. al->thread = thread__get(thread);
  612. al->addr = addr;
  613. al->cpumode = cpumode;
  614. al->filtered = 0;
  615. if (machine == NULL)
  616. return NULL;
  617. if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
  618. al->level = 'k';
  619. maps = machine__kernel_maps(machine);
  620. load_map = !symbol_conf.lazy_load_kernel_maps;
  621. } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
  622. al->level = '.';
  623. } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
  624. al->level = 'g';
  625. maps = machine__kernel_maps(machine);
  626. load_map = !symbol_conf.lazy_load_kernel_maps;
  627. } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
  628. al->level = 'u';
  629. } else {
  630. al->level = 'H';
  631. if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
  632. cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
  633. !perf_guest)
  634. al->filtered |= (1 << HIST_FILTER__GUEST);
  635. if ((cpumode == PERF_RECORD_MISC_USER ||
  636. cpumode == PERF_RECORD_MISC_KERNEL) &&
  637. !perf_host)
  638. al->filtered |= (1 << HIST_FILTER__HOST);
  639. return NULL;
  640. }
  641. al->map = maps__find(maps, al->addr);
  642. if (al->map != NULL) {
  643. /*
  644. * Kernel maps might be changed when loading symbols so loading
  645. * must be done prior to using kernel maps.
  646. */
  647. if (load_map)
  648. map__load(al->map);
  649. al->addr = map__map_ip(al->map, al->addr);
  650. }
  651. return al->map;
  652. }
  653. /*
  654. * For branch stacks or branch samples, the sample cpumode might not be correct
  655. * because it applies only to the sample 'ip' and not necessary to 'addr' or
  656. * branch stack addresses. If possible, use a fallback to deal with those cases.
  657. */
  658. struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
  659. struct addr_location *al)
  660. {
  661. struct map *map = thread__find_map(thread, cpumode, addr, al);
  662. struct machine *machine = maps__machine(thread__maps(thread));
  663. u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr);
  664. if (map || addr_cpumode == cpumode)
  665. return map;
  666. return thread__find_map(thread, addr_cpumode, addr, al);
  667. }
  668. struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
  669. u64 addr, struct addr_location *al)
  670. {
  671. al->sym = NULL;
  672. if (thread__find_map(thread, cpumode, addr, al))
  673. al->sym = map__find_symbol(al->map, al->addr);
  674. return al->sym;
  675. }
  676. struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
  677. u64 addr, struct addr_location *al)
  678. {
  679. al->sym = NULL;
  680. if (thread__find_map_fb(thread, cpumode, addr, al))
  681. al->sym = map__find_symbol(al->map, al->addr);
  682. return al->sym;
  683. }
  684. static bool check_address_range(struct intlist *addr_list, int addr_range,
  685. unsigned long addr)
  686. {
  687. struct int_node *pos;
  688. intlist__for_each_entry(pos, addr_list) {
  689. if (addr >= pos->i && addr < pos->i + addr_range)
  690. return true;
  691. }
  692. return false;
  693. }
  694. /*
  695. * Callers need to drop the reference to al->thread, obtained in
  696. * machine__findnew_thread()
  697. */
  698. int machine__resolve(struct machine *machine, struct addr_location *al,
  699. struct perf_sample *sample)
  700. {
  701. struct thread *thread;
  702. struct dso *dso;
  703. if (symbol_conf.guest_code && !machine__is_host(machine))
  704. thread = machine__findnew_guest_code(machine, sample->pid);
  705. else
  706. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  707. if (thread == NULL)
  708. return -1;
  709. dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread__tid(thread));
  710. thread__find_map(thread, sample->cpumode, sample->ip, al);
  711. dso = al->map ? map__dso(al->map) : NULL;
  712. dump_printf(" ...... dso: %s\n",
  713. dso
  714. ? dso__long_name(dso)
  715. : (al->level == 'H' ? "[hypervisor]" : "<not found>"));
  716. if (thread__is_filtered(thread))
  717. al->filtered |= (1 << HIST_FILTER__THREAD);
  718. thread__put(thread);
  719. thread = NULL;
  720. al->sym = NULL;
  721. al->cpu = sample->cpu;
  722. al->socket = -1;
  723. al->srcline = NULL;
  724. if (al->cpu >= 0) {
  725. struct perf_env *env = machine->env;
  726. if (env && env->cpu)
  727. al->socket = env->cpu[al->cpu].socket_id;
  728. }
  729. /* Account for possible out-of-order switch events. */
  730. al->parallelism = max(1, min(machine->parallelism, machine__nr_cpus_avail(machine)));
  731. if (test_bit(al->parallelism, symbol_conf.parallelism_filter))
  732. al->filtered |= (1 << HIST_FILTER__PARALLELISM);
  733. /*
  734. * Multiply it by some const to avoid precision loss or dealing
  735. * with floats. The multiplier does not matter otherwise since
  736. * we only print it as percents.
  737. */
  738. al->latency = sample->period * 1000 / al->parallelism;
  739. if (al->map) {
  740. if (symbol_conf.dso_list &&
  741. (!dso || !(strlist__has_entry(symbol_conf.dso_list,
  742. dso__short_name(dso)) ||
  743. (dso__short_name(dso) != dso__long_name(dso) &&
  744. strlist__has_entry(symbol_conf.dso_list,
  745. dso__long_name(dso)))))) {
  746. al->filtered |= (1 << HIST_FILTER__DSO);
  747. }
  748. al->sym = map__find_symbol(al->map, al->addr);
  749. } else if (symbol_conf.dso_list) {
  750. al->filtered |= (1 << HIST_FILTER__DSO);
  751. }
  752. if (symbol_conf.sym_list) {
  753. int ret = 0;
  754. char al_addr_str[32];
  755. size_t sz = sizeof(al_addr_str);
  756. if (al->sym) {
  757. ret = strlist__has_entry(symbol_conf.sym_list,
  758. al->sym->name);
  759. }
  760. if (!ret && al->sym) {
  761. snprintf(al_addr_str, sz, "0x%"PRIx64,
  762. map__unmap_ip(al->map, al->sym->start));
  763. ret = strlist__has_entry(symbol_conf.sym_list,
  764. al_addr_str);
  765. }
  766. if (!ret && symbol_conf.addr_list && al->map) {
  767. unsigned long addr = map__unmap_ip(al->map, al->addr);
  768. ret = intlist__has_entry(symbol_conf.addr_list, addr);
  769. if (!ret && symbol_conf.addr_range) {
  770. ret = check_address_range(symbol_conf.addr_list,
  771. symbol_conf.addr_range,
  772. addr);
  773. }
  774. }
  775. if (!ret)
  776. al->filtered |= (1 << HIST_FILTER__SYMBOL);
  777. }
  778. return 0;
  779. }
  780. bool is_bts_event(struct perf_event_attr *attr)
  781. {
  782. return attr->type == PERF_TYPE_HARDWARE &&
  783. (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
  784. attr->sample_period == 1;
  785. }
  786. bool sample_addr_correlates_sym(struct perf_event_attr *attr)
  787. {
  788. if (attr->type == PERF_TYPE_SOFTWARE &&
  789. (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
  790. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  791. attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
  792. return true;
  793. if (is_bts_event(attr))
  794. return true;
  795. return false;
  796. }
  797. void thread__resolve(struct thread *thread, struct addr_location *al,
  798. struct perf_sample *sample)
  799. {
  800. thread__find_map_fb(thread, sample->cpumode, sample->addr, al);
  801. al->cpu = sample->cpu;
  802. al->sym = NULL;
  803. if (al->map)
  804. al->sym = map__find_symbol(al->map, al->addr);
  805. }