Set Environment Variable in Gdb from Output of Command

How to set environment variable within GDB using shell command?

Option 2 is possible.

(gdb) unset environment
(gdb) python gdb.execute("set environment Myvar=\xff")
(gdb) show environment
Myvar=ÿ

Option 1 can be done with env(1).

$ env -i MyVar=$(python -c 'print("xyz")') gdb
(gdb) show environment
MyVar=xyz
LINES=35
COLUMNS=80

Then you just have to clear LINES and COLUMNS.

set environment variable in GDB from output of command

Well, if you really need to do it from GDB, here is one example:

hello.c

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv) {
printf("argv[1]=%s\n", argv[1]);
printf("VAR=%s\n", getenv("VAR"));
return 0;
}

Example:

$ gcc -g -o hello hello.c
$ gdb ./hello
...
(gdb) set exec-wrapper bash -c 'exec env VAR="`echo myEnv`" "$@"' --
(gdb) r myArg
...
argv[1]=myArg
VAR=myEnv

Change VAR and echo myEnv to a variable and command you need.


But note that setting VAR from shell before starting GDB also works:

$ VAR=`echo Hey there` gdb ./hello
...
(gdb) r myArg
...
argv[1]=myArg
VAR=Hey there

storing shell output into GDB variable in gdbinit?

If you have a new-ish version of GDB (I believe that means 7.x) with python support built, you could add a section like:

python
import subprocess
gdb.execute('set solib-search-path ' +
subprocess.check_output('which gdb',shell=True).rstrip() +
'../project/lib')
end

I can't claim it's not possible without using python, but it's the only way I know of to do it. [I also assumed you meant ../project/lib and not ..\project\lib, but that's an easy change...].

In GDB, how can I set 'exec-wrapper env' to multiple environmental variables?

You can use

set exec-wrapper env VAR1=val1 VAR2=val2

to set multiple environment variables. The values should be appropriately quoted for your shell, so putting single quotes around them would be a good idea.

In slightly more detail:

The set exec-wrapper command sets a string variable to contain the rest of the command line.

When it comes time to run your executable, gdb does something like the following pseudo-code:

shell_cmd = "exec ";
if (exec_wrapper)
shell_cmd += exec_wrapper + " ";
shell_cmd += quote_shell_metacharacters(exec_file);
execl(getenv("SHELL"), "sh", "-c", shell_cmd, (char *)0);

So, exec-wrapper can be any command line that makes sense when preceded by "exec " in your shell.

Setting up gdb's environment when running it through emacs

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

Pass gdb variable to a shell command excuted from gdb

With a new version of gdb, you can use "eval":

(gdb) set $val = 2
(gdb) eval "shell echo %d", $val
2

If you have an older version of gdb, then the best you can do is use "set logging" to write things to a file, then use shell utilities like sed to rewrite the file into the form you want.

Setting an environment variable to a space in GDB

It does not appear to be possible directly. The in build documentation (help set env) states:

VALUES of environment variables are uninterpreted strings.

and GDB's source code (specifically environ.c and infcmd.c) concur with this and do not show any explicit manipulation of the value other than removing training whitespace.



Related Topics



Leave a reply



Submit