@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"os"
7
8
"os/signal"
33
34
"timeout" , 0 ,
34
35
"timeout after which to give up" ,
35
36
)
37
+ flgQPidParentSkip = cmdQuery .PersistentFlags ().Int (
38
+ "pid-parent-skip" , 0 ,
39
+ "skip this many parent PIDs when determining true parent process (when run from ZSH ZLE descriptor we end up with extra PID nesting)" ,
40
+ )
41
+ flgQPidChain = cmdQuery .PersistentFlags ().Bool (
42
+ "pid-chain" , false ,
43
+ "add extra pid parent chain information" ,
44
+ )
36
45
)
37
46
38
47
func init () {
@@ -51,13 +60,15 @@ const (
51
60
_partWorkDir = "wd"
52
61
_partWorkDirShort = "wd_trim"
53
62
54
- _partPid = "pid"
55
- _partPidShell = "pid_shell"
56
- _partPidShellExec = "pid_shell_exec"
57
- _partPidParent = "pid_parent"
58
- _partPidParentExec = "pid_parent_exec"
59
- _partPidRemote = "pid_remote"
60
- _partPidRemoteExec = "pid_remote_exec"
63
+ _partPid = "pid"
64
+ _partPidShell = "pid_shell"
65
+ _partPidShellExec = "pid_shell_exec"
66
+ _partPidParent = "pid_parent"
67
+ _partPidParentExec = "pid_parent_exec"
68
+ _partPidRemote = "pid_remote"
69
+ _partPidRemoteExec = "pid_remote_exec"
70
+ _partPidChain = "pid_chain"
71
+ _partPidChainLength = "pid_chain_length"
61
72
62
73
_partSessionUsername = "session_username"
63
74
_partSessionHostname = "session_hostname"
@@ -75,8 +86,8 @@ const (
75
86
_partVcsStgTop = "vcs_git_stg_top"
76
87
_partVcsStgDirty = "vcs_git_stg_dirty"
77
88
78
- _partVcsGitRebaseOp = "vcs_git_rebase_op"
79
- _partVcsGitRebaseLeft = "vcs_git_rebase_op_left"
89
+ _partVcsGitRebaseOp = "vcs_git_rebase_op"
90
+ _partVcsGitRebaseLeft = "vcs_git_rebase_op_left"
80
91
81
92
_partVcsGitIdxTotal = "vcs_git_idx_total"
82
93
_partVcsGitIdxIncluded = "vcs_git_idx_incl"
@@ -190,37 +201,53 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
190
201
})
191
202
192
203
tasks .Go (func (_ context.Context ) error {
204
+ type list []interface {}
205
+ type dict map [string ]interface {}
206
+
193
207
psChain , err := moduleFindProcessChain ()
194
208
if err != nil {
195
209
return nil
196
210
}
197
211
198
- if len (psChain ) > 3 {
199
- pidShell := psChain [1 ]
200
- pidShellParent := psChain [2 ]
212
+ printPart (_partPidChainLength , len (psChain ))
201
213
202
- pidShellExecName , _ , _ := strings .Cut (pidShell .Executable (), " " )
203
- printPart (_partPidShell , pidShell .Pid ())
204
- printPart (_partPidShellExec , pidShellExecName )
214
+ var pidRemote ps.Process
215
+ var pidChain list
216
+ for psIdx , ps := range psChain {
217
+ pidChain = append (pidChain , dict {
218
+ "name" : ps .Executable (),
219
+ "pid" : ps .Pid (),
220
+ })
221
+
222
+ // Find if we are in a remote session.
223
+ if strings .Contains (ps .Executable (), "ssh" ) && pidRemote == nil {
224
+ pidRemote = ps
225
+ }
205
226
206
- pidShellParentExecName , _ , _ := strings .Cut (pidShellParent .Executable (), " " )
207
- printPart (_partPidParent , pidShellParent .Pid ())
208
- printPart (_partPidParentExec , pidShellParentExecName )
209
- }
227
+ psIdxAdj := psIdx - * flgQPidParentSkip
210
228
211
- var pidRemote ps.Process
212
- for _ , psInfo := range psChain {
213
- if strings .Contains (psInfo .Executable (), "ssh" ) {
214
- pidRemote = psInfo
215
- break
229
+ if psIdxAdj == 1 {
230
+ pidShellExecName , _ , _ := strings .Cut (ps .Executable (), " " )
231
+ printPart (_partPidShell , ps .Pid ())
232
+ printPart (_partPidShellExec , pidShellExecName )
233
+ } else if psIdxAdj == 2 {
234
+ pidShellParentExecName , _ , _ := strings .Cut (ps .Executable (), " " )
235
+ printPart (_partPidParent , ps .Pid ())
236
+ printPart (_partPidParentExec , pidShellParentExecName )
216
237
}
217
238
}
239
+
218
240
if pidRemote != nil {
219
241
pidShellRemoteExecName , _ , _ := strings .Cut (pidRemote .Executable (), " " )
220
242
printPart (_partPidRemote , pidRemote .Pid ())
221
243
printPart (_partPidRemoteExec , pidShellRemoteExecName )
222
244
}
223
245
246
+ if * flgQPidChain {
247
+ pidChainJson , _ := json .Marshal (pidChain )
248
+ printPart (_partPidChain , string (pidChainJson ))
249
+ }
250
+
224
251
return nil
225
252
})
226
253
@@ -253,7 +280,6 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
253
280
return nil
254
281
})
255
282
256
-
257
283
subTasks .Go (func (ctx context.Context ) error {
258
284
if saplStatus , err := stringExec ("sl" , "status" ); err == nil {
259
285
if len (saplStatus ) == 0 {
@@ -284,8 +310,6 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
284
310
285
311
subTasks .Go (func (ctx context.Context ) error {
286
312
287
-
288
-
289
313
headRef := ""
290
314
if cherryHeadB , _ := os .ReadFile (filepath .Join (gitDir , "CHERRY_PICK_HEAD" )); len (cherryHeadB ) > 0 {
291
315
headRef = trim (string (cherryHeadB ))
0 commit comments