From nobody Sun Dec 14 06:29:20 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1754685805; cv=none; d=zohomail.com; s=zohoarc; b=VWLccSPCI/aWhZ9rWXOFNZfjDjBtPlGa9s9IMJk4qiPCQXH3rpO8nn+wBBhErIiFQyxhIE+jUZFzX0c4Ie0ZYvg1PBpcbLah/Oln9I5PxrHj+WoDtI3uwWBWvenJKHAyd8ziWhXMI6NAMJ5PiINxEu/Qm9DMnIS+4umsBSwD88E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1754685805; 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:To:To:Message-Id:Reply-To; bh=n8jo0LSSh8lJpsdcimtvOxocpTr/cr5eTG3kdaxxUXg=; b=Q+4QTbmPUW8FXA6RL/e5GFFGhuIt3/XnMLnAhbGcrLBiWqiJVFK6GWJ22HcUotxvpAPNHlSpMq6r6pKK3iqq5j7OwFE60Fy/2M9fr8aMpUn0FNZ/JLMw73kjRAGcesonwY5+UpYCcI9uq/f9a6PcTF6DZDC0tpf/142ne7SyC8Y= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1754685805003684.118152649568; Fri, 8 Aug 2025 13:43:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ukTut-00030W-Rg; Fri, 08 Aug 2025 16:42:19 -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 1ukTup-0002xd-0M for qemu-devel@nongnu.org; Fri, 08 Aug 2025 16:42:16 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ukTul-0003Cn-D6 for qemu-devel@nongnu.org; Fri, 08 Aug 2025 16:42:14 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-23fc5aedaf0so17958255ad.2 for ; Fri, 08 Aug 2025 13:42:11 -0700 (PDT) Received: from pc.. ([38.41.223.211]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-241e899a814sm217789945ad.117.2025.08.08.13.42.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Aug 2025 13:42:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1754685730; x=1755290530; 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=n8jo0LSSh8lJpsdcimtvOxocpTr/cr5eTG3kdaxxUXg=; b=xR+5p2yf+WaJCXZfrMeAJFHIutsX8m1tK51Mcs2j/8sQ9bn2n/aTdGZRCcf9qftbrO G12uQdy/a1WZCcRDHxVBEyGXL4g6zKLyG1eWWRmu2TeGbDu7kUQKbqGooqxoey8k8L92 k/g0swKW+HikCu7S4sQ/fhL/TrrebxEixJBZXwp5cZOd4KSBsCJzFMhbJmjtH1k5qJal hZH9rSg0YGx70YYVvKCl42xwAG3h8wxY1Dk3FI++3hlFmZL+AnTKTFwLCUNIv8/FT/UT Yn+Y9JqUaLgH9ob9I6NxKfxBSHku30ik4/sZgtKYQDnYaChskUKtchs2jhKef0khmpyS Biyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754685730; x=1755290530; 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=n8jo0LSSh8lJpsdcimtvOxocpTr/cr5eTG3kdaxxUXg=; b=UYoZum8mCTNYt4E53/fyP5ToaLWAvc3ghc1UXnamm0MNUkn+7S+ejkknBAao5lo7OJ Mz4tk1pKt8CcSE4Ok82ly0eLout1KXDM6M83p+l+c4ueH6T1Z6kh/X64sg2ilZ2Rgtnx c/09zhnhtSSPAVBAxXbr6fvJFb2oyCilWlgNZoxc1GEzHP4hW/K6idjjaT9mZsJO3ugy 5RuW1gVTwIm97bfg7zVM10MnkUP27o8vF7rC6TVviLROIBbnuOIGhyyEz1u/2myxRvjD GviZ4VpJ2nwtPJMGUwEFqf/bJatJclBwr3Eb4AjYmR1gRsEWI7FY1/945RFYXn1vUQGl RGww== X-Gm-Message-State: AOJu0YzIwFnxrS/7B7KAcV9C9deNpPSyEB13rmBx+iZh4UeQPbWpHK6v L8PsOK5JJLdHqDv/o60ZIhmtKj4gmhB/F1W8G9WtTER/vyChyYIH968ivrVmTYFBJz+FyJUf/Et ohzqOPR4oUg== X-Gm-Gg: ASbGncuTa5Fi74yh5rFd3Id2ZZWNt+u2SfOGRdiIsAvLCKWCUp9DZLKoKu0EloQwHan XpmZWeNvd538kz8Eoj3YWE/CD90Utvlm81sDp+5j+TJgOamQeIYz9Sw51B63aDYBP/ELjo15lUH fLgc01hyu5dDXTINSO5fOHTKsj/HkmGvvPH9m4ID0kgPiLBV0in/vMcX4Xkr5trteqtDIlL4AYC V+W6hYpvGsB7Rk5/WFUc8vfplmzO6f3fqLcag7Kc1P0Omwf0MOB/71g5W6MT5FouHHdMuu41En9 4Hy914iJDNFPkYjGLmAei+g+isK46O8Xr8wTvxnuTv7Ktt8RvK51cABE781W17uJLj5f/fApJZF YQS9SWyjoVRrJ7CRlvPnjWg== X-Google-Smtp-Source: AGHT+IHdyzwZo6v3pLXWFd5wdi1PwGtBgW4b459IoDCidfZlewJaQGcNMIUZEfzm2EH8PTbD0W6R6Q== X-Received: by 2002:a17:903:40c9:b0:240:5549:708e with SMTP id d9443c01a7336-242c225ad36mr67222365ad.46.1754685729829; Fri, 08 Aug 2025 13:42:09 -0700 (PDT) From: Pierrick Bouvier To: qemu-devel@nongnu.org, pierrick.bouvier@linaro.org Cc: Manos Pitsidianakis , Peter Maydell , rowan Hart , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Gustavo Romero , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Alexandre Iooss , Mahmoud Mandour , Richard Henderson Subject: [PATCH v6 3/9] contrib/plugins/uftrace: track callstack Date: Fri, 8 Aug 2025 13:41:50 -0700 Message-ID: <20250808204156.659715-4-pierrick.bouvier@linaro.org> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250808204156.659715-1-pierrick.bouvier@linaro.org> References: <20250808204156.659715-1-pierrick.bouvier@linaro.org> 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: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=pierrick.bouvier@linaro.org; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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 @linaro.org) X-ZM-MESSAGEID: 1754685807560116600 Content-Type: text/plain; charset="utf-8" We now track callstack, based on frame pointer analysis. We can detect function calls, returns, and discontinuities. We implement a frame pointer based unwinding that is used for discontinuities. Reviewed-by: Manos Pitsidianakis Signed-off-by: Pierrick Bouvier --- contrib/plugins/uftrace.c | 160 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/contrib/plugins/uftrace.c b/contrib/plugins/uftrace.c index d060513446c..bb775916270 100644 --- a/contrib/plugins/uftrace.c +++ b/contrib/plugins/uftrace.c @@ -15,6 +15,15 @@ =20 QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; =20 +typedef struct { + GArray *s; +} Callstack; + +typedef struct { + uint64_t pc; + uint64_t frame_pointer; +} CallstackEntry; + typedef struct Cpu Cpu; =20 typedef struct { @@ -25,6 +34,7 @@ typedef struct { } CpuOps; =20 typedef struct Cpu { + Callstack *cs; GByteArray *buf; CpuOps ops; void *arch; @@ -37,6 +47,71 @@ typedef struct { static struct qemu_plugin_scoreboard *score; static CpuOps arch_ops; =20 +static Callstack *callstack_new(void) +{ + Callstack *cs =3D g_new0(Callstack, 1); + cs->s =3D g_array_new(false, false, sizeof(CallstackEntry)); + return cs; +} + +static void callstack_free(Callstack *cs) +{ + g_array_free(cs->s, true); + cs->s =3D NULL; + g_free(cs); +} + +static size_t callstack_depth(const Callstack *cs) +{ + return cs->s->len; +} + +static size_t callstack_empty(const Callstack *cs) +{ + return callstack_depth(cs) =3D=3D 0; +} + +static void callstack_clear(Callstack *cs) +{ + g_array_set_size(cs->s, 0); +} + +static const CallstackEntry *callstack_at(const Callstack *cs, size_t dept= h) +{ + g_assert(depth > 0); + g_assert(depth <=3D callstack_depth(cs)); + return &g_array_index(cs->s, CallstackEntry, depth - 1); +} + +static CallstackEntry callstack_top(const Callstack *cs) +{ + if (callstack_depth(cs) >=3D 1) { + return *callstack_at(cs, callstack_depth(cs)); + } + return (CallstackEntry){}; +} + +static CallstackEntry callstack_caller(const Callstack *cs) +{ + if (callstack_depth(cs) >=3D 2) { + return *callstack_at(cs, callstack_depth(cs) - 1); + } + return (CallstackEntry){}; +} + +static void callstack_push(Callstack *cs, CallstackEntry e) +{ + g_array_append_val(cs->s, e); +} + +static CallstackEntry callstack_pop(Callstack *cs) +{ + g_assert(!callstack_empty(cs)); + CallstackEntry e =3D callstack_top(cs); + g_array_set_size(cs->s, callstack_depth(cs) - 1); + return e; +} + static uint64_t cpu_read_register64(Cpu *cpu, struct qemu_plugin_register = *reg) { GByteArray *buf =3D cpu->buf; @@ -47,6 +122,50 @@ static uint64_t cpu_read_register64(Cpu *cpu, struct qe= mu_plugin_register *reg) return *((uint64_t *) buf->data); } =20 +static uint64_t cpu_read_memory64(Cpu *cpu, uint64_t addr) +{ + g_assert(addr); + GByteArray *buf =3D cpu->buf; + g_byte_array_set_size(buf, 0); + bool read =3D qemu_plugin_read_memory_vaddr(addr, buf, 8); + if (!read) { + return 0; + } + g_assert(buf->len =3D=3D 8); + return *((uint64_t *) buf->data); +} + +static void cpu_unwind_stack(Cpu *cpu, uint64_t frame_pointer, uint64_t pc) +{ + g_assert(callstack_empty(cpu->cs)); + + #define UNWIND_STACK_MAX_DEPTH 1024 + CallstackEntry unwind[UNWIND_STACK_MAX_DEPTH]; + size_t depth =3D 0; + do { + /* check we don't have an infinite stack */ + for (size_t i =3D 0; i < depth; ++i) { + if (frame_pointer =3D=3D unwind[i].frame_pointer) { + break; + } + } + CallstackEntry e =3D {.frame_pointer =3D frame_pointer, .pc =3D pc= }; + unwind[depth] =3D e; + depth++; + if (frame_pointer) { + frame_pointer =3D cpu_read_memory64(cpu, frame_pointer); + } + pc =3D cpu_read_memory64(cpu, frame_pointer + 8); /* read previous= lr */ + } while (frame_pointer && pc && depth < UNWIND_STACK_MAX_DEPTH); + #undef UNWIND_STACK_MAX_DEPTH + + /* push it from bottom to top */ + while (depth) { + callstack_push(cpu->cs, unwind[depth - 1]); + --depth; + } +} + static struct qemu_plugin_register *plugin_find_register(const char *name) { g_autoptr(GArray) regs =3D qemu_plugin_get_registers(); @@ -102,6 +221,43 @@ static CpuOps aarch64_ops =3D { =20 static void track_callstack(unsigned int cpu_index, void *udata) { + uint64_t pc =3D (uintptr_t) udata; + Cpu *cpu =3D qemu_plugin_scoreboard_find(score, cpu_index); + Callstack *cs =3D cpu->cs; + + uint64_t fp =3D cpu->ops.get_frame_pointer(cpu); + if (!fp && callstack_empty(cs)) { + /* + * We simply push current pc. Note that we won't detect symbol cha= nge as + * long as a proper call does not happen. + */ + callstack_push(cs, (CallstackEntry){.frame_pointer =3D fp, .pc =3D= pc}); + return; + } + + CallstackEntry top =3D callstack_top(cs); + if (fp =3D=3D top.frame_pointer) { + /* same function */ + return; + } + + CallstackEntry caller =3D callstack_caller(cs); + if (fp =3D=3D caller.frame_pointer) { + /* return */ + callstack_pop(cs); + return; + } + + uint64_t caller_fp =3D fp ? cpu_read_memory64(cpu, fp) : 0; + if (caller_fp =3D=3D top.frame_pointer) { + /* call */ + callstack_push(cs, (CallstackEntry){.frame_pointer =3D fp, .pc =3D= pc}); + return; + } + + /* discontinuity, exit current stack and unwind new one */ + callstack_clear(cs); + cpu_unwind_stack(cpu, fp, pc); } =20 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) @@ -140,12 +296,16 @@ static void vcpu_init(qemu_plugin_id_t id, unsigned i= nt vcpu_index) =20 cpu->ops.init(cpu); cpu->buf =3D g_byte_array_new(); + + cpu->cs =3D callstack_new(); } =20 static void vcpu_end(unsigned int vcpu_index) { Cpu *cpu =3D qemu_plugin_scoreboard_find(score, vcpu_index); g_byte_array_free(cpu->buf, true); + + callstack_free(cpu->cs); memset(cpu, 0, sizeof(Cpu)); } =20 --=20 2.47.2