Commit Graph

74 Commits

Author SHA1 Message Date
Alessandro Arzilli
dd4fd5dc9c proc: allow simultaneous call injection to multiple goroutines (#1591)
* proc: allow simultaneous call injection to multiple goroutines

Changes the call injection code so that we can have multiple call
injections going on at the same time as long as they happen on distinct
goroutines.

* proc: fix EvalExpressionWithCalls for constant expressions

The lack of address of constant expressions would confuse EvalExpressionWithCalls

Fixes #1577
2019-06-30 10:44:30 -07:00
Alessandro Arzilli
79ad269bbb proc: support setting string values when it requires an allocation (#1548)
Allow changing the value of a string variable to a new literal string,
which requires calling runtime.mallocgc to allocate the string into the
target process.

This means that a command like:

    call f("some string")

is now supported.

Additionally the command:

    call s = "some string"

is also supported.

Fixes #826
2019-06-17 09:51:29 -07:00
Alessandro Arzilli
2d6d016bf1 proc: fix panic when calling Ancestors on a parked goroutine (#1570)
Fixes #1568
2019-06-03 10:41:33 -07:00
Alessandro Arzilli
60830c2b1d More Function Calls, parts 2 (#1504)
* proc: support nested function calls

Changes the code in fncall.go to support nested function calls.

This changes delays argument evaluation until after we have used
the call injection protocol to allocate an argument frame. When
evaluating the parse tree of an expression we'll initiate each
function call we find on the way down and then complete the function
call on the way up.

For example. in:

	f(g(x))

we will:

1. initiate the call injection protocol for f(...)
2. progress it until the point where we have space for the arguments
   of 'f' (i.e. when we receive the debugCallAXCompleteCall message
   from the target runtime)
3. inititate the call injection protocol for g(...)
4. progress it until the point where we have space for the arguments
   of 'g'
5. copy the value of x into the argument frame of 'g'
6. finish the call to g(...)
7. copy the return value of g(x) into the argument frame of 'f'
8. finish the call to f(...)

Updates #119

* proc: bugfix: closure addr was wrong for non-closure functions
2019-05-30 08:08:37 -07:00
Alessandro Arzilli
04834a781a proc: save location expr and decl line for escaped variables (#1553)
Copy the location expression and declaration line of escaped variables
when auto-dereferencing them.
2019-05-23 13:07:13 -07:00
Alessandro Arzilli
c30a333f7b proc: allow function calls to appear inside an expression (#1503)
The initial implementation of the 'call' command required the
function call to be the root expression, i.e. something like:

	double(3) + 1

was not allowed, because the root expression was the binary operator
'+', not the function call.

With this change expressions like the one above and others are
allowed.

This is the first step necessary to implement nested function calls
(where the result of a function call is used as argument to another
function call).

This is implemented by replacing proc.CallFunction with
proc.EvalExpressionWithCalls. EvalExpressionWithCalls will run
proc.(*EvalScope).EvalExpression in a different goroutine. This
goroutine, the 'eval' goroutine, will communicate with the main
goroutine of the debugger by means of two channels: continueRequest
and continueCompleted.

The eval goroutine evaluates the expression recursively, when
a function call is encountered it takes care of setting up the
function call on the target program and writes a request to the
continueRequest channel, this causes the 'main' goroutine to restart
the target program by calling proc.Continue.

Whenever Continue encounters a breakpoint that belongs to the
function call injection protocol (runtime.debugCallV1 and associated
functions) it writes to continueCompleted which resumes the 'eval'
goroutine.

The 'eval' goroutine takes care of implementing the function call
injection protocol.

When the expression is fully evaluated the 'eval' goroutine will
write a special message to 'continueRequest' signaling that the
expression evaluation is terminated which will cause Continue to
return to the user.

Updates #119
2019-05-09 08:29:58 -07:00
Alessandro Arzilli
f3b149bda7 proc: support debugging plugins (#1414)
This change splits the BinaryInfo object into a slice of Image objects
containing information about the base executable and each loaded shared
library (note: go plugins are shared libraries).

Delve backens are supposed to call BinaryInfo.AddImage whenever they
detect that a new shared library has been loaded.

Member fields of BinaryInfo that are used to speed up access to dwarf
(Functions, packageVars, consts, etc...) remain part of BinaryInfo and
are updated to reference the correct image object. This simplifies this
change.

This approach has a few shortcomings:

1. Multiple shared libraries can define functions or globals with the
   same name and we have no way to disambiguate between them.

2. We don't have a way to handle library unloading.

Both of those affect C shared libraries much more than they affect go
plugins. Go plugins can't be unloaded at all and a lot of name
collisions are prevented by import paths.

There's only one problem that is concerning: if two plugins both import
the same package they will end up with multiple definition for the same
function.
For example if two plugins use fmt.Printf the final in-memory image
(and therefore our BinaryInfo object) will end up with two copies of
fmt.Printf at different memory addresses. If a user types
  break fmt.Printf
a breakpoint should be created at *both* locations.
Allowing this is a relatively complex change that should be done in a
different PR than this.

For this reason I consider this approach an acceptable and sustainable
stopgap.

Updates #865
2019-05-08 14:06:38 -07:00
Alessandro Arzilli
2cadddd787 proc: Update map reading code for Go 1.12 (#1532)
Go 1.12 introduced a change to the internal map representation where
empty map cells can be marked with a tophash value of 1 instead of just
0.

Fixes #1531
2019-04-26 10:23:43 -07:00
aarzilli
9826531597 proc,debugger,terminal: read goroutine ancestors
Add options to the stack command to read the goroutine ancestors.
Ancestor tracking was added to Go 1.12 with CL:
https://go-review.googlesource.com/c/go/+/70993/

Implements #1491
2019-03-28 13:55:32 +01:00
Derek Parker
4c9a72e486 *: Update import name to github.com/go-delve/delve
The repository is being switched from the personal account
github.com/derekparker/delve to the organization account
github.com/go-delve/delve. This patch updates imports and docs, while
preserving things which should not be changed such as my name in the
CHANGELOG and in TODO comments.
2019-01-04 19:43:13 +01:00
aarzilli
34e802a42b proc: make structMember work on pointer Variables created through cast
When casting an integer into a struct pointer we make a fake pointer
variable that doesn't have an address, maybeDereference and
structMember should still work on this kind of Variable.

Fixes #1432
2018-12-03 10:00:22 -08:00
aarzilli
53957e9f4d proc: do not panic if we can't satisfy a composite location for a slice var
The fix in 7f53117 for Issue #1416 had a bug, fix it and add a test.

Fixes #1419
2018-11-26 11:05:00 -08:00
aarzilli
7f53117ea9 proc: do not panic if we can't satisfy a composite location
When a location expression requests a register check that we have as
many bytes in the register as requested and if we don't report the
error.

Updates #1416
2018-11-19 14:46:35 -08:00
aarzilli
89c8da65b6 proc: Improve performance of loadMap on very large sparse maps
Users can create sparse maps in two ways, either by:
a) adding lots of entries to a map and then deleting most of them, or
b) using the make(mapType, N) expression with a very large N

When this happens reading the resulting map will be very slow
because loadMap needs to scan many buckets for each entry it finds.

Technically this is not a bug, the user just created a map that's
very sparse and therefore very slow to read. However it's very
annoying to have the debugger hang for several seconds when trying
to read the local variables just because one of them (which you
might not even be interested into) happens to be a very sparse map.

There is an easy mitigation to this problem: not reading any
additional buckets once we know that we have already read all
entries of the map, or as many entries as we need to fulfill the
MaxArrayValues parameter.

Unfortunately this is mostly useless, a VLSM (Very Large Sparse Map)
with a single entry will still be slow to access, because the single
entry in the map could easily end up in the last bucket.

The obvious solution to this problem is to set a limit to the
number of buckets we read when loading a map. However there is no
good way to set this limit.
If we hardcode it there will be no way to print maps that are beyond
whatever limit we pick.
We could let users (or clients) specify it but the meaning of such
knob would be arcane and they would have no way of picking a good
value (because there is no objectively good value for it).

The solution used in this commit is to set an arbirtray limit on
the number of buckets we read but only when loadMap is invoked
through API calls ListLocalVars and ListFunctionArgs. In this way
`ListLocalVars` and `ListFunctionArgs` (which are often invoked
automatically by GUI clients) remain fast even in presence of a
VLSM, but the contents of the VLSM can still be inspected using
`EvalVariable`.
2018-11-09 08:12:45 -08:00
Derek Parker
3129aa7330 *: Show return values on CLI trace
This patch allows the `trace` CLI subcommand to display return values of
a function. Additionally, it will also display information on where the
function exited, which could also be helpful in determining the path
taken during function execution.

Fixes #388
2018-10-19 20:32:27 +02:00
aarzilli
74c98bc961 proc: support position independent executables (PIE)
Support for position independent executables (PIE) on the native linux
backend, the gdbserver backend on linux and the core backend.
Also implemented in the windows native backend, but it can't be tested
because go doesn't support PIE on windows yet.
2018-10-11 11:21:27 -07:00
aarzilli
a2eb983e3c proc: be more lenient with errors in GoroutinesInfo
Instead of failing on the first goroutine we can't read save the error
message and keep going.

Fixes a bug reported on the mailing list:
https://groups.google.com/d/msgid/delve-dev/3b3bfaa3-83d5-4676-b974-1fec40e5bf53%40googlegroups.com?utm_medium=email&utm_source=footer
2018-09-25 08:44:13 -07:00
Derek Parker
c3f50742b9 *: Misc refactors, and doc additions
Refactors some code, adds a bunch of docstrings and just generally fixes
a bunch of linter complaints.
2018-09-19 20:59:35 +02:00
aarzilli
0461af8392 proc: fix type of some struct global variables
Normally variables that have a named struct as a type will get a
typedef entry as their type, sometimes however the Go linker will
decide to use the DW_TAG_structure_type entry instead.

For consistency always wrap a struct type into a typedef when we are
creating a new variables (see comment in newVariable for exceptions).

This fixes a bug where it would be impossible to call methods on a
global variable.
2018-08-29 16:16:20 -07:00
aarzilli
19ba86c0c9 proc: support calls through function pointers 2018-08-16 12:44:02 -07:00
aarzilli
7c42fc51d7 proc: support calls to methods directly and through interface 2018-08-16 12:44:02 -07:00
aarzilli
9335c54014 proc: use (*Variable).setValue in fncall 2018-08-15 10:29:16 -07:00
aarzilli
12a3f8bb97 proc: change (*Variable).setValue for use in CallFunction
Changes (*Variable).setValue so that it can be used in CallFunction to
set up the argument frame for the function call, adding the ability to:
- nil nillable types
- set strings to the empty string
- copy from one structure to another (including strings and slices)
- convert any interface type to interface{}
- convert pointer shaped types (map, chan, pointers, and structs
  consisting of a single pointer field) to interface{}

This covers all cases where an assignment statement can be evaluated
without allocating memory or calling functions in the target process.
2018-08-15 10:29:16 -07:00
aarzilli
8f1fc63da8 proc,service,terminal: read defer list
Adds -defer flag to the stack command that decorates the stack traces
by associating each stack frame with its deferred calls.

Reworks proc.next to use this feature instead of using proc.DeferPC,
laying the groundwork to implement #1240.
2018-07-24 14:58:56 -07:00
aarzilli
2925c0310a *: function call injection for go 1.11
Implements the function call injection protocol introduced in go 1.11
by https://go-review.googlesource.com/c/go/+/109699.

This is only the basic support, see TODO comments in pkg/proc/fncall.go
for a list of missing features.

Updates #119
2018-07-13 13:37:54 -07:00
aarzilli
2309f728c1 proc,service,terminal: propagate g.startpc
Adds StartPC to proc.G, StartLoc to api.Goroutine and show the start
location in the command line client when appropriate.

Updates #1104
2018-07-09 17:20:55 -07:00
aarzilli
5cd86b1587 proc,service: export declaration line of variables
Adds a field, DeclLine, to Variable containing the declaration line
number of the variable.
2018-06-28 12:29:26 -07:00
aarzilli
9464c23740 proc: in go1.11 runtime.g.waitreason is not a string 2018-06-11 11:09:02 -07:00
aarzilli
8bbccb3e66 proc: use new extended attribute to resolve concrete type of interfaces
go1.11 adds a new extended attribute to all type DIEs containing the
address of the corresponding runtime._type struct, use this attribute
to find the DIE of the concrete type of interface variables when
available.
2018-06-11 11:09:02 -07:00
aarzilli
49d1206b94 proc: do not assume g.gopc is valid
Fixes #1203
2018-05-09 12:33:41 -07:00
aarzilli
7fd47749ef proc: Flag shadowed arguments as shadowed
Fixes #951
2018-04-23 10:13:21 -07:00
aarzilli
21be59469a proc: cache entire frame in FrameToScope instead of variablesByTag
Caching the frame in variablesByTag is problematic:

1. accounting for variables that are (partially) stored in registers is
complicated (see issue #1106)
2. for some types (strings, interfaces...) simply creating the Variable
object reads memory, which therefore happens before we can do any
caching.

Instead cache the entire frame when the EvalScope object is created.
The cached range is between the SP value of the current frame and the
CFA of the preceeding frame, if available, or the CFA of the current
frame otherwise.

Fixes #1106
2018-04-23 10:13:21 -07:00
aarzilli
290e8e7528 proc: support inlining
Go 1.10 added inlined calls to debug_info, this commit adds support
for DW_TAG_inlined_call to delve, both for stack traces (where
inlined calls will appear as normal stack frames) and to correct
the behavior of next, step and stepout.

The calls to Next and Frame of stackIterator continue to work
unchanged and only return real stack frames, after reading each line
appendInlinedCalls is called to unpacked all the inlined calls that
involve the current PC.

The fake stack frames produced by appendInlinedCalls are
distinguished from real stack frames by having the Inlined attribute
set to true. Also their Current and Call locations are treated
differently. The Call location will be changed to represent the
position inside the inlined call, while the Current location will
always reference the real stack frame. This is done because:

* next, step and stepout need to access the debug_info entry of
the real function they are stepping through
* we are already manipulating Call in different ways while Current
is just what we read from the call stack

The strategy remains mostly the same, we disassemble the function
and we set a breakpoint on each instruction corresponding to a
different file:line. The function in question will be the one
corresponding to the first real (i.e. non-inlined) stack frame.

* If the current function contains inlined calls, 'next' will not
set any breakpoints on instructions that belong to inlined calls. We
do not do this for 'step'.

* If we are inside an inlined call that makes other inlined
functions, 'next' will not set any breakpoints that belong to
inlined calls that are children of the current inlined call.

* If the current function is inlined the breakpoint on the return
address won't be set, because inlined frames don't have a return
address.

* The code we use for stepout doesn't work at all if we are inside
an inlined call, instead we call 'next' but instruct it to remove
all PCs belonging to the current inlined call.
2018-03-26 14:30:38 -04:00
aarzilli
ec8dc3a10d proc,vendor: show global variables in disassembly
updates vendored version of x86asm, adds a symbol lookup function to
pass to the disassembler.

This will show global symbol names in the disassembly like go tool
objdump does.
2018-03-22 10:01:00 -07:00
aarzilli
84ce278352 proc: allow evaluating constants specified with a partial package path
Fixes #1151
2018-03-20 09:46:35 -07:00
aarzilli
cd5203e305 proc: fix reading of empty strings in core files
Every time we read an empty string we accidentally issue a read for 0
bytes at address 0, this is fine for real memory but the core file
reader doesn't like it.

Fixes an issue reported on the mailing list.
2018-03-08 11:58:03 -08:00
Derek Parker
4da05d62cd pkg/proc: Fix reporting of 'go' statement (#1136)
Fixes a bug where the 'go' statement that created a goroutine was
incorrectly reported.

Fixes #1135
2018-03-05 10:40:42 +01:00
Alessandro Arzilli
0c40a8f52a dwarf/reader,proc: support DW_AT_abstract_origin (#1111)
debug_info entries can use DW_AT_abstract_origin to inherit the
attributes of another entry, supporting this attribute is necessary to
support DW_TAG_inlined_subroutine.

Go, starting with 1.10, emits DW_TAG_inlined_subroutine entries when
inlining is enabled.
2018-02-13 09:20:45 -08:00
aarzilli
62fe792bfd proc: disable caching for variables with an extended location
Our current frame caching strategy doesn't handle extended locations
expressions correctly, disable it on variables that don't have a simple
address.
2018-01-31 06:39:44 -08:00
aarzilli
74d330a013 proc: Remove unused code 2018-01-26 12:58:21 -08:00
Alessandro Arzilli
bec6a65b15
proc,prettyprint: guard against autodereferenced escaped pointers (#1077)
Much like the bug in issue #1031 and commit
f6f6f0bf13e4c708cb501202b83a6327a0f00e31 pointers can also escape to
the heap and then have a zero address (and no children) when we
autodereference.

1. Mark autodereferenced escaped variables with a 0 address as
   unreadable.
2. Add guards to the pretty printers for unsafe.Pointer and pointers.

Fixes #1075
2018-01-19 15:50:28 +01:00
aarzilli
1758f8523a pkg/terminal: print DWARF location expression with whatis
Adds a configuration option (show-location-expr) that when activated
will cause the whatis command to also print the DWARF location
expression for a variable.
2017-12-20 16:34:47 -08:00
aarzilli
8b4392dc46 pkg/proc: use constants to describe variable value 2017-12-13 12:18:18 -08:00
aarzilli
85669434f6 pkg/proc: use DW_AT_decl_line to determine var visibility
Fixes #186, #83
2017-12-13 12:18:18 -08:00
aarzilli
5372588c61 proc: support cgo stacktraces
When creating a stack trace we should switch between the goroutine
stack and the system stack (where cgo code is executed) as appropriate
to reconstruct the logical stacktrace.

Goroutines that are currently executing on the system stack will have
the SystemStack flag set, frames of the goroutine stack will have a
negative FrameOffset (like always) and frames of the system stack will
have a positive FrameOffset (which is actually just the CFA value for
the frame).

Updates #935
2017-11-28 11:00:53 -08:00
aarzilli
99cad1044b pkg/proc, pkg/dwarf/op: support DW_OP_piece, DW_OP_regX, DW_OP_fbreg
These are emitted by C compilers but also by the current development
version of the go compiler with the dwarflocationlists flag.
2017-11-21 11:51:02 -08:00
aarzilli
f098915192 proc/tests: testing apparatus for complex location expressions 2017-11-21 11:51:02 -08:00
aarzilli
5f0f77f414 proc: automatically dereference interfaces on member access
If 'iv' is an interface variable with a struct as a concrete value let
'iv.A' evaluate to the access to field 'A' of the concrete value of
'iv'.
2017-11-20 12:03:35 -08:00
aarzilli
844762a853 proc: support access to chan buffers
Replace the unsafe.Pointer type of the buf field of channels with the
appropriate array type, allow expressions accessing member field of the
channel struct.

Fixes #962
2017-11-20 12:03:35 -08:00
aarzilli
f4e2000fc8 proc: refactor stack.go to use DWARF registers
Instead of only tracking a few cherrypicked registers in stack.go track
all DWARF registers.

This is needed for cgo code and for the locationlists emitted by go in
1.10:
* The debug_frame sections emitted by C compilers can not be used
  without tracking all registers
* the loclists emitted by go1.10 need all registers of a frame to be
  interpreted.
2017-11-17 10:17:24 -08:00