builtin-trace.c 7.7 KB

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