Setting up gdb's environment when running it through emacs
Question
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.
Solution
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
OTHER TIPS
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.
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.