From nobody Sat Jun 29 02:23:15 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) 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=1556782201; cv=none; d=zoho.com; s=zohoarc; b=BmrFoeIpTyWfCT86KNjvets9WAGrLj9K6Pa+GRDd+Ju6E+mUofwKtQChCbUNDGHWdScbPqxjt2scnssaIkNght2EFiztQK4ZJ/A2e3Ynxdlv+FW8s9Uft21eRTlpCx35BON+rqkB6VhRzSIxRDl1gleR/CObFGZN+fl+BAtWN5k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556782201; 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=9gs1pjZcHlRiuvyF89Xf0dDUFOI/nZh6LcrIM/HDQRg=; b=f/BGLGadSa83QXfHuLiM+wYeY4HMGXI+GtWeOpy/1wWrPLgNcvXSgFBJAycGw5UBGf3vDlgT4xXPcG8imfElIniFaF/3+JcYtCGbwk5g6Ihd32czwoLPb1C6hacFoBpya0nWe2VqcCojYuNag8dBKgof+PPEocaLYGfQ+a/oPSc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=temperror (zoho.com: Error in retrieving data from DNS) 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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1556782201431387.8432315382746; Thu, 2 May 2019 00:30:01 -0700 (PDT) Received: from localhost ([127.0.0.1]:46506 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hM6A6-0004QF-1k for importer@patchew.org; Thu, 02 May 2019 03:29:46 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59365) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hM68c-0003i3-3E for qemu-devel@nongnu.org; Thu, 02 May 2019 03:28:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hM68N-0001CI-RI for qemu-devel@nongnu.org; Thu, 02 May 2019 03:28:04 -0400 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:46888) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hM68E-0008Td-UQ for qemu-devel@nongnu.org; Thu, 02 May 2019 03:27:54 -0400 Received: by mail-wr1-x444.google.com with SMTP id r7so1750480wrr.13 for ; Thu, 02 May 2019 00:26:55 -0700 (PDT) Received: from localhost.localdomain ([176.230.64.186]) by smtp.gmail.com with ESMTPSA id z5sm8769289wre.70.2019.05.02.00.26.52 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 02 May 2019 00:26:53 -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=9gs1pjZcHlRiuvyF89Xf0dDUFOI/nZh6LcrIM/HDQRg=; b=J9qoRGBDNZVmCkGZrD9pQG2s+Svkx1b7tGVlnWLt+o3IFDnrSAbwt3oKGnpytRtfeE NQp4vCPmd09Y5AvvpB515x8Bhm2N9WVTluxm7ZFs5KUkkJy8txPwzNoBewPsJqxzBYCX CIl0/xBsz4HUB9e5AMEKTIkUeJgBDBvo5RI26rxBUQ+kNv3k1ak/lNUWbIjmUvAWhWU4 zoIZ2DjfvW/Q5kYVw6/GZjFroRgIntr74EQ3V8wKfrupX4WVS30UtreO07s9z6svutWF 6yHdDfqvjy3Xu3kJ7CnYRenoJZX6a3mDh2/gTbqbhvXbuDUdy3Szk+X5XyZ7b1nrb1te xKhg== 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=9gs1pjZcHlRiuvyF89Xf0dDUFOI/nZh6LcrIM/HDQRg=; b=qD98PtZ5Awqr0MxMfy7Z3ZBF7Jg1n6nGHYGiXUoFTkeYDcO9tRqP0pCpVQYCrAZOCH 2hHPNszDFk30sbFgy+ZhPQOR3X7c2SYOOTbP1gNBCHFR4k12KDlQ9N/pRtOn6UYVOao4 WtrOKcFTRn29r/M22T4u1ZQkpciwN9s+HBSvVrD2/m3GK/9Pfncyxbubw7JABqKVBiqW k5gW09aCJR/bXLQeSgPWeY4nfmnKqf5KidOKYT9zqNsaYM6klbCilAxXLcB1iWhTfAC2 x+1J0+cj4ToPcrWGM4Zlw6rnQNT81VuU35f0kG2m5dUU/MBCs+0TrPYlnaKvlJbO2WhM N1lA== X-Gm-Message-State: APjAAAWo9N2UTwDHuJESm80f50nh6b/bpDHXxWDwbSta/X2q/oA/2bf3 idJs6sKzDMiQhvAmotZHHqq6SoiC X-Google-Smtp-Source: APXvYqzUZdV/OT/Qp671Wj4UKS5r5ml3LpqrDDoArEx7YEX+66VvONp+4TQWORMG8FvmusSOXeoFvQ== X-Received: by 2002:adf:f2c5:: with SMTP id d5mr1497438wrp.293.1556782013944; Thu, 02 May 2019 00:26:53 -0700 (PDT) From: Jon Doron To: qemu-devel@nongnu.org Date: Thu, 2 May 2019 10:26:15 +0300 Message-Id: <20190502072641.4667-2-arilou@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190502072641.4667-1-arilou@gmail.com> References: <20190502072641.4667-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::444 Subject: [Qemu-devel] [PATCH v8 01/27] 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: liran.alon@oracle.com, alex.bennee@linaro.org, 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..d5e0f3878a 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 + * + * + * 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; + }; + }; + const char *schema; +} GdbCmdParseEntry; + +static inline int startswith(const char *string, const char *pattern) +{ + return !strncmp(string, pattern, strlen(pattern)); +} + +static int process_string_cmd( + GDBState *s, void *user_ctx, const char *data, + const GdbCmdParseEntry *cmds, int num_cmds) + __attribute__((unused)); + +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 =3D 0; + GdbCmdContext gdb_ctx; + + if (!cmds) { + return -1; + } + + for (i =3D 0; i < num_cmds; i++) { + const GdbCmdParseEntry *cmd =3D &cmds[i]; + g_assert(cmd->handler && cmd->cmd); + + if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) || + (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) { + continue; + } + + if (cmd->schema) { + schema_len =3D strlen(cmd->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(cmd->cmd)], cmd->schema, + gdb_ctx.params, &gdb_ctx.num_params)) { + return -1; + } + + gdb_ctx.s =3D s; + cmd->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