bazel test --run_under with gdb not working?

5,894 views
Skip to first unread message

glin...@gmail.com

unread,
Jan 21, 2016, 9:09:37 PM1/21/16
to bazel-discuss
I'm trying to run a cc_test with "bazel test --run_under='gdb --args' :my_test", and it always reports success, without starting up gdb. Is there some magic to get this to work? I've tried --run_under='gdb' alone as well, and same result.

Thanks!

Brian Silverman

unread,
Jan 21, 2016, 9:32:16 PM1/21/16
to Gary Linscott, bazel-discuss
Bazel doesn't connect stdin through to the terminal with `bazel test`, which looks like it makes gdb just exit immediately. You can look at the test output at bazel-testlogs/package/my_test/test.log to confirm. `bazel run --run_under=gdb` should do what you want, although it won't set up the test-specific environment variables (like TEST_TMPDIR) automatically.

On Thu, Jan 21, 2016 at 6:09 PM, <glin...@gmail.com> wrote:
I'm trying to run a cc_test with "bazel test --run_under='gdb --args' :my_test", and it always reports success, without starting up gdb.  Is there some magic to get this to work?  I've tried --run_under='gdb' alone as well, and same result.

Thanks!

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/f8dc6874-2d4f-4002-bfd8-d9ecbfd35e76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

glin...@gmail.com

unread,
Jan 22, 2016, 12:31:19 AM1/22/16
to bazel-discuss, glin...@gmail.com
Ok, thanks Brian! This would definitely be a useful feature, maybe a pull request incoming :).

glin...@gmail.com

unread,
Jan 22, 2016, 11:44:42 AM1/22/16
to bazel-discuss, glin...@gmail.com
It looks like gdb is not happy under bazel run either. gdb exits instantly again, this time it prints the startup info at least though. ie. "bazel run --run_under='gdb --args' :my_test" gives

INFO: Running command line: /bin/bash -c 'gdb --args bazel-bin/my_test '
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from <...>(no debugging symbols found)...done.
(gdb) quit


On Thursday, January 21, 2016 at 6:32:16 PM UTC-8, Brian Silverman wrote:

Austin Schuh

unread,
Jan 22, 2016, 1:02:17 PM1/22/16
to glin...@gmail.com, bazel-discuss
GDB may be looking for a pty.  Shot in the dark.

rodr...@google.com

unread,
May 9, 2019, 1:09:56 PM5/9/19
to bazel-discuss
On Friday, January 22, 2016 at 3:09:37 AM UTC+1, glin...@gmail.com wrote:
> I'm trying to run a cc_test with "bazel test --run_under='gdb --args' :my_test", and it always reports success, without starting up gdb. Is there some magic to get this to work? I've tried --run_under='gdb' alone as well, and same result.
>
> Thanks!

Resurrecting this thread to share a trick I found, --run_under="gdbserver :2345", eg:

bazel test -c gdb --cache_test_results=no --run_under='gdbserver :2345' :my_test
# in another terminal:
gdb -ex "target remote :2345"

Brandon Adams

unread,
May 15, 2019, 3:37:41 PM5/15/19
to bazel-discuss


On Friday, January 22, 2016 at 10:44:42 AM UTC-6, glin...@gmail.com wrote:
It looks like gdb is not happy under bazel run either.  gdb exits instantly again, this time it prints the startup info at least though.  ie. "bazel run --run_under='gdb --args' :my_test" gives

Besides gdbserver, you can use a wrapper script that redirects stdin/stdout/stderr. I have a wrapper on my path that looks like this (replace $$$WORKSPACENAME$$$ with the name of your workspace):

```bash

#!/usr/bin/env bash
# We need to run gdb ... < /dev/tty in order to get stdin as an interactive process under bazel run's --run_under
# Additionally, we get dropped into the runfiles of the target directory. This is suboptimal, so we use sed to find
# the execroot, and tell gdb to cd there so we get both our source files and our external sources.
gdb -ex "directory $(pwd | sed -E 's|(.*/execroot/$$$WORKSPACENAME$$$/).*|\1|')" "$@" < /dev/tty > /dev/tty 2>/dev/tty
 
```

I then say `bazel run --run_under=gdb_wrapper.sh //my:target`

This seems to work okay.

Alternately, you might want to setup a gdbinit to fix your directory instead, so that it works under CLion/gdbserver, etc.

Good luck,

- Brandon
Reply all
Reply to author
Forward
0 new messages