builtin-inject.c 12 KB

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