Commit Graph

65 Commits

Author SHA1 Message Date
Alessandro Arzilli
1b0c4310c4
proc: give unique addresses to registerized variables (#2527)
We told clients that further loading of variables can be done by
specifying a type cast using the address of a variable that we
returned.
This does not work for registerized variables (or, in general,
variables that have a complex location expression) because we don't
give them unique addresses and we throw away the compositeMemory object
we made to read them.

This commit changes proc so that:

1. variables with location expression divided in pieces do get a unique
   memory address
2. the compositeMemory object is saved somewhere
3. when an integer is cast back into a pointer type we look through our
   saved compositeMemory objects to see if there is one that covers the
   specified address and use it.

The unique memory addresses we generate have the MSB set to 1, as
specified by the Intel 86x64 manual addresses in this form are reserved
for kernel memory (which we can not read anyway) so we are guaranteed
to never generate a fake memory address that overlaps a real memory
address of the application.

The unfortunate side effect of this is that it will break clients that
do not deserialize the address to a 64bit integer. This practice is
contrary to how we defined our types and contrary to the specification
of the JSON format, as of json.org, however it is also fairly common,
due to javascript itself having only 53bit integers.

We could come up with a new mechanism but then even more old clients
would have to be changed.
2021-07-02 18:37:55 +02:00
Suzy Mueller
30b3cc2c6f
service/dap: implement array, slice, and map paging (#2512)
If the client supports paging, we allow them to fetch array and slice items in chunks that we assume will be of a reasonable size. For example, VS Code requests indexed variables in chunks of 100.

Fixes golang/vscode-go#1518
2021-06-10 09:34:20 -07:00
Alessandro Arzilli
32946b2d7c
proc: correctly truncate the result of binary ops on integers (#2463)
Truncates the result of binary operations on integers to the size of
the resulting type.
Also rewrites convertInt to not require allocations.

Fixes #2454
2021-05-17 10:31:05 -07:00
Alessandro Arzilli
c5d58f494a
proc: add way to use CPU registers in expressions (#2446)
Changes the expression evaluation code so that register names, when not
shadowed by local or global variables, will evaluate to the current
value of the corresponding CPU register.

This allows a greater flexibility with displaying CPU registers than is
possible with using the ListRegisters API call. Also it allows
debuggers users to view register values even if the frontend they are
using does not implement a register view.
2021-05-04 12:56:17 -07:00
Alessandro Arzilli
f19d5e5c13
proc: fix embedded field search (#2320)
Both structMember and findMethod implemented a depth-first search in
embedded fields but the Go specification requires a breadth-first
search. They also allowed promotion of fields in the concrete type of
embedded interfaces even though this is not allowed by Go.
Furthermore they both lacked protection from infinite recursion
when a type embeds itself and the user requests a non-existent field.

Fixes #2316
2021-01-29 09:25:31 -08:00
Alessandro Arzilli
3c86d68a99
proc: remove dead code (#2321) 2021-01-27 06:54:50 -08:00
Andrei Matei
60b5e9d4ba
dwarf/reader: minor comments around variable resolving (#2253)
Fix some stale comments and add some comments that I would have found
useful.
2020-12-08 11:46:40 -08:00
Alessandro Arzilli
0843376018
proc/*: remove proc.Thread.Blocked, refactor memory access (#2206)
On linux we can not read memory if the thread we use to do it is
occupied doing certain system calls. The exact conditions when this
happens have never been clear.

This problem was worked around by using the Blocked method which
recognized the most common circumstances where this would happen.

However this is a hack: Blocked returning true doesn't mean that the
problem will manifest and Blocked returning false doesn't necessarily
mean the problem will not manifest. A side effect of this is issue
#2151 where sometimes we can't read the memory of a thread and find its
associated goroutine.

This commit fixes this problem by always reading memory using a thread
we know to be good for this, specifically the one returned by
ContinueOnce. In particular the changes are as follows:

1. Remove (ProcessInternal).CurrentThread and
(ProcessInternal).SetCurrentThread, the "current thread" becomes a
field of Target, CurrentThread becomes a (*Target) method and
(*Target).SwitchThread basically just sets a field Target.

2. The backends keep track of their own internal idea of what the
current thread is, to use it to read memory, this is the thread they
return from ContinueOnce as trapthread

3. The current thread in the backend and the current thread in Target
only ever get synchronized in two places: when the backend creates a
Target object the currentThread field of Target is initialized with the
backend's current thread and when (*Target).Restart gets called (when a
recording is rewound the currentThread used by Target might not exist
anymore).

4. We remove the MemoryReadWriter interface embedded in Thread and
instead add a Memory method to Process that returns a MemoryReadWriter.
The  backends will return something here that will read memory using
the current thread saved by the backend.

5. The Thread.Blocked method is removed

One possible problem with this change is processes that have threads
with different memory maps. As far as I can determine this could happen
on old versions of linux but this option was removed in linux 2.5.

Fixes #2151
2020-11-09 11:28:40 -08:00
Alessandro Arzilli
7e00666b9f
proc: use argument position for addr only when injecting function calls (#2181)
* proc: use argument position for addr only when injecting function calls

We can not, in general, use the argument position to determine the
address of a formal parameter, it will not work in presence of
optimizations or inlining. In those cases formal arguments could be
stored in registers.

Fixes #2176

* Travis-CI: add ignorechecksum option to chocolatey command

Looks like a configuration problem on chocolatey's end.

Co-authored-by: a <a@kra>
2020-09-24 08:05:24 -07:00
Alessandro Arzilli
12009e9833
proc/*,service: replace uses of uintptr with uint64 (#2163)
Since proc is supposed to work independently from the target
architecture it shouldn't use architecture-dependent types, like
uintptr. For example when reading a 64bit core file on a 32bit
architecture, uintptr will be 32bit but the addresses proc needs to
represent will be 64bit.
2020-09-09 10:36:15 -07:00
aarzilli
f90134eb4d proc: prevent internal breakpoint conditions from failing
An internal breakpoint condition shouldn't ever error:
* use a ThreadContext to evaluate conditions if a goroutine isn't
  available
* evaluate runtime.curg to a fake g variable containing only
  `goid == 0` when there is no current goroutine

Fixes #2113
2020-09-01 15:01:39 +02:00
Alessandro Arzilli
0fa2ac5a9c
proc: set OnlyAddr on variables created by typecast to pointer (#2142)
If OnlyAddr is not set pretty printing an interface will fail with an
index out of bounds error.
2020-08-24 13:37:06 -07:00
Alessandro Arzilli
f9c8f7f55b
Go 1.15 support (#2011)
* proc: start variable visibility one line after their decl line

In most cases variables shouldn't be visible on their declaration line
because they won't be initialized there.
Function arguments are treated as an exception.

This fix is only applied to programs compiled with Go 1.15 or later as
previous versions of Go did not report the correct declaration line for
variables captured by closures.

Fixes #1134

* proc: silence go vet error

* Makefile: enable PIE tests on windows/Go 1.15

* core: support core files for PIEs on windows

* goversion: add Go 1.15 to supported versions

* proc: fix function call injection for Go 1.15

Go 1.15 changed the call injection protocol so that the runtime will
execute the injected call on a different (new) goroutine.

This commit changes the function call support in delve to:

1. correctly track down the call injection state after the runtime
   switches to a different goroutine.
2. correctly perform the escapeCheck when stack values can come from
   multiple goroutine stacks.

* proc: miscellaneous fixed for call injection under macOS with go 1.15

- create copy of SP in debugCallAXCompleteCall case because the code
  used to assume that regs doesn't change
- fix automatic address calculation for function arguments when an
  argument has a spurious DW_OP_piece at entry
2020-07-28 09:19:51 -07:00
Mostafa Sedaghat Joo
3d896ece07
Update eval.go (#2110)
Check val != nil to ignore possible crash.
Related to #2101
2020-07-26 19:24:29 -07:00
chainhelen
cb39564502 pkg/proc: Fix crash when LocationExpr is nil
Add nil check for v.LocationExpr and keep unreadable error of var.

Fix #2049
2020-07-16 15:37:31 +02:00
Alessandro Arzilli
a72723433b
proc: better support for C pointers (#1997)
- treat C pointers as arrays
- print 'char *' variables as strings
2020-06-05 11:22:40 -07:00
aarzilli
c078223d56 proc: use cached packageVars in proc.(*EvalScope).PackageVariables 2020-05-01 11:01:35 -07:00
aarzilli
a61b6c0d7c proc: avoid constructing unnecessary strings when evaluating variables
Avoids constructing:

1. name of runtime.curg fields while executing parseG
2. the location expression while evaluating any variable.

Benchmark before:

BenchmarkConditionalBreakpoints-4   	       1	4953889884 ns/op

Benchmark after:

BenchmarkConditionalBreakpoints-4   	       1	4419775128 ns/op

Updates #1549
2020-03-31 10:29:26 -07:00
Derek Parker
c4fd80fcd0 pkg/proc: Clean up proc.go
This patch moves out unrelated types, variables and functions from
proc.go into a place where they make more sense.
2020-03-24 09:45:29 +01:00
Alessandro Arzilli
7cd12c34fd
proc,dwarf: cache debug.Entry objects (#1931)
Instead of rescanning debug_info every time we want to read a function
(either to find inlined calls or its variables) cache the tree of
dwarf.Entry that we would generate and use that.

Benchmark before:

BenchmarkConditionalBreakpoints-4   	       1	5164689165 ns/op

Benchmark after:

BenchmarkConditionalBreakpoints-4   	       1	4817425836 ns/op

Updates #1549
2020-03-20 10:23:10 -07:00
chainhelen
65d7f5c65f
pkg/proc: Prevent program crash when called meanless expression (#1934)
If we call one expression which is the fake method of meanless
string, `delve` will panic. Strengthen the inspection of boundary
conditions when supporting function calls on non-struct types.

Update: #1871
2020-03-18 09:26:24 -07:00
Derek Parker
4136512ef3 pkg/proc: Allow function calls on non-struct types
Removes the restriction that the DWARF type for the receiver of a method
must be a TypeDef. This seems reasonable in practice, but it turns out
Go DWARF does not consider

```
type X int
```

to be a typedef. This patch also allows for calling a method where the
receiver is not used or passed in, such as:

```
func (_ X) Method() { println("why") }
```
2020-02-20 10:31:48 +01:00
Alessandro Arzilli
151de14d08 proc: support DW_AT_go_package_name (#1757)
Use the name specified by compile unit attribute DW_AT_go_package_name,
introduced in Go 1.13, to map package names to package paths, instead of
trying to deduce it from names of types.
Also use this mapping for resolving global variables and function
expressions.
2019-11-25 09:10:18 -08:00
aarzilli
5a947bceff proc: always resolve array types even if they don't appear in the
program

When evaluating type casts always resolve array types.

Instead of resolving them by looking up the string in debug_info
construct a fake array type so that a type cast to an array type always
works as long as the element type exists.

We already did this for byte arrays, this commit extends this to any
array type. The reason is that we return a fake array type (that
doesn't exist in the target program) for the array of a channel type.

Fixes #1736
2019-10-29 09:04:36 +01:00
chainhelen
a82e6d6987 pkg/proc: fix can not call method of an embedded filed directly (#1691)
`func (v *Variable) findMethod` should support for searching methods of an
embedded filed.

Fixes #1688
2019-09-26 07:37:23 -07:00
Alessandro Arzilli
4905cff3c8 proc: allow calls to optimized functions (#1684)
Trust argument order to determine argument frame layout when calling
functions, this allows calling optimized functions and removes the
special cases for runtime.mallocgc.

Fixes #1589
2019-09-25 10:23:02 -07:00
Alessandro Arzilli
e994047355 proc: correctly mark closure variables as shadowed (#1674)
If a closure captures a variable but also defines a variable of the
same name in its root scope the shadowed flag would, sometimes, not be
appropriately applied to the captured variable.

This change:

1. sorts the variable list by depth *and* declaration line, so that
closure captured variables always appear before other root-scope
variables, regardless of the order used by the compiler

2. marks variable with the same name as shadowed even if there is only
one scope at play.

This fixes the problem but as a side effect:

1. programs compiled with Go prior to version 1.9 will have the
shadowed flag applied arbitrarily (previously the shadowed flag was not
applied at all)
2. programs compiled with Go prior to versoin 1.11 will still exhibit
the bug, as they do not have DeclLine information.

Fixes #1672
2019-09-15 11:40:35 -07:00
Alessandro Arzilli
c441330822 proc: remove (*EvalScope).globalFor (#1658) 2019-08-11 13:56:16 -07:00
Derek Parker
f0a9031969 pkg/proc: Move EvalScope methods, cleanup others
Moves EvalScope methods to the proper file and organizes everything
together. Also makes some EvalScope methods no longer methods and just
pure functions.
2019-08-10 14:03:12 +02:00
Alessandro Arzilli
158fb7bfac proc: increase maximum string length when loading string for binary ops (#1620)
Increases the maximum string length from 64 to 1MB when loading strings
for a binary operator, also delays the loading until it's necessary.

This ensures that comparison between strings will always succeed in
reasonable situations.

Fixes #1615
2019-07-16 13:11:35 -07:00
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
55eed318fd Go 1.13 support (#1546)
* tests: fix tests for Go 1.13

- Go 1.13 doesn't autogenerate init functions anymore, tests that
  expected that now fail and should be skipped.
- Plugin tests now need -gcflags'all=-N -l' now, we were probably
  getting lucky with -gcflags='-N -l' before.

* proc: allow signed integers as shift counts

Go1.13 allows signed integers to be used as the right hand side of a
shift operator, change eval to match.

* goversion: update maximum supported version

* travis: force Go to use vendor directory

Travis scripts get confused by "go: downloading" lines, the exact
reason is not clear. Testing that the vendor directory is up to date is
a good idea anyway.
2019-06-30 10:34:47 -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
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
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
d2904322fa proc: add flag to disable escape checking in function calls
Fix escape checking in function calls  and add a flag to disable it.
2018-10-15 09:31:35 -07:00
chainhelen
143cf6aebf pkg/proc: extend conversion about string for array/str
Extend `string()`
1.convert `byte/rune array`(not only `slice`) to string.
2.convert string to string(itself), just like `string(str)` in go language.
2018-10-15 09:27:39 -07:00
aarzilli
79a0e216ab proc: add iface.(data) syntax to access concrete value of an interface
With this syntax users do not need to type the concrete type of an
interface variable to access its contents. This also sidesteps the
problem where the serialization of a type by go/printer is different
from the one used for debug_info type names.

Updates #1328
2018-09-27 14:09:26 -07:00
aarzilli
50419b61da proc: implement conversion of integers to string
Go allows converting a single integer value to string, resulting in a
string containing a single unicode rune with the same code as the value
of the integer.
Allow the same conversion to happen.

Fixes #1322
2018-09-25 09:52:04 -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
51994aafd3 proc: evaluate var.method expressions
Evaluates var.method expressions into a variable holding the
corresponding method with the receiver variable as a child, in
preparation for extending CallFunction so that it can call methods.
2018-08-16 12:44:02 -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
2a2d1040e9 proc: allow "package/path".varname syntax
If the application being debugged imports two packages with the same
name (but different paths) there was no way to disambiguate the two,
since the character '/' can not appear inside a go identifier.

By allowing users to use a string literal as the package name a package
path can be specified.
2018-06-14 09:29:23 -07:00
aarzilli
3381404604 pkg/proc: respect specified load configuration after reslicing a map
Maps were always loaded with using the default configuration during a
reslice. This is probably a remnant from when we didn't let clients
configure the load parameters.
2018-06-08 13:20:54 -07:00
aarzilli
7fd47749ef proc: Flag shadowed arguments as shadowed
Fixes #951
2018-04-23 10:13:21 -07:00
aarzilli
b9c4a1d68c proc: Short circuit evaluation of && and || like go does
Change evaluation of binary operators so that both && and || only
evaluate their second argument conditionally, like go does.
2018-04-13 15:34:03 -07:00
aarzilli
a708d00e78 proc/eval: strings of different length are never equal 2018-04-10 14:46:31 -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
Josh Soref
1d3b41f64e all: Spelling 2018-03-20 11:05:35 +01:00