Commit Graph

51 Commits

Author SHA1 Message Date
aarzilli
7bec20e5fc proc: avoid scanning system stack if it's not executing cgo
The runtime calls into g0 in many places, not necessarily using
runtime.systemstack or runtime.asmcgocall.
One example of this is the call to runtime.newstack inside
runtime.morestack.

If we stop the process while one goroutine is executing
runtime.newstack we would be unable to fully scan its stack because we
don't know that we have to switch back to the goroutine stack after
runtime.newstack.

Instead of tracking down every possible way that the runtime switches
to g0 we switch to the goroutine stack immediately after the top of the
stack, unless cgo is being executed on the systemstack.

Fixes #1066
2018-01-05 10:29:31 -08:00
aarzilli
07c716818e proc/test: miscellaneous test changes for go1.10 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
b3246296d7 dwarf/line: handle DW_LNE_end_of_sequence correctly
We need to reset the current file and line number.

Fixes #1008
2017-12-08 02:16:26 -08:00
aarzilli
77c955365f proc: handle DW_TAG_subprogram with a nochildren abbrev
On macOS, externally linked programs will have an abbrev for
DW_TAG_subprogram without the haschildren flag set. We should handle
this case instead of expecting all DW_TAG_subprogram entries to have
list of children.

Fixes #1034
2017-12-07 15:00: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
5fdcd2c91a cmd, proc/test: disable optimizations on the C compiler
Pass CGO_FLAGS='-O0 -g' to go build to disable optimizations when
calling the C compiler.
2017-11-28 11:00:53 -08:00
aarzilli
1ced7c3a60 proc: next should not skip lines with conditional bps
Conditional breakpoints with unmet conditions would cause next and step
to skip the line.

This breakpoint changes the Kind field of proc.Breakpoint from a single
value to a bit field, each breakpoint object can represent
simultaneously a user breakpoint and one internal breakpoint (of which
we have several different kinds).

The breakpoint condition for internal breakpoints is stored in the new
internalCond field of proc.Breakpoint so that it will not conflict with
user specified conditions.

The breakpoint setting code is changed to allow overlapping one
internal breakpoint on a user breakpoint, or a user breakpoint on an
existing internal breakpoint. All other combinations are rejected. The
breakpoint clearing code is changed to clear the UserBreakpoint bit and
only remove the phisical breakpoint if no other bits are set in the
Kind field. ClearInternalBreakpoints does the same thing but clearing
all bits that aren't the UserBreakpoint bit.

Fixes #844
2017-11-20 11:25:35 -08:00
aarzilli
178589a4e7 proc: breakpoints refactoring
Move some duplicate code, related to breakpoints, that was in both
backends into a single place.
This is in preparation to solve issue #844 (conditional breakpoints
make step and next fail) which will make this common breakpoint code
more complicated.
2017-11-20 11:25: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
aarzilli
5c9b2009ca proc: change next to skip deferred functions
Make 'next' skip deferred functions unless they are called via a panic.
Call to a deferred function through 'return' are predictable, if the
user wants to step into them 'step' can be used but without this change
there is no way to avoid stepping into them.

Implements #956
2017-09-25 12:46:25 -07:00
aarzilli
2c0e31160d proc/gdbserial: return error if stopped with a sginal
On macOS we can also stop when we receive a signal,
propagate this reason upwards to the client.

Also clear internal breakpoints after an unrecovered-panic since they
can not be reached anymore.

Fixes #872
2017-09-20 12:39:29 -07:00
aarzilli
9ee21686e6 proc: report errors when loading executable on attach
Fixes #940
2017-08-30 11:20:20 -07:00
aarzilli
f553c95eeb proc/tests: fix intermittent failure of TestNextParked
Always pick a goroutine that we know will have to be resumed before the
program terminates instead of relying on luck.

Fixes #803
2017-08-14 13:24:30 -06:00
aarzilli
2ad9ce6fe3 proc: lexical block support
Fixes #106
2017-08-01 11:20:25 -06:00
aarzilli
1128c26b87 cmd/dlv: do not pass "linkmode internal" for windows on go1.9 and later
go1.9 no longer needs "linkmode internal" on windows.

Fixes #755
Fixes #477
Fixes #631
2017-08-01 11:20:25 -06:00
aarzilli
a17de32c23 proc/variables: support embedded struct fields on go1.9
Before go1.9 embedded struct fields had name == "" in runtime and ==
type name in DWARF. After go1.9 both runtime and DWARF use a simplified
version of the type as name.
Embedded structs are distinguished from normal fields by setting a flag
in the runtime.structfield, for runtime, and by adding a custom
attribute in DWARF.
2017-08-01 11:20:25 -06:00
aarzilli
038fd33999 proc_test: line numbering changed 2017-08-01 11:20:25 -06:00
aarzilli
731829c349 proc: auto-dereference local variables that escape to the heap
The compiler a variable 'v' that escapes to the heap with a '&v' entry.
Auto dereference those local variables.

Fixe #871
2017-08-01 11:20:25 -06:00
aarzilli
2d9a9a76eb proc: fix next when current function is unknown on macOS
Updates #893
2017-07-26 12:50:09 -06:00
aarzilli
5c2673a632 proc/native, proc/gdbserial: StepInstruction without goroutine
proc.Process.StepInstruction should work even if there is no goroutine
selected.
2017-07-26 12:50:09 -06:00
Florin Pățan
135330cbb2 Pass LD_/DYLD_ env vars to debugserver (fixes #877) (#910) 2017-07-18 12:57:41 -06:00
Florin Pățan
32a005de2b Fix various issues detected by megacheck (#880)
* Fix various issues detected by megacheck

I've ran honnef.co/go/tools/cmd/megacheck and fixed a few of the
things that came up there.

* Cleanup using Gogland
2017-06-29 11:15:59 -07:00
aarzilli
16d8bd647f proc/*: remove Process.Running
Implementing proc.Process.Running in a thread safe way is complicated
and nothing actually uses it besides tests, so we are better off
rewriting the tests without Running and removing it.

In particular:

* The call to d.target.Running() in service/debugger/debugger.go
  (Restart) can never return true because that line executes while
  holding processMutex and all continue operations are also executed
  while holding processMutex.
* The call to dbp.Running() pkg/proc/native/proc.go (Detach) can never
  return true, because it's only called from
  debugger.(*Debugger).detach() which is also always called while
  holding processMutex.

Since some tests are hard to write correctly without Process.Running a
simpler interface, Process.NotifyResumed, is introduced.

Fixes #830
2017-06-13 08:53:54 +02:00
aarzilli
40b482130a proc/variables: protect form invalid G struct in parseG
A waitreason string that has invalid length (because the G struct is
corrupted or being modified) could cause a crash.
2017-05-25 21:04:09 +02:00
Alessandro Arzilli
354055836a proc: next, stepout should work on recursive goroutines (#831)
Before this commit our temp breakpoints only checked that we would stay
on the same goroutine.
However this isn't enough for recursive functions we must check that we
stay on the same goroutine AND on the same stack frame (or, in the case
of the StepOut breakpoint, the previous stack frame).

This commit:
1. adds a new synthetic variable runtime.frameoff that returns the
   offset of the current frame from the base of the call stack.
   This is similar to runtime.curg
2. Changes the condition used for breakpoints on the lines of the
   current function to check that runtime.frameoff hasn't changed.
3. Changes the condition used for breakpoints on the return address to
   check that runtime.frameoff corresponds to the previous frame in the
   stack.
4. All other temporary breakpoints (the step-into breakpoints and defer
   breakpoints) remain unchanged.

Fixes #828
2017-05-16 11:23:33 -07:00
Alessandro Arzilli
8d3e74a445 proc/gdbserial: fix passing arguments to target via debugserver (#843)
Debugserver does not work as documented, "--" needs to be specified to
pass arguments to the target process (but only if it's an argument that
starts with a dash).

Fixes #839
2017-05-15 10:11:46 -07:00
Alessandro Arzilli
a843f7944e proc/gdbserial: mozilla rr support (#804)
Implements #727
2017-05-05 15:17:52 -07:00
Alessandro Arzilli
1f1535802e proc/native: Detach should use Kill with child processes we want killed (#822)
While implementing the gdbserial backend everything was changed to call
Detach to "close" a process so that gdbserial could do its clean up in
a single place. However the native implementation of Detach does not
actually kill processes we launched.

Fixes #821
2017-05-05 10:04:32 -07:00
Alessandro Arzilli
f29b9edad2 proc/gdbserial: pass environment variables to target (#820)
debugserver requires a special option to forward the environment to the target.

Fixes #818
2017-05-04 10:46:45 -07:00
Alessandro Arzilli
a731eb661f Minor post-refactoring cleanup (#808)
* proc/native: remove unused utility methods

* proc: turn FindFileLocation, FindFunctionLocation, FirstPCAfterPrologue methods into function
2017-04-28 10:15:39 -07:00
Alessandro Arzilli
d9bd90d7e4 Pull Request #731 but with tests (#802)
* proc/eval: fix length calculation for string concatenation

* proc/variable: find package variables when the package has a path
2017-04-25 10:42:41 -07:00
Nathan Bruer
43f26fb4d6 pkg/proc: Fixed delve's version extraction to allow propsals (#798)
Go recently introduced proposal tags to their version tags, we
are simply allowing delve to handle it appropriately.

See:
0954fdd51e
2017-04-21 17:45:20 -07:00
aarzilli
b6fe5aebaf proc: refactoring: merge target into proc
- moved target.Interface into proc as proc.Process
- rename proc.IThread to proc.Thread
- replaced interfaces DisassembleInfo, Continuable and
  EvalScopeConvertible with Process.
- removed superfluous Gdbserver prefix from types in the gdbserial
  backend.
- removed superfluous Core prefix from types in the core backend.
2017-04-21 14:00:04 -07:00
aarzilli
15bac71979 proc: refactoring: split backends to separate packages
- move native backend to pkg/proc/native
- move gdbserver backend to pkg/proc/gdbserial
- move core dumps backend to pkg/proc/core
2017-04-21 14:00:04 -07:00
aarzilli
182f805094 proc: Use MemoryReader inside memoryReadWriter 2017-04-18 13:25:11 -07:00
aarzilli
c8d9352522 proc: Implement target.Interface for gdbserver backend 2017-04-18 13:25:11 -07:00
aarzilli
3dacc25d2e proc: refactor Continue to work on any Process implementation 2017-04-18 13:25:11 -07:00
aarzilli
510b7db2a7 proc: introduce IThread interface to abstract threads 2017-04-18 13:25:11 -07:00
Alessandro Arzilli
b5a06f7aa8 proc refactoring: make stack, disassemble and eval independent of proc.Process (#786)
* proc: Refactor stackIterator to use memoryReadWriter and BinaryInfo

* proc: refactor EvalScope to use memoryReadWriter and BinaryInfo

* proc: refactor Disassemble to use memoryReadWriter and BinaryInfo
2017-04-13 16:19:57 -07:00
Alessandro Arzilli
436a3c2149 proc refactor: split out BinaryInfo implementation (#745)
* proc: refactor BinaryInfo part of proc.Process to own type

The data structures and associated code used by proc.Process
to implement target.BinaryInfo will also be useful to support a
backend for examining core dumps, split this part of proc.Process
to a different type.

* proc: compile support for all executable formats unconditionally

So far we only compiled in support for loading the executable format
supported by the host operating system.
Once support for core files is introduced it is however useful to
support loading in all executable formats, there is no reason why it
shouldn't be possible to examine a linux coredump on windows, or
viceversa.

* proc: bugfix: do not resume threads on detach if killing

* Replace BinaryInfo interface with BinInfo() method returning proc.BinaryInfo
2017-04-06 11:14:01 -07:00
Alessandro Arzilli
7b19fe9e69 proc: add test for attach/detach, fix detach (#773)
Detach did not work for processes we attach to via PID.

Linux: we were only detaching from the main thread, all threads are
detached independently

Windows: we must resume all threads before detaching.

macOS: still broken.

Updates #772
2017-03-28 09:30:27 -07:00
dr2chase
bd48358de3 pkg/proc: tolerate absence of stack barriers in Go 1.9 (#762)
Stack barriers were removed in Go 1.9, and thus code that
expected various stack-barrier-related symbols to exist
does not find them.  Check for their absence and do not
crash when they are missing.  Disable stack-barrier-handling
test for 1.9 and beyond.

Fixes #754.
2017-03-13 10:53:16 -07:00
Alessandro Arzilli
97e3fc261c proc/tests: make TestStacktraceGoroutine more reliable (#764)
One of those goroutines could also conceivably be stopped on line 8.
2017-03-13 10:45:53 -07:00
Alessandro Arzilli
cda7316421 proc/tests: use Detach(true) instead of Kill in tests (#765)
The rest of the code uses Detach to "close" a proc.Process, the tests
should do the same.
Any cleanup that proc.Process needs to do can then be put inside Detach
and the tests will run it.
2017-03-13 10:44:27 -07:00
aarzilli
f2581e608a proc/test: improve TestStepParked
It was possible for TestStepParked to pick a runtime goroutine and
attempt to step it. Nothing guarantees that a goroutine other than the
ones we are using to run the code would actually ever resume before the
end of the program.

This makes the test more discerning in its choice of goroutines.
2017-02-22 20:39:28 +01:00
aarzilli
92faa95bf9 proc/stack: use BP when FDE is not available
On Windows we can sometimes encounter threads stopped in locations for
which we do not have entries in debug_frame.
These cases seem to be due to calls to Windows API in the go runtime,
we can still produce a (partial) stack trace in this circumstance by
following frame pointers (starting with BP).
We still prefer debug_frame entries when available since go functions
do not have frame pointers before go1.8.
2017-02-22 20:39:28 +01:00
aarzilli
1a68f8d351 proc/windows: handle delayed events
Sometimes windows will send us events about breakpoints we have
already removed from the code despite the fact that we go to great
lengths to avoid this already.

Change waitForDebugEvent to check that when we receive a breakpoint
event the corresponding memory actually contains an INT 3
instruction, if it doesn't ignore the event and restart the thread.
2017-02-22 20:39:28 +01:00
Alessandro Arzilli
b9bdf597b0 proc_test: TestStacktraceWithBarriers failing on go1.8 (#735)
Changes to the GC in 1.8 make this test fail because the GC isn't
inserting stack barriers into our test program anymore. This isn't the
same thing as being unable to print stacktraces in presence of stack
barriers so the test shouldn't fail.
2017-02-21 10:38:13 -08:00
Alessandro Arzilli
fc0d40144a proc/variables: fix infinite recursion with pointer loop (#725)
loadValue didn't react correctly to pointer loops going through
slice -> interface{} -> slice or pointer -> interface{} -> pointer.
2017-02-09 16:26:38 -08:00