⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bashdb.texi

📁 bash debugger. You can use this tool to debug bash shell script
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
down your program quite a bit, and addressing this better would meansome serious changes to @value{BASH} internals.  If you have a@code{configure} script generated by autoconf, and you want to stop inthe middle of the script, it can take quite a while.Furthermore, by necessity, debuggers change the operation of theprogram they are debugging. And this can lead to unexpected andunwanted differences. It has happened so often that the term``Heisenbugs'' (see @url{http://en.wikipedia.org/wiki/Heisenbug}) wascoined to describe the situation where the addition of the use of adebugger (among other possibilities) changes behavior of the programso that the bug doesn't manifest itself anymore.Another way to get into the debugger which adds no overhead orslowdown until you reach the point at which you want to startdebugging. However here you must change the script and make anexplicit call to the debugger. Because the debugger isn't involvedbefore the first call, there is no overhead and the script will run atthe same speed as if there were no debugger.There are two parts to calling the debugger from inside the script,``sourcing'' in the debugger code and then making the call calling thedebugger to issue a ``step'' command. The name of file in@code{source} command is @code{bashdb-trace} and it is located in thedirectory where the other bash debugger files live. For example onGNU/Linux if it is in directory @code{/usr/local/share/bashdb}, youwould first add to a @value{BASH} script the line:@smallexample    source /usr/local/share/bashdb/bashdb-trace@end smallexample Although I said that running under the debugger adds overhead whichslows down you program, the above command in of itself will @emph{not}cause any slowdown. If possible, it's best to put this somewhere inthe main-line code rather than in a function or in a subshell. If itis put in a function of subshell and you step outside of that,some of the global variables set up in @code{bashdb-trace} may belost. One the other hand if you know your debugging will be confinedto just the scope of the @code{source} command there is no problem.The second thing that needs to be done is put add statements to startstepping code, and this line looks like this:@smallexample     _Dbg_debugger ; : ; :@end smallexample (For reasons that are a mystery to me, the stepping seems to kick inonly two statements after the call.)Putting these two components together, here's an example:@smallexample@cartouche  # Lots of stuff here. ...  x=2  source <path-to-program>/_Dbg_debugger  echo There can be lots of other statements between the above  echo and below line.  _Dbg_debugger ; : ; :  y=3@end cartouche@end smallexample You will be stopped before @code{y=3} in the debugger. You can also supply options to @code{_Dbg_debugger} just as you would tothe debugger itself. All of the options listed in @ref{Options for thebashdb script} can be used with the exception of @code{-c} (run acommand) and of course you don't supply the name of a @value{BASH} script.For example to suppress the banner you could use @code{_Dbg_debugger-q} in the above example.@node Program-Controlled Line Tracing@subsection Program-Controlled Line TracingYou can also turn on and off line tracing. Here's an example@smallexample@cartouche  source <path-to-program>/bashdb-trace   ...  _Dbg_linetrace_on  for i in `seq 10` ; do     echo $i  done  _Dbg_linetrace_off   BASHDB_QUIT_ON_QUIT=1  @end cartouche@end smallexample The @code{BASHDB_QUIT_ON_QUIT}  variable make sure the program doesn'tstay inside the debugger after it quits. It can also be set earlier inthe program.Again @code{<path-to-program>} is whatever path needed to located@code{<bashdb-trace>}. For example it might be @code{</usr/local/share>}on some GNU/Linux installations.@node Having the debugger intercept signals@subsection Having the debugger intercept signalsYou can also use the debugger's signal handler to print signals or gointo the debugger when geting a specific signal. Here are some examples: @smallexample@cartouche    _Dbg_handler INT print showstack nostop  # this is the default    _Dbg_handler INT                         # same thing    _Dbg_hander                              # same thing    _Dbg_handler HUP print stop              # stop in debugger when getting@end cartouche@end smallexample @node Running@chapter Script Setup inside the BASH Debugger@menu* Starting::                    Starting your script* Command Files::               Command files* Arguments::                   Your script's arguments* Input/Output::                Your script's input and output* Script/Debugger Interaction::   Keeping out of each other's harm@end menu@need 2000@node Starting@section Starting your script@cindex starting@cindex runningAfter invoking the debugger you should be on the first stoppable lineof your program to be debugged. At this point you can issue debuggercommands to set breakpoints (@pxref{Set Breaks, ,Settingbreakpoints}), or watchpoints (@pxref{Set Watchpoints, ,Settingwatchpoints}), or start continue the execution of the program(@pxref{Resuming Execution, ,Resuming Execution}).@table @code@kindex restart @ovar{args}@kindex run @r{(@code{restart})}@kindex R @r{(@code{restart})}@item restart @ovar{args}@itemx run @ovar{args}@itemx R @ovar{args}Use the @code{restart} command to restart your script under@value{DBG}. Without any arguments, the script name and parametersfrom the last invocation are used. @value{dBGP} tries to maintain thesettings, watchpoints, breakpoints, actions and so on. Internally ituses line numbers and filenames to record he position of interestingplaces in your porgram; so if your program changes some or all ofthese numbers may be off. Environment variable@code{BASHDB_RESTART_FILE} is and a temporary file are used to signala restart, so you shouldn't uset @code{BASHDB_RESTART_FILE} (or anyenvironment variable starting with @code{BASHDB_}.@end table@node Command Files@section Command files@cindex command filesA command file for @DBG is a file of lines that are @DBGcommands.  Comments (lines starting with @kbd{#}) may also be included.An empty line in a command file does nothing; it does not mean to repeatthe last command, as it would from the terminal.@cindex init file@cindex @file{.bashdbinit}@cindex @file{bashdb.ini}When you start @value{DBG}, it automatically executes commands from its@dfn{init files}, normally called @file{.bashdbinit}@footnote{The DJGPPport of @DBG uses the name @file{bashdb.ini} instead, due to thelimitations of file names imposed by DOS filesystems.}.During startup, @DBG does the following:@enumerate@itemReads the init file (if any) in your home directory@footnote{OnDOS/Windows systems, the home directory is the one pointed to by the@code{HOME} environment variable.}.@itemProcesses command line options and operands.@itemReads the init file (if any) in the current working directory.@itemReads command files specified by the @samp{-x} option.@end enumerateThe init file in your home directory can set options (such as @samp{setcomplaints}) that affect subsequent processing of command line optionsand operands.  Init files are not executed if you use the @samp{-x}option (@pxref{Options for the bashdb script, ,bashdb script options}).@cindex init file nameOn some configurations of @value{DBG}, the init file is known by adifferent name (these are typically environments where a specializedform of @DBG may need to coexist with other forms, hence adifferent name for the specialized version's init file).  These are theenvironments with special init file names:You can also request the execution of a command file with the@code{source} command:@table @code@kindex source@item source @var{filename}Execute the command file @var{filename}.@end tableThe lines in a command file are executed sequentially.  They are notprinted as they are executed.  If there is an error, executionproceeds to the next command in the file.@node Arguments@section Your script's arguments@cindex arguments (to your script)The arguments to your script can be specified by the arguments of the@code{restart} command.They are passed to a shell, which expands wildcard characters andperforms redirection of I/O, and thence to your script.  @code{restart} with no arguments uses the same arguments used by the previous@code{restart}, or those set by the @code{set args} command.. @table @code@kindex set args@item set argsSpecify the arguments to be used if your program is rerun. If@code{set args} has no arguments, @code{restart} executes your programwith no arguments.  Once you have run your program with arguments,using @code{set args} before the next @code{restart} is the only way to runit again without arguments.@kindex show args@item show argsShow the arguments to give your program when it is started.@end table@node Input/Output@section Your script's input and output@cindex redirection@cindex I/O@cindex terminalBy default, the script you run under the @acronym{BASH} debugger doesinput and output to the same terminal that @acronym{BASH} uses.Before running the script to be debugged, the debugger records the ttythat was in effect. All of its output is then written to that.However you can change this when using the @samp{bashdb} script usingthe @samp{-t} option.@table @code@kindex info terminal@item info terminalDisplays information recorded by @DBG about the terminal modes yourprogram is using.@end table@kindex tty@cindex controlling terminalAnother way to specify where your script should do input and output iswith the @code{tty} command.  This command accepts a file name asargument, and causes this file to be the default for future @code{restart}commands.  It also resets the controlling terminal for the childprocess, for future @code{restart} commands.  For example,@exampletty /dev/ttyb@end example@noindentdirects that processes started with subsequent @code{restart} commandsdefault to do input and output on the terminal @file{/dev/ttyb} and havethat as their controlling terminal.An explicit redirection in @code{restart} overrides the @code{tty} command'seffect on the input/output device, but not its effect on the controllingterminal.When you use the @code{tty} command or redirect input in the @code{restart}command, only the input @emph{for your script} is affected.  The inputfor @DBG still comes from your terminal.@node Script/Debugger Interaction@section Script/Debugger Interaction@value{dBGP} and your program live in the same variable space so tospeak. @acronym{BASH} does not have a notion of module scoping orlexical hiding (yet) as is found in modern programming langauges andin modern versions of the Korn shell. This then imposes someadditional care and awareness.Most of the variables and functions used inside @DBG start@code{_Dbg_}, so please don't use variables or functions with thesenames in your program. @emph{Note: there are some other variables that begin with just anunderscore (@code{_}); over time these will be phased out. But untilthen, avoid those or consult what is used by the debugger. Run@samp{bashdb --debugger -c "declare -p"} to list all the variables inuse including those used by the debugger.}A number of environment variables are also reserved for use; thesestart with @code{BASHDB_}. For example: @env{BASHDB_INPUT},@env{BASHDB_LEVEL} and, @env{BASHDB_QUIT_ON_QUIT} (@pxref{Debug,,Debug}), @env{BASHDB_RESTART_FILE} (@pxref{Starting, ,Starting}), toname a few.  Finally, there are some @acronym{BASH} environmentdynamic variables and these start with @env{BASH_}. For example@env{BASH_SUBSHELL} (@pxref{Debug, ,Debug}), @env{BASH_COMMAND}(@pxref{Command Display, ,Command Display}), @env{BASH_LINENO}, and@env{BASH_SOURCE} to name a few.Inside the debugger some variables may be redefined. In particular@code{IFS} and @code{PS4}, and various dollar variables @code{$?},@code{$1}, @code{$2}, etc. The values before entering the debugger aresaved and those variables have their old values restroed when leavingthe debugger. However you may notice these difference in variousdebugger commands. For example @code{examine PS4} might not return thesame value as @code{eval declare -p PS4}. The former is picking the debuggervalue while the @code{eval} is careful to restore the value to whatit was before entering the debugger.In order to do its work @value{dBGP} sets up a @code{DEBUG}trap. Consequently a script shouldn't reset this or the debugger willlose control. @value{dBGP} also sets up an @code{EXIT} handler so thatit can gain control after the script finishes. Another signalintercepted is the an interrupt or @code{INT} signal. For moreinformation about signal handling, @pxref{Signals, ,Signals} @node Debugger Command Reference@chapter BASH Debugger Command Reference

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -