Step 1: Create App.go file like below.
App.go
package main import ( "fmt" ) func main() { name := "Krishna" city := "Bangalore" age := 29 fmt.Println("name : ", name) fmt.Println("city : ", city) fmt.Println("age : ", age) }
Step 2: Open terminal or command prompt and
execute ‘dlv debug App.go’.
$ dlv debug App.go Type 'help' for list of commands. (dlv)
As you see
the output, it redirects you to (dlv) prompt, execute the command help, to list
all the commands of dlv.
(dlv) help The following commands are available: args ------------------------ Print function arguments. break (alias: b) ------------ Sets a breakpoint. breakpoints (alias: bp) ----- Print out info for active breakpoints. call ------------------------ Resumes process, injecting a function call (EXPERIMENTAL!!!) clear ----------------------- Deletes breakpoint. clearall -------------------- Deletes multiple breakpoints. condition (alias: cond) ----- Set breakpoint condition. config ---------------------- Changes configuration parameters. continue (alias: c) --------- Run until breakpoint or program termination. deferred -------------------- Executes command in the context of a deferred call. disassemble (alias: disass) - Disassembler. down ------------------------ Move the current frame down. edit (alias: ed) ------------ Open where you are in $DELVE_EDITOR or $EDITOR exit (alias: quit | q) ------ Exit the debugger. frame ----------------------- Set the current frame, or execute command on a different frame. funcs ----------------------- Print list of functions. goroutine ------------------- Shows or changes current goroutine goroutines ------------------ List program goroutines. help (alias: h) ------------- Prints the help message. list (alias: ls | l) -------- Show source code. locals ---------------------- Print local variables. next (alias: n) ------------- Step over to next source line. on -------------------------- Executes a command when a breakpoint is hit. print (alias: p) ------------ Evaluate an expression. regs ------------------------ Print contents of CPU registers. restart (alias: r) ---------- Restart process. set ------------------------- Changes the value of a variable. source ---------------------- Executes a file containing a list of delve commands sources --------------------- Print list of source files. stack (alias: bt) ----------- Print stack trace. step (alias: s) ------------- Single step through program. step-instruction (alias: si) Single step a single cpu instruction. stepout --------------------- Step out of the current function. thread (alias: tr) ---------- Switch to the specified thread. threads --------------------- Print out info for every traced thread. trace (alias: t) ------------ Set tracepoint. types ----------------------- Print list of types up -------------------------- Move the current frame up. vars ------------------------ Print package variables. whatis ---------------------- Prints type of an expression. Type help followed by a command for full documentation.
Step 3: Add break points to the application.
‘break
App.go:8’ set the debug point at line 8 in App.go file
(dlv) break App.go:8 Breakpoint 1 set at 0x10b0942 for main.main() ./App.go:8 (dlv) break App.go:9 Breakpoint 2 set at 0x10b0957 for main.main() ./App.go:9 (dlv) break App.go:10 Breakpoint 3 set at 0x10b096c for main.main() ./App.go:10
Step 4: Execute the command ‘continue’.
(dlv) continue > main.main() ./App.go:8 (hits goroutine(1):1 total:1) (PC: 0x10b0942) 3: import ( 4: "fmt" 5: ) 6: 7: func main() { => 8: name := "Krishna" 9: city := "Bangalore" 10: age := 29 11: 12: fmt.Println("name : ", name) 13: fmt.Println("city : ", city)
As you see
=> symbol, it indicates that the control stops at line 8. Execute the
command to continue again, so control will go to line 9 and initialize the
variable name.
Execute
the command ‘locals’ to see the local variable details.
(dlv) continue > main.main() ./App.go:9 (hits goroutine(1):1 total:1) (PC: 0x10b0957) 4: "fmt" 5: ) 6: 7: func main() { 8: name := "Krishna" => 9: city := "Bangalore" 10: age := 29 11: 12: fmt.Println("name : ", name) 13: fmt.Println("city : ", city) 14: fmt.Println("age : ", age) (dlv) locals city = (unreadable could not read string at 0x8 due to protocol error E08 during memory read for packet $m8,10) name = "Krishna"
As you see
the output, name is initialized with value "Krishna", where as city
is not yet initialized.
Execute
the command ‘continue’ two times, Since there are no breakpoints after this,
application will get terminated by printing the output to console.
(dlv) continue > main.main() ./App.go:10 (hits goroutine(1):1 total:1) (PC: 0x10b096c) 5: ) 6: 7: func main() { 8: name := "Krishna" 9: city := "Bangalore" => 10: age := 29 11: 12: fmt.Println("name : ", name) 13: fmt.Println("city : ", city) 14: fmt.Println("age : ", age) 15: } (dlv) continue name : Krishna city : Bangalore age : 29 Process 14998 has exited with status 0
No comments:
Post a Comment