Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Uwaga na buga! GDB w służbie programisty. Barcamp Semihalf S09:E01

262 views

Published on

Prezentacja z barcampu firmy Semihalf (S09:E01). Zapis wideo dostępny jest na naszym kanale YouTube: https://youtu.be/jAp-bi-3ZiE
Omawiamy najpopularniejsze polecenia GDB, zwracając uwagę na często pomijane niuanse pozwalające ułatwić i przyspieszyć proces debugowania. Wyjaśniamy jak automatyzować i przyspieszać pracę w programie. Prezentujemy niektóre rozszerzenia. Opowiadamy o wstecznym wykonywaniu kodu oraz o możliwościach GDB, które bywają zapominane, a w konkretnych sytuacjach są niezastąpione.

Published in: Software
  • Login to see the comments

  • Be the first to like this

Uwaga na buga! GDB w służbie programisty. Barcamp Semihalf S09:E01

  1. 1. GDB w służbie programisty Łukasz Majczak Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  2. 2. O czym będziemy rozmawiać ● Platforma: linux, x86-64 ● Podstawowe przydatne komendy ● Wbudowane zmienne i funkcje ● Automatyzacja pracy ● Aplikacji wielowątkowe Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  3. 3. Podstawowe komendy Jak zacząć ? ● gdb + file + run ● gdb <program> + run ● gdb attach <program> Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  4. 4. Podstawowe komendy break ... if cond [thread thread-id] Set a breakpoint with condition cond; evaluate the expression cond each time the breakpoint is reached, and stop only if the value is nonzero--that is, if cond evaluates as true. `...' stands for one of the possible arguments: function, +offset, -offset, linenum, filename:linenum, filename:function, *address. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  5. 5. Podstawowe komendy break ... if cond [thread thread-id] commands silent printf “my_var is %dn”,my_var set my_var = my_var + 1 continue end Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  6. 6. Podstawowe komendy [r|a|-]watch [-l|-location] expr [thread thread-id] [mask maskvalue] Set a watchpoint for an expression. GDB will break when the expression expr is written into by the program and its value changes. If the command includes a [thread thread-id] argument, GDB breaks only when the thread identified by thread-id changes the value of expr.Ordinarily a watchpoint respects the scope of variables in expr (see below). The -location argument tells GDB to instead watch the memory referred to by expr. In this case, GDB will evaluate expr, take the address of the result, and watch the memory at that address. The type of the result is used to determine the size of the watched memory. If the expression’s result does not have an address, then GDB will print an error. The [mask maskvalue] argument allows creation of masked watchpoints, if the current architecture supports this feature A masked watchpoint specifies a mask in addition to an address to watch. The mask specifies that some bits of an address should be ignored when matching the address accessed by the inferior against the watchpoint address. Thus, a masked watchpoint watches many addresses simultaneously.The mask argument implies -location. Examples: (gdb) watch foo mask 0xffff00ff (gdb) watch *0xdeadbeef mask 0xffffff00 Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  7. 7. Podstawowe komendy catch event Stop when event occurs. The event can be any of the following: ● throw, ● rethrow, ● catch, ● exec, ● syscall, ● fork, ● vfork, ● load, ● unload, ● signal Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  8. 8. X x [Address expression] x /[Format] [Address expression] x /[Length][Format] [Address expression] Format: o,x,d,u,t,f,a,c,s,i followed by:b,h,w,g print print expr print /f expr Format: o,x,d,u,t,a,c,f Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. Podstawowe komendy
  9. 9. explore explore arg arg is either an expression (in the source language), or a type visible in the current context of the program being debugged. whatis whatis expr Print the data type of expression expr. expr is not actually evaluated, and any side-effecting operations (such as assignments or function calls) inside it do not take place Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. Podstawowe komendy
  10. 10. Podstawowe komendy set var ... To alter the value of a variable, evaluate an assignment expression. Example: set var my_var = 48 set {int}0xdeadbeef = 48 Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  11. 11. backtrace This command will print one line per frame for frames in the stack. By default, all stack frames are printed. frame When used without any argument, this command does not change which frame is selected, but prints a brief description of the currently selected stack frame. It can be abbreviated f. With an argument, this command is used to select a stack frame. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. Podstawowe komendy
  12. 12. step step [count] Continue running your program until control reaches a different source line, then stop it and return control to GDB. This command is abbreviated s. next next [count] Continue to the next source line in the current (innermost) stack frame. This is similar to step, but function calls that appear within the line of code are executed without stopping. Execution stops when control reaches a different line of code at the original stack level that was executing when you gave the next command. This command is abbreviated n. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. Podstawowe komendy
  13. 13. Podstawowe komendy ● continue, until, finish ● info thread / reg / break ● disassemble ● jump ● call, return Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  14. 14. Wbudowane zmienne GDB Convenience Variables GDB provides convenience variables that you can use within GDB to hold on to a value and refer to it later. These variables exist entirely within GDB; they are not part of your program, and setting a convenience variable has no direct effect on further execution of your program. That is why you can use them freely. Convenience variables are prefixed with ‘$’. Any name preceded by ‘$’ can be used for a convenience variable, unless it is one of the predefined machine-specific register names. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  15. 15. Wbudowane funkcje GDB Convenience Functions GDB also supplies some convenience functions. These have a syntax similar to convenience variables. A convenience function can be used in an expression just like an ordinary function; however, a convenience function is implemented internally to GDB. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  16. 16. Wbudowane funkcje GDB Convenience Functions ● $_isvoid (expr) ● $_memeq(buf1, buf2, length) ● $_regex(str, regex) ● $_streq(str1, str2) ● $_strlen(str) ● $_caller_is(name[, number_of_frames]) ● $_caller_matches(regexp[, number_of_frames]) ● $_any_caller_is(name[, number_of_frames]) ● $_any_caller_matches(regexp[, number_of_frames]) ● $_as_string(value) Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  17. 17. Automatyzacja A user-defined command is a sequence of GDB commands to which you assign a new name as a command. This is done with the define command. User commands may accept up to 10 arguments separated by whitespace. Arguments are accessed within the user command via $arg0...$arg9. A trivial example: define adder print $arg0 + $arg1 + $arg2 To execute the command use: adder 1 2 3 Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  18. 18. Automatyzacja define commandname ... end Define a command named commandname. If there is already a command by that name, you are asked to confirm that you want to redefine it. The definition of the command is made up of other GDB command lines, which are given following the define command. The end of these commands is marked by a line containing end. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  19. 19. Automatyzacja if arg Takes a single argument, which is an expression to evaluate. It is followed by a series of commands that are executed only if the expression is true (nonzero). There can then optionally be a line else, followed by a series of commands that are only executed if the expression was false. The end of the list is marked by a line containing end. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  20. 20. Automatyzacja while arg The syntax is similar to if: the command takes a single argument, which is an expression to evaluate, and must be followed by the commands to execute, one per line, terminated by an end. The commands are executed repeatedly as long as the expression evaluates to true. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  21. 21. Automatyzacja document commandname Document the user-defined command commandname, so that it can be accessed by help. The command commandname must already be defined. This command reads lines of documentation just as define reads the lines of the command definition, ending with end. After the document command is finished, help on command commandname displays the documentation you have written. You may use the document command again to change the documentation of a command. Redefining the command with define does not change the documentation. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  22. 22. Automatyzacja show user commandname Display the GDB commands used to define commandname (but not its documentation). If no commandname is given, display the definitions for all user-defined commands. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  23. 23. Automatyzacja Example (gdb) show user User command "example": set $i=0 while $i<16 print tab[$i++] end (gdb) Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.
  24. 24. All-stop vs non-stop All-stop In all-stop mode, whenever your program stops under GDB for any reason, all threads of execution stop, not just the current thread. This allows you to examine the overall state of the program, including switching between threads, without worrying that things may change underfoot. Conversely, whenever you restart the program, all threads start executing. This is true even when single-stepping with commands like step or next. Non-stop For some multi-threaded targets, GDB supports an optional mode of operation in which you can examine stopped program threads in the debugger while other threads continue to execute freely. This minimizes intrusion when debugging live systems, such as programs where some threads have real-time constraints or must continue to respond to external events. This is referred to as non-stop mode. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. Aplikacje wielowątkowe
  25. 25. Background Execution GDB’s execution commands have two variants: the normal foreground (synchronous) behavior, and a background (asynchronous) behavior. In foreground execution, GDB waits for the program to report that some thread has stopped before prompting for another command. In background execution, GDB immediately gives a command prompt so that you can issue other commands while your program runs. If the target doesn’t support async mode, GDB issues an error message if you attempt to use the background execution commands. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. Aplikacje wielowątkowe
  26. 26. Background Execution To specify background execution, add a & to the command. For example, the background form of the continue command is continue&, or just c&. The execution commands that accept background execution are: run, attach, step, stepi, next, nexti, continue, finish, until Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. Aplikacje wielowątkowe
  27. 27. Background Execution You can interrupt your program while it is running in the background by using the interrupt command. interrupt interrupt -a Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. Aplikacje wielowątkowe
  28. 28. Reverse execute Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. When you are debugging a program, it is not unusual to realize that you have gone too far, and some event of interest has already happened. If the target environment supports it, GDB can allow you to “rewind” the program by running it backward. A target environment that supports reverse execution should be able to “undo” the changes in machine state that have taken place as the program was executing normally. Variables, registers etc. should revert to their previous values. Obviously this requires a great deal of sophistication on the part of the target environment; not all target environments can support reverse execution.
  29. 29. Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information. ● reverse-continue ('rc') -- Continue program being debugged but run it in reverse ● reverse-finish -- Execute backward until just before the selected stack frame is called ● reverse-next ('rn') -- Step program backward, proceeding through subroutine calls. ● reverse-nexti ('rni') -- Step backward one instruction, but proceed through called subroutines. ● reverse-step ('rs') -- Step program backward until it reaches the beginning of a previous source line ● reverse-stepi -- Step backward exactly one instruction ● set exec-direction (forward/reverse) -- Set direction of execution. ● All subsequent execution commands (continue, step, until etc.) will run the program being debugged in the selected direction. Reverse execute
  30. 30. Dziękuję za uwagę Copyright © 2018 Semihalf. All rights reserved. Confidential and proprietary information.

×