builtin-inject.c 11 KB

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