From nobody Tue Nov 26 20:36:07 2024 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=reject dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1705398326; cv=none; d=zohomail.com; s=zohoarc; b=hjdWLKEFjT8FrP6GMVKc8PJY2wy9qFOuSOJV56QWH8BJDytq+19s/SMOGNKhrQoCKOCDqgcXUpG10YAsIJNOCwT3cVm2Ka1Ou7POZuXCiUO4goeQk23jOrbd/Z7wdM/zOs24jUA23OEC8lFlaphSrU9VIQIwf1qYc6OMtUjZ/Lo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705398326; 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=COdyMpDqv3CHKV8R5NMjKTpjsfxpFAU7cNblD36ORDU=; b=eHJBQNvZAUg3MyRNrSE2ppAx2eRKvZiCGmJkRyYNuWdBa+fqCT9uo/i9X5r2MBtKxYVWsjneQ2fbjUwNOUE54ae97ggAZ5KnN2MhfTiCna1P7C/Anqk4yFFEbUQgcLCdx1yyqRpMmVqte7y48MGgsx0aLlS8yZc14tMvIySunIA= 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=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1705398326443467.55692768863616; Tue, 16 Jan 2024 01:45:26 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rPfzj-0008Sd-58; Tue, 16 Jan 2024 04:44:31 -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 1rPfzi-0008SG-53 for qemu-devel@nongnu.org; Tue, 16 Jan 2024 04:44:30 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rPfze-00035a-62 for qemu-devel@nongnu.org; Tue, 16 Jan 2024 04:44:29 -0500 Received: from pps.filterd (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40G9G3ON005042; Tue, 16 Jan 2024 09:44:20 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3vnnn1b2nn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jan 2024 09:44:20 +0000 Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 40G7PMvx018611; Tue, 16 Jan 2024 09:44:19 GMT Received: from smtprelay01.fra02v.mail.ibm.com ([9.218.2.227]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3vm5undrt7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jan 2024 09:44:19 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay01.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 40G9iH3g3867236 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 16 Jan 2024 09:44:17 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7FE9120043; Tue, 16 Jan 2024 09:44:17 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 07EB120040; Tue, 16 Jan 2024 09:44:17 +0000 (GMT) Received: from heavy.boeblingen.de.ibm.com (unknown [9.179.4.64]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Tue, 16 Jan 2024 09:44:16 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : mime-version; s=pp1; bh=COdyMpDqv3CHKV8R5NMjKTpjsfxpFAU7cNblD36ORDU=; b=WWn+/z+5ALyjv5n4rq9NjHCybVQ1OZwHR3fWJL9/lWC04rTEjT0RLAoqMPegZhSkZgyv lUX2eBkc7EtePY5tNat12WEYZZu6dHmfUHQXzH/FVzRXH+oE7jumvr76WI5NGV8KH7wu lcqTTePlgxpYbK0ZLa2xRAqAvBr4LlDifEina5H0yeSzWYyPIs5TTfA2M5yRIsPqjMFJ D9NIF+7i+kRwgNFbvtO4Ba2gvSuclRtWS2aXiVf82rs4+fZsbkgfzX32GyCzBy1x4aKO wn09D4kdurzvPryEFgxGg4+pbo99YTYGEwQJslVZamJlaKKjxKZK0ZaAkr8r9wXkCxfW ow== From: Ilya Leoshkevich To: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Riku Voipio Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-devel@nongnu.org, Ilya Leoshkevich Subject: [PATCH v2 1/2] gdbstub: Implement catching syscalls Date: Tue, 16 Jan 2024 10:41:53 +0100 Message-ID: <20240116094411.216665-2-iii@linux.ibm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116094411.216665-1-iii@linux.ibm.com> References: <20240116094411.216665-1-iii@linux.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-GUID: 8LhwRgHf2rTT9PwwKSbdScRp1FLCC5M6 X-Proofpoint-ORIG-GUID: 8LhwRgHf2rTT9PwwKSbdScRp1FLCC5M6 Content-Transfer-Encoding: quoted-printable X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-01-16_04,2024-01-15_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 suspectscore=0 malwarescore=0 lowpriorityscore=0 bulkscore=0 clxscore=1015 mlxlogscore=999 spamscore=0 priorityscore=1501 adultscore=0 impostorscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401160077 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=148.163.158.5; envelope-from=iii@linux.ibm.com; helo=mx0b-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1705398326967100001 Content-Type: text/plain; charset="utf-8" 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-Pa= ckets.html#qSupported [2] https://sourceware.org/gdb/current/onlinedocs/gdb.html/General-Query-Pa= ckets.html#QCatchSyscalls [3] https://sourceware.org/gdb/current/onlinedocs/gdb.html/Stop-Reply-Packe= ts.html Signed-off-by: Ilya Leoshkevich --- gdbstub/gdbstub.c | 9 +++ gdbstub/internals.h | 2 + gdbstub/user-target.c | 5 ++ gdbstub/user.c | 104 ++++++++++++++++++++++++++++++++++- include/gdbstub/user.h | 29 +++++++++- include/user/syscall-trace.h | 7 ++- 6 files changed, 151 insertions(+), 5 deletions(-) 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, vo= id *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[] = =3D { .schema =3D "l0" }, #endif +#if defined(CONFIG_USER_ONLY) + { + .handler =3D gdb_handle_set_catch_syscalls, + .cmd =3D "CatchSyscalls:", + .cmd_startswith =3D 1, + .schema =3D "s0", + }, +#endif }; =20 static void handle_gen_query(GArray *params, void *user_ctx) diff --git a/gdbstub/internals.h b/gdbstub/internals.h index 5c0c725e54c..56b7c13b750 100644 --- a/gdbstub/internals.h +++ b/gdbstub/internals.h @@ -136,6 +136,7 @@ void gdb_append_thread_id(CPUState *cpu, GString *buf); int gdb_get_cpu_index(CPUState *cpu); unsigned int gdb_get_max_cpus(void); /* both */ bool gdb_can_reverse(void); /* softmmu, stub for user */ +int gdb_target_sigtrap(void); /* user */ =20 void gdb_create_default_process(GDBState *s); =20 @@ -194,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); /* u= ser */ +void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx); /* use= r */ =20 void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */ =20 diff --git a/gdbstub/user-target.c b/gdbstub/user-target.c index c4bba4c72c7..b7d4c37cd81 100644 --- a/gdbstub/user-target.c +++ b/gdbstub/user-target.c @@ -418,3 +418,8 @@ void gdb_handle_query_xfer_exec_file(GArray *params, vo= id *user_ctx) ts->bprm->filename + offset); gdb_put_strbuf(); } + +int gdb_target_sigtrap(void) +{ + return TARGET_SIGTRAP; +} diff --git a/gdbstub/user.c b/gdbstub/user.c index dbe1d9b8875..01dd7169258 100644 --- a/gdbstub/user.c +++ b/gdbstub/user.c @@ -10,6 +10,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/bitops.h" #include "qemu/cutils.h" #include "qemu/sockets.h" #include "exec/hwaddr.h" @@ -21,11 +22,25 @@ #include "trace.h" #include "internals.h" =20 +enum GDBCatchSyscallsState { + GDB_CATCH_SYSCALLS_NONE, + GDB_CATCH_SYSCALLS_ALL, + GDB_CATCH_SYSCALLS_SELECTED, +}; +#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. + */ + enum GDBCatchSyscallsState catch_syscalls_state; + GDBSyscallsMask catch_syscalls_mask; } GDBUserState; =20 static GDBUserState gdbserver_user_state; @@ -121,7 +136,7 @@ void gdb_qemu_exit(int code) exit(code); } =20 -int gdb_handlesig(CPUState *cpu, int sig) +int gdb_handlesig_reason(CPUState *cpu, int sig, const char *reason) { char buf[256]; int n; @@ -141,6 +156,9 @@ int gdb_handlesig(CPUState *cpu, int sig) "T%02xthread:", gdb_target_signal_to_gdb(sig)); gdb_append_thread_id(cpu, gdbserver_state.str_buf); g_string_append_c(gdbserver_state.str_buf, ';'); + if (reason) { + g_string_append(gdbserver_state.str_buf, reason); + } gdb_put_strbuf(); gdbserver_state.allow_stop_reply =3D false; } @@ -499,3 +517,87 @@ void gdb_syscall_handling(const char *syscall_packet) gdb_put_packet(syscall_packet); gdb_handlesig(gdbserver_state.c_cpu, 0); } + +static bool should_catch_syscall(int num) +{ + switch (gdbserver_user_state.catch_syscalls_state) { + case GDB_CATCH_SYSCALLS_NONE: + return false; + case GDB_CATCH_SYSCALLS_ALL: + return true; + case GDB_CATCH_SYSCALLS_SELECTED: + if (num < 0 || num >=3D GDB_NR_SYSCALLS) { + return false; + } else { + return test_bit(num, gdbserver_user_state.catch_syscalls_mask); + } + default: + g_assert_not_reached(); + } +} + +void gdb_syscall_entry(CPUState *cs, int num) +{ + char reason[32]; + + if (should_catch_syscall(num)) { + snprintf(reason, sizeof(reason), "syscall_entry:%x;", num); + gdb_handlesig_reason(cs, gdb_target_sigtrap(), reason); + } +} + +void gdb_syscall_return(CPUState *cs, int num) +{ + char reason[32]; + + if (should_catch_syscall(num)) { + snprintf(reason, sizeof(reason), "syscall_return:%x;", num); + gdb_handlesig_reason(cs, gdb_target_sigtrap(), reason); + } +} + +void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx) +{ + enum GDBCatchSyscallsState catch_syscalls_state; + const char *param =3D get_param(params, 0)->data; + GDBSyscallsMask catch_syscalls_mask; + bool catch_syscalls_none; + unsigned int num; + const char *p; + + catch_syscalls_none =3D strcmp(param, "0") =3D=3D 0; + if (catch_syscalls_none || strcmp(param, "1") =3D=3D 0) { + gdbserver_user_state.catch_syscalls_state =3D + catch_syscalls_none ? GDB_CATCH_SYSCALLS_NONE : + GDB_CATCH_SYSCALLS_ALL; + gdb_put_packet("OK"); + return; + } + + if (param[0] =3D=3D '1' && param[1] =3D=3D ';') { + catch_syscalls_state =3D GDB_CATCH_SYSCALLS_SELECTED; + memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask)); + for (p =3D ¶m[2];; p++) { + if (qemu_strtoui(p, &p, 16, &num) || (*p && *p !=3D ';')) { + goto err; + } + if (num >=3D GDB_NR_SYSCALLS) { + /* Fall back to reporting all syscalls. */ + catch_syscalls_state =3D GDB_CATCH_SYSCALLS_ALL; + } else { + set_bit(num, catch_syscalls_mask); + } + if (!*p) { + break; + } + } + gdbserver_user_state.catch_syscalls_state =3D catch_syscalls_state; + memcpy(gdbserver_user_state.catch_syscalls_mask, catch_syscalls_ma= sk, + sizeof(catch_syscalls_mask)); + gdb_put_packet("OK"); + return; + } + +err: + gdb_put_packet("E00"); +} diff --git a/include/gdbstub/user.h b/include/gdbstub/user.h index d392e510c59..68b6534130c 100644 --- a/include/gdbstub/user.h +++ b/include/gdbstub/user.h @@ -10,9 +10,10 @@ #define GDBSTUB_USER_H =20 /** - * gdb_handlesig() - yield control to gdb + * gdb_handlesig_reason() - yield control to gdb * @cpu: CPU * @sig: if non-zero, the signal number which caused us to stop + * @reason: stop reason for stop reply packet or NULL * * This function yields control to gdb, when a user-mode-only target * needs to stop execution. If @sig is non-zero, then we will send a @@ -24,7 +25,18 @@ * or 0 if no signal should be delivered, ie the signal that caused * us to stop should be ignored. */ -int gdb_handlesig(CPUState *, int); +int gdb_handlesig_reason(CPUState *, int, const char *); + +/** + * gdb_handlesig() - yield control to gdb + * @cpu CPU + * @sig: if non-zero, the signal number which caused us to stop + * @see gdb_handlesig_reason() + */ +static inline int gdb_handlesig(CPUState *cpu, int sig) +{ + return gdb_handlesig_reason(cpu, sig, NULL); +} =20 /** * gdb_signalled() - inform remote gdb of sig exit @@ -39,5 +51,18 @@ void gdb_signalled(CPUArchState *as, int sig); */ void gdbserver_fork(CPUState *cs); =20 +/** + * gdb_syscall_entry() - inform gdb of syscall entry and yield control to = it + * @cs: CPU + * @num: syscall number + */ +void gdb_syscall_entry(CPUState *cs, int num); + +/** + * gdb_syscall_entry() - inform gdb of syscall return and yield control to= it + * @cs: CPU + * @num: syscall number + */ +void gdb_syscall_return(CPUState *cs, int num); =20 #endif /* GDBSTUB_USER_H */ diff --git a/include/user/syscall-trace.h b/include/user/syscall-trace.h index 557f881a79b..b48b2b2d0ae 100644 --- a/include/user/syscall-trace.h +++ b/include/user/syscall-trace.h @@ -11,6 +11,7 @@ #define SYSCALL_TRACE_H =20 #include "exec/user/abitypes.h" +#include "gdbstub/user.h" #include "qemu/plugin.h" #include "trace/trace-root.h" =20 @@ -20,7 +21,7 @@ * could potentially unify the -strace code here as well. */ =20 -static inline void record_syscall_start(void *cpu, int num, +static inline void record_syscall_start(CPUState *cpu, int num, abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6, @@ -29,11 +30,13 @@ static inline void record_syscall_start(void *cpu, int = num, qemu_plugin_vcpu_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); + gdb_syscall_entry(cpu, num); } =20 -static inline void record_syscall_return(void *cpu, int num, abi_long ret) +static inline void record_syscall_return(CPUState *cpu, int num, abi_long = ret) { qemu_plugin_vcpu_syscall_ret(cpu, num, ret); + gdb_syscall_return(cpu, num); } =20 =20 --=20 2.43.0