From patchwork Sun Dec 31 14:59:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 25163 Received: (qmail 62065 invoked by alias); 31 Dec 2017 15:00:06 -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 61914 invoked by uid 89); 31 Dec 2017 15:00:05 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy=22511 X-HELO: sessmg23.ericsson.net Received: from sessmg23.ericsson.net (HELO sessmg23.ericsson.net) (193.180.251.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 31 Dec 2017 15:00:03 +0000 Received: from ESESSHC019.ericsson.se (Unknown_Domain [153.88.183.75]) by sessmg23.ericsson.net (Symantec Mail Security) with SMTP id EA.B2.31026.17BF84A5; Sun, 31 Dec 2017 16:00:01 +0100 (CET) Received: from EUR01-DB5-obe.outbound.protection.outlook.com (153.88.183.145) by oa.msg.ericsson.com (153.88.183.75) with Microsoft SMTP Server (TLS) id 14.3.352.0; Sun, 31 Dec 2017 16:00:00 +0100 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=simon.marchi@ericsson.com; Received: from elxacz23q12.lan (192.222.251.162) by DBXPR07MB318.eurprd07.prod.outlook.com (2a01:111:e400:941d::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.386.4; Sun, 31 Dec 2017 14:59:58 +0000 From: Simon Marchi To: CC: Simon Marchi Subject: [PATCH 2/2] Make linux_ptrace_attach_fail_reason return an std::string Date: Sun, 31 Dec 2017 09:59:45 -0500 Message-ID: <1514732385-5506-2-git-send-email-simon.marchi@ericsson.com> In-Reply-To: <1514732385-5506-1-git-send-email-simon.marchi@ericsson.com> References: <1514732385-5506-1-git-send-email-simon.marchi@ericsson.com> MIME-Version: 1.0 X-ClientProxiedBy: BN6PR11CA0072.namprd11.prod.outlook.com (2603:10b6:404:f7::34) To DBXPR07MB318.eurprd07.prod.outlook.com (2a01:111:e400:941d::12) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b182550a-5f80-4412-fe2f-08d5505f2988 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603307)(7153060); SRVR:DBXPR07MB318; X-Microsoft-Exchange-Diagnostics: 1; DBXPR07MB318; 3:f9HScKspmCjLwAvSqzGmP9jOQ+VeBaCqxZz8uEzcBeLCwv3AFOdoMQwLPICawmE8oo5N/FnLtDlJockPxC/FF1PyHY6UiDgoC1s8OOOHW3eTdwnd1GJFUeF7GzE2TiCt3/Qgi9+pLrRWJ1r93bwvOq6GH8XMv/wIZrWxd87/9Ina8rAE4WcZb618/EeBFDDIJE6ebhAVD5J67+7AN2zFy7sz4Ci/ekr/iQADgDF46/3G6HjnI0Yl7CYXUU5rEOIG; 25:ewfp4mWZ+EWmPahSW+8i0EAA0gGrtEVt0soeqQiz+q9hwcymyY9ZtmCWeGqo5iYclaU5shDChYdQBkL9U8xMazphuslAt9VSU6zG9e543Aqv9euVtVNvTBGqtfgl3rjw+ym5hecEShNdGs7Qyvx0EzfJ0NJZYt4WuBXJgFNJINGopkMIIQNbK6N3QOYrA67nEPXfKePk5Vt8d9IdToYKpejOgSBuBE2Us4yK7yYE61Ru1MeXRt16lExLg0SbetT/fpGQRA7ua152tLWAhgS6HZeSbcDYhJXVcbD1aD8w5TAtzxgFxT8wriFpwr1AjByTQWx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GXCtlUOMyg6RuDUE5neuYQvXNkJU0zMoj+J616/DruA== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040470)(2401047)(8121501046)(5005006)(3002001)(10201501046)(93006095)(93001095)(3231023)(944501075)(6041268)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(20161123562045)(20161123564045)(6072148)(201708071742011); SRVR:DBXPR07MB318; BCL:0; PCL:0; RULEID:(100000803101)(100110400095); SRVR:DBXPR07MB318; X-Forefront-PRVS: 0538A71254 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(366004)(39860400002)(396003)(346002)(376002)(39380400002)(54534003)(199004)(189003)(386003)(36756003)(6116002)(106356001)(50226002)(69596002)(68736007)(6916009)(6506007)(97736004)(47776003)(59450400001)(52116002)(6512007)(51416003)(8936002)(66066001)(3846002)(76176011)(2906002)(81156014)(305945005)(7736002)(81166006)(48376002)(5660300001)(8676002)(21086003)(316002)(53936002)(6486002)(478600001)(2950100002)(16586007)(25786009)(6666003)(86362001)(16526018)(107886003)(50466002)(2361001)(105586002)(5890100001)(4326008)(2351001); DIR:OUT; SFP:1101; SCL:1; SRVR:DBXPR07MB318; H:elxacz23q12.lan; 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; DBXPR07MB318; 23:m9LSy2/Bwr597MvAIhvbPR3aitIGFTXv2xWvPopeLj?= =?us-ascii?Q?6KPLMQNYnAfnlmhi/XST8xKFqFfi3gw4qnsvz6Km+UOyYcs6KvSJRWRHpLI/?= =?us-ascii?Q?TInpTX0ZUCrZjt5A62d8oBQQw4ZwHBDS8DSwQotqp8Ai1msTR+k37ZYzYwF7?= =?us-ascii?Q?B1T5vvV66FzneHNWQe7+o4w6vIue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X51Yjyo7+OYq5pAvxrTSyy+DAHc4agw5Wtz7fUuNbFrKYFOEmxEacWUb9ytkoATnGHAggaRRP0go3r7P1vQZ1xh9mXAix7L SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Dec 2017 14:59:58.6283 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b182550a-5f80-4412-fe2f-08d5505f2988 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBXPR07MB318 X-OriginatorOrg: ericsson.com X-IsSubscribed: yes This patch makes linux_ptrace_attach_fail_reason and linux_ptrace_attach_fail_reason_string return std::string. It also replaces usages of struct buffer with std::string. This allows getting rid of a cleanup in in linux_ptrace_attach_fail_reason_string and simplifies the code in general. Something that looks odd to me is that in linux_ptrace_attach_fail_reason, if the two messages are appended, there is no separate space or \n, so the result won't be very nice. I left it as-is for now though. gdb/ChangeLog: * nat/linux-ptrace.h (linux_ptrace_attach_fail_reason): Return std::string. (linux_ptrace_attach_fail_reason_string): Likewise. * nat/linux-ptrace.c (linux_ptrace_attach_fail_reason): Likewise. (linux_ptrace_attach_fail_reason_string): Likewise. * linux-nat.c (attach_proc_task_lwp_callback): Adjust. gdb/gdbserver/ChangeLog: * linux-low.c (attach_proc_task_lwp_callback): Adjust to linux_ptrace_attach_fail_reason_string now returning an std::string. (linux_attach): Likewise. * thread-db.c (attach_thread): Likewise. --- gdb/gdbserver/linux-low.c | 14 +++++++----- gdb/gdbserver/thread-db.c | 6 ++++-- gdb/linux-nat.c | 21 ++++++------------ gdb/nat/linux-ptrace.c | 55 ++++++++++++++++++++--------------------------- gdb/nat/linux-ptrace.h | 8 +++---- 5 files changed, 46 insertions(+), 58 deletions(-) diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c index f6a52d5..711d9e6 100644 --- a/gdb/gdbserver/linux-low.c +++ b/gdb/gdbserver/linux-low.c @@ -1159,9 +1159,10 @@ attach_proc_task_lwp_callback (ptid_t ptid) } else if (err != 0) { - warning (_("Cannot attach to lwp %d: %s"), - lwpid, - linux_ptrace_attach_fail_reason_string (ptid, err)); + std::string reason + = linux_ptrace_attach_fail_reason_string (ptid, err); + + warning (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ()); } return 1; @@ -1186,8 +1187,11 @@ linux_attach (unsigned long pid) soon. */ err = linux_attach_lwp (ptid); if (err != 0) - error ("Cannot attach to process %ld: %s", - pid, linux_ptrace_attach_fail_reason_string (ptid, err)); + { + std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err); + + error ("Cannot attach to process %ld: %s", pid, reason.c_str ()); + } proc = linux_add_process (pid, 1); diff --git a/gdb/gdbserver/thread-db.c b/gdb/gdbserver/thread-db.c index 537758c..1c377ae 100644 --- a/gdb/gdbserver/thread-db.c +++ b/gdb/gdbserver/thread-db.c @@ -225,9 +225,11 @@ attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p) err = linux_attach_lwp (ptid); if (err != 0) { + std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err); + warning ("Could not attach to thread %ld (LWP %d): %s\n", - (unsigned long) ti_p->ti_tid, ti_p->ti_lid, - linux_ptrace_attach_fail_reason_string (ptid, err)); + (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ()); + return 0; } diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 1d1d6d3..ef5a58e 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -1167,10 +1167,11 @@ attach_proc_task_lwp_callback (ptid_t ptid) } else { + std::string reason + = linux_ptrace_attach_fail_reason_string (ptid, err); + warning (_("Cannot attach to lwp %d: %s"), - lwpid, - linux_ptrace_attach_fail_reason_string (ptid, - err)); + lwpid, reason.c_str ()); } } else @@ -1223,18 +1224,10 @@ linux_nat_attach (struct target_ops *ops, const char *args, int from_tty) CATCH (ex, RETURN_MASK_ERROR) { pid_t pid = parse_pid_to_attach (args); - struct buffer buffer; - char *buffer_s; - - buffer_init (&buffer); - linux_ptrace_attach_fail_reason (pid, &buffer); - - buffer_grow_str0 (&buffer, ""); - buffer_s = buffer_finish (&buffer); - make_cleanup (xfree, buffer_s); + std::string reason = linux_ptrace_attach_fail_reason (pid); - if (*buffer_s != '\0') - throw_error (ex.error, "warning: %s\n%s", buffer_s, ex.message); + if (!reason.empty ()) + throw_error (ex.error, "warning: %s\n%s", reason.c_str (), ex.message); else throw_error (ex.error, "%s", ex.message); } diff --git a/gdb/nat/linux-ptrace.c b/gdb/nat/linux-ptrace.c index 438177f..2533b6c 100644 --- a/gdb/nat/linux-ptrace.c +++ b/gdb/nat/linux-ptrace.c @@ -32,51 +32,42 @@ of 0 means there are no supported features. */ static int supported_ptrace_options = -1; -/* Find all possible reasons we could fail to attach PID and append - these as strings to the already initialized BUFFER. '\0' - termination of BUFFER must be done by the caller. */ +/* Find all possible reasons we could fail to attach PID and return these + as a string. An empty string is returned if we didn't find any reason. */ -void -linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer) +std::string +linux_ptrace_attach_fail_reason (pid_t pid) { - pid_t tracerpid; + pid_t tracerpid = linux_proc_get_tracerpid_nowarn (pid); + std::string result; - tracerpid = linux_proc_get_tracerpid_nowarn (pid); if (tracerpid > 0) - buffer_xml_printf (buffer, _("process %d is already traced " - "by process %d"), - (int) pid, (int) tracerpid); + string_appendf (result, + _("process %d is already traced by process %d"), + (int) pid, (int) tracerpid); if (linux_proc_pid_is_zombie_nowarn (pid)) - buffer_xml_printf (buffer, _("process %d is a zombie " - "- the process has already terminated"), - (int) pid); + string_appendf (result, + _("process %d is a zombie - the process has already " + "terminated"), + (int) pid); + + return result; } /* See linux-ptrace.h. */ -char * +std::string linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err) { - static char *reason_string; - struct buffer buffer; - char *warnings; - long lwpid = ptid_get_lwp (ptid); - - xfree (reason_string); - - buffer_init (&buffer); - linux_ptrace_attach_fail_reason (lwpid, &buffer); - buffer_grow_str0 (&buffer, ""); - warnings = buffer_finish (&buffer); - if (warnings[0] != '\0') - reason_string = xstrprintf ("%s (%d), %s", - safe_strerror (err), err, warnings); + long lwpid = ptid.lwp (); + std::string reason = linux_ptrace_attach_fail_reason (lwpid); + + if (!reason.empty ()) + return string_printf ("%s (%d), %s", safe_strerror (err), err, + reason.c_str ()); else - reason_string = xstrprintf ("%s (%d)", - safe_strerror (err), err); - xfree (warnings); - return reason_string; + return string_printf ("%s (%d)", safe_strerror (err), err); } #if defined __i386__ || defined __x86_64__ diff --git a/gdb/nat/linux-ptrace.h b/gdb/nat/linux-ptrace.h index 8a8c4c6..eab4154 100644 --- a/gdb/nat/linux-ptrace.h +++ b/gdb/nat/linux-ptrace.h @@ -180,14 +180,12 @@ struct buffer; # define TRAP_HWBKPT 4 #endif -extern void linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer); +extern std::string linux_ptrace_attach_fail_reason (pid_t pid); /* Find all possible reasons we could have failed to attach to PTID and return them as a string. ERR is the error PTRACE_ATTACH failed - with (an errno). The result is stored in a static buffer. This - string should be copied into a buffer by the client if the string - will not be immediately used, or if it must persist. */ -extern char *linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err); + with (an errno). */ +extern std::string linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err); extern void linux_ptrace_init_warnings (void); extern void linux_check_ptrace_features (void);