builtin-inject.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * builtin-inject.c
  3. *
  4. * Builtin inject command: Examine the live mode (stdin) event stream
  5. * and repipe it to stdout while optionally injecting additional
  6. * events into it.
  7. */
  8. #include "builtin.h"
  9. #include "perf.h"
  10. #include "util/color.h"
  11. #include "util/evlist.h"
  12. #include "util/evsel.h"
  13. #include "util/session.h"
  14. #include "util/tool.h"
  15. #include "util/debug.h"
  16. #include "util/build-id.h"
  17. #include "util/data.h"
  18. #include "util/parse-options.h"
  19. #include <linux/list.h>
  20. struct perf_inject {
  21. struct perf_tool tool;
  22. struct perf_session *session;
  23. bool build_ids;
  24. bool sched_stat;
  25. const char *input_name;
  26. struct perf_data_file output;
  27. u64 bytes_written;
  28. struct list_head samples;
  29. };
  30. struct event_entry {
  31. struct list_head node;
  32. u32 tid;
  33. union perf_event event[0];
  34. };
  35. static int perf_event__repipe_synth(struct perf_tool *tool,
  36. union perf_event *event)
  37. {
  38. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  39. ssize_t size;
  40. size = perf_data_file__write(&inject->output, event,
  41. event->header.size);
  42. if (size < 0)
  43. return -errno;
  44. inject->bytes_written += size;
  45. return 0;
  46. }
  47. static int perf_event__repipe_op2_synth(struct perf_tool *tool,
  48. union perf_event *event,
  49. struct perf_session *session
  50. __maybe_unused)
  51. {
  52. return perf_event__repipe_synth(tool, event);
  53. }
  54. static int perf_event__repipe_attr(struct perf_tool *tool,
  55. union perf_event *event,
  56. struct perf_evlist **pevlist)
  57. {
  58. struct perf_inject *inject = container_of(tool, struct perf_inject,
  59. tool);
  60. int ret;
  61. ret = perf_event__process_attr(tool, event, pevlist);
  62. if (ret)
  63. return ret;
  64. if (!inject->output.is_pipe)
  65. return 0;
  66. return perf_event__repipe_synth(tool, event);
  67. }
  68. static int perf_event__repipe(struct perf_tool *tool,
  69. union perf_event *event,
  70. struct perf_sample *sample __maybe_unused,
  71. struct machine *machine __maybe_unused)
  72. {
  73. return perf_event__repipe_synth(tool, event);
  74. }
  75. typedef int (*inject_handler)(struct perf_tool *tool,
  76. union perf_event *event,
  77. struct perf_sample *sample,
  78. struct perf_evsel *evsel,
  79. struct machine *machine);
  80. static int perf_event__repipe_sample(struct perf_tool *tool,
  81. union perf_event *event,
  82. struct perf_sample *sample,
  83. struct perf_evsel *evsel,
  84. struct machine *machine)
  85. {
  86. if (evsel->handler) {
  87. inject_handler f = evsel->handler;
  88. return f(tool, event, sample, evsel, machine);
  89. }
  90. build_id__mark_dso_hit(tool, event, sample, evsel, machine);
  91. return perf_event__repipe_synth(tool, event);
  92. }
  93. static int perf_event__repipe_mmap(struct perf_tool *tool,
  94. union perf_event *event,
  95. struct perf_sample *sample,
  96. struct machine *machine)
  97. {
  98. int err;
  99. err = perf_event__process_mmap(tool, event, sample, machine);
  100. perf_event__repipe(tool, event, sample, machine);
  101. return err;
  102. }
  103. static int perf_event__repipe_mmap2(struct perf_tool *tool,
  104. union perf_event *event,
  105. struct perf_sample *sample,
  106. struct machine *machine)
  107. {
  108. int err;
  109. err = perf_event__process_mmap2(tool, event, sample, machine);
  110. perf_event__repipe(tool, event, sample, machine);
  111. return err;
  112. }
  113. static int perf_event__repipe_fork(struct perf_tool *tool,
  114. union perf_event *event,
  115. struct perf_sample *sample,
  116. struct machine *machine)
  117. {
  118. int err;
  119. err = perf_event__process_fork(tool, event, sample, machine);
  120. perf_event__repipe(tool, event, sample, machine);
  121. return err;
  122. }
  123. static int perf_event__repipe_tracing_data(struct perf_tool *tool,
  124. union perf_event *event,
  125. struct perf_session *session)
  126. {
  127. int err;
  128. perf_event__repipe_synth(tool, event);
  129. err = perf_event__process_tracing_data(tool, event, session);
  130. return err;
  131. }
  132. static int dso__read_build_id(struct dso *dso)
  133. {
  134. if (dso->has_build_id)
  135. return 0;
  136. if (filename__read_build_id(dso->long_name, dso->build_id,
  137. sizeof(dso->build_id)) > 0) {
  138. dso->has_build_id = true;
  139. return 0;
  140. }
  141. return -1;
  142. }
  143. static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
  144. struct machine *machine)
  145. {
  146. u16 misc = PERF_RECORD_MISC_USER;
  147. int err;
  148. if (dso__read_build_id(dso) < 0) {
  149. pr_debug("no build_id found for %s\n", dso->long_name);
  150. return -1;
  151. }
  152. if (dso->kernel)
  153. misc = PERF_RECORD_MISC_KERNEL;
  154. err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
  155. machine);
  156. if (err) {
  157. pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. static int perf_event__inject_buildid(struct perf_tool *tool,
  163. union perf_event *event,
  164. struct perf_sample *sample,
  165. struct perf_evsel *evsel __maybe_unused,
  166. struct machine *machine)
  167. {
  168. struct addr_location al;
  169. struct thread *thread;
  170. u8 cpumode;
  171. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  172. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  173. if (thread == NULL) {
  174. pr_err("problem processing %d event, skipping it.\n",
  175. event->header.type);
  176. goto repipe;
  177. }
  178. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  179. sample->ip, &al);
  180. if (al.map != NULL) {
  181. if (!al.map->dso->hit) {
  182. al.map->dso->hit = 1;
  183. if (map__load(al.map, NULL) >= 0) {
  184. dso__inject_build_id(al.map->dso, tool, machine);
  185. /*
  186. * If this fails, too bad, let the other side
  187. * account this as unresolved.
  188. */
  189. } else {
  190. #ifdef HAVE_LIBELF_SUPPORT
  191. pr_warning("no symbols found in %s, maybe "
  192. "install a debug package?\n",
  193. al.map->dso->long_name);
  194. #endif
  195. }
  196. }
  197. }
  198. repipe:
  199. perf_event__repipe(tool, event, sample, machine);
  200. return 0;
  201. }
  202. static int perf_inject__sched_process_exit(struct perf_tool *tool,
  203. union perf_event *event __maybe_unused,
  204. struct perf_sample *sample,
  205. struct perf_evsel *evsel __maybe_unused,
  206. struct machine *machine __maybe_unused)
  207. {
  208. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  209. struct event_entry *ent;
  210. list_for_each_entry(ent, &inject->samples, node) {
  211. if (sample->tid == ent->tid) {
  212. list_del_init(&ent->node);
  213. free(ent);
  214. break;
  215. }
  216. }
  217. return 0;
  218. }
  219. static int perf_inject__sched_switch(struct perf_tool *tool,
  220. union perf_event *event,
  221. struct perf_sample *sample,
  222. struct perf_evsel *evsel,
  223. struct machine *machine)
  224. {
  225. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  226. struct event_entry *ent;
  227. perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
  228. ent = malloc(event->header.size + sizeof(struct event_entry));
  229. if (ent == NULL) {
  230. color_fprintf(stderr, PERF_COLOR_RED,
  231. "Not enough memory to process sched switch event!");
  232. return -1;
  233. }
  234. ent->tid = sample->tid;
  235. memcpy(&ent->event, event, event->header.size);
  236. list_add(&ent->node, &inject->samples);
  237. return 0;
  238. }
  239. static int perf_inject__sched_stat(struct perf_tool *tool,
  240. union perf_event *event __maybe_unused,
  241. struct perf_sample *sample,
  242. struct perf_evsel *evsel,
  243. struct machine *machine)
  244. {
  245. struct event_entry *ent;
  246. union perf_event *event_sw;
  247. struct perf_sample sample_sw;
  248. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  249. u32 pid = perf_evsel__intval(evsel, sample, "pid");
  250. list_for_each_entry(ent, &inject->samples, node) {
  251. if (pid == ent->tid)
  252. goto found;
  253. }
  254. return 0;
  255. found:
  256. event_sw = &ent->event[0];
  257. perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
  258. sample_sw.period = sample->period;
  259. sample_sw.time = sample->time;
  260. perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
  261. evsel->attr.read_format, &sample_sw,
  262. false);
  263. build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
  264. return perf_event__repipe(tool, event_sw, &sample_sw, machine);
  265. }
  266. static void sig_handler(int sig __maybe_unused)
  267. {
  268. session_done = 1;
  269. }
  270. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  271. u64 sample_type, const char *sample_msg)
  272. {
  273. struct perf_event_attr *attr = &evsel->attr;
  274. const char *name = perf_evsel__name(evsel);
  275. if (!(attr->sample_type & sample_type)) {
  276. pr_err("Samples for %s event do not have %s attribute set.",
  277. name, sample_msg);
  278. return -EINVAL;
  279. }
  280. return 0;
  281. }
  282. static int __cmd_inject(struct perf_inject *inject)
  283. {
  284. int ret = -EINVAL;
  285. struct perf_session *session = inject->session;
  286. struct perf_data_file *file_out = &inject->output;
  287. signal(SIGINT, sig_handler);
  288. if (inject->build_ids || inject->sched_stat) {
  289. inject->tool.mmap = perf_event__repipe_mmap;
  290. inject->tool.mmap2 = perf_event__repipe_mmap2;
  291. inject->tool.fork = perf_event__repipe_fork;
  292. inject->tool.tracing_data = perf_event__repipe_tracing_data;
  293. }
  294. if (inject->build_ids) {
  295. inject->tool.sample = perf_event__inject_buildid;
  296. } else if (inject->sched_stat) {
  297. struct perf_evsel *evsel;
  298. inject->tool.ordered_events = true;
  299. evlist__for_each(session->evlist, evsel) {
  300. const char *name = perf_evsel__name(evsel);
  301. if (!strcmp(name, "sched:sched_switch")) {
  302. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
  303. return -EINVAL;
  304. evsel->handler = perf_inject__sched_switch;
  305. } else if (!strcmp(name, "sched:sched_process_exit"))
  306. evsel->handler = perf_inject__sched_process_exit;
  307. else if (!strncmp(name, "sched:sched_stat_", 17))
  308. evsel->handler = perf_inject__sched_stat;
  309. }
  310. }
  311. if (!file_out->is_pipe)
  312. lseek(file_out->fd, session->header.data_offset, SEEK_SET);
  313. ret = perf_session__process_events(session, &inject->tool);
  314. if (!file_out->is_pipe) {
  315. if (inject->build_ids)
  316. perf_header__set_feat(&session->header,
  317. HEADER_BUILD_ID);
  318. session->header.data_size = inject->bytes_written;
  319. perf_session__write_header(session, session->evlist, file_out->fd, true);
  320. }
  321. return ret;
  322. }
  323. int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
  324. {
  325. struct perf_inject inject = {
  326. .tool = {
  327. .sample = perf_event__repipe_sample,
  328. .mmap = perf_event__repipe,
  329. .mmap2 = perf_event__repipe,
  330. .comm = perf_event__repipe,
  331. .fork = perf_event__repipe,
  332. .exit = perf_event__repipe,
  333. .lost = perf_event__repipe,
  334. .read = perf_event__repipe_sample,
  335. .throttle = perf_event__repipe,
  336. .unthrottle = perf_event__repipe,
  337. .attr = perf_event__repipe_attr,
  338. .tracing_data = perf_event__repipe_op2_synth,
  339. .finished_round = perf_event__repipe_op2_synth,
  340. .build_id = perf_event__repipe_op2_synth,
  341. },
  342. .input_name = "-",
  343. .samples = LIST_HEAD_INIT(inject.samples),
  344. .output = {
  345. .path = "-",
  346. .mode = PERF_DATA_MODE_WRITE,
  347. },
  348. };
  349. struct perf_data_file file = {
  350. .mode = PERF_DATA_MODE_READ,
  351. };
  352. int ret;
  353. const struct option options[] = {
  354. OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
  355. "Inject build-ids into the output stream"),
  356. OPT_STRING('i', "input", &inject.input_name, "file",
  357. "input file name"),
  358. OPT_STRING('o', "output", &inject.output.path, "file",
  359. "output file name"),
  360. OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
  361. "Merge sched-stat and sched-switch for getting events "
  362. "where and how long tasks slept"),
  363. OPT_INCR('v', "verbose", &verbose,
  364. "be more verbose (show build ids, etc)"),
  365. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
  366. "kallsyms pathname"),
  367. OPT_END()
  368. };
  369. const char * const inject_usage[] = {
  370. "perf inject [<options>]",
  371. NULL
  372. };
  373. argc = parse_options(argc, argv, options, inject_usage, 0);
  374. /*
  375. * Any (unrecognized) arguments left?
  376. */
  377. if (argc)
  378. usage_with_options(inject_usage, options);
  379. if (perf_data_file__open(&inject.output)) {
  380. perror("failed to create output file");
  381. return -1;
  382. }
  383. file.path = inject.input_name;
  384. inject.session = perf_session__new(&file, true, &inject.tool);
  385. if (inject.session == NULL)
  386. return -1;
  387. if (symbol__init(&inject.session->header.env) < 0)
  388. return -1;
  389. ret = __cmd_inject(&inject);
  390. perf_session__delete(inject.session);
  391. return ret;
  392. }