builtin-inject.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/parse-options.h"
  17. #include <linux/list.h>
  18. struct perf_inject {
  19. struct perf_tool tool;
  20. bool build_ids;
  21. bool sched_stat;
  22. const char *input_name;
  23. int pipe_output,
  24. output;
  25. u64 bytes_written;
  26. struct list_head samples;
  27. };
  28. struct event_entry {
  29. struct list_head node;
  30. u32 tid;
  31. union perf_event event[0];
  32. };
  33. static int perf_event__repipe_synth(struct perf_tool *tool,
  34. union perf_event *event,
  35. struct machine *machine __maybe_unused)
  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, NULL);
  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, NULL);
  62. }
  63. static int perf_event__repipe_tracing_data_synth(union perf_event *event,
  64. struct perf_session *session
  65. __maybe_unused)
  66. {
  67. return perf_event__repipe_synth(NULL, event, NULL);
  68. }
  69. static int perf_event__repipe_attr(union perf_event *event,
  70. struct perf_evlist **pevlist __maybe_unused)
  71. {
  72. int ret;
  73. ret = perf_event__process_attr(event, pevlist);
  74. if (ret)
  75. return ret;
  76. return perf_event__repipe_synth(NULL, event, NULL);
  77. }
  78. static int perf_event__repipe(struct perf_tool *tool,
  79. union perf_event *event,
  80. struct perf_sample *sample __maybe_unused,
  81. struct machine *machine)
  82. {
  83. return perf_event__repipe_synth(tool, event, machine);
  84. }
  85. typedef int (*inject_handler)(struct perf_tool *tool,
  86. union perf_event *event,
  87. struct perf_sample *sample,
  88. struct perf_evsel *evsel,
  89. struct machine *machine);
  90. static int perf_event__repipe_sample(struct perf_tool *tool,
  91. union perf_event *event,
  92. struct perf_sample *sample,
  93. struct perf_evsel *evsel,
  94. struct machine *machine)
  95. {
  96. if (evsel->handler.func) {
  97. inject_handler f = evsel->handler.func;
  98. return f(tool, event, sample, evsel, machine);
  99. }
  100. return perf_event__repipe_synth(tool, event, machine);
  101. }
  102. static int perf_event__repipe_mmap(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_mmap(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(union perf_event *event,
  123. struct perf_session *session)
  124. {
  125. int err;
  126. perf_event__repipe_synth(NULL, event, NULL);
  127. err = perf_event__process_tracing_data(event, session);
  128. return err;
  129. }
  130. static int dso__read_build_id(struct dso *self)
  131. {
  132. if (self->has_build_id)
  133. return 0;
  134. if (filename__read_build_id(self->long_name, self->build_id,
  135. sizeof(self->build_id)) > 0) {
  136. self->has_build_id = true;
  137. return 0;
  138. }
  139. return -1;
  140. }
  141. static int dso__inject_build_id(struct dso *self, struct perf_tool *tool,
  142. struct machine *machine)
  143. {
  144. u16 misc = PERF_RECORD_MISC_USER;
  145. int err;
  146. if (dso__read_build_id(self) < 0) {
  147. pr_debug("no build_id found for %s\n", self->long_name);
  148. return -1;
  149. }
  150. if (self->kernel)
  151. misc = PERF_RECORD_MISC_KERNEL;
  152. err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe,
  153. machine);
  154. if (err) {
  155. pr_err("Can't synthesize build_id event for %s\n", self->long_name);
  156. return -1;
  157. }
  158. return 0;
  159. }
  160. static int perf_event__inject_buildid(struct perf_tool *tool,
  161. union perf_event *event,
  162. struct perf_sample *sample,
  163. struct perf_evsel *evsel __maybe_unused,
  164. struct machine *machine)
  165. {
  166. struct addr_location al;
  167. struct thread *thread;
  168. u8 cpumode;
  169. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  170. thread = machine__findnew_thread(machine, event->ip.pid);
  171. if (thread == NULL) {
  172. pr_err("problem processing %d event, skipping it.\n",
  173. event->header.type);
  174. goto repipe;
  175. }
  176. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  177. event->ip.ip, &al);
  178. if (al.map != NULL) {
  179. if (!al.map->dso->hit) {
  180. al.map->dso->hit = 1;
  181. if (map__load(al.map, NULL) >= 0) {
  182. dso__inject_build_id(al.map->dso, tool, machine);
  183. /*
  184. * If this fails, too bad, let the other side
  185. * account this as unresolved.
  186. */
  187. } else {
  188. #ifdef LIBELF_SUPPORT
  189. pr_warning("no symbols found in %s, maybe "
  190. "install a debug package?\n",
  191. al.map->dso->long_name);
  192. #endif
  193. }
  194. }
  195. }
  196. repipe:
  197. perf_event__repipe(tool, event, sample, machine);
  198. return 0;
  199. }
  200. static int perf_inject__sched_process_exit(struct perf_tool *tool,
  201. union perf_event *event __maybe_unused,
  202. struct perf_sample *sample,
  203. struct perf_evsel *evsel __maybe_unused,
  204. struct machine *machine __maybe_unused)
  205. {
  206. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  207. struct event_entry *ent;
  208. list_for_each_entry(ent, &inject->samples, node) {
  209. if (sample->tid == ent->tid) {
  210. list_del_init(&ent->node);
  211. free(ent);
  212. break;
  213. }
  214. }
  215. return 0;
  216. }
  217. static int perf_inject__sched_switch(struct perf_tool *tool,
  218. union perf_event *event,
  219. struct perf_sample *sample,
  220. struct perf_evsel *evsel,
  221. struct machine *machine)
  222. {
  223. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  224. struct event_entry *ent;
  225. perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
  226. ent = malloc(event->header.size + sizeof(struct event_entry));
  227. if (ent == NULL) {
  228. color_fprintf(stderr, PERF_COLOR_RED,
  229. "Not enough memory to process sched switch event!");
  230. return -1;
  231. }
  232. ent->tid = sample->tid;
  233. memcpy(&ent->event, event, event->header.size);
  234. list_add(&ent->node, &inject->samples);
  235. return 0;
  236. }
  237. static int perf_inject__sched_stat(struct perf_tool *tool,
  238. union perf_event *event __maybe_unused,
  239. struct perf_sample *sample,
  240. struct perf_evsel *evsel,
  241. struct machine *machine)
  242. {
  243. struct event_entry *ent;
  244. union perf_event *event_sw;
  245. struct perf_sample sample_sw;
  246. struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
  247. u32 pid = perf_evsel__intval(evsel, sample, "pid");
  248. list_for_each_entry(ent, &inject->samples, node) {
  249. if (pid == ent->tid)
  250. goto found;
  251. }
  252. return 0;
  253. found:
  254. event_sw = &ent->event[0];
  255. perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
  256. sample_sw.period = sample->period;
  257. sample_sw.time = sample->time;
  258. perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
  259. &sample_sw, false);
  260. return perf_event__repipe(tool, event_sw, &sample_sw, machine);
  261. }
  262. extern volatile int session_done;
  263. static void sig_handler(int sig __maybe_unused)
  264. {
  265. session_done = 1;
  266. }
  267. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  268. u64 sample_type, const char *sample_msg)
  269. {
  270. struct perf_event_attr *attr = &evsel->attr;
  271. const char *name = perf_evsel__name(evsel);
  272. if (!(attr->sample_type & sample_type)) {
  273. pr_err("Samples for %s event do not have %s attribute set.",
  274. name, sample_msg);
  275. return -EINVAL;
  276. }
  277. return 0;
  278. }
  279. static int __cmd_inject(struct perf_inject *inject)
  280. {
  281. struct perf_session *session;
  282. int ret = -EINVAL;
  283. signal(SIGINT, sig_handler);
  284. if (inject->build_ids) {
  285. inject->tool.sample = perf_event__inject_buildid;
  286. inject->tool.mmap = perf_event__repipe_mmap;
  287. inject->tool.fork = perf_event__repipe_fork;
  288. inject->tool.tracing_data = perf_event__repipe_tracing_data;
  289. }
  290. session = perf_session__new(inject->input_name, O_RDONLY, false, true, &inject->tool);
  291. if (session == NULL)
  292. return -ENOMEM;
  293. if (inject->sched_stat) {
  294. struct perf_evsel *evsel;
  295. inject->tool.ordered_samples = true;
  296. list_for_each_entry(evsel, &session->evlist->entries, node) {
  297. const char *name = perf_evsel__name(evsel);
  298. if (!strcmp(name, "sched:sched_switch")) {
  299. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
  300. return -EINVAL;
  301. evsel->handler.func = perf_inject__sched_switch;
  302. } else if (!strcmp(name, "sched:sched_process_exit"))
  303. evsel->handler.func = perf_inject__sched_process_exit;
  304. else if (!strncmp(name, "sched:sched_stat_", 17))
  305. evsel->handler.func = perf_inject__sched_stat;
  306. }
  307. }
  308. if (!inject->pipe_output)
  309. lseek(inject->output, session->header.data_offset, SEEK_SET);
  310. ret = perf_session__process_events(session, &inject->tool);
  311. if (!inject->pipe_output) {
  312. session->header.data_size = inject->bytes_written;
  313. perf_session__write_header(session, session->evlist, inject->output, true);
  314. }
  315. perf_session__delete(session);
  316. return ret;
  317. }
  318. int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
  319. {
  320. struct perf_inject inject = {
  321. .tool = {
  322. .sample = perf_event__repipe_sample,
  323. .mmap = perf_event__repipe,
  324. .comm = perf_event__repipe,
  325. .fork = perf_event__repipe,
  326. .exit = perf_event__repipe,
  327. .lost = perf_event__repipe,
  328. .read = perf_event__repipe_sample,
  329. .throttle = perf_event__repipe,
  330. .unthrottle = perf_event__repipe,
  331. .attr = perf_event__repipe_attr,
  332. .event_type = perf_event__repipe_event_type_synth,
  333. .tracing_data = perf_event__repipe_tracing_data_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. }