Skip to content

Commit 9d5daaf

Browse files
committed
pkg/proc: add an event for spawned processes
1 parent f3f3d57 commit 9d5daaf

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

pkg/proc/target_group.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ func (grp *TargetGroup) addTarget(p ProcessInternal, pid int, currentThread Thre
117117
}
118118
t, err := grp.newTarget(p, pid, currentThread, path, cmdline)
119119
if err != nil {
120+
// Notify listeners that a child process was spawned even though we
121+
// can't debug it - listeners may care about non-Go processes.
122+
grp.notifyProcessSpawned(pid, currentThread.ThreadID(), cmdline)
120123
return nil, err
121124
}
122125
t.StopReason = stopReason
@@ -145,9 +148,29 @@ func (grp *TargetGroup) addTarget(p ProcessInternal, pid int, currentThread Thre
145148
}
146149
}
147150
grp.targets = append(grp.targets, t)
151+
152+
// Notify listeners that a child process was spawned. We do this after the
153+
// new target has been set up.
154+
grp.notifyProcessSpawned(pid, currentThread.ThreadID(), cmdline)
148155
return t, nil
149156
}
150157

158+
func (grp *TargetGroup) notifyProcessSpawned(pid, threadID int, cmdline string) {
159+
fn := grp.Selected.BinInfo().eventsFn
160+
if fn == nil {
161+
return
162+
}
163+
164+
fn(&Event{
165+
Kind: EventProcessSpawned,
166+
ProcessSpawnedEventDetails: &ProcessSpawnedEventDetails{
167+
PID: pid,
168+
ThreadID: threadID,
169+
Cmdline: cmdline,
170+
},
171+
})
172+
}
173+
151174
// Targets returns a slice of all targets in the group, including the
152175
// ones that are no longer valid.
153176
func (grp *TargetGroup) Targets() []*Target {
@@ -576,6 +599,7 @@ type Event struct {
576599
Kind EventKind
577600
*BinaryInfoDownloadEventDetails
578601
*BreakpointMaterializedEventDetails
602+
*ProcessSpawnedEventDetails
579603
}
580604

581605
type EventKind uint8
@@ -585,6 +609,7 @@ const (
585609
EventStopped
586610
EventBinaryInfoDownload
587611
EventBreakpointMaterialized
612+
EventProcessSpawned
588613
)
589614

590615
// BinaryInfoDownloadEventDetails describes the details of a BinaryInfoDownloadEvent
@@ -596,3 +621,10 @@ type BinaryInfoDownloadEventDetails struct {
596621
type BreakpointMaterializedEventDetails struct {
597622
Breakpoint *LogicalBreakpoint
598623
}
624+
625+
// ProcessSpawnedEventDetails describes the details of a ProcessSpawnedEvent
626+
type ProcessSpawnedEventDetails struct {
627+
PID int
628+
ThreadID int
629+
Cmdline string
630+
}

pkg/terminal/terminal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ func New(client service.Client, conf *config.Config) *Term {
148148
}
149149

150150
fmt.Fprintf(t.stdout, "Breakpoint %d materialized at %s:%d%s\n", bp.ID, file, bp.Line, extra)
151+
case api.EventProcessSpawned:
152+
fmt.Fprintf(t.stdout, "Spawned new process '%s' (%d)\n", event.Cmdline, event.ProcessSpawnedEventDetails.PID)
151153
}
152154
})
153155
}

service/api/conversions.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,5 +479,13 @@ func ConvertEvent(event *proc.Event) *Event {
479479
}
480480
}
481481

482+
if event.ProcessSpawnedEventDetails != nil {
483+
r.ProcessSpawnedEventDetails = &ProcessSpawnedEventDetails{
484+
PID: event.ProcessSpawnedEventDetails.PID,
485+
ThreadID: event.ProcessSpawnedEventDetails.ThreadID,
486+
Cmdline: event.ProcessSpawnedEventDetails.Cmdline,
487+
}
488+
}
489+
482490
return r
483491
}

service/api/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ type Event struct {
694694
Kind EventKind
695695
*BinaryInfoDownloadEventDetails
696696
*BreakpointMaterializedEventDetails
697+
*ProcessSpawnedEventDetails
697698
}
698699

699700
type EventKind uint8
@@ -703,6 +704,7 @@ const (
703704
EventStopped
704705
EventBinaryInfoDownload
705706
EventBreakpointMaterialized
707+
EventProcessSpawned
706708
)
707709

708710
// BinaryInfoDownloadEventDetails describes the details of a BinaryInfoDownloadEvent
@@ -714,3 +716,10 @@ type BinaryInfoDownloadEventDetails struct {
714716
type BreakpointMaterializedEventDetails struct {
715717
Breakpoint *Breakpoint
716718
}
719+
720+
// ProcessSpawnedEventDetails describes the details of a ProcessSpawnedEvent
721+
type ProcessSpawnedEventDetails struct {
722+
PID int
723+
ThreadID int
724+
Cmdline string
725+
}

service/dap/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4103,6 +4103,12 @@ func (s *Session) convertDebuggerEvent(event *proc.Event) {
41034103
},
41044104
})
41054105
}
4106+
s.send(&dap.ProcessEvent{
4107+
Event: *newEvent("process"),
4108+
Body: dap.ProcessEventBody{
4109+
4110+
},
4111+
})
41064112
}
41074113

41084114
type logMessage struct {

0 commit comments

Comments
 (0)