Next: , Previous: , Up: Debugger Command Reference   [Contents][Index]


4.11 Running Arbitrary BASH and Shell commands (‘eval’, ‘shell’)

The two most general commands and most “low-level” are eval and shell.

eval [ bash-code ]
e

In contrast to the commands of the last section the most general way to examine data is through eval. But you do much more with this; you can change the values of variables, since, you are just evaluating BASH code.

If you expect output, you should arrange that in the command, such as via echo or printf. For example, to print the value of foo, you would type ‘e echo $foo’. This is bit longer than ‘p $foo’ or (when possible) ‘x foo’. However suppose you wanted to find out how the builtin test operator ‘[’ works with the ‘-z’ test condition. You could use eval to do this such as ‘e [ -z "$foo"] && echo "yes"’.

eval

I find I sometimes want to run the line that’s about to be executed to see if I want to step into methods that are called.

For example:

(/etc/apparmor/functions:24):
PROFILES="/etc/apparmor.d"
bashdb<2>

I had been cutting and pasting the command as shown, but realized I could do better if I made a command for this. So that’s what I’ve done.

If you run the ‘eval’ command without any arguments, it will run the command that is about to be run.

(/etc/apparmor/functions:24):
PROFILES="/etc/apparmor.d"
bashdb<2> eval
eval: PROFILES="/etc/apparmor.d"
$? is 0
bashdb<3>

This was working fine, until I started coming across tests inside if, elsif, case, return or while blocks. For example:

(/etc/init.d/apparmor:70):
if [ "$1" = "recache" ]

Suppose I want to know which branch I’m going to take before taking the branch. That way I might even be able to change which way to go by changing the test before it runs in the debugged program. (In the above example, I could print $1

bashdb<2> pr $1
status

But I’m lazy. I’d rather let the debugger do the work for me:

bashdb<1> eval?
eval: [ "$1" = "recache" ]
$? is 1

If you alias eval with a name that ends in ? it will strip off any leading if, case, while, elsif, or return.

!! command string

If you need to execute occasional shell commands during your debugging session, there is no need to leave or suspend the BASH debugger; you can just use the shell command or its alias !!.

Invoke a shell to execute command string.

shell

Although the debugger allows one to evaluate arbitrary BASH code using eval, or via the set autoeval mode, sometimes you might prefer to work inside a BASH shell to see variables, experiment, issue commands (using the currently-set up environment), and even change variables and functions.

For this we, the debugger shell command, enters a nested shell session. But before it does this, it saves out variable and function definitions in the current context of the running program. That way, you have access to those.

This however creates a new problem: getting changes you make reflected back into the running program. Right now any variable you change can be flagged to have its value re-read when the shell exits. This is done using the save_var function inside the nested shell. save_var takes a list of variable names.

Here is an example session

bashdb /etc/init.d/apparmor status
bashdb debugger, release 4.2-0.8

Copyright 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Rocky Bernstein
This is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
...
(/etc/init.d/apparmor:35):
. /etc/apparmor/functions
bashdb<1> s
(/etc/apparmor/functions:24):
PROFILES="/etc/apparmor.d"
bashdb<2> s
(/etc/apparmor/functions:25):
PARSER="/sbin/apparmor_parser"
bashdb<3> shell
bashdb $ typeset -p PROFILES
typeset -p PROFILES
typeset PROFILES=/etc/apparmor.d
bashdb $ PROFILES='Hi, Mom!'
bashdb $ save_vars PROFILES
bashdb $  <EOF>
(/etc/apparmor/functions:25):
PARSER="/sbin/apparmor_parser"
bashdb<4> x PROFILES
typeset PROFILES='Hi, Mom!'

Note that inside the nested shell the we have set the prompt has been set to bashdb $ .


Next: , Previous: , Up: Debugger Command Reference   [Contents][Index]