From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15578 invoked by alias); 6 Mar 2014 21:41:28 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 15563 invoked by uid 89); 6 Mar 2014 21:41:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.9 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail-vc0-f201.google.com Received: from mail-vc0-f201.google.com (HELO mail-vc0-f201.google.com) (209.85.220.201) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Thu, 06 Mar 2014 21:41:25 +0000 Received: by mail-vc0-f201.google.com with SMTP id hq11so432378vcb.4 for ; Thu, 06 Mar 2014 13:41:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:mime-version:content-type :content-transfer-encoding:message-id:date:to:subject:in-reply-to :references; bh=7Z0tHYBoO/zuvq9Xsu+abBW0uT7Wd5n/2bW2W7KsWyg=; b=iSIA/qssJ2TnKI2Dy5Kpycyezr374XsP0hF+UVcBqKtFkIqMmd2H8tWPGB/QnneFHV NQ82hJWxGl3Hiu/5Ayfp1zySMdiHM4jHR0DU45cgQiHBTLiePeAzeCRGUBQbTbabuzVS 3O14sj7adBHHtDbwkGv4NFruTeKnDl6+r7j8e1P7jWq/FPWTEdbJvxsVcZjM5KGLHGtf cIVWkLcWq/40NAnOktKRX+ASZgpZdcaBSkfl0tKMAbm6JbqjW9WI+AT9H1C6w2NAz3b6 ih59mvqX6F2c5D6WWfMAWXuraC4ker8yJp29BzHJ10apVJJ5WcOfWpvFzTibYCkir7+V vHQw== X-Gm-Message-State: ALoCoQm+J7TGF5Wp202sUEkdJ+N4nr5Y/PGrnpFxLpcfOXOwYsIHglD2t1n31P7ypPU0vip9aEF2vR5EK3ht/EKvhwkmK2UuD8su+qHx6y6zZNnYZOdd5MGR0ijd3MwpNiOxIcSXr9PI1VHPCdEmq/pX4CdOg6Gaq8QLo+MHnfeSzHtUvbEqlUP0TRAbwktXeg5zkTgWfKZmFoih+xCuSEl0hDYW1tHoow== X-Received: by 10.236.132.231 with SMTP id o67mr5717232yhi.15.1394142082792; Thu, 06 Mar 2014 13:41:22 -0800 (PST) Received: from corp2gmr1-2.hot.corp.google.com (corp2gmr1-2.hot.corp.google.com [172.24.189.93]) by gmr-mx.google.com with ESMTPS id x29si1218501yha.0.2014.03.06.13.41.22 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 06 Mar 2014 13:41:22 -0800 (PST) Received: from ruffy.mtv.corp.google.com (ruffy.mtv.corp.google.com [172.17.128.44]) by corp2gmr1-2.hot.corp.google.com (Postfix) with ESMTP id 44E625A4446; Thu, 6 Mar 2014 13:41:22 -0800 (PST) From: Doug Evans MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <21272.60289.723000.990365@ruffy.mtv.corp.google.com> Date: Thu, 06 Mar 2014 21:41:00 -0000 To: gdb-patches@sourceware.org, eliz@gnu.org Subject: [PATCH, doc RFA] New option -B: simplify running gdb from build directory In-Reply-To: References: X-IsSubscribed: yes X-SW-Source: 2014-03/txt/msg00164.txt.bz2 Doug Evans writes: > Hi. > > The "make run" Makefile rule simplifies running gdb from the shell, > but it doesn't simplify running gdb from gdb: I'm always typing > "--data-directory=$(pwd)/data-directory". > And since we can't agree on a way to let gdb auto-detect being run > from the build directory, how about this? > > This patch provides a new option, -B, which if provided tells gdb > it *may* have been run from the build directory. If -B was provided > and --data-directory was not provided, use a heuristic to determine > if gdb was run from the build directory and if so set gdb_datadir > appropriately. > > We can certainly discuss what's the best heuristic to use here. > We don't, IMO, have to employ too complicated a heuristic because > the user has explicitly passed -B. > OTOH, I'm sure one can improve a bit on the heuristic that's here. > One thought I have is to have the Makefile create a file with a name that > is reasonably obscure (maybe even pseudo-cryptographically secure and encode > the name in gdb?), and then simplify the test by just checking for that file. > > If we can agree on this approach, I'll add NEWS and docs. I believe I've addressed Eli's comments, [ref: https://sourceware.org/ml/gdb-patches/2013-12/msg00969.html] and I didn't see any other discussion, so here's the patch. 2014-03-06 Doug Evans New option -B. * NEWS: Mention it. * main.c (maybe_run_from_builddir): New static global. (get_build_data_directory): New function. (captured_main): New enum value OPT_BATCH_SILENT. Use it instead of 'B' for --batch-silent. Recognize and handle -B option. doc/ * gdb.texinfo (Mode Options): Document -B. testsuite/ * gdb.base/dash-b.exp: New file. diff --git a/gdb/NEWS b/gdb/NEWS index 2a384ba..a1561e7 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -3,6 +3,11 @@ *** Changes since GDB 7.7 +* New command line options: + +-B Indicates GDB may have been run from its build directory, and GDB + should first look for its ancillary files there. + * Guile scripting GDB now has support for scripting using Guile. Whether this is diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index de5ac63..ab6a643 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -1169,6 +1169,18 @@ Run @value{GDBN} using @var{directory} as its data directory. The data directory is where @value{GDBN} searches for its auxiliary files. @xref{Data Files}. +@item -B +@cindex @code{-B} +This option tells @value{GDBN} it may have been run from its build directory, +and it should first look for its auxiliary files there. @xref{Data Files}. +If @value{GDBN} cannot determine it has been run from its build directory, +then it will expect to find its auxiliary files in their installed +location as specified by the @code{--prefix} option when @value{GDBN} was +configured. + +This option is ignored if the data directory has been explicitly specified +with @code{--data-directory}. + @item -fullname @itemx -f @cindex @code{--fullname} diff --git a/gdb/main.c b/gdb/main.c index 59015f5..536f9b7 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -57,6 +57,9 @@ int xdb_commands = 0; /* Whether dbx commands will be handled. */ int dbx_commands = 0; +/* Whether GDB may have been run from the build directory. */ +static int maybe_run_from_builddir = 0; + /* System root path, used to find libraries etc. */ char *gdb_sysroot = 0; @@ -160,6 +163,60 @@ relocate_gdb_directory (const char *initial, int flag) return dir; } +/* Return the path of the data directory in the build tree if we can + reasonably determine that gdb was run from the build directory. + Otherwise return NULL. + Space for the result is obtained with malloc, caller must free. + + We say "reasonably" because gdb *could* be run from a place that *looks* + like the build directory, and we want to avoid false positives as much as + is reasonably possible. This code is only used if the user passes -B to + gdb so our tests needn't be *perfect*. + + The test is done by seeing if various files that we expect to find in a + build directory exist. There are a few constraints on our choices: + + 1) We don't test for object files so that we don't have to deal with the + complexity of different suffixes (e.g., .o vs .obj). + 2) We can't (easily) test for files that only exist for a particular + configuration. + + In the end we choose three files from the data-directory since it's the + data-directory that we need to find: + 1) Makefile - only exists in build tree, + 2) stamp-syscalls - only exists in build tree that has been built, + 3) syscalls/amd64-linux.xml - gdb needs this + (installed regardless of platform so it doesn't matter if the current + platform is not amd64-linux). */ + +static char * +get_build_data_directory (void) +{ + char *gdb_dirname = ldirname (gdb_program_name); + char *full_gdb_dirname = gdb_realpath (gdb_dirname); + char *datadir = concat (full_gdb_dirname, SLASH_STRING "data-directory", + NULL); + char *test_file1 = concat (datadir, SLASH_STRING, "Makefile", NULL); + char *test_file2 = concat (datadir, SLASH_STRING, "stamp-syscalls", NULL); + char *test_file3 = concat (datadir, SLASH_STRING, "syscalls", + SLASH_STRING, "amd64-linux.xml", NULL); + int builddir_found; + + builddir_found = (access (test_file1, R_OK) == 0 + && access (test_file2, R_OK) == 0 + && access (test_file3, R_OK) == 0); + xfree (gdb_dirname); + xfree (full_gdb_dirname); + xfree (test_file1); + xfree (test_file2); + xfree (test_file3); + + if (builddir_found) + return datadir; + xfree (datadir); + return NULL; +} + /* Compute the locations of init files that GDB should source and return them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT. If there is no system gdbinit (resp. home gdbinit and local gdbinit) @@ -461,7 +518,8 @@ captured_main (void *data) OPT_NOWINDOWS, OPT_WINDOWS, OPT_IX, - OPT_IEX + OPT_IEX, + OPT_BATCH_SILENT }; static struct option long_options[] = { @@ -476,7 +534,7 @@ captured_main (void *data) {"nh", no_argument, &inhibit_home_gdbinit, 1}, {"nx", no_argument, &inhibit_gdbinit, 1}, {"n", no_argument, &inhibit_gdbinit, 1}, - {"batch-silent", no_argument, 0, 'B'}, + {"batch-silent", no_argument, 0, OPT_BATCH_SILENT}, {"batch", no_argument, &batch_flag, 1}, /* This is a synonym for "--annotate=1". --annotate is now @@ -530,6 +588,7 @@ captured_main (void *data) {"args", no_argument, &set_args, 1}, {"l", required_argument, 0, 'l'}, {"return-child-result", no_argument, &return_child_result, 1}, + {"B", no_argument, &maybe_run_from_builddir, 1}, {0, no_argument, 0, 0} }; @@ -636,7 +695,7 @@ captured_main (void *data) VEC_safe_push (cmdarg_s, cmdarg_vec, &cmdarg); } break; - case 'B': + case OPT_BATCH_SILENT: batch_flag = batch_silent = 1; gdb_stdout = ui_file_new(); break; @@ -742,6 +801,20 @@ captured_main (void *data) quiet = 1; } + /* If -B was provided and --data-directory was not, then see if we have been + run from the build directory and if so use the data-directory there. */ + if (maybe_run_from_builddir && !gdb_datadir_provided) + { + char *builddir = get_build_data_directory (); + + if (builddir != NULL) + { + xfree (gdb_datadir); + gdb_datadir = builddir; + gdb_datadir_provided = 1; + } + } + /* Initialize all files. Give the interpreter a chance to take control of the console via the deprecated_init_ui_hook (). */ gdb_init (gdb_program_name); diff --git a/gdb/testsuite/gdb.base/dash-b.exp b/gdb/testsuite/gdb.base/dash-b.exp new file mode 100644 index 0000000..24ac6f9 --- /dev/null +++ b/gdb/testsuite/gdb.base/dash-b.exp @@ -0,0 +1,34 @@ +# Copyright 2014 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . */ + +# Test the -B option. +# The test here needs to handle the case where gdb has already been installed, +# in which case if -B isn't working probably the test might still pass whereas +# it should fail. The way we test this is to verify the value of the +# "data-directory" parameter after gdb has started. + +set save_INTERNAL_GDBFLAGS $INTERNAL_GDBFLAGS +set INTERNAL_GDBFLAGS "-nw -nx -B" + +gdb_exit +gdb_start +gdb_reinitialize_dir $srcdir/$subdir + +set normalized_objdir [file normalize $objdir] +verbose -log "normalized objdir: $normalized_objdir" +gdb_test "show data-directory" \ + "GDB's data directory is \"[file dirname $normalized_objdir]/data-directory\"\." + +set INTERNAL_GDBFLAGS $save_INTERNAL_GDBFLAGS