ppc/ppc4xx: Convert printfs()
[qemu.git] / plugins / api.c
blobb143b09ce9b39fa09f73959c628abfcdd627230d
1 /*
2 * QEMU Plugin API
4 * This provides the API that is available to the plugins to interact
5 * with QEMU. We have to be careful not to expose internal details of
6 * how QEMU works so we abstract out things like translation and
7 * instructions to anonymous data types:
9 * qemu_plugin_tb
10 * qemu_plugin_insn
12 * Which can then be passed back into the API to do additional things.
13 * As such all the public functions in here are exported in
14 * qemu-plugin.h.
16 * The general life-cycle of a plugin is:
18 * - plugin is loaded, public qemu_plugin_install called
19 * - the install func registers callbacks for events
20 * - usually an atexit_cb is registered to dump info at the end
21 * - when a registered event occurs the plugin is called
22 * - some events pass additional info
23 * - during translation the plugin can decide to instrument any
24 * instruction
25 * - when QEMU exits all the registered atexit callbacks are called
27 * Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
28 * Copyright (C) 2019, Linaro
30 * License: GNU GPL, version 2 or later.
31 * See the COPYING file in the top-level directory.
33 * SPDX-License-Identifier: GPL-2.0-or-later
37 #include "qemu/osdep.h"
38 #include "qemu/plugin.h"
39 #include "tcg/tcg.h"
40 #include "exec/exec-all.h"
41 #include "exec/ram_addr.h"
42 #include "disas/disas.h"
43 #include "plugin.h"
44 #ifndef CONFIG_USER_ONLY
45 #include "qemu/plugin-memory.h"
46 #include "hw/boards.h"
47 #endif
49 /* Uninstall and Reset handlers */
51 void qemu_plugin_uninstall(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
53 plugin_reset_uninstall(id, cb, false);
56 void qemu_plugin_reset(qemu_plugin_id_t id, qemu_plugin_simple_cb_t cb)
58 plugin_reset_uninstall(id, cb, true);
62 * Plugin Register Functions
64 * This allows the plugin to register callbacks for various events
65 * during the translation.
68 void qemu_plugin_register_vcpu_init_cb(qemu_plugin_id_t id,
69 qemu_plugin_vcpu_simple_cb_t cb)
71 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_INIT, cb);
74 void qemu_plugin_register_vcpu_exit_cb(qemu_plugin_id_t id,
75 qemu_plugin_vcpu_simple_cb_t cb)
77 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_EXIT, cb);
80 void qemu_plugin_register_vcpu_tb_exec_cb(struct qemu_plugin_tb *tb,
81 qemu_plugin_vcpu_udata_cb_t cb,
82 enum qemu_plugin_cb_flags flags,
83 void *udata)
85 if (!tb->mem_only) {
86 plugin_register_dyn_cb__udata(&tb->cbs[PLUGIN_CB_REGULAR],
87 cb, flags, udata);
91 void qemu_plugin_register_vcpu_tb_exec_inline(struct qemu_plugin_tb *tb,
92 enum qemu_plugin_op op,
93 void *ptr, uint64_t imm)
95 if (!tb->mem_only) {
96 plugin_register_inline_op(&tb->cbs[PLUGIN_CB_INLINE], 0, op, ptr, imm);
100 void qemu_plugin_register_vcpu_insn_exec_cb(struct qemu_plugin_insn *insn,
101 qemu_plugin_vcpu_udata_cb_t cb,
102 enum qemu_plugin_cb_flags flags,
103 void *udata)
105 if (!insn->mem_only) {
106 plugin_register_dyn_cb__udata(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_REGULAR],
107 cb, flags, udata);
111 void qemu_plugin_register_vcpu_insn_exec_inline(struct qemu_plugin_insn *insn,
112 enum qemu_plugin_op op,
113 void *ptr, uint64_t imm)
115 if (!insn->mem_only) {
116 plugin_register_inline_op(&insn->cbs[PLUGIN_CB_INSN][PLUGIN_CB_INLINE],
117 0, op, ptr, imm);
123 * We always plant memory instrumentation because they don't finalise until
124 * after the operation has complete.
126 void qemu_plugin_register_vcpu_mem_cb(struct qemu_plugin_insn *insn,
127 qemu_plugin_vcpu_mem_cb_t cb,
128 enum qemu_plugin_cb_flags flags,
129 enum qemu_plugin_mem_rw rw,
130 void *udata)
132 plugin_register_vcpu_mem_cb(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_REGULAR],
133 cb, flags, rw, udata);
136 void qemu_plugin_register_vcpu_mem_inline(struct qemu_plugin_insn *insn,
137 enum qemu_plugin_mem_rw rw,
138 enum qemu_plugin_op op, void *ptr,
139 uint64_t imm)
141 plugin_register_inline_op(&insn->cbs[PLUGIN_CB_MEM][PLUGIN_CB_INLINE],
142 rw, op, ptr, imm);
145 void qemu_plugin_register_vcpu_tb_trans_cb(qemu_plugin_id_t id,
146 qemu_plugin_vcpu_tb_trans_cb_t cb)
148 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_TB_TRANS, cb);
151 void qemu_plugin_register_vcpu_syscall_cb(qemu_plugin_id_t id,
152 qemu_plugin_vcpu_syscall_cb_t cb)
154 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL, cb);
157 void
158 qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id,
159 qemu_plugin_vcpu_syscall_ret_cb_t cb)
161 plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_SYSCALL_RET, cb);
165 * Plugin Queries
167 * These are queries that the plugin can make to gauge information
168 * from our opaque data types. We do not want to leak internal details
169 * here just information useful to the plugin.
173 * Translation block information:
175 * A plugin can query the virtual address of the start of the block
176 * and the number of instructions in it. It can also get access to
177 * each translated instruction.
180 size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb)
182 return tb->n;
185 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb)
187 return tb->vaddr;
190 struct qemu_plugin_insn *
191 qemu_plugin_tb_get_insn(const struct qemu_plugin_tb *tb, size_t idx)
193 struct qemu_plugin_insn *insn;
194 if (unlikely(idx >= tb->n)) {
195 return NULL;
197 insn = g_ptr_array_index(tb->insns, idx);
198 insn->mem_only = tb->mem_only;
199 return insn;
203 * Instruction information
205 * These queries allow the plugin to retrieve information about each
206 * instruction being translated.
209 const void *qemu_plugin_insn_data(const struct qemu_plugin_insn *insn)
211 return insn->data->data;
214 size_t qemu_plugin_insn_size(const struct qemu_plugin_insn *insn)
216 return insn->data->len;
219 uint64_t qemu_plugin_insn_vaddr(const struct qemu_plugin_insn *insn)
221 return insn->vaddr;
224 void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn)
226 return insn->haddr;
229 char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn)
231 CPUState *cpu = current_cpu;
232 return plugin_disas(cpu, insn->vaddr, insn->data->len);
235 const char *qemu_plugin_insn_symbol(const struct qemu_plugin_insn *insn)
237 const char *sym = lookup_symbol(insn->vaddr);
238 return sym[0] != 0 ? sym : NULL;
242 * The memory queries allow the plugin to query information about a
243 * memory access.
246 unsigned qemu_plugin_mem_size_shift(qemu_plugin_meminfo_t info)
248 MemOp op = get_memop(info);
249 return op & MO_SIZE;
252 bool qemu_plugin_mem_is_sign_extended(qemu_plugin_meminfo_t info)
254 MemOp op = get_memop(info);
255 return op & MO_SIGN;
258 bool qemu_plugin_mem_is_big_endian(qemu_plugin_meminfo_t info)
260 MemOp op = get_memop(info);
261 return (op & MO_BSWAP) == MO_BE;
264 bool qemu_plugin_mem_is_store(qemu_plugin_meminfo_t info)
266 return get_plugin_meminfo_rw(info) & QEMU_PLUGIN_MEM_W;
270 * Virtual Memory queries
273 #ifdef CONFIG_SOFTMMU
274 static __thread struct qemu_plugin_hwaddr hwaddr_info;
275 #endif
277 struct qemu_plugin_hwaddr *qemu_plugin_get_hwaddr(qemu_plugin_meminfo_t info,
278 uint64_t vaddr)
280 #ifdef CONFIG_SOFTMMU
281 CPUState *cpu = current_cpu;
282 unsigned int mmu_idx = get_mmuidx(info);
283 enum qemu_plugin_mem_rw rw = get_plugin_meminfo_rw(info);
284 hwaddr_info.is_store = (rw & QEMU_PLUGIN_MEM_W) != 0;
286 if (!tlb_plugin_lookup(cpu, vaddr, mmu_idx,
287 hwaddr_info.is_store, &hwaddr_info)) {
288 error_report("invalid use of qemu_plugin_get_hwaddr");
289 return NULL;
292 return &hwaddr_info;
293 #else
294 return NULL;
295 #endif
298 bool qemu_plugin_hwaddr_is_io(const struct qemu_plugin_hwaddr *haddr)
300 #ifdef CONFIG_SOFTMMU
301 return haddr->is_io;
302 #else
303 return false;
304 #endif
307 uint64_t qemu_plugin_hwaddr_phys_addr(const struct qemu_plugin_hwaddr *haddr)
309 #ifdef CONFIG_SOFTMMU
310 if (haddr) {
311 if (!haddr->is_io) {
312 RAMBlock *block;
313 ram_addr_t offset;
314 void *hostaddr = haddr->v.ram.hostaddr;
316 block = qemu_ram_block_from_host(hostaddr, false, &offset);
317 if (!block) {
318 error_report("Bad host ram pointer %p", haddr->v.ram.hostaddr);
319 abort();
322 return block->offset + offset + block->mr->addr;
323 } else {
324 MemoryRegionSection *mrs = haddr->v.io.section;
325 return mrs->offset_within_address_space + haddr->v.io.offset;
328 #endif
329 return 0;
332 const char *qemu_plugin_hwaddr_device_name(const struct qemu_plugin_hwaddr *h)
334 #ifdef CONFIG_SOFTMMU
335 if (h && h->is_io) {
336 MemoryRegionSection *mrs = h->v.io.section;
337 if (!mrs->mr->name) {
338 unsigned long maddr = 0xffffffff & (uintptr_t) mrs->mr;
339 g_autofree char *temp = g_strdup_printf("anon%08lx", maddr);
340 return g_intern_string(temp);
341 } else {
342 return g_intern_string(mrs->mr->name);
344 } else {
345 return g_intern_static_string("RAM");
347 #else
348 return g_intern_static_string("Invalid");
349 #endif
353 * Queries to the number and potential maximum number of vCPUs there
354 * will be. This helps the plugin dimension per-vcpu arrays.
357 #ifndef CONFIG_USER_ONLY
358 static MachineState * get_ms(void)
360 return MACHINE(qdev_get_machine());
362 #endif
364 int qemu_plugin_n_vcpus(void)
366 #ifdef CONFIG_USER_ONLY
367 return -1;
368 #else
369 return get_ms()->smp.cpus;
370 #endif
373 int qemu_plugin_n_max_vcpus(void)
375 #ifdef CONFIG_USER_ONLY
376 return -1;
377 #else
378 return get_ms()->smp.max_cpus;
379 #endif
383 * Plugin output
385 void qemu_plugin_outs(const char *string)
387 qemu_log_mask(CPU_LOG_PLUGIN, "%s", string);
390 bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
392 return name && value && qapi_bool_parse(name, value, ret, NULL);