builtin-trace.c 7.4 KB

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