builtin-trace.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "builtin.h"
  2. #include "util/util.h"
  3. #include "util/cache.h"
  4. #include "util/symbol.h"
  5. #include "util/thread.h"
  6. #include "util/header.h"
  7. #include "util/exec_cmd.h"
  8. #include "util/trace-event.h"
  9. #include "util/session.h"
  10. static char const *script_name;
  11. static char const *generate_script_lang;
  12. static int default_start_script(const char *script __attribute((unused)))
  13. {
  14. return 0;
  15. }
  16. static int default_stop_script(void)
  17. {
  18. return 0;
  19. }
  20. static int default_generate_script(const char *outfile __attribute ((unused)))
  21. {
  22. return 0;
  23. }
  24. static struct scripting_ops default_scripting_ops = {
  25. .start_script = default_start_script,
  26. .stop_script = default_stop_script,
  27. .process_event = print_event,
  28. .generate_script = default_generate_script,
  29. };
  30. static struct scripting_ops *scripting_ops;
  31. static void setup_scripting(void)
  32. {
  33. /* make sure PERF_EXEC_PATH is set for scripts */
  34. perf_set_argv_exec_path(perf_exec_path());
  35. setup_perl_scripting();
  36. scripting_ops = &default_scripting_ops;
  37. }
  38. static int cleanup_scripting(void)
  39. {
  40. return scripting_ops->stop_script();
  41. }
  42. #include "util/parse-options.h"
  43. #include "perf.h"
  44. #include "util/debug.h"
  45. #include "util/trace-event.h"
  46. #include "util/exec_cmd.h"
  47. static char const *input_name = "perf.data";
  48. static int process_sample_event(event_t *event, struct perf_session *session)
  49. {
  50. struct sample_data data;
  51. struct thread *thread;
  52. memset(&data, 0, sizeof(data));
  53. data.time = -1;
  54. data.cpu = -1;
  55. data.period = 1;
  56. event__parse_sample(event, session->sample_type, &data);
  57. dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
  58. event->header.misc,
  59. data.pid, data.tid,
  60. (void *)(long)data.ip,
  61. (long long)data.period);
  62. thread = perf_session__findnew(session, event->ip.pid);
  63. if (thread == NULL) {
  64. pr_debug("problem processing %d event, skipping it.\n",
  65. event->header.type);
  66. return -1;
  67. }
  68. if (session->sample_type & PERF_SAMPLE_RAW) {
  69. /*
  70. * FIXME: better resolve from pid from the struct trace_entry
  71. * field, although it should be the same than this perf
  72. * event pid
  73. */
  74. scripting_ops->process_event(data.cpu, data.raw_data,
  75. data.raw_size,
  76. data.time, thread->comm);
  77. }
  78. event__stats.total += data.period;
  79. return 0;
  80. }
  81. static int sample_type_check(struct perf_session *session)
  82. {
  83. if (!(session->sample_type & PERF_SAMPLE_RAW)) {
  84. fprintf(stderr,
  85. "No trace sample to read. Did you call perf record "
  86. "without -R?");
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. static struct perf_event_ops event_ops = {
  92. .process_sample_event = process_sample_event,
  93. .process_comm_event = event__process_comm,
  94. .sample_type_check = sample_type_check,
  95. };
  96. static int __cmd_trace(struct perf_session *session)
  97. {
  98. return perf_session__process_events(session, &event_ops);
  99. }
  100. struct script_spec {
  101. struct list_head node;
  102. struct scripting_ops *ops;
  103. char spec[0];
  104. };
  105. LIST_HEAD(script_specs);
  106. static struct script_spec *script_spec__new(const char *spec,
  107. struct scripting_ops *ops)
  108. {
  109. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  110. if (s != NULL) {
  111. strcpy(s->spec, spec);
  112. s->ops = ops;
  113. }
  114. return s;
  115. }
  116. static void script_spec__delete(struct script_spec *s)
  117. {
  118. free(s->spec);
  119. free(s);
  120. }
  121. static void script_spec__add(struct script_spec *s)
  122. {
  123. list_add_tail(&s->node, &script_specs);
  124. }
  125. static struct script_spec *script_spec__find(const char *spec)
  126. {
  127. struct script_spec *s;
  128. list_for_each_entry(s, &script_specs, node)
  129. if (strcasecmp(s->spec, spec) == 0)
  130. return s;
  131. return NULL;
  132. }
  133. static struct script_spec *script_spec__findnew(const char *spec,
  134. struct scripting_ops *ops)
  135. {
  136. struct script_spec *s = script_spec__find(spec);
  137. if (s)
  138. return s;
  139. s = script_spec__new(spec, ops);
  140. if (!s)
  141. goto out_delete_spec;
  142. script_spec__add(s);
  143. return s;
  144. out_delete_spec:
  145. script_spec__delete(s);
  146. return NULL;
  147. }
  148. int script_spec_register(const char *spec, struct scripting_ops *ops)
  149. {
  150. struct script_spec *s;
  151. s = script_spec__find(spec);
  152. if (s)
  153. return -1;
  154. s = script_spec__findnew(spec, ops);
  155. if (!s)
  156. return -1;
  157. return 0;
  158. }
  159. static struct scripting_ops *script_spec__lookup(const char *spec)
  160. {
  161. struct script_spec *s = script_spec__find(spec);
  162. if (!s)
  163. return NULL;
  164. return s->ops;
  165. }
  166. static void list_available_languages(void)
  167. {
  168. struct script_spec *s;
  169. fprintf(stderr, "\n");
  170. fprintf(stderr, "Scripting language extensions (used in "
  171. "perf trace -s [spec:]script.[spec]):\n\n");
  172. list_for_each_entry(s, &script_specs, node)
  173. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  174. fprintf(stderr, "\n");
  175. }
  176. static int parse_scriptname(const struct option *opt __used,
  177. const char *str, int unset __used)
  178. {
  179. char spec[PATH_MAX];
  180. const char *script, *ext;
  181. int len;
  182. if (strcmp(str, "list") == 0) {
  183. list_available_languages();
  184. return 0;
  185. }
  186. script = strchr(str, ':');
  187. if (script) {
  188. len = script - str;
  189. if (len >= PATH_MAX) {
  190. fprintf(stderr, "invalid language specifier");
  191. return -1;
  192. }
  193. strncpy(spec, str, len);
  194. spec[len] = '\0';
  195. scripting_ops = script_spec__lookup(spec);
  196. if (!scripting_ops) {
  197. fprintf(stderr, "invalid language specifier");
  198. return -1;
  199. }
  200. script++;
  201. } else {
  202. script = str;
  203. ext = strchr(script, '.');
  204. if (!ext) {
  205. fprintf(stderr, "invalid script extension");
  206. return -1;
  207. }
  208. scripting_ops = script_spec__lookup(++ext);
  209. if (!scripting_ops) {
  210. fprintf(stderr, "invalid script extension");
  211. return -1;
  212. }
  213. }
  214. script_name = strdup(script);
  215. return 0;
  216. }
  217. static const char * const annotate_usage[] = {
  218. "perf trace [<options>] <command>",
  219. NULL
  220. };
  221. static const struct option options[] = {
  222. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  223. "dump raw trace in ASCII"),
  224. OPT_BOOLEAN('v', "verbose", &verbose,
  225. "be more verbose (show symbol address, etc)"),
  226. OPT_BOOLEAN('l', "latency", &latency_format,
  227. "show latency attributes (irqs/preemption disabled, etc)"),
  228. OPT_CALLBACK('s', "script", NULL, "name",
  229. "script file name (lang:script name, script name, or *)",
  230. parse_scriptname),
  231. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  232. "generate perf-trace.xx script in specified language"),
  233. OPT_END()
  234. };
  235. int cmd_trace(int argc, const char **argv, const char *prefix __used)
  236. {
  237. int err;
  238. struct perf_session *session;
  239. symbol__init(0);
  240. setup_scripting();
  241. argc = parse_options(argc, argv, options, annotate_usage, 0);
  242. if (argc) {
  243. /*
  244. * Special case: if there's an argument left then assume tha
  245. * it's a symbol filter:
  246. */
  247. if (argc > 1)
  248. usage_with_options(annotate_usage, options);
  249. }
  250. setup_pager();
  251. session = perf_session__new(input_name, O_RDONLY, 0, NULL);
  252. if (session == NULL)
  253. return -ENOMEM;
  254. if (generate_script_lang) {
  255. struct stat perf_stat;
  256. int input = open(input_name, O_RDONLY);
  257. if (input < 0) {
  258. perror("failed to open file");
  259. exit(-1);
  260. }
  261. err = fstat(input, &perf_stat);
  262. if (err < 0) {
  263. perror("failed to stat file");
  264. exit(-1);
  265. }
  266. if (!perf_stat.st_size) {
  267. fprintf(stderr, "zero-sized file, nothing to do!\n");
  268. exit(0);
  269. }
  270. scripting_ops = script_spec__lookup(generate_script_lang);
  271. if (!scripting_ops) {
  272. fprintf(stderr, "invalid language specifier");
  273. return -1;
  274. }
  275. perf_header__read(&session->header, input);
  276. err = scripting_ops->generate_script("perf-trace");
  277. goto out;
  278. }
  279. if (script_name) {
  280. err = scripting_ops->start_script(script_name);
  281. if (err)
  282. goto out;
  283. }
  284. err = __cmd_trace(session);
  285. perf_session__delete(session);
  286. cleanup_scripting();
  287. out:
  288. return err;
  289. }