/
Debugger Presented by  李明璋 Debugger Presented by  李明璋

Debugger Presented by 李明璋 - PowerPoint Presentation

ariel
ariel . @ariel
Follow
66 views
Uploaded On 2023-07-14

Debugger Presented by 李明璋 - PPT Presentation

20120508 The Definition of Bug Part of the code which would result in an error fault or malfunctioning of the program Common Bugs Bugs with pointers and memory Memory leaks The allocated memory is not freed subsequently ID: 1009088

str debugger char gdb debugger str gdb char code int set main variable return printf program common blocks scanf

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Debugger Presented by 李明璋" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. DebuggerPresented by 李明璋2012/05/08

2. The Definition of BugPart of the code which would result in an error, fault or malfunctioning of the program.

3. Common BugsBugs with pointers and memoryMemory leaksThe allocated memory is not freed subsequently.Free the already freed resourceint main(){ char* str = ( char* ) malloc( sizeof(char) * 10 ); if ( global == 0 ) free( str ); /* Doing Something here … */ free ( str ); return 0;}

4. Common BugsBugs with pointers and memoryMemory leaksThe allocated memory is not freed subsequently.Free the already freed resourceNULL-pointer dereferencingImproper initializationint main(){ char* str = NULL; strcpy( str, “def” ); printf( “%s”, str ); return 0;}int main(){ char* str = “abc”; strcpy( str, “def” ); printf( “%s”, str ); return 0;}

5. Common BugsError of scanf()int main(){ int num; char* str = (char*)malloc(sizeof(char) * SIZE); scanf("%d", &num); // require to pass address to scanf() scanf("%s", &str); // Not need & here, str points to variable itself printf("%d %s\n", num, str); return 0;}

6. Common BugsUsing ‘=’ instead of ‘==’int main(){ int i; int a = 0; for(i = 0; i < 10; i++) a += i; if(a = 0) a = 1000; printf("%d", a); return 0;}Output: 0

7. Common BugsLoop errorint x = 5; while(x > 0); x--;Infinite loopint main() { int a = 0; while(a < 10){ printf("%d\n", a); if (a = 5) printf("a equals 5!\n"); a++; } return 0; } Infinite loop

8. DebuggerA debugger is a computer program used to test and debug other programs.The code to be examined might alternatively be running on an instruction set simulator (ISS), a technique that allows great power in its ability to halt when specific conditions are encountered.

9. Debugger (GDB)GDB – GNU DebuggerCompiler your program with ‘-g’ for debugginggcc -g [your program] -o [executable file]

10. Debugger (GDB)Start GDBgdbgdb [executable file]GDB window

11. Debugger (GDB)Load your program on GDBfile [executable file]Run your programstart (開始執行程式,並停留在main入口處)run (開始執行程式,直到遭遇break point)Stop at the beginning of ‘main’

12. Debugger (GDB)Set breakpointsbreak [location]break [function]Stop at the line 43The command “list” is used to list part of the code of your program.

13. Debugger (GDB)Set breakpointsbreak [function / location] if [condition]Stop at the function ‘InsertSorted’ if (head->data < new->data)

14. Debugger (GDB)Print the value of some variablesprint [variable]Continue executing your programcontinue (c)Continue executingPrint the value of variable ‘new->data’, and the value is 10.

15. Debugger (GDB)Set watchpointswatch [variable]The value of variable ‘head’ has been changed.

16. Debugger (GDB)Set value of variableset [variable] = [value]Set the value of new->data as 1.

17. Debugger (GDB)List breakpoints and watchpointslist breakDelete breakpoint or watchpointdelete [point number]The list of all breakpoints and watchpoints.Delete the watchpoint which Num is 4.

18. Debugger (Code Blocks)Settings → Compiler and debugger…

19. Debugger (Code Blocks)Add breakpoint

20. Debugger (Code Blocks)Open debugging window

21. Debugger (Code Blocks)Start debuggingWatching window (the state of all variables)Press to start debugging

22. ReferencesGDB manualhttp://sourceware.org/gdb/current/onlinedocs/gdbCode::Blocks Debugger manualhttp://ez2learn.com/index.php/c-tutorials/codeblocks-tutorials/206-codeblocks-debuggerDev C++ Debugger manualhttp://ez2learn.com/index.php/c-tutorials/dev-c-/203-dev-cdebugger