Difference between revisions of "CSC231 DDD Short Tutorial"
m (Thiebaut moved page CSC231 DDD Version 2 to CSC231 DDD Lab Version 2) |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 20:25, 4 April 2017 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 20:25, 4 April 2017 (EDT) | ||
---- | ---- | ||
+ | <meta name="keywords" content="computer science, assembly language, pentium, exercise, machine language, intel" /> | ||
+ | <meta name="description" content="Dominique Thiebaut's Web Page" /> | ||
+ | <meta name="title" content="Dominique Thiebaut -- Computer Science" /> | ||
+ | <meta name="abstract" content="Dominique Thiebaut's Computer Science Web pages" /> | ||
+ | <meta name="author" content="thiebaut at cs.smith.edu" /> | ||
+ | <meta name="distribution" content="Global" /> | ||
+ | <meta name="revisit-after" content="10 days" /> | ||
+ | <meta name="copyright" content="(c) D. Thiebaut 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,2008" /> | ||
+ | <meta name="robots" content="FOLLOW,INDEX" /> | ||
+ | |||
+ | |||
+ | __TOC__ | ||
+ | |||
+ | <br /><br /> | ||
+ | <tanbox> | ||
+ | This tutorial will introduce you to the ddd debugger, and simple steps you can take to debug assembly programs.<br /><br /> | ||
+ | </tanbox> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | |||
+ | <br /> | ||
+ | =The Target Program= | ||
+ | <br /> | ||
+ | Create a simple program that adds the contents of two variables and stores the result in a third one. | ||
+ | |||
+ | The data section should be something like this: | ||
+ | <br /> | ||
+ | ::<source lang="asm"> | ||
+ | section .data | ||
+ | a dd 3 | ||
+ | b dd 5 | ||
+ | sum dd 0 | ||
+ | table dd 6, 7, 8 | ||
+ | |||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | nop ; we put nops to | ||
+ | nop ; set a breakpoint first | ||
+ | |||
+ | mov eax, dword[a] | ||
+ | add eax, dword[b] | ||
+ | mov dword[sum], eax | ||
+ | |||
+ | mov ebx, dword[table] | ||
+ | add ebx, dword[table+4] | ||
+ | mov dword[table+8], ebx | ||
+ | |||
+ | ;;; exit | ||
+ | mov ebx, 0 | ||
+ | mov eax, 1 | ||
+ | int 0x80 | ||
+ | </source> | ||
+ | <br /> | ||
+ | Save your program as '''demoDDD.asm''', and generate the executable as follows: | ||
+ | |||
+ | nasm -f elf '''-F stabs''' demoDDD.asm | ||
+ | ld -melf_i386 -o demoDDD demoDDD.o | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | =Test= | ||
+ | <br /> | ||
+ | Run your program... | ||
+ | |||
+ | Do you see anything? | ||
+ | |||
+ | Should you? | ||
+ | |||
+ | You probably figured out that since we are not printing anything, it's impossible to know if it computed what we expected. | ||
+ | |||
+ | The debugger will allow us to figure it out! | ||
+ | <br /> | ||
+ | =The DDD Debugger= | ||
+ | <br /> | ||
+ | ==Start the Debugger== | ||
+ | <br /> | ||
+ | Start the debugger and instruct it to open the program '''demoDDD'''. At the prompt, type: | ||
+ | |||
+ | ddd demoDDD & | ||
+ | |||
+ | <br /> | ||
+ | The first thing you'll want to do is change a couple default behaviors: | ||
+ | <br /> | ||
+ | ==Configuration== | ||
+ | <br /> | ||
+ | Before we can start using DDD, we need to change some of its default settings. | ||
+ | |||
+ | Select "Edit->Preferences" from the main menu. On the window that will appear, open the "Source" tab (top of the window). Then, check the "Display Source Line Numbers" option. Click OK to close the window. | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | [[Image:ddd_preferences_2017.png|center|600px]] | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | Next, select "Edit->GDB Settings" from the main menu. In the window that pops up, scroll approximately half way down through the list of options, until you find the option "Disassembly flavor". Change its value to "Intel" and click on CLOSE. | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | [[Image:231_ddd_preferences2.png|600px|center]] | ||
+ | |||
+ | <br /> | ||
+ | ==Running the Debugger== | ||
+ | <br /> | ||
+ | # Click on '''Status''', '''Registers''' to see the register window | ||
+ | # Click on '''View''', then '''Machine Code Window''' to see the raw instructions in memory. | ||
+ | # To display the 4 variables, click on '''Data''', then '''Memory''', then select to display 1 hex double-word at location '''&a'''. Same thing for '''&b''' and '''&sum'''. Then similarly for '''&table''', but display 3 hex double-words. | ||
+ | # Put the cursor on the second '''nop''' instruction and set a '''breakpoint''' there by clicking on the breakpoint button (stop sign) in the top menu. | ||
+ | # Run the program by clicking '''Program''' in the top menu, then on the '''Run''' option. A window opens with information about arguments to pass to the program. Just click on the '''Run''' button in this new window to close it. <font color="magenta">The debugger will then execute the first NOP and stop on the second one.</font> | ||
+ | # Single step the rest of the program with '''Stepi''', and observe the registers and the memory change. | ||
+ | <br /> | ||
+ | Your ddd windows should look something like this when you have single-stepped the whole program: | ||
+ | <br /> | ||
+ | [[Image:dddEndExecution.png|750px|center]] | ||
+ | <br /> | ||
+ | |||
+ | =Exercise = | ||
+ | <br /> | ||
+ | Edit your program so that now it computes the sum of 5 variables, a, b, c, d, and e, and saves the sum in result. | ||
+ | |||
+ | Single step your program and verify that it generates the correct answer. | ||
+ | <br /> | ||
+ | =Buggy ddd= | ||
+ | <br /> | ||
+ | <tanbox> | ||
+ | The current ddd on aurora has a tendency to run once, well, and the second time gets stuck. If this happens to you, run the command '''cleanupddd''' which will remove a directory called ".ddd" that ddd creates in your account. This seems to solve the buggy behavior. Unfortunately, you'll have to go back to the settings and modify the line-numbering and the AT&T/Intel switch. | ||
+ | </tanbox> | ||
+ | <br /> | ||
+ | =References= | ||
+ | <br /> | ||
+ | Here is a good reference for learning more advanced ddd features: | ||
+ | * [https://www.gnu.org/software/ddd/manual/pdf/ddd.pdf The GNU manual on ddd], see Section 8 on Machine-Level Debugging. | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | [[Category:CSC231]][[Category:Labs]] |
Latest revision as of 09:59, 5 April 2017
--D. Thiebaut (talk) 20:25, 4 April 2017 (EDT)
<meta name="keywords" content="computer science, assembly language, pentium, exercise, machine language, intel" /> <meta name="description" content="Dominique Thiebaut's Web Page" /> <meta name="title" content="Dominique Thiebaut -- Computer Science" /> <meta name="abstract" content="Dominique Thiebaut's Computer Science Web pages" /> <meta name="author" content="thiebaut at cs.smith.edu" /> <meta name="distribution" content="Global" /> <meta name="revisit-after" content="10 days" /> <meta name="copyright" content="(c) D. Thiebaut 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,2008" /> <meta name="robots" content="FOLLOW,INDEX" />
Contents
This tutorial will introduce you to the ddd debugger, and simple steps you can take to debug assembly programs.
The Target Program
Create a simple program that adds the contents of two variables and stores the result in a third one.
The data section should be something like this:
section .data a dd 3 b dd 5 sum dd 0 table dd 6, 7, 8 section .text global _start _start: nop ; we put nops to nop ; set a breakpoint first mov eax, dword[a] add eax, dword[b] mov dword[sum], eax mov ebx, dword[table] add ebx, dword[table+4] mov dword[table+8], ebx ;;; exit mov ebx, 0 mov eax, 1 int 0x80
Save your program as demoDDD.asm, and generate the executable as follows:
nasm -f elf -F stabs demoDDD.asm ld -melf_i386 -o demoDDD demoDDD.o
Test
Run your program...
Do you see anything?
Should you?
You probably figured out that since we are not printing anything, it's impossible to know if it computed what we expected.
The debugger will allow us to figure it out!
The DDD Debugger
Start the Debugger
Start the debugger and instruct it to open the program demoDDD. At the prompt, type:
ddd demoDDD &
The first thing you'll want to do is change a couple default behaviors:
Configuration
Before we can start using DDD, we need to change some of its default settings.
Select "Edit->Preferences" from the main menu. On the window that will appear, open the "Source" tab (top of the window). Then, check the "Display Source Line Numbers" option. Click OK to close the window.
Next, select "Edit->GDB Settings" from the main menu. In the window that pops up, scroll approximately half way down through the list of options, until you find the option "Disassembly flavor". Change its value to "Intel" and click on CLOSE.
Running the Debugger
- Click on Status, Registers to see the register window
- Click on View, then Machine Code Window to see the raw instructions in memory.
- To display the 4 variables, click on Data, then Memory, then select to display 1 hex double-word at location &a. Same thing for &b and &sum. Then similarly for &table, but display 3 hex double-words.
- Put the cursor on the second nop instruction and set a breakpoint there by clicking on the breakpoint button (stop sign) in the top menu.
- Run the program by clicking Program in the top menu, then on the Run option. A window opens with information about arguments to pass to the program. Just click on the Run button in this new window to close it. The debugger will then execute the first NOP and stop on the second one.
- Single step the rest of the program with Stepi, and observe the registers and the memory change.
Your ddd windows should look something like this when you have single-stepped the whole program:
Exercise
Edit your program so that now it computes the sum of 5 variables, a, b, c, d, and e, and saves the sum in result.
Single step your program and verify that it generates the correct answer.
Buggy ddd
The current ddd on aurora has a tendency to run once, well, and the second time gets stuck. If this happens to you, run the command cleanupddd which will remove a directory called ".ddd" that ddd creates in your account. This seems to solve the buggy behavior. Unfortunately, you'll have to go back to the settings and modify the line-numbering and the AT&T/Intel switch.
References
Here is a good reference for learning more advanced ddd features:
- The GNU manual on ddd, see Section 8 on Machine-Level Debugging.