builtin-trace.c 7.3 KB

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