NB. debug definitions and utilities

NB. e.g.  dbss 'f 0'   monadic line 0
NB.       dbss 'f :2'  dyadic line 2
NB.       dbss 'f *:*' all lines
NB.
NB. The call stack (dbstk'') is an 8-column boxed matrix:
NB.   0  name
NB.   1  error number, or 0 if this call has no error.
NB.   2  line number
NB.   3  name class
NB.   4  definition
NB.   5  source script
NB.   6  argument list
NB.   7  locals

cocurrent 'z'

NB.*dbr v reset, set suspension mode (0=disable, 1=enable)
NB.*dbs v display stack
NB.*dbsq v stop query
NB.*dbss v stop set
NB.*dbrun v run again (from current stop)
NB.*dbnxt v run next (skip line and run)
NB.*dbret v exit and return argument
NB.*dbjmp v jump to line number
NB.*dbsig v signal error
NB.*dbrr v re-run with specified arguments
NB.*dbrrx v re-run with specified executed arguments
NB.*dberr v last error number
NB.*dberm v last error message
NB.*dbstk v call stack
NB.*dblxq v latent expression query
NB.*dblxs v latent expression set
NB.*dbtrace v trace control
NB.*dbq v queries suspension mode (set by dbr)
NB.*dbst v returns stack text

dbr=: 13!:0
dbs=: 13!:1
dbsq=: 13!:2
dbss=: 13!:3
dbrun=: 13!:4
dbnxt=: 13!:5
dbret=: 13!:6
dbjmp=: 13!:7
dbsig=: 13!:8
dbrr=: 13!:9
dbrrx=: 13!:10
dberr=: 13!:11
dberm=: 13!:12
dbstk=: 13!:13
dblxq=: 13!:14
dblxs=: 13!:15
dbtrace=: 13!:16
dbq=: 13!:17
dbst=: 13!:18

NB. =========================================================
NB. utilities:
NB. dbg         turn debug window on/off
NB. dblocals    display local names on stack
NB. dbstack     display stack
NB. dbstop      add stop definitions
NB. dbstops     set all stop definitions
NB. dbstopme    stop current definition

NB. =========================================================
NB.*dbg v turn debugging window on/off
dbg=: 3 : 0
if. y. do.
  if. _1 = 4!:0 <'jdb_open_jdebug_' do.
    0!:0 <jpath '~system\extras\util\debugs.ijs'
  end.
  jdb_open_jdebug_''
  smfocusact_jijs_''
  13!:0 [ 1
else.
  jdb_close_jdebug_ :: ] ''
  13!:15 ''
  13!:0 [ 0
end.
)

NB. =========================================================
NB.*dblocals v display names and locals on stack
NB. form: [namelist] dblocals stack indices (default all)
NB. example:
NB.          dblocals ''     display all local names in stack
NB.  'abc Z' dblocals i.5    display names abc and Z where found
NB.                          in first 5 definitions on stack
dblocals=: _1&$: : (4 : 0)
stk=. }. 13!:13''
if. 0=#y. do. y=. a: else. y=. (y. e. i.#stk) # y. end.
loc=. (<y ; 0 7) { stk
if. -. x.-:_1 do.
  x=. ;:^:(L. = 0:) x.
  f=. ({."1 e. x"_) # ]
  ({."1 loc) ,. f &.> {:"1 loc
end.
)

NB. =========================================================
NB.*dbstack v displays call stack with header
NB. if  x.=0  ignores definition and source script (default)
NB.        1  displays full stack
NB. y. is the number of lines to display, all if empty
NB. limits display of &.> item to length 30.
dbstack=: 0&$: : (4 : 0)
hdr=. ;:'name en ln nc def script args locals susp'
stk=. }. 13!:13''
if. #y. do. stk=. (,y.){.stk end.
stk=. hdr,stk
stk=. stk #~"1 (0~:x.) >. 1 1 1 1 0 0 1 1 1
tc=. 31&<.@$ {.!.'.' ({.~ 30&<.@$)
tc@": &.>stk
)

NB. =========================================================
NB.*dbstop v set stops on all lines in namelist
NB.
NB. Adds to current set of stops
dbstop=: 3 : 0
if. 0 e. #y. -. ' ' do. 13!:3'' return. end.
t=. 13!:2''
if. #t do. t=. <;._2 t, ';' -. {:t end.
t=. ~. t, (;: ^: (L.=0:) y.) ,&.> <' *:*'
13!:3 ; t ,&.> ';'
)

NB. =========================================================
NB.*dbstops v set stops on all lines in namelist
NB.
NB. Replaces current set of stops
dbstops=: 3 : 0
13!:3 ; (;: ^: (L.=0:) y.) ,&.> <' *:*;'
)

NB. =========================================================
NB.*dbstopme v set stops on current definition if y.
NB. does nothing if suspension is off
dbstopme=: 3 : 0
if. y. do.
  if. 0 e. $c=. }. 13!:13'' do. return. end.
  c=. (> {. {. c), ' *:*'
  t=. 13!:2''
  if. #t do. t=. <;._2 t, ';' -. {:t end.
  t=. ~. t, <c
  13!:3 }: ; t ,&.> ';'
end.
)
