7

I have a program that I'd like to debug with gdb via emacs. In order to run development versions of this program, I have a shell script that I can source that sets up the calling environment to see the right libraries, etc. What I can't sort out is how to ask emacs/gud to source this file before executing gdb.

I've tried using a command like "source env.sourceme && gdb my_program", but emacs complains that it doesn't know what "source" means. I guess it's not really running gdb in a shell, so these kinds of tricks won't work.

So, how can I convince gud/emacs/whatever to run gdb in my custom environment? I've got a hacky solution in place, but I feel like I must be missing something.

abingham
  • 1,294
  • 1
  • 10
  • 17

3 Answers3

5

gdb has its own syntax for setting environment variables:

set environment varname [=value]

Instead of a shell script, write your variable definitions in a file using the above syntax, then source the file from a running gdb session. Note that this is not bash's built-in source command, but gdb's own, so naturally bash-style environment variable definitions will not work.

Thomas
  • 17,016
  • 4
  • 46
  • 70
  • 1
    Similar to my comment on the previous response, I would like to leverage my existing infrastructure rather than write yet more code to appease gdb. – abingham Mar 13 '12 at 07:36
  • This works for me if I put a list of "set environment varname [=value]" lines in a file and then execute `source file` in emacs gdb. – 719016 Oct 03 '13 at 15:02
  • @200508519211022689616937 Oh, good, because that's exactly what I wrote in my answer :-) [Emacs gud-gdb simply runs gdb.] – Thomas Oct 05 '13 at 07:28
3

You can modify the Emacs environment using setenv, either interactively (M-x setenv) or programmatically:

(setenv "FOOBAR" "whatever")

When you run gud-gdb, whatever you set using setenv will be passed to the gdb process.

N.N.
  • 8,336
  • 12
  • 54
  • 94
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
  • I guess I should clarify. I would like to leverage the existing shell script (which is generated by a build and thus relatively convenient) rather than need to duplicate its functionality for the sake of emacs. And in any event, I don't want to change the environment variable for my entire emacs process, just for the gdb subprocess. – abingham Mar 13 '12 at 07:34
3

What's your hacky solution?

Why wouldn't you just have a wrapper script that sources env.sourceme and then run gdb?

#!/usr/bin/env bash

source env.sourceme
gdb -i=mi $1
event_jr
  • 17,467
  • 4
  • 47
  • 62
  • That is essentially my "hacky solution" ;) It's actually a but more general purpose in that it'll run any command (via `$*`), but the same in spirit. I guess I'm just surprised that it's not a simple thing to get gud to source the file for me... – abingham Mar 13 '12 at 07:39