]> git.karo-electronics.de Git - mv-sheeva.git/blob - tools/perf/builtin-inject.c
978751ec64ce950f1b29312a92d2e60f1e7d0509
[mv-sheeva.git] / tools / perf / builtin-inject.c
1 /*
2  * builtin-inject.c
3  *
4  * Builtin inject command: Examine the live mode (stdin) event stream
5  * and repipe it to stdout while optionally injecting additional
6  * events into it.
7  */
8 #include "builtin.h"
9
10 #include "perf.h"
11 #include "util/session.h"
12 #include "util/debug.h"
13
14 #include "util/parse-options.h"
15
16 static char             const *input_name = "-";
17 static bool             inject_build_ids;
18
19 static int perf_event__repipe_synth(union perf_event *event,
20                                     struct perf_session *session __used)
21 {
22         uint32_t size;
23         void *buf = event;
24
25         size = event->header.size;
26
27         while (size) {
28                 int ret = write(STDOUT_FILENO, buf, size);
29                 if (ret < 0)
30                         return -errno;
31
32                 size -= ret;
33                 buf += ret;
34         }
35
36         return 0;
37 }
38
39 static int perf_event__repipe_attr(union perf_event *event,
40                                    struct perf_evlist **pevlist __used)
41 {
42         return perf_event__repipe_synth(event, NULL);
43 }
44
45 static int perf_event__repipe(union perf_event *event,
46                               struct perf_sample *sample __used,
47                               struct perf_session *session)
48 {
49         return perf_event__repipe_synth(event, session);
50 }
51
52 static int perf_event__repipe_sample(union perf_event *event,
53                               struct perf_sample *sample __used,
54                               struct perf_evsel *evsel __used,
55                               struct perf_session *session)
56 {
57         return perf_event__repipe_synth(event, session);
58 }
59
60 static int perf_event__repipe_mmap(union perf_event *event,
61                                    struct perf_sample *sample,
62                                    struct perf_session *session)
63 {
64         int err;
65
66         err = perf_event__process_mmap(event, sample, session);
67         perf_event__repipe(event, sample, session);
68
69         return err;
70 }
71
72 static int perf_event__repipe_task(union perf_event *event,
73                                    struct perf_sample *sample,
74                                    struct perf_session *session)
75 {
76         int err;
77
78         err = perf_event__process_task(event, sample, session);
79         perf_event__repipe(event, sample, session);
80
81         return err;
82 }
83
84 static int perf_event__repipe_tracing_data(union perf_event *event,
85                                            struct perf_session *session)
86 {
87         int err;
88
89         perf_event__repipe_synth(event, session);
90         err = perf_event__process_tracing_data(event, session);
91
92         return err;
93 }
94
95 static int dso__read_build_id(struct dso *self)
96 {
97         if (self->has_build_id)
98                 return 0;
99
100         if (filename__read_build_id(self->long_name, self->build_id,
101                                     sizeof(self->build_id)) > 0) {
102                 self->has_build_id = true;
103                 return 0;
104         }
105
106         return -1;
107 }
108
109 static int dso__inject_build_id(struct dso *self, struct perf_session *session)
110 {
111         u16 misc = PERF_RECORD_MISC_USER;
112         struct machine *machine;
113         int err;
114
115         if (dso__read_build_id(self) < 0) {
116                 pr_debug("no build_id found for %s\n", self->long_name);
117                 return -1;
118         }
119
120         machine = perf_session__find_host_machine(session);
121         if (machine == NULL) {
122                 pr_err("Can't find machine for session\n");
123                 return -1;
124         }
125
126         if (self->kernel)
127                 misc = PERF_RECORD_MISC_KERNEL;
128
129         err = perf_event__synthesize_build_id(self, misc, perf_event__repipe,
130                                               machine, session);
131         if (err) {
132                 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
133                 return -1;
134         }
135
136         return 0;
137 }
138
139 static int perf_event__inject_buildid(union perf_event *event,
140                                       struct perf_sample *sample,
141                                       struct perf_evsel *evsel __used,
142                                       struct perf_session *session)
143 {
144         struct addr_location al;
145         struct thread *thread;
146         u8 cpumode;
147
148         cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
149
150         thread = perf_session__findnew(session, event->ip.pid);
151         if (thread == NULL) {
152                 pr_err("problem processing %d event, skipping it.\n",
153                        event->header.type);
154                 goto repipe;
155         }
156
157         thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
158                               event->ip.pid, event->ip.ip, &al);
159
160         if (al.map != NULL) {
161                 if (!al.map->dso->hit) {
162                         al.map->dso->hit = 1;
163                         if (map__load(al.map, NULL) >= 0) {
164                                 dso__inject_build_id(al.map->dso, session);
165                                 /*
166                                  * If this fails, too bad, let the other side
167                                  * account this as unresolved.
168                                  */
169                         } else
170                                 pr_warning("no symbols found in %s, maybe "
171                                            "install a debug package?\n",
172                                            al.map->dso->long_name);
173                 }
174         }
175
176 repipe:
177         perf_event__repipe(event, sample, session);
178         return 0;
179 }
180
181 struct perf_event_ops inject_ops = {
182         .sample         = perf_event__repipe_sample,
183         .mmap           = perf_event__repipe,
184         .comm           = perf_event__repipe,
185         .fork           = perf_event__repipe,
186         .exit           = perf_event__repipe,
187         .lost           = perf_event__repipe,
188         .read           = perf_event__repipe,
189         .throttle       = perf_event__repipe,
190         .unthrottle     = perf_event__repipe,
191         .attr           = perf_event__repipe_attr,
192         .event_type     = perf_event__repipe_synth,
193         .tracing_data   = perf_event__repipe_synth,
194         .build_id       = perf_event__repipe_synth,
195 };
196
197 extern volatile int session_done;
198
199 static void sig_handler(int sig __attribute__((__unused__)))
200 {
201         session_done = 1;
202 }
203
204 static int __cmd_inject(void)
205 {
206         struct perf_session *session;
207         int ret = -EINVAL;
208
209         signal(SIGINT, sig_handler);
210
211         if (inject_build_ids) {
212                 inject_ops.sample       = perf_event__inject_buildid;
213                 inject_ops.mmap         = perf_event__repipe_mmap;
214                 inject_ops.fork         = perf_event__repipe_task;
215                 inject_ops.tracing_data = perf_event__repipe_tracing_data;
216         }
217
218         session = perf_session__new(input_name, O_RDONLY, false, true, &inject_ops);
219         if (session == NULL)
220                 return -ENOMEM;
221
222         ret = perf_session__process_events(session, &inject_ops);
223
224         perf_session__delete(session);
225
226         return ret;
227 }
228
229 static const char * const report_usage[] = {
230         "perf inject [<options>]",
231         NULL
232 };
233
234 static const struct option options[] = {
235         OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
236                     "Inject build-ids into the output stream"),
237         OPT_INCR('v', "verbose", &verbose,
238                  "be more verbose (show build ids, etc)"),
239         OPT_END()
240 };
241
242 int cmd_inject(int argc, const char **argv, const char *prefix __used)
243 {
244         argc = parse_options(argc, argv, options, report_usage, 0);
245
246         /*
247          * Any (unrecognized) arguments left?
248          */
249         if (argc)
250                 usage_with_options(report_usage, options);
251
252         if (symbol__init() < 0)
253                 return -1;
254
255         return __cmd_inject();
256 }