builtin-inject.c 11 KB

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