From nobody Thu Jan 1 07:24:03 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=1698226473; cv=none; d=zohomail.com; s=zohoarc; b=hs0lm97UhsvjMtgpTY2r8Zg646RMZCjF0irucFajKxZL9khmf493orvY9B5WkulxmFfdiIuQwOR4U5/BtZzRECsEA98KAL+t8CUGsXRblyChFYmd/P0Waz6co39u2UW+slGU89prQAWEnf3SFjk/3jnfeXJvrq8t4h0UpTzGq3w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1698226473; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:Message-Id:Reply-To:To; bh=JCJCzqQ6Ba4FCVkTkN3rupr67lFRisvbzNM+HnYlWm0=; b=STBXzhimZlvKx9TiT5q5B47fakwPNOvpqhkf8fQx3PKxhn7pRLnvNzSO4g0hqlcL7YXpAqAJpctZse21022k29G1qflyao/qfo0UMe39i9ICwyYwJU68i83QlK+cd9PVlAHSnlQDDBQTSll4HRI7XbCXWO1CABQO1m6zxpvB0mU= 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 1698226473218194.28832644172633; Wed, 25 Oct 2023 02:34:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qvaGS-00085B-5r; Wed, 25 Oct 2023 05:33:25 -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 1qvaGQ-000802-Eb for qemu-devel@nongnu.org; Wed, 25 Oct 2023 05:33:22 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qvaGM-0002Xq-27 for qemu-devel@nongnu.org; Wed, 25 Oct 2023 05:33:22 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6b6f4c118b7so4548206b3a.0 for ; Wed, 25 Oct 2023 02:33:17 -0700 (PDT) Received: from localhost ([157.82.205.15]) by smtp.gmail.com with UTF8SMTPSA id e1-20020a630f01000000b005b856fab5e9sm8377316pgl.18.2023.10.25.02.33.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 Oct 2023 02:33:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1698226397; x=1698831197; 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=itV9w+Ss6i1N1ru8YtKG2pnO/mkiVOZyXV53i4bxjY1G6UsZy1wP7zgY1FQMjS8YII dFNytPIoH4Izn8C2iOR6RVvBvtnwx5OKqXB6eEXLfAK3b79O9lJTRISWoZ4HZbqUgBXj +kMeO250bVpKsYT2EFUnkrLdVapnwsfo4mvD6OY+WncPmQd6Rd6r/FeoyII4HuduT0f7 sKojZJT6TVEsOCYSplYFdgUQr//N4QBN2AdMWYINli776iXASnWTrvsR7eAKe6CzTl5k Dl9J/X4H1m2yuTD92WKqMdIvIQdup1l8YHKtOI8MkF3joIJNfXnvu9ODjb6nl+VMuYSc BfgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698226397; x=1698831197; 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=UnbG+25SGv/3TdaU0j+Nr0VgEGGPGpbx+dfsn9ORP/yTUji9p5/6NGCMUfgsuNJg8b J1kb9wTUAxwgAcyd1jx/aVKieJiH1bpP7Uo/G72WkgSI3VnHJKQTuqZF1eADh90ggG25 MV3qsPCVKbwS3oX/wY5wMMOUdXPY4U9Jy2X9tYxqQG5+0ZEPZZfgXVb+bj07luXy9xtH hrFJGH7OpVjqwVfdhtYGl4BP7Y/dovMXvIaVe96MqvJb+x/BIOp7hIi/O05wZ+5TINr+ A9ToIhL6SzImEymYFD2173MpLCcc83rfU+vPlMhs8RHyyyUJ2GMq3+DaWianCHauYUq+ rGcA== X-Gm-Message-State: AOJu0YzKJueXEm9M11BMbdUx5nAyrcVpidFkZNQ8tOcvxd8Gsk33mvWk qXbtoykcZlIhHd/z3/iZyHV7Rg== X-Google-Smtp-Source: AGHT+IHMI0vCnRaLppA5Y05yybnIp2u5RzooBDfeUtQ7Z2R+umY7T4dCbEoTTKP1IuYcwidSVkAYlQ== X-Received: by 2002:a05:6a20:c102:b0:15c:b7ba:ea44 with SMTP id bh2-20020a056a20c10200b0015cb7baea44mr4681468pzb.60.1698226396718; Wed, 25 Oct 2023 02:33:16 -0700 (PDT) From: Akihiko Odaki To: Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Mikhail Tyutin , Aleksandr Anenkov , qemu-devel@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Akihiko Odaki , Alexandre Iooss , Mahmoud Mandour , Richard Henderson , Paolo Bonzini Subject: [PATCH v16 18/18] contrib/plugins: Allow to log registers Date: Wed, 25 Oct 2023 18:31:18 +0900 Message-ID: <20231025093128.33116-19-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231025093128.33116-1-akihiko.odaki@daynix.com> References: <20231025093128.33116-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::430; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x430.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: 1698226475175100007 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