Merge commit '3b57743bbc75144e5edae7a82f516d7ee55b21a5' into ja
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
During the development process of any application, developers will always need to perform some kind of code debugging. PHP, Python, and most of the other dynamic languages, are able to be modified at runtime, as long as the modifications do not explicitly need to be compiled. We can easily print data in dynamic operating environments, outputting our changes and printing variable information directly. In Go, you can of course speckle your code with `Println`s before-hand to display variable information for debugging purposes, but any changes to your code need to be recompiled every time. This can quickly become cumbersome. If you've programmed in Python or Javascript, you'll know that the former provides tools such as pdb and ipdb for debugging, and the latter has similar tools that are able to dynamically display variable information and facilitate single-step debugging. Fortunately, Go has native support for a similar tool which provides such debugging features: GDB. This section serves as a brief introduction into debugging Go applications using GDB.
|
||||
|
||||
## GDB debugging Profile
|
||||
## GDB debugging profile
|
||||
|
||||
GDB is a powerful debugging tool targeting UNIX-like systems, released by the FSF (Free Software Foundation). GDB allows us to do the following things:
|
||||
|
||||
@@ -93,15 +93,15 @@ Abbreviated as `n`, `next` is used in single-step debugging to skip to the next
|
||||
|
||||
- continue
|
||||
|
||||
Abbreviated as `c`, `command` is used to jump out of the current break point and can be followed by a parameter N, which specifies the number of times to skip the break point
|
||||
Abbreviated as `c`, `continue` is used to jump out of the current break point and can be followed by a parameter N, which specifies the number of times to skip the break point
|
||||
|
||||
- set variable
|
||||
|
||||
This command is used to change the value of a variable in the process. It can be used like so: `set variable <var> = <value>`
|
||||
|
||||
## Debugging process
|
||||
## The debugging process
|
||||
|
||||
We use the following code to demonstrate how this GDB to debug Go program, the following is the code that will be demonstrated :
|
||||
Now, let's take a look at the following code to see how GDB is typically used to debug Go programs:
|
||||
|
||||
|
||||
package main
|
||||
@@ -131,15 +131,15 @@ We use the following code to demonstrate how this GDB to debug Go program, the f
|
||||
}
|
||||
|
||||
|
||||
Compiled file, an executable file gdbfile:
|
||||
Now we compile the file, creating an executable file called "gdbfile":
|
||||
|
||||
go build -gcflags "-N -l" gdbfile.go
|
||||
|
||||
By GDB command to start debugging :
|
||||
Use the GDB command to start debugging :
|
||||
|
||||
gdb gdbfile
|
||||
|
||||
After the first start is not possible to look at this program up and running, just enter the `run` command carriage return after the program starts to run, the program correctly, then you can see the program output is as follows, and we execute the program directly from the command line output is the same:
|
||||
After first starting GDB, you'll have to enter the `run` command to see your program running. You will then see the program output the following; executing the program directly from the command line will output exactly the same thing:
|
||||
|
||||
(gdb) run
|
||||
Starting program: /home/xiemengjun/gdbfile
|
||||
@@ -157,7 +157,7 @@ After the first start is not possible to look at this program up and running, ju
|
||||
[LWP 2771 exited]
|
||||
[Inferior 1 (process 2771) exited normally]
|
||||
|
||||
Well, now we know how to make the program run up, then began to give the code to set a break point :
|
||||
Ok, now that we know how to get the program up and running, let's take a look at setting breakpoints:
|
||||
|
||||
(gdb) b 23
|
||||
Breakpoint 1 at 0x400d8d: file /home/xiemengjun/gdbfile.go, line 23.
|
||||
@@ -171,7 +171,7 @@ Well, now we know how to make the program run up, then began to give the code to
|
||||
23 fmt.Println("count:", count)
|
||||
|
||||
|
||||
The above example shows the `b 23` set a break point on line 23, then enter `run` start the program running. The program now in place to set a break point in front stopped, we need to look at the context of the break point corresponding source code, enter `list` you can see the display from the current source line before stopping five starts :
|
||||
In the above example, we use the `b 23` command to set a break point on line 23 of our code, then enter `run` to start the program. When our program stops at our breakpoint, we typically need to look at the corresponding source code context. Entering the `list` command into our GDB session, we can see the five lines of code preceding our breakpoint:
|
||||
|
||||
(gdb) list
|
||||
18 fmt.Println(msg)
|
||||
@@ -183,7 +183,7 @@ The above example shows the `b 23` set a break point on line 23, then enter `run
|
||||
24 }
|
||||
25 }
|
||||
|
||||
GDB now running the current program environment has retained some useful debugging information, we just print out the corresponding variables, see the corresponding variable types and values:
|
||||
Now that GDB is running the current program environment, we have access to some useful debugging information that we can print out. To see the corresponding variable types and values, type `info locals`:
|
||||
|
||||
(gdb) info locals
|
||||
count = 0
|
||||
@@ -195,7 +195,7 @@ GDB now running the current program environment has retained some useful debuggi
|
||||
(gdb) whatis bus
|
||||
type = chan int
|
||||
|
||||
Then let the program continue down the execution, please read the following command:
|
||||
To let the program continue its execution until the next breakpoint, enter the `c` command:
|
||||
|
||||
(gdb) c
|
||||
Continuing.
|
||||
@@ -214,9 +214,9 @@ Then let the program continue down the execution, please read the following comm
|
||||
23 fmt.Println("count:", count)
|
||||
|
||||
|
||||
After each entry `c` code will be executed once, and jump to the next for loop, continue to print out the appropriate information.
|
||||
After each `c`, the code will execute once then jump to the next iteration of the `for` loop. It will, of course, continue to print out the appropriate information.
|
||||
|
||||
Assume that current need to change the context variables, skipping the process and continue to the next step, the modified desired results obtained :
|
||||
Let's say that you need to change the context variables in the current execution environment, skip the process then continue to the next step. You can do so by first using `info locals` to get the variable states, then the `set variable` command to modify them:
|
||||
|
||||
(gdb) info locals
|
||||
count = 2
|
||||
@@ -233,7 +233,7 @@ Assume that current need to change the context variables, skipping the process a
|
||||
Breakpoint 1, main.main () at /home/xiemengjun/gdbfile.go:23
|
||||
23 fmt.Println("count:", count)
|
||||
|
||||
Finally a little thought, in front of the entire program is running in the process in the end created a number goroutine, each goroutine are doing :
|
||||
Finally, while running, the program creates a number of number goroutines. We can see what each goroutine is doing using `info goroutines`:
|
||||
|
||||
(gdb) info goroutines
|
||||
* 1 running runtime.gosched
|
||||
@@ -251,7 +251,7 @@ Finally a little thought, in front of the entire program is running in the proce
|
||||
#5 0x000000000040d16a in schedunlock () at /home/xiemengjun/go/src/pkg/runtime/proc.c:267
|
||||
#6 0x0000000000000000 in ?? ()
|
||||
|
||||
Commands by viewing goroutines we can clearly understand goruntine performed internally how each function call sequence has plainly shown.
|
||||
From the `goroutines` command, we can have a better picture of what Go's runtime system is doing internally; the calling sequence for each function is plainly displayed.
|
||||
|
||||
## Summary
|
||||
|
||||
|
||||
Reference in New Issue
Block a user