2 * db-export.c: Support for exporting data suitable for import to a database
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24 #include "db-export.h"
26 int db_export__init(struct db_export *dbe)
28 memset(dbe, 0, sizeof(struct db_export));
32 void db_export__exit(struct db_export *dbe __maybe_unused)
36 int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
41 evsel->db_id = ++dbe->evsel_last_db_id;
43 if (dbe->export_evsel)
44 return dbe->export_evsel(dbe, evsel);
49 int db_export__machine(struct db_export *dbe, struct machine *machine)
54 machine->db_id = ++dbe->machine_last_db_id;
56 if (dbe->export_machine)
57 return dbe->export_machine(dbe, machine);
62 int db_export__thread(struct db_export *dbe, struct thread *thread,
63 struct machine *machine, struct comm *comm)
65 u64 main_thread_db_id = 0;
71 thread->db_id = ++dbe->thread_last_db_id;
73 if (thread->pid_ != -1) {
74 struct thread *main_thread;
76 if (thread->pid_ == thread->tid) {
79 main_thread = machine__findnew_thread(machine,
84 err = db_export__thread(dbe, main_thread, machine,
89 err = db_export__comm_thread(dbe, comm, thread);
94 main_thread_db_id = main_thread->db_id;
97 if (dbe->export_thread)
98 return dbe->export_thread(dbe, thread, main_thread_db_id,
104 int db_export__comm(struct db_export *dbe, struct comm *comm,
105 struct thread *main_thread)
112 comm->db_id = ++dbe->comm_last_db_id;
114 if (dbe->export_comm) {
115 err = dbe->export_comm(dbe, comm);
120 return db_export__comm_thread(dbe, comm, main_thread);
123 int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
124 struct thread *thread)
128 db_id = ++dbe->comm_thread_last_db_id;
130 if (dbe->export_comm_thread)
131 return dbe->export_comm_thread(dbe, db_id, comm, thread);
136 int db_export__dso(struct db_export *dbe, struct dso *dso,
137 struct machine *machine)
142 dso->db_id = ++dbe->dso_last_db_id;
145 return dbe->export_dso(dbe, dso, machine);
150 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
153 u64 *sym_db_id = symbol__priv(sym);
158 *sym_db_id = ++dbe->symbol_last_db_id;
160 if (dbe->export_symbol)
161 return dbe->export_symbol(dbe, sym, dso);
166 static struct thread *get_main_thread(struct machine *machine, struct thread *thread)
168 if (thread->pid_ == thread->tid)
171 if (thread->pid_ == -1)
174 return machine__find_thread(machine, thread->pid_, thread->pid_);
177 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
178 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
183 struct dso *dso = al->map->dso;
185 err = db_export__dso(dbe, dso, al->machine);
188 *dso_db_id = dso->db_id;
191 al->sym = symbol__new(al->addr, 0, 0, "unknown");
193 symbols__insert(&dso->symbols[al->map->type],
198 u64 *db_id = symbol__priv(al->sym);
200 err = db_export__symbol(dbe, al->sym, dso);
204 *offset = al->addr - al->sym->start;
211 int db_export__sample(struct db_export *dbe, union perf_event *event,
212 struct perf_sample *sample, struct perf_evsel *evsel,
213 struct thread *thread, struct addr_location *al)
215 struct export_sample es = {
222 struct thread *main_thread;
223 struct comm *comm = NULL;
226 err = db_export__evsel(dbe, evsel);
230 err = db_export__machine(dbe, al->machine);
234 main_thread = get_main_thread(al->machine, thread);
236 comm = machine__thread_exec_comm(al->machine, main_thread);
238 err = db_export__thread(dbe, thread, al->machine, comm);
243 err = db_export__comm(dbe, comm, main_thread);
246 es.comm_db_id = comm->db_id;
249 es.db_id = ++dbe->sample_last_db_id;
251 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
255 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
256 sample_addr_correlates_sym(&evsel->attr)) {
257 struct addr_location addr_al;
259 perf_event__preprocess_sample_addr(event, sample, thread, &addr_al);
260 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
261 &es.addr_sym_db_id, &es.addr_offset);
266 if (dbe->export_sample)
267 return dbe->export_sample(dbe, &es);