From patchwork Thu Mar 20 02:58:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hui Zhu X-Patchwork-Id: 184 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 21AC536018E for ; Wed, 19 Mar 2014 19:58:33 -0700 (PDT) Received: by homiemail-mx22.g.dreamhost.com (Postfix, from userid 14314964) id B63484F6E62A; Wed, 19 Mar 2014 19:58:32 -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 8D92B4F6E61A for ; Wed, 19 Mar 2014 19:58:32 -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=WR7F/g9wWJ+t15RX 3Sy8xz6JG7w4+tgJXAgj/9Jmd57E/PS1/v+RlD/08m6HT7xrQXy3dDPXGlTVpnjW blhX6fcVCpsihH11S6E9ufkTHJkso3kWKzONZW1V+rw8L9lDyjMhiHTHX1muPGmf jcClwU7BXsuv5+lYRy6qr54L2j8= 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=OhPGks74Kqd+8WBxxMi4Bs 3qgBo=; b=OYebUP+I+10Uu/KqRZKOtuZal+EAU6Lnr8SWXa4b/5m/z/bA55QUUx HvtrAqAM1thGiur37FmuLYkPPT0UI0Zfk0WqMdulsVP3sl/HziSkrR7a2gQEF2AP vYdnITtvSaa4m0yBHlAVrE09mLdQdC9cbm99bvU37N5CxePk7qpFE= Received: (qmail 12562 invoked by alias); 20 Mar 2014 02:58: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 12546 invoked by uid 89); 20 Mar 2014 02:58:28 -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; Thu, 20 Mar 2014 02:58:27 +0000 Received: from svr-orw-exc-10.mgc.mentorg.com ([147.34.98.58]) by relay1.mentorg.com with esmtp id 1WQTBW-0005Q6-8Z from Hui_Zhu@mentor.com ; Wed, 19 Mar 2014 19:58:22 -0700 Received: from SVR-ORW-FEM-02.mgc.mentorg.com ([147.34.96.206]) by SVR-ORW-EXC-10.mgc.mentorg.com with Microsoft SMTPSVC(6.0.3790.4675); Wed, 19 Mar 2014 19:58:22 -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; Wed, 19 Mar 2014 19:58:21 -0700 Message-ID: <532A5943.8040705@mentor.com> Date: Thu, 20 Mar 2014 10:58:11 +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> <5327F7D0.1050304@mentor.com> <53281C69.7090703@redhat.com> <532915BF.2090608@mentor.com> <53296E88.9040503@redhat.com> In-Reply-To: <53296E88.9040503@redhat.com> X-IsSubscribed: yes X-DH-Original-To: gdb@patchwork.siddhesh.in On 03/19/14 18:16, Pedro Alves wrote: > On 03/19/2014 03:57 AM, Hui Zhu wrote: >>>> >>>> Same with the nul termination. The most standard solution is: >>>> >>>> static char buf[PATH_MAX]; >>>> char name[PATH_MAX]; >>>> ssize_t len; >>>> >>>> xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid); >>>> len = readlink (name, buf, PATH_MAX - 1); >>>> if (len != -1) >>>> { >>>> buf[len] = '\0'; >>>> return buf; >>>> } >>>> return NULL; >>>> >> I make a new patch according to your comments. >> Please help me review it. > > The patch changes the bsd implementations's behavior, because > you made them return the /proc path when readlink fails (like > the Linux version does), instead of what the current code does > or what I suggested above. > I made a new version that change fbsd_pid_to_exec_file and nbsd_pid_to_exec_file to your suggested above. Please help me review it. Thanks, Hui 2014-03-20 Hui Zhu Pedro Alves * 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 name[PATH_MAX]; #ifdef KERN_PROC_PATHNAME int mib[4]; @@ -54,15 +54,15 @@ fbsd_pid_to_exec_file (struct target_ops return buf; #endif - path = xstrprintf ("/proc/%d/file", pid); - if (readlink (path, buf, PATH_MAX - 1) == -1) + xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid); + len = readlink (name, buf, PATH_MAX - 1); + if (len != -1) { - xfree (buf); - buf = NULL; + buf[len] = '\0'; + return buf; } - xfree (path); - return buf; + return NULL; } static int --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -4011,19 +4011,15 @@ 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 name[PATH_MAX]; - name1 = xmalloc (PATH_MAX); - name2 = xmalloc (PATH_MAX); - make_cleanup (xfree, name1); - make_cleanup (xfree, name2); - memset (name2, 0, PATH_MAX); + xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid); + memset (buf, 0, PATH_MAX); + if (readlink (name, buf, PATH_MAX - 1) <= 0) + strcpy (buf, name); - xsnprintf (name1, PATH_MAX, "/proc/%d/exe", pid); - if (readlink (name1, name2, PATH_MAX - 1) > 0) - return name2; - else - return name1; + return buf; } /* Records the thread's register state for the corefile note --- a/gdb/nbsd-nat.c +++ b/gdb/nbsd-nat.c @@ -27,17 +27,17 @@ 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; + size_t len; + static char buf[PATH_MAX]; + char name[PATH_MAX]; - path = xstrprintf ("/proc/%d/exe", pid); - if (readlink (path, buf, PATH_MAX - 1) == -1) + xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid); + len = readlink (name, buf, PATH_MAX - 1); + if (len != -1) { - xfree (buf); - buf = NULL; + buf[len] = '\0'; + return buf; } - xfree (path); - return buf; + return NULL; }