From nobody Mon Nov 10 12:50:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1556199787; cv=none; d=zoho.com; s=zohoarc; b=hwkonLd+mWoKgJxjO6MdQR1RiyEzMAWv5GanhO2R+WdjTKzYWzYSgSnXDSKavGWQ4YOcbTC3MfgNUaMmPuRjKblQNrOuAayLNI3s0O658fSRAdy9HqPG+Z8OK/OITsTyUFSyuozXtW3BhsP1IF4WjhkZH2BPqrCZcDmsAEdKvvo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556199787; 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:To:ARC-Authentication-Results; bh=IiZwt5zDLPIRu6kHlSDJ1N64973ynZWO1IliJrwiIQ0=; b=kSbNde6CWemhw3ZB8t+4ItyQE+SgvhPs2bt/R2jP1fyvfD4+WE4dJCLD+DrUnNj5vXsV0o7hzT4WM736JOjLPIyNvDX4HlFEyFB6VmaP2K0Zvuha0o4Rnc7KPRy5FVqir2gRZQ7m6Z67zdg+vfFp7oGAPPFXFyJcTsymwPigpsU= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.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 1556199787880253.36820324535915; Thu, 25 Apr 2019 06:43:07 -0700 (PDT) Received: from localhost ([127.0.0.1]:57412 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hJeeP-0003eD-NL for importer@patchew.org; Thu, 25 Apr 2019 09:42:57 -0400 Received: from eggs.gnu.org ([209.51.188.92]:60145) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hJeP3-0007ju-43 for qemu-devel@nongnu.org; Thu, 25 Apr 2019 09:27:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hJeP1-00007U-Vg for qemu-devel@nongnu.org; Thu, 25 Apr 2019 09:27:05 -0400 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]:33995) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hJeP1-00006V-PZ for qemu-devel@nongnu.org; Thu, 25 Apr 2019 09:27:03 -0400 Received: by mail-wm1-x344.google.com with SMTP id r186so1692588wmf.1 for ; Thu, 25 Apr 2019 06:27:03 -0700 (PDT) Received: from localhost.localdomain (bzq-79-178-247-46.red.bezeqint.net. [79.178.247.46]) by smtp.gmail.com with ESMTPSA id y4sm2316540wrt.34.2019.04.25.06.26.54 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 25 Apr 2019 06:26:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IiZwt5zDLPIRu6kHlSDJ1N64973ynZWO1IliJrwiIQ0=; b=CtCXRcwEwI4vdDD1uwDqTs8VwjuZXI+ytXyuSYaJBm6Ka/PFZGM4SEVnxMQBdoEMMa oHio1ejeuipzsjPDxjoFJzPJa/Y3GXMV3vMXTaSVyShi6IVe6Hrvy5Cy6po/4iU3wGy0 DgB25f/FzXzt2ua/c0U3Nf0cuQcDQvywYQ9JsWp69tc8cHTe4VEc5BIlMHtbz6uGWvUm sJiGXbusnHq5AOFZkSutsnmIouUNsEwj3F0ixNQfVqBBosJjlc0qqw+OhJKVmQffGS2E siA9dLDEyDMXhdFz7fzXRRnQidQ+j5d/dW1/zMxiuV1ojDrY2a9yPs2iGgwsM3DfQurh yB+A== 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=IiZwt5zDLPIRu6kHlSDJ1N64973ynZWO1IliJrwiIQ0=; b=W/aW5nC2dPK9hN2DYw7fyZUQsg1BvQhmQddH9Dk717jWzwyYpXKwTujxnS7JNDCnyG AJH/xDp955fyFvvhwXrbqCE5qDZpLSrp1Qaujcrd4sZ5lTV9n+JZfQUWtCRKPimLs1qV DgZ2mNJTZFDnLZHt0UcVzc5PXq1Itya52AH76XB4CvgqcW/NefgALeFiwWaphn/N5PRF vWli9Zfk80gbFHL2v5aVDz3OtoTiVBHxTQLNrgXoHrdhevXGr9VmL0DJp4ykXGvgEzC1 R055q4W9dxVioqoHNqWzvXeYc9rzfNDS8BxuBPFT5Mz9ro9h7WrFs15RX6bmlfAweo2W 4Sdg== X-Gm-Message-State: APjAAAU3rXmjq4HTXq/6/dmpxXnkAy4n387B/le7bghsvzRu0c4vWD+O mVJ2S4+T4GB2YpWKi6UuVNC/rS2X X-Google-Smtp-Source: APXvYqxIEsjh2f+ySZxAOYd2NLXNV0l/Pi+FjnNLPzYC63OCM8YURNSBP6bj3LdK2wo2GlDK4sFpnA== X-Received: by 2002:a1c:f311:: with SMTP id q17mr3616459wmq.144.1556198820974; Thu, 25 Apr 2019 06:27:00 -0700 (PDT) From: Jon Doron To: qemu-devel@nongnu.org Date: Thu, 25 Apr 2019 16:26:16 +0300 Message-Id: <20190425132636.31636-2-arilou@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190425132636.31636-1-arilou@gmail.com> References: <20190425132636.31636-1-arilou@gmail.com> MIME-Version: 1.0 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::344 Subject: [Qemu-devel] [PATCH v6 01/21] gdbstub: Add infrastructure to parse cmd packets X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jon Doron Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Jon Doron --- gdbstub.c | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/gdbstub.c b/gdbstub.c index d54abd17cc..3308279fa8 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -1268,6 +1268,206 @@ out: return res; } =20 +typedef union GdbCmdVariant { + const char *data; + uint8_t opcode; + unsigned long val_ul; + unsigned long long val_ull; + struct { + GDBThreadIdKind kind; + uint32_t pid; + uint32_t tid; + } thread_id; +} GdbCmdVariant; + +static const char *cmd_next_param(const char *param, const char delimiter) +{ + static const char all_delimiters[] =3D ",;:=3D"; + char curr_delimiters[2] =3D {0}; + const char *delimiters; + + if (delimiter =3D=3D '?') { + delimiters =3D all_delimiters; + } else if (delimiter =3D=3D '0') { + return strchr(param, '\0'); + } else if (delimiter =3D=3D '.' && *param) { + return param + 1; + } else { + curr_delimiters[0] =3D delimiter; + delimiters =3D curr_delimiters; + } + + param +=3D strcspn(param, delimiters); + if (*param) { + param++; + } + return param; +} + +static int cmd_parse_params(const char *data, const char *schema, + GdbCmdVariant *params, int *num_params) +{ + int curr_param; + const char *curr_schema, *curr_data; + + *num_params =3D 0; + + if (!schema) { + return 0; + } + + curr_schema =3D schema; + curr_param =3D 0; + curr_data =3D data; + while (curr_schema[0] && curr_schema[1] && *curr_data) { + switch (curr_schema[0]) { + case 'l': + if (qemu_strtoul(curr_data, &curr_data, 16, + ¶ms[curr_param].val_ul)) { + return -EINVAL; + } + curr_param++; + curr_data =3D cmd_next_param(curr_data, curr_schema[1]); + break; + case 'L': + if (qemu_strtou64(curr_data, &curr_data, 16, + (uint64_t *)¶ms[curr_param].val_ull)) { + return -EINVAL; + } + curr_param++; + curr_data =3D cmd_next_param(curr_data, curr_schema[1]); + break; + case 's': + params[curr_param].data =3D curr_data; + curr_param++; + curr_data =3D cmd_next_param(curr_data, curr_schema[1]); + break; + case 'o': + params[curr_param].opcode =3D *(uint8_t *)curr_data; + curr_param++; + curr_data =3D cmd_next_param(curr_data, curr_schema[1]); + break; + case 't': + params[curr_param].thread_id.kind =3D + read_thread_id(curr_data, &curr_data, + ¶ms[curr_param].thread_id.pid, + ¶ms[curr_param].thread_id.tid); + curr_param++; + curr_data =3D cmd_next_param(curr_data, curr_schema[1]); + break; + case '?': + curr_data =3D cmd_next_param(curr_data, curr_schema[1]); + break; + default: + return -EINVAL; + } + curr_schema +=3D 2; + } + + *num_params =3D curr_param; + return 0; +} + +typedef struct GdbCmdContext { + GDBState *s; + GdbCmdVariant *params; + int num_params; + uint8_t mem_buf[MAX_PACKET_LENGTH]; + char str_buf[MAX_PACKET_LENGTH + 1]; +} GdbCmdContext; + +typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx); + +/* + * cmd_startswith -> cmd is compared using startswith + * cmd_full_match -> cmd is compared using strcmp + * + * + * schema definitions: + * Each schema parameter entry consists of 2 chars, + * the first char represents the parameter type handling + * the second char represents the delimiter for the next parameter + * + * Currently supported schema types: + * 'l' -> unsigned long (stored in .val_ul) + * 'L' -> unsigned long long (stored in .val_ull) + * 's' -> string (stored in .data) + * 'o' -> single char (stored in .opcode) + * 't' -> thread id (stored in .thread_id) + * '?' -> skip according to delimiter + * + * Currently supported delimiters: + * '?' -> Stop at any delimiter (",;:=3D\0") + * '0' -> Stop at "\0" + * '.' -> Skip 1 char unless reached "\0" + * Any other value is treated as the delimiter value itself + */ +typedef struct GdbCmdParseEntry { + GdbCmdHandler handler; + const char *cmd; + union { + int flags; + struct { + int cmd_startswith:1; + int cmd_full_match:1; + }; + }; + const char *schema; +} GdbCmdParseEntry; + +static inline int startswith(const char *string, const char *pattern) +{ + return !strncmp(string, pattern, strlen(pattern)); +} + +__attribute__((unused)) static int process_string_cmd( + GDBState *s, void *user_ctx, const char *data, + const GdbCmdParseEntry *cmds, int num_cmds); + +static int process_string_cmd(GDBState *s, void *user_ctx, const char *dat= a, + const GdbCmdParseEntry *cmds, int num_cmds) +{ + int i, schema_len, max_num_params; + GdbCmdContext gdb_ctx; + + if (!cmds) { + return -1; + } + + for (i =3D 0; i < num_cmds; i++) { + if (!cmds[i].handler || !cmds[i].cmd || + (cmds[i].cmd_startswith && !startswith(data, cmds[i].cmd)) || + (cmds[i].cmd_full_match && strcmp(data, cmds[i].cmd))) { + continue; + } + + max_num_params =3D 0; + if (cmds[i].schema) { + schema_len =3D strlen(cmds[i].schema); + if (schema_len % 2) { + return -2; + } + + max_num_params =3D schema_len / 2; + } + + gdb_ctx.params =3D + (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_para= ms); + memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params= ); + + if (cmd_parse_params(&data[strlen(cmds[i].cmd)], cmds[i].schema, + gdb_ctx.params, &gdb_ctx.num_params)) { + return -1; + } + + gdb_ctx.s =3D s; + cmds[i].handler(&gdb_ctx, user_ctx); + return 0; + } + + return -1; +} + static int gdb_handle_packet(GDBState *s, const char *line_buf) { CPUState *cpu; --=20 2.20.1