Due: Thursday, September 22, 2011 CSE 361S Intr t Systems Sftware Lab #2 Intrductin This lab will intrduce yu t the GNU tls in the Linux prgramming envirnment we will be using fr CSE 361S this semester, which includes gcc, as, and gdb. Yu will learn hw t cmpile, assemble, execute, and debug C and assembly language prgrams. Assembling and Executing 1. Lgin and pen a terminal windw. Lgin using yur cec accunt. If yu are in the Windws lab (Lpata 401), bt the Linux virtual machine (VM). Lgin t the VM, again using yur cec accunt. After yu are lgged in t Linux, click n Applicatins > System Tls > Terminal t pen a new terminal shell windw. The terminal windw will default t the hme directry fr yur cec accunt. If yu wish, use the mkdir {name} cmmand t create a sub-directry with the name f yur class (e.g. use the cmmand: mkdir cse361s). Then switch t this directry using the cd {name} cmmand (e.g. cd cse361s). 2. Dwnlad files fr Lab 2. Dwnlad the files needed fr Lab 2 int the current directry (either yur hme directry r the directry yu just created) by ging t the class webpage and rightclicking n the link labeled cse361s_lab2.tar and selecting "Save Link Target As..."). Unpack the files using the Linux cmmand tar xf cse361s_lab2.tar. This will create a directry named lab2 in yur directry. This directry cntains the C and assembly files needed fr this lab. 3. Cmpile/Assemble the Lab 2 prgrams. After untarring the Lab 2 files, use the cd lab2 cmmand t switch t the lab2/ directry. Then cmpile and assemble the prgrams fr Lab 2 using either the make r the make all cmmand (make and make all amunt t the same cmmand since typing make by itself defaults t make all). Nte: make -n will shw yu which 1
cmmands make will execute, withut actually executing them. This can be quite helpful at times. In the prcess f "making" the prgrams, yu will see a listing f the cmmands used t cmpile and assemble the C and assembly prgrams using gcc and as. It is recmmended that yu try executing each ne f these cmmands n yur wn and use ls t check what files are generated as a result f each cmmand. Be sure t use the make clean cmmand t delete all the executables and. files befre executing the "make" cmmands n yur wn. Yu may als want t use the less Makefile cmmand t see hw the Makefile perfrms the cmpilatin/assembly prcess. 4. Execute the C and assembly executables. After "making" the prgrams, try running the C and assembly prgrams using the lab2_cprg and lab2_asmprg executables, respectively. T execute a prgram that resides in the current directry, yu need t tell the shell t lk in the current directry. The current directry is dented with a dt,., s lab2_cprg is executed with the cmmand:./lab2_cprg 5. Debug the C executable, lab2_cprg. T begin debugging the C executable using the gdb debugger, execute the cmmand: gdb lab2_cprg. We will start ff by intrducing a few simple debugging cmmands in gdb. First f all, start running the prgram in gdb. T begin debugging, yu must first begin executing the prgram within gdb. When gdb starts up, the prgram is merely laded; it is nt yet executing. Befre running the prgram, use the break main cmmand t set a break pint at the first instructin in main(). Then type run (r r) t begin running the prgram. Yu will nte that the prgram breaks immediately at the entrance t main(). Use the stepi (r si) cmmand t trace int instructins. The stepi (r si) cmmand traces int each instructin. Effectively, this means that it executes the next single machine/assembly instructin. Fr prcedure r interrupt calls, this means that it executes nly the instructin that transfers cntrl t the prcedure, causing it t jump int that prcedure/interrupt (i.e. it steps 'int' the prcedure/interrupt). 2
Use the si cmmand a cuple times t watch the prgram's prgress as yu step thrugh the cde. When yu get t the functin call fr get_number(), ntice that yu actually "step int" the prcedure s that yu can watch the executin f each individual statement in that prcedure. If yu ever want t re-start frm the beginning f the prgram, simply use the run (r) cmmand again, which will re-start executin f the prgram frm the beginning, stpping immediately at the first breakpint (at main()). Use the nexti (r ni) cmmand t trace ver instructins. The nexti (r ni) cmmand traces ver each instructin. Effectively, this means that it executes the next single instructin in the active prcedure. Fr prcedure r interrupt calls, this means that it executes the full prcedure r interrupt (i.e. it steps 'ver' the prcedure/interrupt). Try stepping thrugh the prgram again, but this time use the ni cmmand instead. Ntice that this time, yu dn't jump int the get_number() functin. Stepping "ver" this functin simply causes it t execute "behind the scenes", s that yu dn't have t watch all the gry details if yu dn't want/need t. Use the step and next cmmands and cmpare them t stepi and nexti. Use the nline help (h) facility t learn abut step and next. Cmpare them with stepi and nexti. Yu will likely find these tw cmmands mre cnvenient when wrking with C cde. Watching variables. Smetimes it is desirable t watch hw the value f a variable changes as yu trace thrugh the prgram. It is pssible t watch variables in this fashin using the display {variable name} cmmand. Fr example, re-start executin frm the beginning f the prgram again, but this time use the cmmand display n t watch the value f variable "n" as yu step thrugh the beginning f the prgram. T see a full listing f all the variables that are currently being watched, use the inf display cmmand. Likewise, if yu want t delete a variable frm being watched, yu can use the cmmand delete display {variable number}. Display memry. Often, yu will want t watch a whle blck f memry as ppsed t a small set f variables. There's n way yu can autmatically have a blck f memry displayed after each step like yu can with variables, but yu can still easily display the cntents f memry with a single cmmand after each step. The cmmand x/{number f byes} {start address} will display the specified number f bytes starting at the specified 3
address (e.g. x/32 0x7f3e9a10 will shw the cntents f memry between addresses 0x7f3e9a10 and 0x7f3e9a2f), inclusive. Furthermre, t prevent yu frm having t retype this cmmand after each step, yu can simply use the up and dwn arrws t scrll thrugh the list f cmmands recently executed in gdb. The primary prblem in displaying memry is that yu must knw the address(es) f the memry lcatins yu which t view. Frtunately, the memry addresses f variables can be easily determined in gdb using the print &{variable name} cmmand (e.g. print &n will give the address fr variable "n"). Display registers. It is als ften useful (mres with assembly debugging than C debugging) t view the values in the register file. Use the inf registers cmmand t d this. View (lcal) prgram cde. While the si and ni cmmands display the next instructin t execute, yu may ften want t see mre cmplete sectins f cde. Yu can display the 10 lines f cde arund the next statement t execute in the debugger using the list (r l) cmmand. Or if yu want t display the 10 lines f cde arund a functin declaratin, yu can use the list {functin name} cmmand (e.g. l main). View assembly cde. An alternate way t view the assembly cde (r when debugging in C and needing t view the equivalent assembly), use the disassemble {label/functin name} (r disas {label/functin name}) cmmand (e.g. disas main). Other debugging functins: Exit the debugger using the quit (r q) cmmand. Cntinue executing a prgram using the cntinue (r c) cmmand. Executin will nly stp after reaching a breakpint, the end f the prgram, r a similar end-f-prgram prcedure/interrupt call. This functin is particularly useful in cnjunctin with breakpints. Als feel free t experiment with ther debugging functins. Yu can use the help (r h) cmmand t find directins fr using ther debugging utilities. Cntinue t trace thrugh lab2_cprg in gdb and examine what each instructin des. Be sure t use si and ni as needed. 6. Debug the assembly executable, lab2_asmprg. 4
T begin debugging the assembly executable using the gdb debugger, execute the cmmand: gdb lab2_asmprg. Prceed t trace thrugh the lab2_asmprg executable just like yu did with the C prgram -- first start by setting a break pint at the main functin, run the prgram, and then prceed t step thrugh fllwing the break pint. Nte that in assembly, any label in the surce cde (e.g. main:, i_lp:, exit_i_lp:, print_lp, etc.) is treated as a functin name, s it wrks best t use si when stepping thrugh the cde, but making sure t use ni n thse assembly functin calls t external functins (e.g. get_number and printf). 7. Questins: Try t answer the fllwing questins. Put yur answers in a file using the same naming cnventin as befre: <lastname><firstinitial>_lab2.txt, and uplad yur file t telesis fr submissin. 1. What number series is prduced by the C prgram? By the assembly prgram? Are the tw prgrams equivalent? 2. What happens when the user enters a value nt in the allwable range? 3. What d the CMP and JAE instructins d? What des JMP d? Hw are they similar? Hw are they different? 4. What are the #define preprcessing directives fr in the C prgram? Is there an equivalent in the assembly prgram (and if s, what)? 5. Yu likely fund the experiences f debugging in C versus debugging in assembly bth smewhat similar and smewhat different. What debugging cmmands did yu find mre useful fr debugging C? Fr debugging assembly? 6. Hw culd yu change the C cde s that it printed ut every 2nd dd number, starting frm 1 (i.e. 1, 5, 9, 13, etc.)? Hw culd yu similarly change the assembly cde (Nte: this is a bit tugher t figure ut...)? 7. Challenge Questin: Hw might yu g abut changing the C cde s that it requested tw numbers, n and x, s that the prgram wuld print ut n numbers including every xth number starting frm 1? Hw abut in the assembly cde (Nte: this is the really tricky part...)? 5