Setting up gdb's environment when running it through emacs
Asked Answered
F

3

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.

Foundry answered 12/3, 2012 at 13:21 Comment(0)
S
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
Spearman answered 12/3, 2012 at 14:42 Comment(1)
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...Foundry
P
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.

Purgative answered 13/3, 2012 at 0:25 Comment(3)
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.Foundry
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.Breazeale
@200508519211022689616937 Oh, good, because that's exactly what I wrote in my answer :-) [Emacs gud-gdb simply runs gdb.]Purgative
E
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.

Essive answered 12/3, 2012 at 13:39 Comment(1)
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.Foundry
S
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
Spearman answered 12/3, 2012 at 14:42 Comment(1)
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...Foundry

© 2022 - 2024 — McMap. All rights reserved.