?? nesc-debugging.html
字號(hào):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <title>Debugging nesC code with gdb</title> <meta name="author" content="David Gay"></head><body><h1>Debugging nesC code in GDB</h1>gdb does not (yet!) have a nesC-specific mode. Instead, in gdb you areeffectively debugging the C code generated by the nesC compiler.However, the nesC compiler includes #line directives in the code itgenerates, so single-stepping through nesC code will display the correctnesC source code, and breakpoints can be set based on the line numbersand file names of nesC components. The situation for variable,function, command and event names is however not as straightforward. Ifyou wish to refer to one of these, you must use its name in thegenerated C code, as explained below.<br><br>When debugging tossim code, life is further complicated by the factthat the generated C code emulates multiple motes. Thus all modulevariables (but not global variables in C files) become arrays, indexedby mote id (if the variable was itself an array, the mote id is thefirst dimension). At any point in time, the "current" mote is found in <span style="font-family: monospace;">tos_state.current_node</span>.<br><br>By default, nesC does a lot of inlining, which makes debugging tricky.In most cases, pass the -<span style="font-family: monospace;">g -O1-fnesc-no-inline</span> options to nesC to produce code that is easierto debug (the <span style="font-family: monospace;">debug</span> optionto the standard TinyOS Makefile does this). If you have a problem whichrequires debugging fully optimised code, just include <span style="font-family: monospace;">-g</span> and prepare for a slightlypainful experience... (the <span style="font-family: monospace;">debugopt</span>to the standard TinyOS Makefile does this).<br><h2>Mapping from nesC names to C names</h2>Types, variables and functions in C files (included via the <span style="font-family: monospace;">includes</span> statement) are leftunchanged in the generated C code, except if they correspond to a nesCkeyword. In this last case, the name is prefixed with <span style="font-family: monospace;">__nesc_keyword_</span>, so componentsbecomes <span style="font-family: monospace;">__nesc_keyword_components</span>.<br><br>A module variable (top-level data declarations in modules) <span style="font-style: italic;">X </span>in module <span style="font-style: italic;">M</span> is called <span style="font-style: italic;">M</span>$<span style="font-style: italic;">X</span>in the generated C code.<br><br>A function <span style="font-style: italic;">F</span> in module <span style="font-style: italic;">M</span> is called <span style="font-style: italic;">M</span>$<span style="font-style: italic;">F</span>in the generated C code.<br><br>Local variable names in modules are left unchanged in the generated Ccode.<br><br>A command or event <span style="font-style: italic;">C</span> in module <span style="font-style: italic;">M</span> is called <span style="font-style: italic;">M</span>$<span style="font-style: italic;">C</span>in the generated C code.<br><br>A command or event <span style="font-style: italic;">C</span> ofinterface instance <span style="font-style: italic;">I</span> in module <span style="font-style: italic;">M</span> is called <span style="font-style: italic;">M</span>$<span style="font-style: italic;">I</span>$<span style="font-style: italic;">C</span> in the generated C code.<br><br>To complicate matters a little, gdb does not directly accept $ infunction names in <span style="font-family: monospace;">break</span>(set a breakpoint) statements. Instead, you must precede the functionname with a *, e.g, <span style="font-family: monospace;">b*BlinkM$StdControl$init</span>. Note that this sets a breakpoint on thefirst instruction of the function (normally part of the functionpreamble setting up the function's stack frame) rather than on the firstexecutable statement of the function. As a result, gdb may notcorrectly display argument values, etc, until you single-step into thefunction body.<br><h2>Example</h2>This example debugs the CntToLeds application from TinyOS on a micamote, using on-chip debugging with a JTAG ICE pod (see <a href="debugging.html">this document</a> for instructions on setting theJTAG ICE up):<br><br>We compile the application with debugging and no inlining by passingthe TinyOS-specific <span style="font-family: monospace;">debug</span>option to make:<br><div style="margin-left: 40px;"><span style="font-family: monospace;">[dgay@barnowlCntToLeds]$ make mica debug</span><br style="font-family: monospace;"><span style="font-family: monospace;"> compilingCntToLeds to a mica binary</span><br style="font-family: monospace;"><span style="font-family: monospace;">ncc -o build/mica/main.exe -O1 -g-fnesc-no-inline -board=micasb -target=mica -I%T/lib/Counters -Wall-Wshadow -DDEF_TOS_AM_GROUP=0x42 -Wnesc-all -finline-limit=100000-fnesc-cfile=build/mica/app.c CntToLeds.nc -lm</span><br style="font-family: monospace;"><span style="font-family: monospace;"> compiledCntToLeds to build/mica/main.exe</span><br style="font-family: monospace;"><span style="font-family: monospace;"> 2588 bytes in ROM</span><br style="font-family: monospace;"><span style="font-family: monospace;"> 46 bytes in RAM</span><br style="font-family: monospace;"><span style="font-family: monospace;">avr-objcopy --output-target=srecbuild/mica/main.exe build/mica/main.srec</span><br><span style="font-family: monospace;"></span></div><br>We start ice-gdb to download and debug CntToLeds:<br><div style="margin-left: 40px;"><span style="font-family: monospace;">[dgay@barnowlCntToLeds]$ ice-gdb build/mica/main.exe</span><br style="font-family: monospace;"><span style="font-family: monospace;">AVaRICE version 2.0.20030821cvs,Aug 21 2003 15:36:04</span><br style="font-family: monospace;"><span style="font-family: monospace;"> </span><br style="font-family: monospace;"><span style="font-family: monospace;">JTAG config starting.</span><br style="font-family: monospace;"><span style="font-family: monospace;">Hardware Version: 0xc0</span><br style="font-family: monospace;"><span style="font-family: monospace;">Software Version: 0x69</span><br style="font-family: monospace;"><span style="font-family: monospace;">Reported JTAG device ID: 0x9702</span><br style="font-family: monospace;"><span style="font-family: monospace;">Configured for device ID: 0x9702atmega128</span><br style="font-family: monospace;"><span style="font-family: monospace;">LockBits -> 0xff</span><br style="font-family: monospace;"><span style="font-family: monospace;"> </span><br style="font-family: monospace;"><span style="font-family: monospace;">Reading Fuse Bytes:</span><br style="font-family: monospace;"><span style="font-family: monospace;"> Extended Fuse byte ->0xfd</span><br style="font-family: monospace;"><span style="font-family: monospace;"> High Fuse byte -> 0x19</span><br style="font-family: monospace;"><span style="font-family: monospace;"> Low Fuse byte -> 0xfe</span><br style="font-family: monospace;"><span style="font-family: monospace;">JTAG config complete.</span><br style="font-family: monospace;"><span style="font-family: monospace;">Downloading FLASH image totarget......................</span><br style="font-family: monospace;"><span style="font-family: monospace;"> </span><br style="font-family: monospace;"><span style="font-family: monospace;">Download complete.</span><br style="font-family: monospace;"><span style="font-family: monospace;">Waiting for connection on port6423.</span><br style="font-family: monospace;"><span style="font-family: monospace;">GNU gdb cvs-pre6.0-tinyos</span><br style="font-family: monospace;"><span style="font-family: monospace;">Copyright 2003 Free SoftwareFoundation, Inc.</span><br style="font-family: monospace;"><span style="font-family: monospace;">GDB is free software, covered bythe GNU General Public License, and you are</span><br style="font-family: monospace;"><span style="font-family: monospace;">welcome to change it and/ordistribute copies of it under certain conditions.</span><br style="font-family: monospace;"><span style="font-family: monospace;">Type "show copying" to see theconditions.</span><br style="font-family: monospace;"><span style="font-family: monospace;">There is absolutely no warrantyfor GDB. Type "show warranty" for details.</span><br style="font-family: monospace;"><span style="font-family: monospace;">This GDB was configured as"--host=i686-pc-linux-gnu --target=avr"...</span><br style="font-family: monospace;"><span style="font-family: monospace;">Connection opened by host127.0.0.1, port 33805.</span><br style="font-family: monospace;"><span style="font-family: monospace;">0x00000000 in __vectors ()</span><br><span style="font-family: monospace;"></span></div><span style="font-family: monospace;"><br></span>The program is stopped. We set a breakpoint at the Timer.firedevent in the Counter module. Note the *:<span style="font-family: monospace;"><br style="font-family: monospace;"></span><div style="margin-left: 40px;"><span style="font-family: monospace;">(gdb)b *Counter$Timer$fired</span><br style="font-family: monospace;"><span style="font-family: monospace;">Hardware assisted breakpoint 1 at0x826: file /home/dgay/motes/tinyos-1.x/tos/lib/Counters/Counter.nc,line 67.</span><br><span style="font-family: monospace;"></span></div><span style="font-family: monospace;"><br></span>And let the program continue:<br><div style="margin-left: 40px;"><span style="font-family: monospace;"></span><span style="font-family: monospace;">(gdb) c</span><br style="font-family: monospace;"><span style="font-family: monospace;">Continuing.</span><br style="font-family: monospace;"><span style="font-family: monospace;"> </span><br style="font-family: monospace;"><span style="font-family: monospace;">Breakpoint 1, Counter$Timer$fired() at /home/dgay/motes/tinyos-1.x/tos/lib/Counters/Counter.nc:67</span><br style="font-family: monospace;"><span style="font-family: monospace;">67 state++;</span><br><span style="font-family: monospace;"></span></div><span style="font-family: monospace;"><br></span>The breakpont was reached. We continue again...<span style="font-family: monospace;"><br style="font-family: monospace;"></span><div style="margin-left: 40px;"><span style="font-family: monospace;">(gdb)c</span><br style="font-family: monospace;"><span style="font-family: monospace;">Continuing.</span><br style="font-family: monospace;"><span style="font-family: monospace;"> </span><br style="font-family: monospace;"><span style="font-family: monospace;">Breakpoint 1, Counter$Timer$fired() at /home/dgay/motes/tinyos-1.x/tos/lib/Counters/Counter.nc:67</span><br style="font-family: monospace;"><span style="font-family: monospace;">67 state++;</span><br><span style="font-family: monospace;"></span></div><span style="font-family: monospace;"><br></span>Let's examine the state variable of the Counter module:<span style="font-family: monospace;"><br style="font-family: monospace;"></span><div style="margin-left: 40px;"><span style="font-family: monospace;">(gdb)p Counter$state</span><br style="font-family: monospace;"><span style="font-family: monospace;">$1 = 1</span><br><span style="font-family: monospace;"></span></div><span style="font-family: monospace;"><br></span>And then follow where the IntOutput.output command takes us.<span style="font-family: monospace;"><br style="font-family: monospace;"></span><div style="margin-left: 40px;"><span style="font-family: monospace;">(gdb)n</span><br style="font-family: monospace;"><span style="font-family: monospace;">68 return call IntOutput.output(state);</span><br style="font-family: monospace;"><span style="font-family: monospace;">(gdb) s</span><br style="font-family: monospace;"><span style="font-family: monospace;">Counter$IntOutput$output(arg_0x84fde28=2) at/home/dgay/motes/tinyos-1.x/tos/interfaces/IntOutput.nc:52</span><br style="font-family: monospace;"><span style="font-family: monospace;">52 command result_t output(uint16_t value);</span><br><span style="font-family: monospace;"></span></div><span style="font-family: monospace;"><br></span>The debugger shows us the command in the interface, we must stepagain to reach our destination:<span style="font-family: monospace;"><br style="font-family: monospace;"></span><div style="margin-left: 40px;"><span style="font-family: monospace;">(gdb)s</span><br style="font-family: monospace;"><span style="font-family: monospace;">IntToLedsM$IntOutput$output(value=2) at/home/dgay/motes/tinyos-1.x/tos/lib/Counters/IntToLedsM.nc:70</span><br style="font-family: monospace;"><span style="font-family: monospace;">70 if (value & 1) call Leds.redOn();</span><br><span style="font-family: monospace;"></span></div><span style="font-family: monospace;"><br></span>The names of local variables (and function parameters) areunchanged:<span style="font-family: monospace;"><br style="font-family: monospace;"></span><div style="margin-left: 40px;"><span style="font-family: monospace;">(gdb)p value</span><br style="font-family: monospace;"><span style="font-family: monospace;">$2 = 2</span><br><span style="font-family: monospace;"></span></div><span style="font-family: monospace;"><br></span>That's all folks!<span style="font-family: monospace;"><br style="font-family: monospace;"></span><div style="margin-left: 40px;"><span style="font-family: monospace;">(gdb)quit</span><br style="font-family: monospace;"></div><br></body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -