builtin-inject.c 11 KB

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