From patchwork Tue Mar 18 07:37:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hui Zhu X-Patchwork-Id: 138 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx22.g.dreamhost.com (caibbdcaabij.dreamhost.com [208.113.200.189]) by wilcox.dreamhost.com (Postfix) with ESMTP id 23E2836010C for ; Tue, 18 Mar 2014 00:38:08 -0700 (PDT) Received: by homiemail-mx22.g.dreamhost.com (Postfix, from userid 14314964) id B904D4F86E08; Tue, 18 Mar 2014 00:38:07 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx22.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx22.g.dreamhost.com (Postfix) with ESMTPS id 9188A4F86E1C for ; Tue, 18 Mar 2014 00:38:07 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:cc :subject:references:in-reply-to:content-type :content-transfer-encoding; q=dns; s=default; b=LEh0xAEn8vSumMPV MS0CTqSbvRUzU3ymp5/0s6Ahh5MGslvB8EAvr0JTMDG3KACgY1wBxaNgeiTJFdpu oNDpPF5mHjptup1gDRCVUMWZLz+rlRpacysQoHQmri8VnlncSzn3UL9QpShuSUko Mix0kdfu0Gs+H3BCDIdICROdgvA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:cc :subject:references:in-reply-to:content-type :content-transfer-encoding; s=default; bh=RqVRT1WrLJeB3dxHBSKE1i hus3s=; b=RWYTaqlh5SY+7s2pTYcUcEyyVxJALQwVUVyTfEzregpVFv54bcwxer 3QYLwl+k70VevQKCkf8SADWpFOpRjDUKD7SdnTVASqfvS+Afc3/GoXNHmNEfcfYc JBxywPiDhUA2xB3++9wdgTbW5ytlv60uMAQoQceX3RIkR12BqGiiU= Received: (qmail 30063 invoked by alias); 18 Mar 2014 07:38:05 -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 30051 invoked by uid 89); 18 Mar 2014 07:38:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00 autolearn=ham version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 18 Mar 2014 07:38:02 +0000 Received: from svr-orw-fem-01.mgc.mentorg.com ([147.34.98.93]) by relay1.mentorg.com with esmtp id 1WPob0-0005kX-Fe from Hui_Zhu@mentor.com ; Tue, 18 Mar 2014 00:37:58 -0700 Received: from SVR-ORW-FEM-02.mgc.mentorg.com ([147.34.96.206]) by svr-orw-fem-01.mgc.mentorg.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Tue, 18 Mar 2014 00:37:58 -0700 Received: from localhost.localdomain (147.34.91.1) by svr-orw-fem-02.mgc.mentorg.com (147.34.96.168) with Microsoft SMTP Server id 14.2.247.3; Tue, 18 Mar 2014 00:37:57 -0700 Message-ID: <5327F7D0.1050304@mentor.com> Date: Tue, 18 Mar 2014 15:37:52 +0800 From: Hui Zhu User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 MIME-Version: 1.0 To: Pedro Alves CC: gdb-patches ml Subject: Re: [PATCH 0/1] Fix internal warning when "gdb -p xxx" References: <53271C09.5000709@mentor.com> <53271ED2.6010707@redhat.com> <53272958.1080605@redhat.com> In-Reply-To: <53272958.1080605@redhat.com> X-IsSubscribed: yes X-DH-Original-To: gdb@patchwork.siddhesh.in On 03/18/14 00:56, Pedro Alves wrote: > On 03/17/2014 04:12 PM, Pedro Alves wrote: >> On 03/17/2014 04:00 PM, Hui Zhu wrote: >> >>> The most of the pid_to_exec_file target_ops method for some platforms will >>> allocate memory for exec_file and add them to cleanup. >> >> Which platforms? > > OK, I see several do that, including linux-nat.c. I am sorry that I should not use "most". Just darwin and linux add buf to cleanup. fbsd and nbsd allocate memory but don't add them to cleanup. windows use the static buf. > > IMO, that ends up being a silly interface. The current > interface is documented as: > > /* Attempts to find the pathname of the executable file > that was run to create a specified process. > > The process PID must be stopped when this operation is used. > > If the executable file cannot be determined, NULL is returned. > > Else, a pointer to a character string containing the pathname > is returned. This string should be copied into a buffer by > the client if the string will not be immediately used, or if > it must persist. */ > > #define target_pid_to_exec_file(pid) \ > (current_target.to_pid_to_exec_file) (¤t_target, pid) > > The "This string should be copied into a buffer by the client if > the string will not be immediately used, or if it must persist." > part hints that the implementation is supposed to return a pointer > to a static buffer, like e.g., target_pid_to_str, paddress, and > friends, etc. > > Either we make target_pid_to_exec_file return a pointer to > a malloc buffer that the caller is responsible for xfree'ing (and > adjust the interface comments in target.h) or we make targets > indeed return a pointer to a static buffer, as the current > method's description hints at. Returning a malloced buffer, and > installing a cleanup like that is a silly interface, IMO. Note > that GDB used to have more random memory-release cleanups installed > like this, but we've removed most, I believe. Although it's really > not harmful to install a cleanup that just releases memory > later at any random time, OTOH, it potentially makes debugging > nasty cleanup issues harder, so we've moved away from doing that, > and we now have that warning. According to your comments, I make a new patch that change all methods return a static buffer. Please help me review it. > > > Bummer that we don't have a test that caught this. :-( > Yes, I found it when I did regression test. Does testsuite have some example to test a GDB feature like "gdb -p"? Thanks, Hui 2014-03-18 Hui Zhu * darwin-nat.c (darwin_pid_to_exec_file): Change xmalloc to static buffer. * fbsd-nat.c (fbsd_pid_to_exec_file): Ditto. * linux-nat.c (linux_child_pid_to_exec_file): Ditto. * nbsd-nat.c (nbsd_pid_to_exec_file): Ditto. --- a/gdb/darwin-nat.c +++ b/gdb/darwin-nat.c @@ -1991,12 +1991,9 @@ set_enable_mach_exceptions (char *args, static char * darwin_pid_to_exec_file (struct target_ops *self, int pid) { - char *path; + static char path[PATH_MAX]; int res; - path = xmalloc (PATH_MAX); - make_cleanup (xfree, path); - res = proc_pidinfo (pid, PROC_PIDPATHINFO, 0, path, PATH_MAX); if (res >= 0) return path; --- a/gdb/fbsd-nat.c +++ b/gdb/fbsd-nat.c @@ -40,8 +40,8 @@ char * fbsd_pid_to_exec_file (struct target_ops *self, int pid) { size_t len = PATH_MAX; - char *buf = xcalloc (len, sizeof (char)); - char *path; + static char buf[PATH_MAX]; + char *path, *ret; #ifdef KERN_PROC_PATHNAME int mib[4]; @@ -56,13 +56,12 @@ fbsd_pid_to_exec_file (struct target_ops path = xstrprintf ("/proc/%d/file", pid); if (readlink (path, buf, PATH_MAX - 1) == -1) - { - xfree (buf); - buf = NULL; - } + ret = NULL; + else + ret = buf; xfree (path); - return buf; + return ret; } static int --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -4011,19 +4011,18 @@ linux_nat_thread_name (struct target_ops static char * linux_child_pid_to_exec_file (struct target_ops *self, int pid) { - char *name1, *name2; + static char buf[PATH_MAX]; + char name1[PATH_MAX], name2[PATH_MAX]; - name1 = xmalloc (PATH_MAX); - name2 = xmalloc (PATH_MAX); - make_cleanup (xfree, name1); - make_cleanup (xfree, name2); memset (name2, 0, PATH_MAX); xsnprintf (name1, PATH_MAX, "/proc/%d/exe", pid); if (readlink (name1, name2, PATH_MAX - 1) > 0) - return name2; + strcpy (buf, name2); else - return name1; + strcpy (buf, name1); + + return buf; } /* Records the thread's register state for the corefile note --- a/gdb/nbsd-nat.c +++ b/gdb/nbsd-nat.c @@ -28,16 +28,15 @@ char * nbsd_pid_to_exec_file (struct target_ops *self, int pid) { size_t len = PATH_MAX; - char *buf = xcalloc (len, sizeof (char)); - char *path; + static char buf[PATH_MAX]; + char *path, *ret; path = xstrprintf ("/proc/%d/exe", pid); if (readlink (path, buf, PATH_MAX - 1) == -1) - { - xfree (buf); - buf = NULL; - } + ret = NULL; + else + ret = buf; xfree (path); - return buf; + return ret; }