From patchwork Thu Oct 17 13:15:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 176679 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp1058222ill; Thu, 17 Oct 2019 08:05:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqyYXSNXkXF1K4Fs1G3AeFm0GdoC//En20GbqVyQSe9ilRnxAChvcj88mEeSf1CW8IVNsrVm X-Received: by 2002:ac8:5556:: with SMTP id o22mr4284797qtr.217.1571324749661; Thu, 17 Oct 2019 08:05:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571324749; cv=none; d=google.com; s=arc-20160816; b=eYgYdDCsj7O+T/rjXbdYNiJNqXD90a8BmooY4sbwxazEWQ+40Iom4vwFcP88nGNkao KjrEheYPykb4qJlzFaPjmfUk4stTDPZ0IFFDUV/0I3Wq9INj157xe3BbZgCbVDp77na1 l9bWmN04BV4Awqi52/yFO8CEarb3ApHLIjWvn1EzkedOg51hL5jRV3Bvt9pKscx5QCUC BL4ZXHk93qAC2DXK3G6e/0OuuIPY1gyn4g3b9QmMyeALfT/0qcLmwv7G6YasviMKpmxN d7824i53oulmPJ+bReg1sKpKAAhgKyASHkR30Gyptf9f/mxbF4bH8FYQS1hpi93PaIy4 s0Fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=IUolK1djo1mG/qeP/V3N37e2P0JzDbNnrEogdbMOcvc=; b=ZedJq14h81qEseuq/dDDdtnapnX0DwtBDamU5q44dQqRfRMbUnHakU3GLRqDkZ2/Yt PPySzYefks57LMizzvPJIvIUG9srKc+rXLgfGpUFowKwcSZymBYQdzuLDA+zh7lpK+cd cKaEj1HoX7b3XJfaXSzKIRy59GEu13v0SzUzAIC0a3DrrRaKqZk5j0Vwopoco1E6sqOV Yay/SzeDjEBzjcXiZ1ziW0a2ycWCZCTWoD71ysRKT4st2Uybfo9U/7COc7fhjRiwbNhJ SuewlRvOuSw7JKdz36HTcXG87WZ+Z23CfgAwmHanQj5PR1k7+L0AKVAExjKegFPIFnsd p3Sw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="IjE/7y0O"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id f52si2253512qtk.208.2019.10.17.08.05.49 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 17 Oct 2019 08:05:49 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="IjE/7y0O"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:50282 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iL7LY-00081Z-FY for patch@linaro.org; Thu, 17 Oct 2019 11:05:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41259) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iL6FW-0004eR-FY for qemu-devel@nongnu.org; Thu, 17 Oct 2019 09:55:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iL6FV-0004ER-0B for qemu-devel@nongnu.org; Thu, 17 Oct 2019 09:55:30 -0400 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]:41293) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iL6FU-0004Dm-8R for qemu-devel@nongnu.org; Thu, 17 Oct 2019 09:55:28 -0400 Received: by mail-wr1-x433.google.com with SMTP id p4so2465158wrm.8 for ; Thu, 17 Oct 2019 06:55:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IUolK1djo1mG/qeP/V3N37e2P0JzDbNnrEogdbMOcvc=; b=IjE/7y0OXIQ2X3aDICESxenZsSZW9mEVtQskugt3rIxDOMEPFSwNEmuRwJ/UVqIFhq BaDLMmeKK6MEH8KUvSJfbcTr6UtU7ncZjojdpAkXeaYZGfUvdQVsqLN24H+Jfjt5Mfgo IgKgWOzqfDpcNCM+HNEyl/C3fXAQVRUrWmDwC2BNNoW+FkgLYoQasZJjS2Pk6GW6uZ/P aEmSVDaL0s3YkEN45ypPfclXbQYwBJ46YXNE+YmhhWpfhUg9fEYsH2JiZnHucq7eg0r3 z7s+YyoyVcCrCgIa084pqZaHLx3eNgeojEvg+ArjgI065pyDFDrDExjPbSXQnPd3YYAl Fiaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IUolK1djo1mG/qeP/V3N37e2P0JzDbNnrEogdbMOcvc=; b=aN+c99eizyrUiI8Mnh3/SxPd6JOTEnmVEUdN4G5RMPko7PdBiEoQSmD8xnXc2c8M8L /4iaKjAgxMMsv8/RaS39yUDZLH2K/CyYBAVgAp89lXMYeXHSxb70OoxFq/K1Jv4ifOU+ b6ucMU+EvK5WH+NXUuN8qny3FYxD0XLmXopCWWyrD7PZAOWlpcz/l0X//lGEU3I1mhw0 jD3Q66zPGpkRlOo8ka4QXkiTkc6pje80LORbxWpPhJy+1aXNqED71utD4cbfI0iV6KXS RxydQXVyxn8nGKrCS+4L4Lrkn57Coqv6aYcvYO2gzqqPEzmv7U8K4CZx6CWaeLevUJbg eiHA== X-Gm-Message-State: APjAAAVN93wkcEjSvc+IUw9dX+yi25fqg3G0tmHfcwIHR6i3OeicBr6F Z/dHINEmKW/U5xG38vUFVjWCMA== X-Received: by 2002:a5d:5052:: with SMTP id h18mr3343591wrt.143.1571320524084; Thu, 17 Oct 2019 06:55:24 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id f143sm4398695wme.40.2019.10.17.06.55.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 06:55:21 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 74DD31FFBD; Thu, 17 Oct 2019 14:16:19 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v6 38/54] plugin: add qemu_plugin_insn_disas helper Date: Thu, 17 Oct 2019 14:15:59 +0100 Message-Id: <20191017131615.19660-39-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191017131615.19660-1-alex.bennee@linaro.org> References: <20191017131615.19660-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::433 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: robert.foley@futurewei.com, Richard Henderson , peter.puhov@futurewei.com, aaron@os.amperecomputing.com, cota@braap.org, =?utf-8?q?Alex_Benn?= =?utf-8?b?w6ll?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Give the plugins access to the QEMU dissasembler so they don't have to re-invent the wheel. We generate a warning when there are spare bytes in the decode buffer. This is usually due to the front end loading in more bytes than decoded. Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson --- v5 - fix plugin_printf to use vprintf with va - use __thread storage for string - only decode single instruction for bfd case - don't assert, warn_report when we have spare bytes in insn buffer v6 - checkpatch fixes --- disas.c | 110 +++++++++++++++++++++++++++++++++++ include/disas/disas.h | 2 + include/qemu/qemu-plugin.h | 9 +++ plugins/api.c | 9 ++- plugins/qemu-plugins.symbols | 1 + 5 files changed, 130 insertions(+), 1 deletion(-) -- 2.20.1 diff --git a/disas.c b/disas.c index 3e2bfa572b..3937da6157 100644 --- a/disas.c +++ b/disas.c @@ -418,6 +418,7 @@ static bool cap_disas_monitor(disassemble_info *info, uint64_t pc, int count) # define cap_disas_target(i, p, s) false # define cap_disas_host(i, p, s) false # define cap_disas_monitor(i, p, c) false +# define cap_disas_plugin(i, p, c) false #endif /* CONFIG_CAPSTONE */ /* Disassemble this for me please... (debugging). */ @@ -475,6 +476,115 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong code, } } +static __thread GString plugin_disas_output; + +static int plugin_printf(FILE *stream, const char *fmt, ...) +{ + va_list va; + GString *s = &plugin_disas_output; + int initial_len = s->len; + + va_start(va, fmt); + g_string_append_vprintf(s, fmt, va); + va_end(va); + + return s->len - initial_len; +} + +static void plugin_print_address(bfd_vma addr, struct disassemble_info *info) +{ + /* does nothing */ +} + + +#ifdef CONFIG_CAPSTONE +/* Disassemble a single instruction directly into plugin output */ +static +bool cap_disas_plugin(disassemble_info *info, uint64_t pc, size_t size) +{ + uint8_t cap_buf[1024]; + csh handle; + cs_insn *insn; + size_t csize = 0; + int count; + GString *s = &plugin_disas_output; + + if (cap_disas_start(info, &handle) != CS_ERR_OK) { + return false; + } + insn = cap_insn; + + size_t tsize = MIN(sizeof(cap_buf) - csize, size); + const uint8_t *cbuf = cap_buf; + target_read_memory(pc, cap_buf, tsize, info); + + count = cs_disasm(handle, cbuf, size, 0, 1, &insn); + + if (count) { + g_string_printf(s, "%s %s", insn->mnemonic, insn->op_str); + } else { + g_string_printf(s, "cs_disasm failed"); + } + + cs_close(&handle); + return true; +} +#endif + +/* + * We should only be dissembling one instruction at a time here. If + * there is left over it usually indicates the front end has read more + * bytes than it needed. + */ +char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size) +{ + CPUClass *cc = CPU_GET_CLASS(cpu); + int count; + CPUDebug s; + GString *ds = g_string_set_size(&plugin_disas_output, 0); + + g_assert(ds == &plugin_disas_output); + + INIT_DISASSEMBLE_INFO(s.info, NULL, plugin_printf); + + s.cpu = cpu; + s.info.read_memory_func = target_read_memory; + s.info.buffer_vma = addr; + s.info.buffer_length = size; + s.info.print_address_func = plugin_print_address; + s.info.cap_arch = -1; + s.info.cap_mode = 0; + s.info.cap_insn_unit = 4; + s.info.cap_insn_split = 4; + +#ifdef TARGET_WORDS_BIGENDIAN + s.info.endian = BFD_ENDIAN_BIG; +#else + s.info.endian = BFD_ENDIAN_LITTLE; +#endif + + if (cc->disas_set_info) { + cc->disas_set_info(cpu, &s.info); + } + + if (s.info.cap_arch >= 0 && cap_disas_plugin(&s.info, addr, size)) { + return g_strdup(ds->str); + } + + if (s.info.print_insn == NULL) { + s.info.print_insn = print_insn_od_target; + } + + count = s.info.print_insn(addr, &s.info); + + /* The decoder probably read more than it needed it's not critical */ + if (count < size) { + warn_report("%s: %zu bytes left over", __func__, size - count); + } + + return g_strdup(ds->str); +} + /* Disassemble this for me please... (debugging). */ void disas(FILE *out, void *code, unsigned long size) { diff --git a/include/disas/disas.h b/include/disas/disas.h index ba47e9197c..36c33f6f19 100644 --- a/include/disas/disas.h +++ b/include/disas/disas.h @@ -14,6 +14,8 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong code, void monitor_disas(Monitor *mon, CPUState *cpu, target_ulong pc, int nb_insn, int is_physical); +char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size); + /* Look up symbol for debugging purpose. Returns "" if unknown. */ const char *lookup_symbol(target_ulong orig_addr); #endif diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 784f1dfc3d..ddf267fbfe 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -351,6 +351,15 @@ qemu_plugin_register_vcpu_syscall_ret_cb(qemu_plugin_id_t id, qemu_plugin_vcpu_syscall_ret_cb_t cb); +/** + * qemu_plugin_insn_disas() - return disassembly string for instruction + * @insn: instruction reference + * + * Returns an allocated string containing the disassembly + */ + +char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn); + /** * qemu_plugin_vcpu_for_each() - iterate over the existing vCPU * @id: plugin ID diff --git a/plugins/api.c b/plugins/api.c index 33dac8e790..5adc4d25a1 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -39,7 +39,8 @@ #include "cpu.h" #include "sysemu/sysemu.h" #include "tcg/tcg.h" -#include "trace/mem-internal.h" /* mem_info macros */ +#include "exec/exec-all.h" +#include "disas/disas.h" #include "plugin.h" #ifndef CONFIG_USER_ONLY #include "qemu/plugin-memory.h" @@ -212,6 +213,12 @@ void *qemu_plugin_insn_haddr(const struct qemu_plugin_insn *insn) return insn->haddr; } +char *qemu_plugin_insn_disas(const struct qemu_plugin_insn *insn) +{ + CPUState *cpu = current_cpu; + return plugin_disas(cpu, insn->vaddr, insn->data->len); +} + /* * The memory queries allow the plugin to query information about a * memory access. diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols index 40c0d1abd2..267ec381b4 100644 --- a/plugins/qemu-plugins.symbols +++ b/plugins/qemu-plugins.symbols @@ -25,6 +25,7 @@ qemu_plugin_insn_size; qemu_plugin_insn_vaddr; qemu_plugin_insn_haddr; + qemu_plugin_insn_disas; qemu_plugin_mem_size_shift; qemu_plugin_mem_is_sign_extended; qemu_plugin_mem_is_big_endian;