Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access environment variables inside .gdbinit and inside gdb itself?

Tags:

I am looking to set up the path for the source code when debugging with gdb. I chose to do that with a .gdbinit file.

Basically, it contains a command:

directory="/path/to/src". 

However, I would like to be able to specify that command as:

directory="$SOURCESROOT/src" 

where SOURCESROOT is an environment variable. And, if possible, being able to do that inside gdb debuuging session too, by entering directory=$SOURCESROOT/folder.

Basically, I am looking to access inside gdb (or inside .gdbinit) the environment variables.

But not the environment of the debugee (set env and so on), but the environment of the gdb itself (ie. of the bash prompt where I type in the first place "gdb program").

While typing shell $SOURCESROOT inside gdb session shows the content of the environment variable, this is quite useless, as I cannot enter: directory=shell $SOURCESROOT.

PS: Anybody found an ideal setup for Linux (Debian) to download the sources with "apt-get source", to update those with some kind of "apt-get update" utopic command and to install them so that gdb will automatically find these sources?

like image 772
user1284631 Avatar asked Mar 21 '12 23:03

user1284631


People also ask

How do I check global Environment Variables?

Right click the Computer icon on your desktop and choose Properties from the menu. Click on the Advanced system settings link and then click Environment Variables. Under the section System Variables, select the environment variable you want to edit, and click Edit.

How do you enter an environment variable?

Windows Environment Variables Do so by pressing the Windows and R key on your keyboard at the same time. Type sysdm. cpl into the input field and hit Enter or press Ok. In the new window that opens, click on the Advanced tab and afterwards on the Environment Variables button in the bottom right of the window.

Where are global Environment Variables stored?

The Global environment variables of your system are stored in /etc/environment . Any changes here will get reflected throughout the system and will affect all users of the system.

How to check if the env variable is set in gdb?

To check if the env variable is set I am using GDB command show environment. This prints all the environment variables and their values. But it dose not show TZ being set. Even command show environment TZ says Environment variable "TZ" not defined. Is their another way to check the environment of the debugged program?

What is the use of set environment in gdb?

Gdb maintains an environment array, initially copied from its own environment, which it uses to start each new child process. show environment and set environment work on this environment, so set environment will change an environment variable for the next time you start the program being debugged.

What environment variables are unset on GDB remote inferior?

Environment variables that are unset by the user are also unset on gdbserver when starting the remote inferior. see QEnvironmentUnset . Warning: On Unix systems, GDB runs your program using the shell indicated by your SHELL environment variable if it exists (or /bin/sh if not).

Why can't I debug with GDB?

GDB inherits the environment variables from your shell. Some environment variables, like LD_LIBRARY_PATH, might not be inherited for safety reasons. This can cause errors like this when you try to debug: You can set the LD_LIBRARY_PATH environment variable at the gdb shell using the set env command like this:


1 Answers

Nevermind, I found how to do it by using Python scripting.

My .gdbinit file is now:

python import os gdb.execute('directory' + os.environ['SOURCES'] + '/package_name/src') end  show directories 
like image 193
user1284631 Avatar answered Oct 10 '22 06:10

user1284631