From patchwork Fri Sep 15 13:35:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 22895 Received: (qmail 108591 invoked by alias); 15 Sep 2017 13:35:34 -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 108459 invoked by uid 89); 15 Sep 2017 13:35:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.2 spammy=tracing X-HELO: sesbmg23.ericsson.net Received: from sesbmg23.ericsson.net (HELO sesbmg23.ericsson.net) (193.180.251.37) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 15 Sep 2017 13:35:32 +0000 Received: from ESESSHC005.ericsson.se (Unknown_Domain [153.88.183.33]) by sesbmg23.ericsson.net (Symantec Mail Security) with SMTP id BE.B8.21299.E17DBB95; Fri, 15 Sep 2017 15:35:26 +0200 (CEST) Received: from EUR02-AM5-obe.outbound.protection.outlook.com (153.88.183.145) by oa.msg.ericsson.com (153.88.183.33) with Microsoft SMTP Server (TLS) id 14.3.352.0; Fri, 15 Sep 2017 15:35:26 +0200 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=simon.marchi@ericsson.com; Received: from elxacz23q12.ki.sw.ericsson.se (192.176.1.82) by DBXPR07MB320.eurprd07.prod.outlook.com (2a01:111:e400:941d::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.5; Fri, 15 Sep 2017 13:35:24 +0000 From: Simon Marchi To: CC: Simon Marchi Subject: [PATCH 2/2] gdbserver: Move detach code to its own function Date: Fri, 15 Sep 2017 15:35:14 +0200 Message-ID: <1505482514-10206-2-git-send-email-simon.marchi@ericsson.com> In-Reply-To: <1505482514-10206-1-git-send-email-simon.marchi@ericsson.com> References: <1505482514-10206-1-git-send-email-simon.marchi@ericsson.com> MIME-Version: 1.0 X-ClientProxiedBy: HE1PR05CA0192.eurprd05.prod.outlook.com (2603:10a6:3:f9::16) To DBXPR07MB320.eurprd07.prod.outlook.com (2a01:111:e400:941d::21) X-MS-Office365-Filtering-Correlation-Id: 4cd2f405-40e0-4f25-cad0-08d4fc3e9f09 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:DBXPR07MB320; X-Microsoft-Exchange-Diagnostics: 1; DBXPR07MB320; 3:A0cpNAU/jpFpqhdnbQ4pgThUC4MpbfTDk7Jc02fa0RMhDwNyl5jkG5J9BmjATOGJv9hvFQBd7Tpif3zQiCkRPLKnn76gDU1IaO1zftUuNbu4LFVfeoAqN3IMiOC8vRlqL9UpsyAdz9dlOr3Zqz9oB4/QUNIX+7yofMVrPwcCG62Hq3YdbPxDCmaoTyv1Ii25g9ex+qZ0a9uLDQYlWTCe+PpOASIRuudVhHrtx/sOFQL6x9MQHmsHZ1QPgGcAwxhn; 25:GQc5VsTaF2ojwtO2vguhWrsyHmdRrVdPkCUc5/UqcZQLbeFOyPYAOaBeq48vByybWK+AcvFC4Lv8U3h309Yuioa17MbnsLVrsLIBLYMJRlJVbw11IndOm7J1ZXhx5C4jiVBJKB10r3sZEkpiuhB0uuME+8QXtqqPPP6K2lLpmbNBj8X8QNSJsuINepmvs2ByRtQk9uZhEQ+rsgzwQzIlS7/slipANr7zmSjtDLGABFSVmx1bZ8D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ebYVbp4GZuUzm3u0JffGqjhHRcsYTG1LvZhBz9w7T0e4NkMEEEtaeaQFTfXH2Y4+QxI3k24C6SoyncXdM5wUNYzEu+PJAvBwMiQCIqBdwWgJs0BS1WXQw X-Exchange-Antispam-Report-Test: UriScan:; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(100000703101)(100105400095)(3002001)(6041248)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(20161123562025)(20161123558100)(20161123560025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:DBXPR07MB320; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:DBXPR07MB320; X-Forefront-PRVS: 0431F981D8 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(39860400002)(346002)(376002)(54534003)(199003)(189002)(575784001)(101416001)(3846002)(478600001)(2361001)(2351001)(106356001)(6116002)(6512007)(105586002)(305945005)(7736002)(36756003)(5890100001)(2906002)(5003940100001)(33646002)(66066001)(47776003)(2950100002)(6486002)(6916009)(6506006)(25786009)(50226002)(8936002)(68736007)(6666003)(81166006)(81156014)(5660300001)(8676002)(189998001)(86362001)(16586007)(107886003)(48376002)(97736004)(16526017)(50986999)(110136004)(50466002)(53936002)(4326008)(316002)(76176999); DIR:OUT; SFP:1101; SCL:1; SRVR:DBXPR07MB320; H:elxacz23q12.ki.sw.ericsson.se; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: ericsson.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DBXPR07MB320; 23:Px6I0EFN0jzLyHo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K1jT+82MttPY+RvuL7PnfnOWo=; 7:J+cGPu1rJBhUcJIhQ+qKKTJfptBhXVyjxVqmXolhpE6diWqfh91jDH86nrJXnGUcm0LiW/Hr9IJciTFF5Ul9Ih4GWxZ+dkWzrjc0/XpE7jJoE4TrSdGMxHil9DffETKLOhQMMV0qdK9cXDAEhx4p3hLSsofhosnOq8AOIeXVgdwkIaPoL9Gut1646mhx385ewV4pGGgAD9RXv2C+vy4JCVDr+d1HlvAZdIVzqbMxF0A= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Sep 2017 13:35:24.9973 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBXPR07MB320 X-OriginatorOrg: ericsson.com X-IsSubscribed: yes The code required to handle the 'D' packet is non trivial, so move it out to its own function. The moved out code is identical, except for the call to strtol and some breaks that became returns. Tested manually, and by running gdb.base/*detach*.exp with native-gdbserver and native-extended-gdbserver. gdb/gdbserver/ChangeLog: * server.c (handle_detach): New function. (process_serial_event): Move code out, call handle_detach. --- gdb/gdbserver/server.c | 179 +++++++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 86 deletions(-) diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c index 8e0bf5b..59d648d 100644 --- a/gdb/gdbserver/server.c +++ b/gdb/gdbserver/server.c @@ -1157,6 +1157,98 @@ handle_search_memory (char *own_buf, int packet_len) free (pattern); } +static void +handle_detach (char *own_buf) +{ + require_running_or_return (own_buf); + + int pid; + + if (multi_process) + { + /* skip 'D;' */ + pid = strtol (&own_buf[2], NULL, 16); + } + else + pid = ptid_get_pid (current_ptid); + + if ((tracing && disconnected_tracing) || any_persistent_commands ()) + { + struct process_info *process = find_process_pid (pid); + + if (process == NULL) + { + write_enn (own_buf); + return; + } + + if (tracing && disconnected_tracing) + fprintf (stderr, + "Disconnected tracing in effect, " + "leaving gdbserver attached to the process\n"); + + if (any_persistent_commands ()) + fprintf (stderr, + "Persistent commands are present, " + "leaving gdbserver attached to the process\n"); + + /* Make sure we're in non-stop/async mode, so we we can both + wait for an async socket accept, and handle async target + events simultaneously. There's also no point either in + having the target stop all threads, when we're going to + pass signals down without informing GDB. */ + if (!non_stop) + { + if (debug_threads) + debug_printf ("Forcing non-stop mode\n"); + + non_stop = 1; + start_non_stop (1); + } + + process->gdb_detached = 1; + + /* Detaching implicitly resumes all threads. */ + target_continue_no_signal (minus_one_ptid); + + write_ok (own_buf); + return; + } + + fprintf (stderr, "Detaching from process %d\n", pid); + stop_tracing (); + if (detach_inferior (pid) != 0) + write_enn (own_buf); + else + { + discard_queued_stop_replies (pid_to_ptid (pid)); + write_ok (own_buf); + + if (extended_protocol || target_running ()) + { + /* There is still at least one inferior remaining or + we are in extended mode, so don't terminate gdbserver, + and instead treat this like a normal program exit. */ + last_status.kind = TARGET_WAITKIND_EXITED; + last_status.value.integer = 0; + last_ptid = pid_to_ptid (pid); + + current_thread = NULL; + } + else + { + putpkt (own_buf); + remote_close (); + + /* If we are attached, then we can exit. Otherwise, we + need to hang around doing nothing, until the child is + gone. */ + join_inferior (pid); + exit (0); + } + } +} + /* Parse options to --debug-format= and "monitor set debug-format". ARG is the text after "--debug-format=" or "monitor set debug-format". IS_MONITOR is non-zero if we're invoked via "monitor set debug-format". @@ -4009,7 +4101,6 @@ process_serial_event (void) unsigned int len; int res; CORE_ADDR mem_addr; - int pid; unsigned char sig; int packet_len; int new_packet_len = -1; @@ -4037,91 +4128,7 @@ process_serial_event (void) handle_general_set (own_buf); break; case 'D': - require_running_or_break (own_buf); - - if (multi_process) - { - i++; /* skip ';' */ - pid = strtol (&own_buf[i], NULL, 16); - } - else - pid = ptid_get_pid (current_ptid); - - if ((tracing && disconnected_tracing) || any_persistent_commands ()) - { - struct process_info *process = find_process_pid (pid); - - if (process == NULL) - { - write_enn (own_buf); - break; - } - - if (tracing && disconnected_tracing) - fprintf (stderr, - "Disconnected tracing in effect, " - "leaving gdbserver attached to the process\n"); - - if (any_persistent_commands ()) - fprintf (stderr, - "Persistent commands are present, " - "leaving gdbserver attached to the process\n"); - - /* Make sure we're in non-stop/async mode, so we we can both - wait for an async socket accept, and handle async target - events simultaneously. There's also no point either in - having the target stop all threads, when we're going to - pass signals down without informing GDB. */ - if (!non_stop) - { - if (debug_threads) - debug_printf ("Forcing non-stop mode\n"); - - non_stop = 1; - start_non_stop (1); - } - - process->gdb_detached = 1; - - /* Detaching implicitly resumes all threads. */ - target_continue_no_signal (minus_one_ptid); - - write_ok (own_buf); - break; /* from switch/case */ - } - - fprintf (stderr, "Detaching from process %d\n", pid); - stop_tracing (); - if (detach_inferior (pid) != 0) - write_enn (own_buf); - else - { - discard_queued_stop_replies (pid_to_ptid (pid)); - write_ok (own_buf); - - if (extended_protocol || target_running ()) - { - /* There is still at least one inferior remaining or - we are in extended mode, so don't terminate gdbserver, - and instead treat this like a normal program exit. */ - last_status.kind = TARGET_WAITKIND_EXITED; - last_status.value.integer = 0; - last_ptid = pid_to_ptid (pid); - - current_thread = NULL; - } - else - { - putpkt (own_buf); - remote_close (); - - /* If we are attached, then we can exit. Otherwise, we - need to hang around doing nothing, until the child is - gone. */ - join_inferior (pid); - exit (0); - } - } + handle_detach (own_buf); break; case '!': extended_protocol = 1;