From nobody Wed Feb 11 07:13:12 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1694502932; cv=none; d=zohomail.com; s=zohoarc; b=PJIYrN1fmgVQqJEKJMiTHAFRkRjd9LTmwZecN8+JB5EeRWGiCprY87+9M5qheQnAp/2kjPUq0I9GOcBCD/wvKfsc5+ek3b1TPDDT0rBahkj1kXH8kk974ydtMT8DZ/Vsc008xIDcuxKS2+x5lPFUnUSjSCMSgW++cov9/NatFNE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694502932; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject; bh=JCJCzqQ6Ba4FCVkTkN3rupr67lFRisvbzNM+HnYlWm0=; b=EYWkG1uqc6HiP6SaoH/3+esiJWpdcgnEYAEal3EUHWx67aANFhWBqX/zIcDz86DGRK5Dnw9qoSgdV7NjOEfe39WO6s0v/1K3Fijv/xoxoVK74YvJMDFBbfdMu/BaUuI0hyol+fBoHdf9Cbv25uPliAuLZDH550VpTmtopQ5bWO0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1694502932141888.3379867739445; Tue, 12 Sep 2023 00:15:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qfxaD-0003CV-OW; Tue, 12 Sep 2023 03:13:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qfxaC-0003Bn-4x for qemu-devel@nongnu.org; Tue, 12 Sep 2023 03:13:12 -0400 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qfxa9-0006fy-9m for qemu-devel@nongnu.org; Tue, 12 Sep 2023 03:13:11 -0400 Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-6bcb5df95c5so3805775a34.1 for ; Tue, 12 Sep 2023 00:13:08 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id f13-20020aa78b0d000000b0068be348e35fsm6647495pfd.166.2023.09.12.00.13.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 00:13:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1694502788; x=1695107588; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JCJCzqQ6Ba4FCVkTkN3rupr67lFRisvbzNM+HnYlWm0=; b=FC1b05LH78/ON+Xv13SxqLK9sFO48hYIXtot5YlEeOucRIv93Tcku8hVbgY1JA7EPY KGLStUMjmMd5zssNtFt8QvDbCYjjklr1WNOps1o7uPW6YB3xCdhisSsa8Xj3HG1iFMbj JIEFktLTj5j8toomJnuuohIxQv2pgwNarE62u7dpRccpSWngOMvVf5Vzv3KdFTHMDgDX JIGkHdwwc7oBrQcIVIUtq2hDXHwq0mOS8epx1EzxqLmilP0DAdVzwVzzHh7tgwKVxjb3 iLANoOiqt5OJ1X9WW+GkP5Y8zqRJUuN0+x8H0KCZ54Xp4MjeCXv66pX66MKt2T8VuJTw m91Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694502788; x=1695107588; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JCJCzqQ6Ba4FCVkTkN3rupr67lFRisvbzNM+HnYlWm0=; b=NQ0a4Gwz2h4ukuVbA5x8QzRqCOuXRaQ/RxiFo9XcZqhSOgaBPCQ0sgOdoTI0kaZYbV VD8KUr+Kh+XYmJrNMTYGT+8pcVR+gtLn2J12uqX+DDuw0CRmjkaPvHJwRFet1ngDZ8gh 84++5TGswkXt1boIxEJrEpUD+XnUf+awkf6l1txSCyFkP7GBWDmogcnfgGt8jUTWX/s6 ce30hMB2AfzaZ1YTXuQ4lzrSqFZNoAWMCuUHaWMPLReQOQaJjjnjYC6b42/BdQ24ki8V Q4D8i1Y5n1DP0Y4kxz6ueG4939tOsdvG089wmvvMzWTdzCylfEmbXkUPZk0JoqM83VfJ 30/Q== X-Gm-Message-State: AOJu0Yz1poLocJVUKVgCrgPvaTQqXsRX0sC35xoAq0OvNz7Ti/+F15P5 UGFyw52O3q0NP/8lanLehj31+OUeM5Y1M9NmShE= X-Google-Smtp-Source: AGHT+IGwOelZlbCfSJpQ8EmWnRt5I4JfzpQnbkjJ7QApr9CQhCDaxmf9iRzSkujZf958GL6Y8mVhBw== X-Received: by 2002:a05:6358:9992:b0:13e:b477:8015 with SMTP id j18-20020a056358999200b0013eb4778015mr13248448rwb.9.1694502787999; Tue, 12 Sep 2023 00:13:07 -0700 (PDT) From: Akihiko Odaki To: Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Mikhail Tyutin , Aleksandr Anenkov , qemu-devel@nongnu.org, Akihiko Odaki , Alexandre Iooss , Mahmoud Mandour , Richard Henderson , Paolo Bonzini Subject: [PATCH v7 16/18] contrib/plugins: Allow to log registers Date: Tue, 12 Sep 2023 16:12:02 +0900 Message-ID: <20230912071206.30751-17-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230912071206.30751-1-akihiko.odaki@daynix.com> References: <20230912071206.30751-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::333; envelope-from=akihiko.odaki@daynix.com; helo=mail-ot1-x333.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1694502933289100006 Content-Type: text/plain; charset="utf-8" This demonstrates how a register can be read from a plugin. Signed-off-by: Akihiko Odaki --- docs/devel/tcg-plugins.rst | 10 +++- contrib/plugins/execlog.c | 120 +++++++++++++++++++++++++++---------- 2 files changed, 97 insertions(+), 33 deletions(-) diff --git a/docs/devel/tcg-plugins.rst b/docs/devel/tcg-plugins.rst index 81dcd43a61..c9f8b27590 100644 --- a/docs/devel/tcg-plugins.rst +++ b/docs/devel/tcg-plugins.rst @@ -497,6 +497,15 @@ arguments if required:: $ qemu-system-arm $(QEMU_ARGS) \ -plugin ./contrib/plugins/libexeclog.so,ifilter=3Dst1w,afilter=3D0x400= 01808 -d plugin =20 +This plugin can also dump a specified register. The specification of regis= ter +follows `GDB standard target features `__. + +Specify the name of the feature that contains the register and the name of= the +register with ``rfile`` and ``reg`` options, respectively:: + + $ qemu-system-arm $(QEMU_ARGS) \ + -plugin ./contrib/plugins/libexeclog.so,rfile=3Dorg.gnu.gdb.arm.core,r= eg=3Dsp -d plugin + - contrib/plugins/cache.c =20 Cache modelling plugin that measures the performance of a given L1 cache @@ -583,4 +592,3 @@ The following API is generated from the inline document= ation in include the full kernel-doc annotations. =20 .. kernel-doc:: include/qemu/qemu-plugin.h - diff --git a/contrib/plugins/execlog.c b/contrib/plugins/execlog.c index 82dc2f584e..f3e714c888 100644 --- a/contrib/plugins/execlog.c +++ b/contrib/plugins/execlog.c @@ -15,27 +15,43 @@ =20 #include =20 +typedef struct CPU { + /* Store last executed instruction on each vCPU as a GString */ + GString *last_exec; + GByteArray *reg_history[2]; + + int reg; +} CPU; + QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; =20 -/* Store last executed instruction on each vCPU as a GString */ -static GPtrArray *last_exec; +static CPU *cpus; +static int num_cpus; static GRWLock expand_array_lock; =20 static GPtrArray *imatches; static GArray *amatches; =20 +static char *rfile_name; +static char *reg_name; + /* - * Expand last_exec array. + * Expand cpu array. * * As we could have multiple threads trying to do this we need to * serialise the expansion under a lock. */ -static void expand_last_exec(int cpu_index) +static void expand_cpu(int cpu_index) { g_rw_lock_writer_lock(&expand_array_lock); - while (cpu_index >=3D last_exec->len) { - GString *s =3D g_string_new(NULL); - g_ptr_array_add(last_exec, s); + if (cpu_index >=3D num_cpus) { + cpus =3D g_realloc_n(cpus, cpu_index + 1, sizeof(*cpus)); + while (cpu_index >=3D num_cpus) { + cpus[num_cpus].last_exec =3D g_string_new(NULL); + cpus[num_cpus].reg_history[0] =3D g_byte_array_new(); + cpus[num_cpus].reg_history[1] =3D g_byte_array_new(); + num_cpus++; + } } g_rw_lock_writer_unlock(&expand_array_lock); } @@ -50,8 +66,8 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugin_= meminfo_t info, =20 /* Find vCPU in array */ g_rw_lock_reader_lock(&expand_array_lock); - g_assert(cpu_index < last_exec->len); - s =3D g_ptr_array_index(last_exec, cpu_index); + g_assert(cpu_index < num_cpus); + s =3D cpus[cpu_index].last_exec; g_rw_lock_reader_unlock(&expand_array_lock); =20 /* Indicate type of memory access */ @@ -77,28 +93,42 @@ static void vcpu_mem(unsigned int cpu_index, qemu_plugi= n_meminfo_t info, */ static void vcpu_insn_exec(unsigned int cpu_index, void *udata) { - GString *s; + int n; + int i; =20 - /* Find or create vCPU in array */ g_rw_lock_reader_lock(&expand_array_lock); - if (cpu_index >=3D last_exec->len) { - g_rw_lock_reader_unlock(&expand_array_lock); - expand_last_exec(cpu_index); - g_rw_lock_reader_lock(&expand_array_lock); - } - s =3D g_ptr_array_index(last_exec, cpu_index); - g_rw_lock_reader_unlock(&expand_array_lock); =20 /* Print previous instruction in cache */ - if (s->len) { - qemu_plugin_outs(s->str); + if (cpus[cpu_index].last_exec->len) { + if (cpus[cpu_index].reg >=3D 0) { + GByteArray *current =3D cpus[cpu_index].reg_history[0]; + GByteArray *last =3D cpus[cpu_index].reg_history[1]; + + g_byte_array_set_size(current, 0); + n =3D qemu_plugin_read_register(current, cpus[cpu_index].reg); + + if (n !=3D last->len || memcmp(current->data, last->data, n)) { + g_string_append(cpus[cpu_index].last_exec, ", reg,"); + for (i =3D 0; i < n; i++) { + g_string_append_printf(cpus[cpu_index].last_exec, " %0= 2x", + current->data[i]); + } + } + + cpus[cpu_index].reg_history[0] =3D last; + cpus[cpu_index].reg_history[1] =3D current; + } + + qemu_plugin_outs(cpus[cpu_index].last_exec->str); qemu_plugin_outs("\n"); } =20 /* Store new instruction in cache */ /* vcpu_mem will add memory access information to last_exec */ - g_string_printf(s, "%u, ", cpu_index); - g_string_append(s, (char *)udata); + g_string_printf(cpus[cpu_index].last_exec, "%u, ", cpu_index); + g_string_append(cpus[cpu_index].last_exec, (char *)udata); + + g_rw_lock_reader_unlock(&expand_array_lock); } =20 /** @@ -167,8 +197,10 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct = qemu_plugin_tb *tb) QEMU_PLUGIN_MEM_RW, NULL); =20 /* Register callback on instruction */ - qemu_plugin_register_vcpu_insn_exec_cb(insn, vcpu_insn_exec, - QEMU_PLUGIN_CB_NO_REGS,= output); + qemu_plugin_register_vcpu_insn_exec_cb( + insn, vcpu_insn_exec, + rfile_name ? QEMU_PLUGIN_CB_R_REGS : QEMU_PLUGIN_CB_NO_REG= S, + output); =20 /* reset skip */ skip =3D (imatches || amatches); @@ -177,17 +209,33 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct= qemu_plugin_tb *tb) } } =20 +static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index) +{ + int reg =3D -1; + + expand_cpu(vcpu_index); + + if (rfile_name) { + int rfile =3D qemu_plugin_find_register_file(vcpu_index, rfile_nam= e); + if (rfile >=3D 0) { + reg =3D qemu_plugin_find_register(vcpu_index, rfile, reg_name); + } + } + + g_rw_lock_writer_lock(&expand_array_lock); + cpus[vcpu_index].reg =3D reg; + g_rw_lock_writer_unlock(&expand_array_lock); +} + /** * On plugin exit, print last instruction in cache */ static void plugin_exit(qemu_plugin_id_t id, void *p) { guint i; - GString *s; - for (i =3D 0; i < last_exec->len; i++) { - s =3D g_ptr_array_index(last_exec, i); - if (s->str) { - qemu_plugin_outs(s->str); + for (i =3D 0; i < num_cpus; i++) { + if (cpus[i].last_exec->str) { + qemu_plugin_outs(cpus[i].last_exec->str); qemu_plugin_outs("\n"); } } @@ -224,9 +272,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_= id_t id, * we don't know the size before emulation. */ if (info->system_emulation) { - last_exec =3D g_ptr_array_sized_new(info->system.max_vcpus); - } else { - last_exec =3D g_ptr_array_new(); + cpus =3D g_new(CPU, info->system.max_vcpus); } =20 for (int i =3D 0; i < argc; i++) { @@ -236,13 +282,23 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugi= n_id_t id, parse_insn_match(tokens[1]); } else if (g_strcmp0(tokens[0], "afilter") =3D=3D 0) { parse_vaddr_match(tokens[1]); + } else if (g_strcmp0(tokens[0], "rfile") =3D=3D 0) { + rfile_name =3D g_strdup(tokens[1]); + } else if (g_strcmp0(tokens[0], "reg") =3D=3D 0) { + reg_name =3D g_strdup(tokens[1]); } else { fprintf(stderr, "option parsing failed: %s\n", opt); return -1; } } =20 + if ((!rfile_name) !=3D (!reg_name)) { + fputs("file and reg need to be set at the same time\n", stderr); + return -1; + } + /* Register translation block and exit callbacks */ + qemu_plugin_register_vcpu_init_cb(id, vcpu_init); qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); =20 --=20 2.42.0