From nobody Mon Feb 9 22:38:44 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1544098228543321.1708495761511; Thu, 6 Dec 2018 04:10:28 -0800 (PST) Received: from localhost ([::1]:40363 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUsU7-0007mA-6r for importer@patchew.org; Thu, 06 Dec 2018 07:10:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41535) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUsK2-0008C9-1I for qemu-devel@nongnu.org; Thu, 06 Dec 2018 07:00:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gUsJx-0004nz-DV for qemu-devel@nongnu.org; Thu, 06 Dec 2018 07:00:01 -0500 Received: from mail.ispras.ru ([83.149.199.45]:59978) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUsJw-0004nm-Vv for qemu-devel@nongnu.org; Thu, 06 Dec 2018 06:59:57 -0500 Received: from Misha-PC.lan02.inno (unknown [85.142.117.226]) by mail.ispras.ru (Postfix) with ESMTPSA id 43092540082; Thu, 6 Dec 2018 14:59:56 +0300 (MSK) From: Mikhail Abakumov To: qemu-devel@nongnu.org Date: Thu, 06 Dec 2018 14:59:51 +0300 Message-ID: <154409759149.5432.7433579017265187980.stgit@Misha-PC.lan02.inno> In-Reply-To: <154409751316.5432.3325938832238028060.stgit@Misha-PC.lan02.inno> References: <154409751316.5432.3325938832238028060.stgit@Misha-PC.lan02.inno> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 83.149.199.45 Subject: [Qemu-devel] [PATCH v3 13/39] windbg: parsing data stream 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: sw@weilnetz.de, lprosek@redhat.com, dovgaluk@ispras.ru, rkagan@virtuozzo.com, pbonzini@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Add parsing data stream to packets from windbg client. Signed-off-by: Mikhail Abakumov Signed-off-by: Pavel Dovgalyuk --- include/exec/windbgstub-utils.h | 11 +++ windbgstub.c | 139 +++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 150 insertions(+) diff --git a/include/exec/windbgstub-utils.h b/include/exec/windbgstub-util= s.h index 2760684cfb..a28068eecd 100644 --- a/include/exec/windbgstub-utils.h +++ b/include/exec/windbgstub-utils.h @@ -53,6 +53,17 @@ typedef struct InitedAddr { bool is_init; } InitedAddr; =20 +typedef struct PacketData { + union { + struct { + DBGKD_MANIPULATE_STATE64 m64; + uint8_t extra[0]; + }; + uint8_t buf[PACKET_MAX_SIZE]; + }; + uint16_t extra_size; +} PacketData; + const char *kd_api_name(int id); const char *kd_pkt_type_name(int id); =20 diff --git a/windbgstub.c b/windbgstub.c index d7fadda096..2869d94389 100644 --- a/windbgstub.c +++ b/windbgstub.c @@ -19,12 +19,43 @@ #include "exec/windbgstub.h" #include "exec/windbgstub-utils.h" =20 +typedef enum ParsingState { + STATE_LEADER, + STATE_PACKET_TYPE, + STATE_PACKET_BYTE_COUNT, + STATE_PACKET_ID, + STATE_PACKET_CHECKSUM, + STATE_PACKET_DATA, + STATE_TRAILING_BYTE, +} ParsingState; + +typedef enum ParsingResult { + RESULT_NONE, + RESULT_BREAKIN_BYTE, + RESULT_UNKNOWN_PACKET, + RESULT_CONTROL_PACKET, + RESULT_DATA_PACKET, + RESULT_ERROR, +} ParsingResult; + +typedef struct ParsingContext { + /* index in the current buffer, + which depends on the current state */ + int index; + ParsingState state; + ParsingResult result; + KD_PACKET packet; + PacketData data; + const char *name; +} ParsingContext; + typedef struct WindbgState { bool is_loaded; bool catched_breakin_byte; uint32_t wait_packet_type; uint32_t curr_packet_id; =20 + ParsingContext ctx; CharBackend chr; } WindbgState; =20 @@ -36,6 +67,108 @@ static void windbg_state_clean(WindbgState *state) state->catched_breakin_byte =3D false; state->wait_packet_type =3D 0; state->curr_packet_id =3D INITIAL_PACKET_ID | SYNC_PACKET_ID; + state->ctx.state =3D STATE_LEADER; + state->ctx.result =3D RESULT_NONE; +} + +static void windbg_ctx_handler(WindbgState *state) +{ +} + +static void windbg_read_byte(ParsingContext *ctx, uint8_t byte) +{ + switch (ctx->state) { + case STATE_LEADER: + ctx->result =3D RESULT_NONE; + if (byte =3D=3D PACKET_LEADER_BYTE || byte =3D=3D CONTROL_PACKET_L= EADER_BYTE) { + if (ctx->index > 0 && byte !=3D PTR(ctx->packet.PacketLeader)[= 0]) { + ctx->index =3D 0; + } + PTR(ctx->packet.PacketLeader)[ctx->index] =3D byte; + ++ctx->index; + if (ctx->index =3D=3D sizeof(ctx->packet.PacketLeader)) { + ctx->state =3D STATE_PACKET_TYPE; + ctx->index =3D 0; + } + } else if (byte =3D=3D BREAKIN_PACKET_BYTE) { + ctx->result =3D RESULT_BREAKIN_BYTE; + ctx->index =3D 0; + } else { + ctx->index =3D 0; + } + break; + + case STATE_PACKET_TYPE: + PTR(ctx->packet.PacketType)[ctx->index] =3D byte; + ++ctx->index; + if (ctx->index =3D=3D sizeof(ctx->packet.PacketType)) { + ctx->packet.PacketType =3D lduw_p(&ctx->packet.PacketType); + if (ctx->packet.PacketType >=3D PACKET_TYPE_MAX) { + ctx->state =3D STATE_LEADER; + ctx->result =3D RESULT_UNKNOWN_PACKET; + } else { + ctx->state =3D STATE_PACKET_BYTE_COUNT; + } + ctx->index =3D 0; + } + break; + + case STATE_PACKET_BYTE_COUNT: + PTR(ctx->packet.ByteCount)[ctx->index] =3D byte; + ++ctx->index; + if (ctx->index =3D=3D sizeof(ctx->packet.ByteCount)) { + ctx->packet.ByteCount =3D lduw_p(&ctx->packet.ByteCount); + ctx->state =3D STATE_PACKET_ID; + ctx->index =3D 0; + } + break; + + case STATE_PACKET_ID: + PTR(ctx->packet.PacketId)[ctx->index] =3D byte; + ++ctx->index; + if (ctx->index =3D=3D sizeof(ctx->packet.PacketId)) { + ctx->packet.PacketId =3D ldl_p(&ctx->packet.PacketId); + ctx->state =3D STATE_PACKET_CHECKSUM; + ctx->index =3D 0; + } + break; + + case STATE_PACKET_CHECKSUM: + PTR(ctx->packet.Checksum)[ctx->index] =3D byte; + ++ctx->index; + if (ctx->index =3D=3D sizeof(ctx->packet.Checksum)) { + ctx->packet.Checksum =3D ldl_p(&ctx->packet.Checksum); + if (ctx->packet.PacketLeader =3D=3D CONTROL_PACKET_LEADER) { + ctx->state =3D STATE_LEADER; + ctx->result =3D RESULT_CONTROL_PACKET; + } else if (ctx->packet.ByteCount > PACKET_MAX_SIZE) { + ctx->state =3D STATE_LEADER; + ctx->result =3D RESULT_ERROR; + } else { + ctx->state =3D STATE_PACKET_DATA; + } + ctx->index =3D 0; + } + break; + + case STATE_PACKET_DATA: + ctx->data.buf[ctx->index] =3D byte; + ++ctx->index; + if (ctx->index =3D=3D ctx->packet.ByteCount) { + ctx->state =3D STATE_TRAILING_BYTE; + ctx->index =3D 0; + } + break; + + case STATE_TRAILING_BYTE: + if (byte =3D=3D PACKET_TRAILING_BYTE) { + ctx->result =3D RESULT_DATA_PACKET; + } else { + ctx->result =3D RESULT_ERROR; + } + ctx->state =3D STATE_LEADER; + break; + } } =20 static int windbg_chr_can_receive(void *opaque) @@ -45,6 +178,11 @@ static int windbg_chr_can_receive(void *opaque) =20 static void windbg_chr_receive(void *opaque, const uint8_t *buf, int size) { + int i; + for (i =3D 0; i < size; i++) { + windbg_read_byte(&windbg_state->ctx, buf[i]); + windbg_ctx_handler(windbg_state); + } } =20 static void windbg_exit(void) @@ -87,6 +225,7 @@ int windbg_server_start(const char *device) } =20 windbg_state =3D g_new0(WindbgState, 1); + windbg_state->ctx.name =3D "Windbg"; windbg_state_clean(windbg_state); =20 chr =3D qemu_chr_new_noreplay("windbg", device, true);