From nobody Mon Feb 9 23:40:32 2026 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1576843753; cv=none; d=zohomail.com; s=zohoarc; b=MK8bNGKarAReoiIE7iSdmLzc32SKd+6vR01Q6sTHo/IS46OuPwdRPthnBEfUY4oyK+Ec2HM80YTL0Yh/F5VCiUpSpM0/HQlGpe/wDZ+UH+b1NnxXFmzXS4PaA7rgik4h/Lh6IVnmFcRod200Snvj6uB8tv1oRe4nNbZYJEEhd44= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576843753; h=Content-Type: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:To; bh=OtFgNihQmjwXIczGB6RrVvTGCHXdssZoK9FktuuxCfA=; b=PyJlFsJD9MpgxCQVaMNymIz1cS2o/b0baYAQG5ZqloJ59MsVB894/h6CfWfWGASXisRDGnSqAZVxRIe+QLwvXBmsbrO04p4MCGcBlaeR/9S0UgDx+XMoCmyMJwNwbu1OHGfNToUcqplDzVjz12mE5vEPVSHLs4Uov1CiqcRBTyM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 157684375303676.86250187761755; Fri, 20 Dec 2019 04:09:13 -0800 (PST) Received: from localhost ([::1]:54010 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iiH5j-00018N-6b for importer@patchew.org; Fri, 20 Dec 2019 07:09:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51739) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iiH1S-0004Ye-3F for qemu-devel@nongnu.org; Fri, 20 Dec 2019 07:04:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iiH1P-0007HQ-RR for qemu-devel@nongnu.org; Fri, 20 Dec 2019 07:04:45 -0500 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]:40895) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iiH1P-0007AL-DM for qemu-devel@nongnu.org; Fri, 20 Dec 2019 07:04:43 -0500 Received: by mail-wm1-x341.google.com with SMTP id t14so8960418wmi.5 for ; Fri, 20 Dec 2019 04:04:43 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id x10sm9420713wrv.60.2019.12.20.04.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2019 04:04:39 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id E6BEA1FF8C; Fri, 20 Dec 2019 12:04:38 +0000 (GMT) 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=OtFgNihQmjwXIczGB6RrVvTGCHXdssZoK9FktuuxCfA=; b=LbCMyns60IYsl2fr34xczYIcdxEpgga2aL+YUPYbIJan0EKbOwPWT9hzJlUbcl+d9f AwZ6eJvNmbG63CJYFUYGIRVbOUTUwNF9SgzjEsg9QwFPzPAmzx/+D/B3TXdIZnDqkxBW Axd6pRuAnxWmJTl0gGBE9cMggpiQWSzBuamL+aWrkkJvDNRRU/Fxg39aDj/OER0nybnA 9GLDP99RipYiAhzg3WlDnfckxlzqrt+Fb39iGTm3z1haFYr7ecP4Krmj+TZEixzBFVQE k3sEWBmuYcDYK3U4f9Yq+1gRRZ/PBl/6mOPlYHcqh+e1Z7FbwCRQELvFVuPMv52N3YTe Fqzg== 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=OtFgNihQmjwXIczGB6RrVvTGCHXdssZoK9FktuuxCfA=; b=BvGxsma8af2VvHBVcUobJ0p9Ii8FAYPCbaJUwPWRKnoI8mLwlDUP4t0sFcvFGHH6et simYIknbDw08vaG/EAubfwJbrAhb3uCBTQ4s3SCqdudMDQTbLaPTMC12JTzfG0K/p7ce Ub+Ua4pWniEH7NRzJiCBmQeN2IyEixiL9nSV8A8freXg/4K4RnNTLNNgRvuq+PNqeuK1 O7nVPHIDmIB9kyBNUcu4czgDewEJYeqcOTa9dTWKEEOZ2YBMxI43z/XFM3GXq9B0YTro EqP+YbK7IFKiPDvk/ewnNDoYjIAiOhyP3kSWkf2rkFa2AnerEEvqKwYryr07mRZInNFn /jaQ== X-Gm-Message-State: APjAAAVPMgbl8gMb45JzLY2zhSuKdc2ya/wtd2npPVBTySPOxu5lAjnY ZH0FDCurqqM2cWbSDTlKrWPMvQ== X-Google-Smtp-Source: APXvYqzq2CnJjb8LIV16gGOrJLMRrXM/oY3m01E3DCVOVnsP/Z5yd/67DwGBYJv+kn8oJMKwXgTUtA== X-Received: by 2002:a1c:3dc3:: with SMTP id k186mr15459707wma.95.1576843481218; Fri, 20 Dec 2019 04:04:41 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v4 01/21] gdbstub: make GDBState static and have common init function Date: Fri, 20 Dec 2019 12:04:18 +0000 Message-Id: <20191220120438.16114-2-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191220120438.16114-1-alex.bennee@linaro.org> References: <20191220120438.16114-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::341 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: damien.hedde@greensocs.com, luis.machado@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Richard Henderson , alan.hayward@arm.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Instead of allocating make this entirely static. We shall reduce the size of the structure in later commits and dynamically allocate parts of it. We introduce an init and reset helper function to keep all the manipulation in one place. Signed-off-by: Alex Benn=C3=A9e Reviewed-by: Richard Henderson Reviewed-by: Damien Hedde --- v2 - made entirely static, dropped dh/rth r-b tags due to changes --- gdbstub.c | 168 ++++++++++++++++++++++++++---------------------------- 1 file changed, 81 insertions(+), 87 deletions(-) diff --git a/gdbstub.c b/gdbstub.c index 4cf8af365e2..36b1d7a9408 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -342,6 +342,7 @@ enum RSState { RS_CHKSUM2, }; typedef struct GDBState { + bool init; /* have we been initialised? */ CPUState *c_cpu; /* current CPU for step/continue ops */ CPUState *g_cpu; /* current CPU for other ops */ CPUState *query_cpu; /* for q{f|s}ThreadInfo */ @@ -372,7 +373,23 @@ typedef struct GDBState { */ static int sstep_flags =3D SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER; =20 -static GDBState *gdbserver_state; +static GDBState gdbserver_state; + +static void init_gdbserver_state(void) +{ + g_assert(!gdbserver_state.init); + memset(&gdbserver_state, 0, sizeof(GDBState)); + gdbserver_state.init =3D true; +} + +#ifndef CONFIG_USER_ONLY +static void reset_gdbserver_state(void) +{ + g_free(gdbserver_state.processes); + gdbserver_state.processes =3D NULL; + gdbserver_state.process_num =3D 0; +} +#endif =20 bool gdb_has_xml; =20 @@ -425,8 +442,8 @@ int use_gdb_syscalls(void) /* -semihosting-config target=3Dauto */ /* On the first call check if gdb is connected and remember. */ if (gdb_syscall_mode =3D=3D GDB_SYS_UNKNOWN) { - gdb_syscall_mode =3D (gdbserver_state ? GDB_SYS_ENABLED - : GDB_SYS_DISABLED); + gdb_syscall_mode =3D gdbserver_state.init ? + GDB_SYS_ENABLED : GDB_SYS_DISABLED; } return gdb_syscall_mode =3D=3D GDB_SYS_ENABLED; } @@ -984,7 +1001,7 @@ static int gdb_breakpoint_insert(int type, target_ulon= g addr, target_ulong len) int err =3D 0; =20 if (kvm_enabled()) { - return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, ty= pe); + return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, typ= e); } =20 switch (type) { @@ -1021,7 +1038,7 @@ static int gdb_breakpoint_remove(int type, target_ulo= ng addr, target_ulong len) int err =3D 0; =20 if (kvm_enabled()) { - return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, ty= pe); + return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, typ= e); } =20 switch (type) { @@ -1074,7 +1091,7 @@ static void gdb_breakpoint_remove_all(void) CPUState *cpu; =20 if (kvm_enabled()) { - kvm_remove_all_breakpoints(gdbserver_state->c_cpu); + kvm_remove_all_breakpoints(gdbserver_state.c_cpu); return; } =20 @@ -2601,7 +2618,7 @@ static int gdb_handle_packet(GDBState *s, const char = *line_buf) =20 void gdb_set_stop_cpu(CPUState *cpu) { - GDBProcess *p =3D gdb_get_cpu_process(gdbserver_state, cpu); + GDBProcess *p =3D gdb_get_cpu_process(&gdbserver_state, cpu); =20 if (!p->attached) { /* @@ -2611,14 +2628,14 @@ void gdb_set_stop_cpu(CPUState *cpu) return; } =20 - gdbserver_state->c_cpu =3D cpu; - gdbserver_state->g_cpu =3D cpu; + gdbserver_state.c_cpu =3D cpu; + gdbserver_state.g_cpu =3D cpu; } =20 #ifndef CONFIG_USER_ONLY static void gdb_vm_state_change(void *opaque, int running, RunState state) { - GDBState *s =3D gdbserver_state; + GDBState *s =3D &gdbserver_state; CPUState *cpu =3D s->c_cpu; char buf[256]; char thread_id[16]; @@ -2722,17 +2739,16 @@ void gdb_do_syscallv(gdb_syscall_complete_cb cb, co= nst char *fmt, va_list va) char *p_end; target_ulong addr; uint64_t i64; - GDBState *s; =20 - s =3D gdbserver_state; - if (!s) + if (!gdbserver_state.init) return; - s->current_syscall_cb =3D cb; + + gdbserver_state.current_syscall_cb =3D cb; #ifndef CONFIG_USER_ONLY vm_stop(RUN_STATE_DEBUG); #endif - p =3D s->syscall_buf; - p_end =3D &s->syscall_buf[sizeof(s->syscall_buf)]; + p =3D &gdbserver_state.syscall_buf[0]; + p_end =3D &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_= buf)]; *(p++) =3D 'F'; while (*fmt) { if (*fmt =3D=3D '%') { @@ -2765,14 +2781,14 @@ void gdb_do_syscallv(gdb_syscall_complete_cb cb, co= nst char *fmt, va_list va) } *p =3D 0; #ifdef CONFIG_USER_ONLY - put_packet(s, s->syscall_buf); + put_packet(&gdbserver_state, gdbserver_state.syscall_buf); /* Return control to gdb for it to process the syscall request. * Since the protocol requires that gdb hands control back to us * using a "here are the results" F packet, we don't need to check * gdb_handlesig's return value (which is the signal to deliver if * execution was resumed via a continue packet). */ - gdb_handlesig(s->c_cpu, 0); + gdb_handlesig(gdbserver_state.c_cpu, 0); #else /* In this case wait to send the syscall packet until notification that the CPU has stopped. This must be done because if the packet is se= nt @@ -2780,7 +2796,7 @@ void gdb_do_syscallv(gdb_syscall_complete_cb cb, cons= t char *fmt, va_list va) is still in the running state, which can cause packets to be dropped and state transition 'T' packets to be sent while the syscall is st= ill being processed. */ - qemu_cpu_kick(s->c_cpu); + qemu_cpu_kick(gdbserver_state.c_cpu); #endif } =20 @@ -2941,15 +2957,13 @@ static void gdb_read_byte(GDBState *s, uint8_t ch) /* Tell the remote gdb that the process has exited. */ void gdb_exit(CPUArchState *env, int code) { - GDBState *s; char buf[4]; =20 - s =3D gdbserver_state; - if (!s) { + if (!gdbserver_state.init) { return; } #ifdef CONFIG_USER_ONLY - if (gdbserver_fd < 0 || s->fd < 0) { + if (gdbserver_fd < 0 || gdbserver_state.fd < 0) { return; } #endif @@ -2957,10 +2971,10 @@ void gdb_exit(CPUArchState *env, int code) trace_gdbstub_op_exiting((uint8_t)code); =20 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); - put_packet(s, buf); + put_packet(&gdbserver_state, buf); =20 #ifndef CONFIG_USER_ONLY - qemu_chr_fe_deinit(&s->chr, true); + qemu_chr_fe_deinit(&gdbserver_state.chr, true); #endif } =20 @@ -2993,12 +3007,10 @@ static void create_default_process(GDBState *s) int gdb_handlesig(CPUState *cpu, int sig) { - GDBState *s; char buf[256]; int n; =20 - s =3D gdbserver_state; - if (gdbserver_fd < 0 || s->fd < 0) { + if (gdbserver_fd < 0 || gdbserver_state.fd < 0) { return sig; } =20 @@ -3008,58 +3020,55 @@ gdb_handlesig(CPUState *cpu, int sig) =20 if (sig !=3D 0) { snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig)); - put_packet(s, buf); + put_packet(&gdbserver_state, buf); } /* put_packet() might have detected that the peer terminated the connection. */ - if (s->fd < 0) { + if (gdbserver_state.fd < 0) { return sig; } =20 sig =3D 0; - s->state =3D RS_IDLE; - s->running_state =3D 0; - while (s->running_state =3D=3D 0) { - n =3D read(s->fd, buf, 256); + gdbserver_state.state =3D RS_IDLE; + gdbserver_state.running_state =3D 0; + while (gdbserver_state.running_state =3D=3D 0) { + n =3D read(gdbserver_state.fd, buf, 256); if (n > 0) { int i; =20 for (i =3D 0; i < n; i++) { - gdb_read_byte(s, buf[i]); + gdb_read_byte(&gdbserver_state, buf[i]); } } else { /* XXX: Connection closed. Should probably wait for another connection before continuing. */ if (n =3D=3D 0) { - close(s->fd); + close(gdbserver_state.fd); } - s->fd =3D -1; + gdbserver_state.fd =3D -1; return sig; } } - sig =3D s->signal; - s->signal =3D 0; + sig =3D gdbserver_state.signal; + gdbserver_state.signal =3D 0; return sig; } =20 /* Tell the remote gdb that the process has exited due to SIG. */ void gdb_signalled(CPUArchState *env, int sig) { - GDBState *s; char buf[4]; =20 - s =3D gdbserver_state; - if (gdbserver_fd < 0 || s->fd < 0) { + if (gdbserver_fd < 0 || gdbserver_state.fd < 0) { return; } =20 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig)); - put_packet(s, buf); + put_packet(&gdbserver_state, buf); } =20 static bool gdb_accept(void) { - GDBState *s; struct sockaddr_in sockaddr; socklen_t len; int fd; @@ -3083,15 +3092,13 @@ static bool gdb_accept(void) return false; } =20 - s =3D g_malloc0(sizeof(GDBState)); - create_default_process(s); - s->processes[0].attached =3D true; - s->c_cpu =3D gdb_first_attached_cpu(s); - s->g_cpu =3D s->c_cpu; - s->fd =3D fd; + init_gdbserver_state(); + create_default_process(&gdbserver_state); + gdbserver_state.processes[0].attached =3D true; + gdbserver_state.c_cpu =3D gdb_first_attached_cpu(&gdbserver_state); + gdbserver_state.g_cpu =3D gdbserver_state.c_cpu; + gdbserver_state.fd =3D fd; gdb_has_xml =3D false; - - gdbserver_state =3D s; return true; } =20 @@ -3144,13 +3151,11 @@ int gdbserver_start(int port) /* Disable gdb stub for child processes. */ void gdbserver_fork(CPUState *cpu) { - GDBState *s =3D gdbserver_state; - - if (gdbserver_fd < 0 || s->fd < 0) { + if (gdbserver_fd < 0 || gdbserver_state.fd < 0) { return; } - close(s->fd); - s->fd =3D -1; + close(gdbserver_state.fd); + gdbserver_state.fd =3D -1; cpu_breakpoint_remove_all(cpu, BP_GDB); cpu_watchpoint_remove_all(cpu, BP_GDB); } @@ -3167,7 +3172,7 @@ static void gdb_chr_receive(void *opaque, const uint8= _t *buf, int size) int i; =20 for (i =3D 0; i < size; i++) { - gdb_read_byte(gdbserver_state, buf[i]); + gdb_read_byte(&gdbserver_state, buf[i]); } } =20 @@ -3210,13 +3215,13 @@ static int gdb_monitor_write(Chardev *chr, const ui= nt8_t *buf, int len) const char *p =3D (const char *)buf; int max_sz; =20 - max_sz =3D (sizeof(gdbserver_state->last_packet) - 2) / 2; + max_sz =3D (sizeof(gdbserver_state.last_packet) - 2) / 2; for (;;) { if (len <=3D max_sz) { - gdb_monitor_output(gdbserver_state, p, len); + gdb_monitor_output(&gdbserver_state, p, len); break; } - gdb_monitor_output(gdbserver_state, p, max_sz); + gdb_monitor_output(&gdbserver_state, p, max_sz); p +=3D max_sz; len -=3D max_sz; } @@ -3308,18 +3313,10 @@ static void create_processes(GDBState *s) create_default_process(s); } =20 -static void cleanup_processes(GDBState *s) -{ - g_free(s->processes); - s->process_num =3D 0; - s->processes =3D NULL; -} - int gdbserver_start(const char *device) { trace_gdbstub_op_start(device); =20 - GDBState *s; char gdbstub_device_name[128]; Chardev *chr =3D NULL; Chardev *mon_chr; @@ -3357,10 +3354,8 @@ int gdbserver_start(const char *device) return -1; } =20 - s =3D gdbserver_state; - if (!s) { - s =3D g_malloc0(sizeof(GDBState)); - gdbserver_state =3D s; + if (!gdbserver_state.init) { + init_gdbserver_state(); =20 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); =20 @@ -3369,31 +3364,30 @@ int gdbserver_start(const char *device) NULL, NULL, &error_abort); monitor_init_hmp(mon_chr, false); } else { - qemu_chr_fe_deinit(&s->chr, true); - mon_chr =3D s->mon_chr; - cleanup_processes(s); - memset(s, 0, sizeof(GDBState)); - s->mon_chr =3D mon_chr; + qemu_chr_fe_deinit(&gdbserver_state.chr, true); + mon_chr =3D gdbserver_state.mon_chr; + reset_gdbserver_state(); } =20 - create_processes(s); + create_processes(&gdbserver_state); =20 if (chr) { - qemu_chr_fe_init(&s->chr, chr, &error_abort); - qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_rec= eive, - gdb_chr_event, NULL, s, NULL, true); + qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort); + qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive, + gdb_chr_receive, gdb_chr_event, + NULL, &gdbserver_state, NULL, true); } - s->state =3D chr ? RS_IDLE : RS_INACTIVE; - s->mon_chr =3D mon_chr; - s->current_syscall_cb =3D NULL; + gdbserver_state.state =3D chr ? RS_IDLE : RS_INACTIVE; + gdbserver_state.mon_chr =3D mon_chr; + gdbserver_state.current_syscall_cb =3D NULL; =20 return 0; } =20 void gdbserver_cleanup(void) { - if (gdbserver_state) { - put_packet(gdbserver_state, "W00"); + if (gdbserver_state.init) { + put_packet(&gdbserver_state, "W00"); } } =20 --=20 2.20.1