From patchwork Wed Apr 24 14:26:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Doron X-Patchwork-Id: 10914971 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BEE9713B5 for ; Wed, 24 Apr 2019 14:49:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD6121FF40 for ; Wed, 24 Apr 2019 14:49:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A169B28796; Wed, 24 Apr 2019 14:49:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 137851FF40 for ; Wed, 24 Apr 2019 14:49:46 +0000 (UTC) Received: from localhost ([127.0.0.1]:42855 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hJJDV-0000kE-Eb for patchwork-qemu-devel@patchwork.kernel.org; Wed, 24 Apr 2019 10:49:45 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43876) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hJIzN-0004gC-6G for qemu-devel@nongnu.org; Wed, 24 Apr 2019 10:35:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hJIrt-0000KA-Jj for qemu-devel@nongnu.org; Wed, 24 Apr 2019 10:27:26 -0400 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]:33636) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hJIrt-0000Iv-CT for qemu-devel@nongnu.org; Wed, 24 Apr 2019 10:27:25 -0400 Received: by mail-wr1-x442.google.com with SMTP id s18so630476wrp.0 for ; Wed, 24 Apr 2019 07:27:25 -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:mime-version :content-transfer-encoding; bh=5TvE4pfl6f5eEGUEFOQhBOkuedA1G1QP7QDZaqPSExU=; b=q3XvPy3vd+RQrkOfkFDqYu5qiJU/ahYqHJYMJUuqR/LjAWtqV++fmRK4NH0wBL/Axw NuWdbgkkqL7k1inz++Kdfz8hQ/pKzwPosAHx32dzz8GAV8ttkROJ8X/Ik2idyTd3hpVD ytzpy8LRx4uArqrArTNNWJnmjY5oxesQAJf0Gnw3vEoQs1HUIam6fNW+V+rYUWUjhVxn xDikA8Q1ngb/Twzws1+5olcAu6kX7BQwkXMpWY1mKpqNMNUBHynztnZlyQTnS01MPG7r a3YYsCoUSrWNvMXXu3Q5C529PBRC3D1m+H8uC+MzJsRAu9sSadQiwILOBTxwIM3oNEY6 VO4Q== 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:mime-version :content-transfer-encoding; bh=5TvE4pfl6f5eEGUEFOQhBOkuedA1G1QP7QDZaqPSExU=; b=RPBQ5TyLWJBVoRJGFkZrdoQMA/2nvuYurbi79bdKczxTNZdFeHxR/u4WlIURbSWyRe Vx1glXETRFNrNNn9JvVQwKsvaQZizVYQWCvtgSwx8GtMdcVZOVYbrGK0tuBYddE3NnEv KGe+dEUcbRY7PcjjG8oO4TzkzEtVV9IVjFlavIjQPQKIOK6Czkvqw/Q6v/m33GEqqH6j hbdxEBaXg99agf1A5F4RHtx7yGbtO9fkHX6lQ2WC3bh1GuGH0Hd3cnqKrGT5pv7j8EGc yFImrMvmLy4NG/ivY9oSCr194JtX57xiWwIxJjNVtvwYB/VGY3jdggvyO1ZwHXBYhWw1 ZnRg== X-Gm-Message-State: APjAAAUR9HAKD3/Ux7RvDTE9/k+YSDjEeEo02Huvp/q1xG6rXkkmoaSD A71pXXI2y/BJVYZojN1mU/hIotRP X-Google-Smtp-Source: APXvYqxd24h1Uz5CQID/i0Eh7HAmfP1kwEePvK23yYiltgzDs3f4Ql45DKMXXmbOqVTRRO0pHoCDuw== X-Received: by 2002:adf:b641:: with SMTP id i1mr21654807wre.288.1556116043114; Wed, 24 Apr 2019 07:27:23 -0700 (PDT) Received: from linux.local ([31.154.166.148]) by smtp.gmail.com with ESMTPSA id q7sm5518041wmc.11.2019.04.24.07.27.20 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 24 Apr 2019 07:27:20 -0700 (PDT) From: arilou@gmail.com To: qemu-devel@nongnu.org Date: Wed, 24 Apr 2019 17:26:55 +0300 Message-Id: <20190424142714.28460-1-arilou@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::442 Subject: [Qemu-devel] [PATCH v3 01/20] 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Jon Doron Signed-off-by: Jon Doron --- gdbstub.c | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) diff --git a/gdbstub.c b/gdbstub.c index d54abd17cc..b5bd01b913 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -1268,6 +1268,221 @@ out: return res; } +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) +{ + const char *delim; + static char all_delimiters[] = ",;:="; + static char no_delimiter[] = "\0"; + char curr_delimiters[2] = {0}; + const char *delimiters; + + if (delimiter == '?') { + delimiters = all_delimiters; + } else if (delimiter == '0') { + delimiters = no_delimiter; + } else if (delimiter == '.' && *param) { + return param + 1; + } else { + curr_delimiters[0] = delimiter; + delimiters = curr_delimiters; + } + + while (*param) { + delim = delimiters; + while (*delim) { + if (*param == *delim) { + return param + 1; + } + delim++; + } + 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 = 0; + + if (!schema) { + return 0; + } + + curr_schema = schema; + curr_param = 0; + curr_data = 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 = 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 = cmd_next_param(curr_data, curr_schema[1]); + break; + case 's': + params[curr_param].data = curr_data; + curr_param++; + curr_data = cmd_next_param(curr_data, curr_schema[1]); + break; + case 'o': + params[curr_param].opcode = *(uint8_t *)curr_data; + curr_param++; + curr_data = cmd_next_param(curr_data, curr_schema[1]); + break; + case 't': + params[curr_param].thread_id.kind = + read_thread_id(curr_data, &curr_data, + ¶ms[curr_param].thread_id.pid, + ¶ms[curr_param].thread_id.tid); + curr_param++; + curr_data = cmd_next_param(curr_data, curr_schema[1]); + break; + case 'x': + params[curr_param].data = curr_data; + curr_param++; + curr_data = cmd_next_param(curr_data, curr_schema[1]); + break; + case '?': + curr_data = cmd_next_param(curr_data, curr_schema[1]); + break; + default: + return -EINVAL; + } + curr_schema += 2; + } + + *num_params = 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) + * 'x' -> xml (stored in .data), must have a ':' delimiter + * '?' -> skip according to delimiter + * + * Currently supported delimiters: + * '?' -> Stop at any delimiter (",;:=\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, + GdbCmdParseEntry *cmds, int num_cmds); + +static int process_string_cmd(GDBState *s, void *user_ctx, const char *data, + GdbCmdParseEntry *cmds, int num_cmds) +{ + int i, schema_len, max_num_params; + GdbCmdContext gdb_ctx; + + if (!cmds) { + return -1; + } + + for (i = 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 = 0; + if (cmds[i].schema) { + schema_len = strlen(cmds[i].schema); + if (schema_len % 2) { + return -2; + } + + max_num_params = schema_len / 2; + } + + gdb_ctx.params = + (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params); + 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 = 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;