From nobody Fri Dec 19 20:55:42 2025 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=1693717650; cv=none; d=zohomail.com; s=zohoarc; b=OqfnnUYDM/3+hM11fJ7yqCS65X8wcrvt8JwsNHLmZfssYoaLv1k//drvLcfN5iCH43W8xXRPw1ugrBQ1nIPY7ALbCaL8gEggSu2x4l4n10fGfbB2pgYoIdXqSPTPvNt8fRq1I1qV6+S79npGbJOCkkZtQcqz8yyaZ6eRd+YnhlI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693717650; 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=KuvdESSySZtL3wl8sJ7C7/yP6SWTfRm12yxSgh3o1cM=; b=myXjjeUrsKfBRMYPLE+b3NYKVdDw4Paq1r0jL52fNfuPQ+Op2qEnGw1yp9TjOclTFOTxaHaVA4bScXCg/ebfBX3r245ZF8ruEnLrE319LIqUWFnbxnc+l4/DG0qoZ181FVCjs278M3WiZnTdj5l8UCpkayFdr3odnHBGM10M9Fw= 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 1693717650554822.4716174402357; Sat, 2 Sep 2023 22:07:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qcfKT-00081N-5X; Sun, 03 Sep 2023 01:07:22 -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 1qcfHo-0002rX-Cc for qemu-devel@nongnu.org; Sun, 03 Sep 2023 01:04:36 -0400 Received: from mail-oi1-x233.google.com ([2607:f8b0:4864:20::233]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qcfHl-0001os-JW for qemu-devel@nongnu.org; Sun, 03 Sep 2023 01:04:36 -0400 Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3a99eeb95aaso346298b6e.2 for ; Sat, 02 Sep 2023 22:04:33 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id l11-20020a170902f68b00b001b8af7f632asm5324835plg.176.2023.09.02.22.04.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Sep 2023 22:04:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1693717472; x=1694322272; 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=KuvdESSySZtL3wl8sJ7C7/yP6SWTfRm12yxSgh3o1cM=; b=Lsz/fAK8B7HWyZApLluzSGx+tR/CCTRtrypRIxbCw8pNqwo2eBnEqF65ElMFbGwimP /ZjngLrjlHyvAR/KlvGnqGSCnNIY+V3t171u82MgiDb07rEru6uP4nNQF5SqTAENMZ0C Gf41A+N8Cdmowdc1MFjBG4V5nL3CLQ/oHyeYEmxFoXVEVd9NMQVCHqqrQF51cNLa+LpE f++HS9dSeikCPqCmM0UbnYW5FoOoNrPO7T3SxZfvexO+mkRWjmeSu5Af0n1c5O62E60u qdxyieHVGBHEh+I0HVLUqP+jY+Y58fIB9sDZeSrttBkbzCEnan2d0gfig5ZZmQ9Gwt1L 0UDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693717472; x=1694322272; 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=KuvdESSySZtL3wl8sJ7C7/yP6SWTfRm12yxSgh3o1cM=; b=IJz9ama4UQi4Y2BdBJHN0fueqHLgBR6kyBX0bSAGcDuroMxxnkOs6dlyBKPZ7CAYho HbsNS882VVy/yz1WByaa3DjUP9eeH5CevY8rRQj9FNeQ19YmyppEWdV/pbEmWBJm/4J4 fdD4cmnI/A4Nv26s0Up+kc7ONq/y2BISJHSQP/WCiXcOF1Tuwa4QwV8DsSzrK+o2KigE 9lH6+WB/9g2o3gCOGs6Wb0HBC0ZwZJeBWvknSSkRDwCV5OrZ4tRDQkjmaxjQ1tJhBCZt rqW0gz+rQbE11jXw7/yaf6k7YuPFsn14nPGuKLkeLYechVqScObUM0hYW+wHV2nfgjzY KZXA== X-Gm-Message-State: AOJu0YzcL8XASCjIYs9bD4iNcmpdZCzFKUWU4psNgHjwGoZ+4W/IwZyY K19SvLUI5fnavwPFeQp27bjydQ== X-Google-Smtp-Source: AGHT+IEZf5qZaleVz2m8hGM8w46y4Y04hpnOBH9F90e0wDEpBHyNGaELtYkOxGwshN3ea2HqmjInTw== X-Received: by 2002:a05:6808:2394:b0:3a8:29bc:495a with SMTP id bp20-20020a056808239400b003a829bc495amr9526350oib.32.1693717472113; Sat, 02 Sep 2023 22:04:32 -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 v6 16/18] contrib/plugins: Allow to log registers Date: Sun, 3 Sep 2023 14:03:24 +0900 Message-ID: <20230903050338.35256-17-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230903050338.35256-1-akihiko.odaki@daynix.com> References: <20230903050338.35256-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::233; envelope-from=akihiko.odaki@daynix.com; helo=mail-oi1-x233.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: 1693717651130100003 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 | 140 ++++++++++++++++++++++++++++--------- 2 files changed, 117 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..aa05840fd0 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,53 @@ 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 0; + bool found =3D false; + + expand_cpu(vcpu_index); + + if (rfile_name) { + int i; + int j; + int n; + + qemu_plugin_register_file_t *rfiles =3D + qemu_plugin_get_register_files(vcpu_index, &n); + + for (i =3D 0; i < n; i++) { + if (g_strcmp0(rfiles[i].name, rfile_name) =3D=3D 0) { + for (j =3D 0; j < rfiles[i].num_regs; j++) { + if (g_strcmp0(rfiles[i].regs[j], reg_name) =3D=3D 0) { + reg +=3D j; + found =3D true; + break; + } + } + break; + } + + reg +=3D rfiles[i].num_regs; + } + + g_free(rfiles); + } + + g_rw_lock_writer_lock(&expand_array_lock); + cpus[vcpu_index].reg =3D found ? reg : -1; + 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 +292,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 +302,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