From patchwork Tue Nov 26 18:01:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stan Cox X-Patchwork-Id: 36239 Received: (qmail 41626 invoked by alias); 26 Nov 2019 18:01:30 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 41565 invoked by uid 89); 26 Nov 2019 18:01:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.1 spammy=stan, 20214, protocol X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 26 Nov 2019 18:01:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574791286; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rWEpe/p2c6u0Ryx/KxBmz7YX9FaJJGXhoWY61CggMCk=; b=ADtf/N8b7iUFJmxmLkyS+uH1WyFxxLCQp4im3Ry/ofI4v7dC8x83vepJPFG27xa9YfnqwC aP6DT2lrHZchR2UAJizso4j6dkYaMWbZlRZYBWB/zI/yLu13S8i93kleBFmdE49iq+rR+g EGQyhM9Zv6CGtmhOVHyt4AgEmxXtIHo= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-260-E_3XLKxKPtipOdzRnBOhyQ-1; Tue, 26 Nov 2019 13:01:24 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A49C9104FC01 for ; Tue, 26 Nov 2019 18:01:23 +0000 (UTC) Received: from [10.13.129.218] (dhcp129-218.rdu.redhat.com [10.13.129.218]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6FB177A5D2 for ; Tue, 26 Nov 2019 18:01:23 +0000 (UTC) Subject: [PATCH] add file desc to gdbserver client_state To: gdb-patches@sourceware.org References: <20191122233003.211567-1-cbiesinger@google.com> From: Stan Cox Message-ID: <8448b37f-ebdf-d2d9-829a-87f7f6ea102c@redhat.com> Date: Tue, 26 Nov 2019 13:01:23 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1 MIME-Version: 1.0 In-Reply-To: <20191122233003.211567-1-cbiesinger@google.com> X-Mimecast-Spam-Score: 0 This patch adds gdb_fildes_t support to gdbserver for later use by the gdbserver multi-client patch. That patch enables multiple clients with each client associated with its corresponding file desc. This patch adds the corresponding file desc to the client_state and adds a file desc parameter to the gdbserver I/O ops. It also adds a minimal multi_client_states which currently just manages a single client_state but later will be expanded to be a collection of multiple client_states where each client_state is the state for an individual client. The gdbserver multi-client patch is mentioned in: https://sourceware.org/ml/gdb-patches/2019-08/msg00577.html gdbserver/ChangeLog: 2019-11-25 Stan Cox * server.h (struct multi_client_states): New. * server.c (g_client_states) (get_client_states, multi_client_sates::set_client_state): New. (captured_main): Set the client state for this remote file desc. * remote-utils.c: (get_remote_desc): New to access remote file desc. (handle_accept_event): Set the client state for this remote file desc. (write_prim, read_prim, readchar): Add gdb_fildes_t parameter. Change all callers. --- gdb/gdbserver/ChangeLog | 11 ++++++++ gdb/gdbserver/remote-utils.c | 55 +++++++++++++++++++++--------------- gdb/gdbserver/remote-utils.h | 2 ++ gdb/gdbserver/server.c | 21 ++++++++++++++ gdb/gdbserver/server.h | 11 +++++++- 5 files changed, 77 insertions(+), 23 deletions(-) diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c index d7da4b7aed..5c5a119863 100644 --- a/gdb/gdbserver/remote-utils.c +++ b/gdb/gdbserver/remote-utils.c @@ -93,7 +93,7 @@ enum { Either NOT_SCHEDULED or the callback id. */ static int readchar_callback = NOT_SCHEDULED; -static int readchar (void); +static int readchar (gdb_fildes_t); static void reset_readchar (void); static void reschedule (void); @@ -115,6 +115,13 @@ static gdb_fildes_t listen_desc = INVALID_DESCRIPTOR; # define write(fd, buf, len) send (fd, (char *) buf, len, 0) #endif + +int +get_remote_desc (void) +{ + return remote_desc; +} + int gdb_connected (void) { @@ -156,6 +163,9 @@ handle_accept_event (int err, gdb_client_data client_data) if (remote_desc == -1) perror_with_name ("Accept failed"); + struct multi_client_states & client_states = get_client_states(); + client_states.set_client_state (remote_desc); + /* Enable TCP keep alive process. */ socklen_t tmp = 1; setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE, @@ -607,12 +617,12 @@ read_ptid (const char *buf, const char **obuf) This may return less than COUNT. */ static int -write_prim (const void *buf, int count) +write_prim (gdb_fildes_t fd, const void *buf, int count) { if (remote_connection_is_stdio ()) return write (fileno (stdout), buf, count); else - return write (remote_desc, buf, count); + return write (fd, buf, count); } /* Read COUNT bytes from the client and store in BUF. @@ -620,12 +630,12 @@ write_prim (const void *buf, int count) This may return less than COUNT. */ static int -read_prim (void *buf, int count) +read_prim (gdb_fildes_t fd, void *buf, int count) { if (remote_connection_is_stdio ()) return read (fileno (stdin), buf, count); else - return read (remote_desc, buf, count); + return read (fd, buf, count); } /* Send a packet to the remote machine, with error checking. @@ -666,7 +676,7 @@ putpkt_binary_1 (char *buf, int cnt, int is_notif) do { - if (write_prim (buf2, p - buf2) != p - buf2) + if (write_prim (cs.file_desc, buf2, p - buf2) != p - buf2) { perror ("putpkt(write)"); free (buf2); @@ -679,9 +689,9 @@ putpkt_binary_1 (char *buf, int cnt, int is_notif) if (remote_debug) { if (is_notif) - debug_printf ("putpkt (\"%s\"); [notif]\n", buf2); + debug_printf ("putpkt (%d, \"%s\"); [notif]\n", (int)cs.file_desc, buf2); else - debug_printf ("putpkt (\"%s\"); [noack mode]\n", buf2); + debug_printf ("putpkt (%d, \"%s\"); [noack mode]\n", (int)cs.file_desc, buf2); debug_flush (); } break; @@ -689,11 +699,11 @@ putpkt_binary_1 (char *buf, int cnt, int is_notif) if (remote_debug) { - debug_printf ("putpkt (\"%s\"); [looking for ack]\n", buf2); + debug_printf ("putpkt (%d, \"%s\"); [looking for ack]\n", (int)cs.file_desc, buf2); debug_flush (); } - cc = readchar (); + cc = readchar (cs.file_desc); if (cc < 0) { @@ -761,7 +771,7 @@ input_interrupt (int unused) int cc; char c = 0; - cc = read_prim (&c, 1); + cc = read_prim (remote_desc, &c, 1); if (cc == 0) { @@ -891,13 +901,13 @@ static unsigned char *readchar_bufp; /* Returns next char from remote GDB. -1 if error. */ static int -readchar (void) +readchar (gdb_fildes_t fd) { int ch; if (readchar_bufcnt == 0) { - readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf)); + readchar_bufcnt = read_prim (fd, readchar_buf, sizeof (readchar_buf)); if (readchar_bufcnt <= 0) { @@ -972,6 +982,7 @@ getpkt (char *buf) char *bp; unsigned char csum, c1, c2; int c; + gdb_fildes_t fd = cs.file_desc; while (1) { @@ -979,7 +990,7 @@ getpkt (char *buf) while (1) { - c = readchar (); + c = readchar (fd); /* The '\003' may appear before or after each packet, so check for an input interrupt. */ @@ -1004,7 +1015,7 @@ getpkt (char *buf) bp = buf; while (1) { - c = readchar (); + c = readchar (fd); if (c < 0) return -1; if (c == '#') @@ -1014,8 +1025,8 @@ getpkt (char *buf) } *bp = 0; - c1 = fromhex (readchar ()); - c2 = fromhex (readchar ()); + c1 = fromhex (readchar (fd)); + c2 = fromhex (readchar (fd)); if (csum == (c1 << 4) + c2) break; @@ -1032,7 +1043,7 @@ getpkt (char *buf) fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", (c1 << 4) + c2, csum, buf); - if (write_prim ("-", 1) != 1) + if (write_prim (fd, "-", 1) != 1) return -1; } @@ -1040,11 +1051,11 @@ getpkt (char *buf) { if (remote_debug) { - debug_printf ("getpkt (\"%s\"); [sending ack] \n", buf); + debug_printf ("getpkt (%d, \"%s\"); [sending ack] \n", (int)fd, buf); debug_flush (); } - if (write_prim ("+", 1) != 1) + if (write_prim (fd, "+", 1) != 1) return -1; if (remote_debug) @@ -1057,7 +1068,7 @@ getpkt (char *buf) { if (remote_debug) { - debug_printf ("getpkt (\"%s\"); [no ack sent] \n", buf); + debug_printf ("getpkt (%d, \"%s\"); [no ack sent] \n", (int)fd, buf); debug_flush (); } } @@ -1074,7 +1085,7 @@ getpkt (char *buf) while (readchar_bufcnt > 0 && *readchar_bufp == '\003') { /* Consume the interrupt character in the buffer. */ - readchar (); + readchar (fd); (*the_target->request_interrupt) (); } diff --git a/gdb/gdbserver/remote-utils.h b/gdb/gdbserver/remote-utils.h index 4ca5d9435e..606592852c 100644 --- a/gdb/gdbserver/remote-utils.h +++ b/gdb/gdbserver/remote-utils.h @@ -19,6 +19,8 @@ #ifndef GDBSERVER_REMOTE_UTILS_H #define GDBSERVER_REMOTE_UTILS_H +int get_remote_desc (void); + int gdb_connected (void); #define STDIO_CONNECTION_NAME "stdio" diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c index c5f7176cff..a686038af7 100644 --- a/gdb/gdbserver/server.c +++ b/gdb/gdbserver/server.c @@ -152,6 +152,23 @@ static struct btrace_config current_btrace_conf; /* The client remote protocol state. */ +static struct multi_client_states g_client_states; + +multi_client_states& +get_client_states (void) +{ + return g_client_states; +} + +client_state& +multi_client_states::set_client_state (gdb_fildes_t fd) +{ + client_state &cs = get_client_state (); + + cs.file_desc = fd; + return cs; +} + static client_state g_client_state; client_state & @@ -3556,6 +3573,10 @@ captured_main (int argc, char *argv[]) #endif current_directory = getcwd (NULL, 0); + + int remote_desc = get_remote_desc(); + multi_client_states &client_states = get_client_states (); + client_states.set_client_state (remote_desc); client_state &cs = get_client_state (); if (current_directory == NULL) diff --git a/gdb/gdbserver/server.h b/gdb/gdbserver/server.h index e01c4f146e..52fae32e3e 100644 --- a/gdb/gdbserver/server.h +++ b/gdb/gdbserver/server.h @@ -142,6 +142,8 @@ struct client_state own_buf ((char *) xmalloc (PBUFSIZ + 1)) {} + gdb_fildes_t file_desc; + /* The thread set with an `Hc' packet. `Hc' is deprecated in favor of `vCont'. Note the multi-process extensions made `vCont' a requirement, so `Hc pPID.TID' is pretty much undefined. So @@ -200,7 +202,14 @@ struct client_state }; -client_state &get_client_state (); +struct multi_client_states +{ +public: + client_state & set_client_state (gdb_fildes_t); +}; + +client_state & get_client_state (); +struct multi_client_states & get_client_states (void); #include "gdbthread.h" #include "inferiors.h"