From patchwork Fri Feb 9 19:47:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 771338 Delivered-To: patch@linaro.org Received: by 2002:adf:9dca:0:b0:33b:4db1:f5b3 with SMTP id q10csp1036685wre; Fri, 9 Feb 2024 11:49:12 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWuphjRdSvxC/Ej2rXOg8Q6yGH8yI1MqxyGa36D0FLDvx4DrSQv3d6txW0xy/VQQhwyKA8RNzJEyBJXbjgU67Wn X-Google-Smtp-Source: AGHT+IGXCiUzX3R4+ertNbtnzg0tDPMdzdTXzdg7tVbdCdmhTdhBRDsK3QUpqoBUZqmVAZiAmY0g X-Received: by 2002:a9d:7f14:0:b0:6dc:155b:49dc with SMTP id j20-20020a9d7f14000000b006dc155b49dcmr82990otq.12.1707508151543; Fri, 09 Feb 2024 11:49:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1707508151; cv=none; d=google.com; s=arc-20160816; b=GoFpZju0Ejx+Rh921MmZqsJk0p+aA7O8jSov35P8hef3EfVbQf72QwsLz1WJAaazQj JAmq/M3Ch91tkddRr1F4nkYbY1Pboswe32vfBr6a0TOwDWD2vXpcce12h8l+zMIMgKJh VfD9GMQF5Z6b0kXEwkAvG05A60BOpfu5bKp0cTpLuEDGBZHBFtbK7A3iACv7MKb/MwHQ BVIaiZychxFskQvHCho0NVx4tcAz+uaBQWInbCY8iKp2TpKSJks/ZjwVD59LvXGR/M4n OB+7atWPSR50QlaECvR6c0DxlNKd98DKMbBE44v8wajG2GPFfWBrk1Gw2V1me/ZZRg55 Vd9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Fspe01bX4KX8hKRjqhxvWfR3ZGNlHrXdv6yZvN6uP9U=; fh=R7dva3gNNX72u+nMZBgNy+AzwYRW0jmJRdOFJQZVGdI=; b=OCL5KH0F1C1ThlMQB/0UutLxFiNsG/tnz5cIEdK2kKg9hJAtryrP6pNc/N+s2pZaMW bHXZS0En+ns7qtPO5G0RnqyEwxjcGVOpWMhCjCrO1hM84xj2LvH7V8Vjc03+pwf8Ep8Z V0Bmn1m3j53ymUpffnCTKVW8RCxTsyCB4bgowWnoQJi0KURiY1k3JyjOHgq1jFU2gGbl 8V5vMHaJTB5x88Kun1GnFEqEukmt5dbumUXfZe+x3NABqHxGW4wImED+hWDQL7VpD1CR RFSbnp6jJQElL93PyesWomC06Dq2q26+QQ8/9zZy4/tz5vujwGOa9WoB4QS0eTkFC+tf b7UA==; darn=linaro.org ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sHkbUqbA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org X-Forwarded-Encrypted: i=1; AJvYcCVYfLhAX9zqzqR2L6OVVrZ2euRNvSAjNGx5ekTKhP+YUGlcS6RxE+0yx+M1m2ZSew/qn9LJe/w6iqMsIZoz1W62 Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id m13-20020a05622a054d00b0042c2eac1cf6si2559622qtx.720.2024.02.09.11.49.11 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 09 Feb 2024 11:49:11 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sHkbUqbA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rYWrH-0006Er-NQ; Fri, 09 Feb 2024 14:48:25 -0500 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 1rYWqf-0005WY-1P for qemu-devel@nongnu.org; Fri, 09 Feb 2024 14:47:45 -0500 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rYWqc-0005Bt-Pu for qemu-devel@nongnu.org; Fri, 09 Feb 2024 14:47:44 -0500 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-3392b045e0aso703883f8f.2 for ; Fri, 09 Feb 2024 11:47:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1707508061; x=1708112861; 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=Fspe01bX4KX8hKRjqhxvWfR3ZGNlHrXdv6yZvN6uP9U=; b=sHkbUqbAFOKAQk+xxgir4LXkbp/BHkRBfdnTYY0gmMOnpL4AVu6gNtdeDH1SAYSUhj gEL++t2GbMos8YUR6p58sHL6HWA9MVVtPlctKkuSNwBx7pmi+QbxXq2Z6U9xDwmjBeSv 9rNyuekcAWIIlUxnoSYxpMI4BaLzDlPAcpvDApAINKicziQNIBTWx9nmUJpuo/Rwu0Uu BC3H6zT7vuBxHzc43c2X1tMG08F9UtzuICv8tMuWtuNb40LbkvuNRalkwQuV9+jT7dC/ wL6g7vYgBxBTV95J1SPdyb1Ja5dPNKgPig1grG+TaANcnIdZak37U9N40zCeFQUwmJ84 ZYdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707508061; x=1708112861; 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=Fspe01bX4KX8hKRjqhxvWfR3ZGNlHrXdv6yZvN6uP9U=; b=U/iI0MzMglxgU2nta3mzl7O1pY6vAM16PoqcUSfljJk8deHUaEm7SksovbeRvSV+fg 1OVQ7isjSIRA/NpzuZbJuOKq/8SQF5COrp0iWE5357eppB/XFO7AN9aho1W7boZUdMR3 2kK29HnpM5vEP1DtqtemLAoiMbLlBf5wbHiguf24XsHpobqEdiwg8qTUgw/blVK4n0OK ZHr2f0K4g377EAAR5+4eJ4Enhoz376Bwz5Gt7K8X2NNSUv1q2qX4DEQEyJdCN7ccG4JK 7yThMyQwJyLYlLTrtgaE/kT/p50JEnVWrMhFrcoG/hrKCNKNzSzH+WBy/NQDGZRqrmrU tssA== X-Gm-Message-State: AOJu0YwyvXOVQODojLfJaND3v635xZiLtUYzCK711wwL71Sy0SdEjHas UsRHl48x1c363ZuutJaGzccHMX+ZmWgR36P3B1yA26k6OuEUmv+Zz8DOQbFeNfg= X-Received: by 2002:a5d:664d:0:b0:33b:649a:19f6 with SMTP id f13-20020a5d664d000000b0033b649a19f6mr8397wrw.35.1707508061471; Fri, 09 Feb 2024 11:47:41 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWmHZqaGAZO4uWzETZYAhM/YftpreNJ+82Ub/10ojXK9H/kKBD6m4VtlEfaI4CJ+9k6XiSO+D8ggz0smfe2USgGQvPLS1qBzZUSY4SmAkrPf1Hsx0RaMLL3Uw== Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id m10-20020adff38a000000b0033b0d2ba3a1sm50386wro.63.2024.02.09.11.47.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 11:47:37 -0800 (PST) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 8E0255F8E1; Fri, 9 Feb 2024 19:47:35 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Ilya Leoshkevich , =?utf-8?q?Alex_Benn=C3=A9e?= , =?utf-8?q?Philippe_M?= =?utf-8?q?athieu-Daud=C3=A9?= Subject: [PULL 09/10] gdbstub: Implement catching syscalls Date: Fri, 9 Feb 2024 19:47:33 +0000 Message-Id: <20240209194734.3424785-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240209194734.3424785-1-alex.bennee@linaro.org> References: <20240209194734.3424785-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::434; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x434.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, T_SCC_BODY_TEXT_LINE=-0.01 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Ilya Leoshkevich GDB supports stopping on syscall entry and exit using the "catch syscall" command. It relies on 3 packets, which are currently not supported by QEMU: * qSupported:QCatchSyscalls+ [1] * QCatchSyscalls: [2] * T05syscall_entry: and T05syscall_return: [3] Implement generation and handling of these packets. [1] https://sourceware.org/gdb/current/onlinedocs/gdb.html/General-Query-Packets.html#qSupported [2] https://sourceware.org/gdb/current/onlinedocs/gdb.html/General-Query-Packets.html#QCatchSyscalls [3] https://sourceware.org/gdb/current/onlinedocs/gdb.html/Stop-Reply-Packets.html Signed-off-by: Ilya Leoshkevich Message-Id: <20240202152506.279476-5-iii@linux.ibm.com> [AJB: GString -> g_strdup_printf] Signed-off-by: Alex BennĂ©e Message-Id: <20240207163812.3231697-14-alex.bennee@linaro.org> diff --git a/gdbstub/internals.h b/gdbstub/internals.h index aeb0d9b5377..56b7c13b750 100644 --- a/gdbstub/internals.h +++ b/gdbstub/internals.h @@ -195,6 +195,7 @@ void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */ void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */ void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */ void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */ +void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx); /* user */ void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */ diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c index 46d752bbc2c..7e73e916bdc 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -1617,6 +1617,7 @@ static void handle_query_supported(GArray *params, void *user_ctx) if (gdbserver_state.c_cpu->opaque) { g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+"); } + g_string_append(gdbserver_state.str_buf, ";QCatchSyscalls+"); #endif g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+"); #endif @@ -1810,6 +1811,14 @@ static const GdbCmdParseEntry gdb_gen_set_table[] = { .schema = "l0" }, #endif +#if defined(CONFIG_USER_ONLY) + { + .handler = gdb_handle_set_catch_syscalls, + .cmd = "CatchSyscalls:", + .cmd_startswith = 1, + .schema = "s0", + }, +#endif }; static void handle_gen_query(GArray *params, void *user_ctx) diff --git a/gdbstub/user.c b/gdbstub/user.c index 2ba01c17faf..14918d1a217 100644 --- a/gdbstub/user.c +++ b/gdbstub/user.c @@ -10,6 +10,7 @@ */ #include "qemu/osdep.h" +#include "qemu/bitops.h" #include "qemu/cutils.h" #include "qemu/sockets.h" #include "exec/hwaddr.h" @@ -21,11 +22,20 @@ #include "trace.h" #include "internals.h" +#define GDB_NR_SYSCALLS 1024 +typedef unsigned long GDBSyscallsMask[BITS_TO_LONGS(GDB_NR_SYSCALLS)]; + /* User-mode specific state */ typedef struct { int fd; char *socket_path; int running_state; + /* + * Store syscalls mask without memory allocation in order to avoid + * implementing synchronization. + */ + bool catch_all_syscalls; + GDBSyscallsMask catch_syscalls_mask; } GDBUserState; static GDBUserState gdbserver_user_state; @@ -503,10 +513,91 @@ void gdb_syscall_handling(const char *syscall_packet) gdb_handlesig(gdbserver_state.c_cpu, 0); } +static bool should_catch_syscall(int num) +{ + if (gdbserver_user_state.catch_all_syscalls) { + return true; + } + if (num < 0 || num >= GDB_NR_SYSCALLS) { + return false; + } + return test_bit(num, gdbserver_user_state.catch_syscalls_mask); +} + void gdb_syscall_entry(CPUState *cs, int num) { + if (should_catch_syscall(num)) { + g_autofree char *reason = g_strdup_printf("syscall_entry:%x;", num); + gdb_handlesig_reason(cs, gdb_target_sigtrap(), reason); + } } void gdb_syscall_return(CPUState *cs, int num) { + if (should_catch_syscall(num)) { + g_autofree char *reason = g_strdup_printf("syscall_return:%x;", num); + gdb_handlesig_reason(cs, gdb_target_sigtrap(), reason); + } +} + +void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx) +{ + const char *param = get_param(params, 0)->data; + GDBSyscallsMask catch_syscalls_mask; + bool catch_all_syscalls; + unsigned int num; + const char *p; + + /* "0" means not catching any syscalls. */ + if (strcmp(param, "0") == 0) { + gdbserver_user_state.catch_all_syscalls = false; + memset(gdbserver_user_state.catch_syscalls_mask, 0, + sizeof(gdbserver_user_state.catch_syscalls_mask)); + gdb_put_packet("OK"); + return; + } + + /* "1" means catching all syscalls. */ + if (strcmp(param, "1") == 0) { + gdbserver_user_state.catch_all_syscalls = true; + gdb_put_packet("OK"); + return; + } + + /* + * "1;..." means catching only the specified syscalls. + * The syscall list must not be empty. + */ + if (param[0] == '1' && param[1] == ';') { + catch_all_syscalls = false; + memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask)); + for (p = ¶m[2];; p++) { + if (qemu_strtoui(p, &p, 16, &num) || (*p && *p != ';')) { + goto err; + } + if (num >= GDB_NR_SYSCALLS) { + /* + * Fall back to reporting all syscalls. Reporting extra + * syscalls is inefficient, but the spec explicitly allows it. + * Keep parsing in case there is a syntax error ahead. + */ + catch_all_syscalls = true; + } else { + set_bit(num, catch_syscalls_mask); + } + if (!*p) { + break; + } + } + gdbserver_user_state.catch_all_syscalls = catch_all_syscalls; + if (!catch_all_syscalls) { + memcpy(gdbserver_user_state.catch_syscalls_mask, + catch_syscalls_mask, sizeof(catch_syscalls_mask)); + } + gdb_put_packet("OK"); + return; + } + +err: + gdb_put_packet("E00"); }