builtin-trace.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/data_map.h"
  47. #include "util/exec_cmd.h"
  48. static char const *input_name = "perf.data";
  49. static u64 sample_type;
  50. static int process_sample_event(event_t *event, struct perf_session *session __used)
  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(struct perf_session *session)
  100. {
  101. register_idle_thread();
  102. register_perf_file_handler(&file_handler);
  103. return perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
  104. }
  105. struct script_spec {
  106. struct list_head node;
  107. struct scripting_ops *ops;
  108. char spec[0];
  109. };
  110. LIST_HEAD(script_specs);
  111. static struct script_spec *script_spec__new(const char *spec,
  112. struct scripting_ops *ops)
  113. {
  114. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  115. if (s != NULL) {
  116. strcpy(s->spec, spec);
  117. s->ops = ops;
  118. }
  119. return s;
  120. }
  121. static void script_spec__delete(struct script_spec *s)
  122. {
  123. free(s->spec);
  124. free(s);
  125. }
  126. static void script_spec__add(struct script_spec *s)
  127. {
  128. list_add_tail(&s->node, &script_specs);
  129. }
  130. static struct script_spec *script_spec__find(const char *spec)
  131. {
  132. struct script_spec *s;
  133. list_for_each_entry(s, &script_specs, node)
  134. if (strcasecmp(s->spec, spec) == 0)
  135. return s;
  136. return NULL;
  137. }
  138. static struct script_spec *script_spec__findnew(const char *spec,
  139. struct scripting_ops *ops)
  140. {
  141. struct script_spec *s = script_spec__find(spec);
  142. if (s)
  143. return s;
  144. s = script_spec__new(spec, ops);
  145. if (!s)
  146. goto out_delete_spec;
  147. script_spec__add(s);
  148. return s;
  149. out_delete_spec:
  150. script_spec__delete(s);
  151. return NULL;
  152. }
  153. int script_spec_register(const char *spec, struct scripting_ops *ops)
  154. {
  155. struct script_spec *s;
  156. s = script_spec__find(spec);
  157. if (s)
  158. return -1;
  159. s = script_spec__findnew(spec, ops);
  160. if (!s)
  161. return -1;
  162. return 0;
  163. }
  164. static struct scripting_ops *script_spec__lookup(const char *spec)
  165. {
  166. struct script_spec *s = script_spec__find(spec);
  167. if (!s)
  168. return NULL;
  169. return s->ops;
  170. }
  171. static void list_available_languages(void)
  172. {
  173. struct script_spec *s;
  174. fprintf(stderr, "\n");
  175. fprintf(stderr, "Scripting language extensions (used in "
  176. "perf trace -s [spec:]script.[spec]):\n\n");
  177. list_for_each_entry(s, &script_specs, node)
  178. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  179. fprintf(stderr, "\n");
  180. }
  181. static int parse_scriptname(const struct option *opt __used,
  182. const char *str, int unset __used)
  183. {
  184. char spec[PATH_MAX];
  185. const char *script, *ext;
  186. int len;
  187. if (strcmp(str, "list") == 0) {
  188. list_available_languages();
  189. return 0;
  190. }
  191. script = strchr(str, ':');
  192. if (script) {
  193. len = script - str;
  194. if (len >= PATH_MAX) {
  195. fprintf(stderr, "invalid language specifier");
  196. return -1;
  197. }
  198. strncpy(spec, str, len);
  199. spec[len] = '\0';
  200. scripting_ops = script_spec__lookup(spec);
  201. if (!scripting_ops) {
  202. fprintf(stderr, "invalid language specifier");
  203. return -1;
  204. }
  205. script++;
  206. } else {
  207. script = str;
  208. ext = strchr(script, '.');
  209. if (!ext) {
  210. fprintf(stderr, "invalid script extension");
  211. return -1;
  212. }
  213. scripting_ops = script_spec__lookup(++ext);
  214. if (!scripting_ops) {
  215. fprintf(stderr, "invalid script extension");
  216. return -1;
  217. }
  218. }
  219. script_name = strdup(script);
  220. return 0;
  221. }
  222. static const char * const annotate_usage[] = {
  223. "perf trace [<options>] <command>",
  224. NULL
  225. };
  226. static const struct option options[] = {
  227. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  228. "dump raw trace in ASCII"),
  229. OPT_BOOLEAN('v', "verbose", &verbose,
  230. "be more verbose (show symbol address, etc)"),
  231. OPT_BOOLEAN('l', "latency", &latency_format,
  232. "show latency attributes (irqs/preemption disabled, etc)"),
  233. OPT_CALLBACK('s', "script", NULL, "name",
  234. "script file name (lang:script name, script name, or *)",
  235. parse_scriptname),
  236. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  237. "generate perf-trace.xx script in specified language"),
  238. OPT_END()
  239. };
  240. int cmd_trace(int argc, const char **argv, const char *prefix __used)
  241. {
  242. int err;
  243. struct perf_session *session;
  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. session = perf_session__new(input_name, O_RDONLY, 0);
  257. if (session == NULL)
  258. return -ENOMEM;
  259. if (generate_script_lang) {
  260. struct stat perf_stat;
  261. int input = open(input_name, O_RDONLY);
  262. if (input < 0) {
  263. perror("failed to open file");
  264. exit(-1);
  265. }
  266. err = fstat(input, &perf_stat);
  267. if (err < 0) {
  268. perror("failed to stat file");
  269. exit(-1);
  270. }
  271. if (!perf_stat.st_size) {
  272. fprintf(stderr, "zero-sized file, nothing to do!\n");
  273. exit(0);
  274. }
  275. scripting_ops = script_spec__lookup(generate_script_lang);
  276. if (!scripting_ops) {
  277. fprintf(stderr, "invalid language specifier");
  278. return -1;
  279. }
  280. perf_header__read(&session->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(session);
  290. perf_session__delete(session);
  291. cleanup_scripting();
  292. out:
  293. return err;
  294. }