github.com/neilgarb/delve@v1.9.2-nobreaks/Documentation/cli/starlark.md (about)

     1  # Introduction
     2  
     3  Passing a file with the .star extension to the `source` command will cause delve to interpret it as a starlark script.
     4  
     5  Starlark is a dialect of python, a [specification of its syntax can be found here](https://github.com/google/starlark-go/blob/master/doc/spec.md).
     6  
     7  In addition to the normal starlark built-ins delve defines [a number of global functions](#Starlark-built-ins) that can be used to interact with the debugger.
     8  
     9  After the file has been evaluated delve will bind any function starting with `command_` to a command-line command: for example `command_goroutines_wait_reason` will be bound to `goroutines_wait_reason`. 
    10  
    11  Then if a function named `main` exists it will be executed.
    12  
    13  Global functions with a name that begins with a capital letter will be available to other scripts.
    14  
    15  # Starlark built-ins
    16  
    17  <!-- BEGIN MAPPING TABLE -->
    18  Function | API Call
    19  ---------|---------
    20  amend_breakpoint(Breakpoint) | Equivalent to API call [AmendBreakpoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.AmendBreakpoint)
    21  ancestors(GoroutineID, NumAncestors, Depth) | Equivalent to API call [Ancestors](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Ancestors)
    22  attached_to_existing_process() | Equivalent to API call [AttachedToExistingProcess](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.AttachedToExistingProcess)
    23  build_id() | Equivalent to API call [BuildID](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.BuildID)
    24  cancel_next() | Equivalent to API call [CancelNext](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.CancelNext)
    25  checkpoint(Where) | Equivalent to API call [Checkpoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Checkpoint)
    26  clear_breakpoint(Id, Name) | Equivalent to API call [ClearBreakpoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ClearBreakpoint)
    27  clear_checkpoint(ID) | Equivalent to API call [ClearCheckpoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ClearCheckpoint)
    28  raw_command(Name, ThreadID, GoroutineID, ReturnInfoLoadConfig, Expr, UnsafeCall) | Equivalent to API call [Command](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Command)
    29  create_breakpoint(Breakpoint) | Equivalent to API call [CreateBreakpoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.CreateBreakpoint)
    30  create_ebpf_tracepoint(FunctionName) | Equivalent to API call [CreateEBPFTracepoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.CreateEBPFTracepoint)
    31  create_watchpoint(Scope, Expr, Type) | Equivalent to API call [CreateWatchpoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.CreateWatchpoint)
    32  detach(Kill) | Equivalent to API call [Detach](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Detach)
    33  disassemble(Scope, StartPC, EndPC, Flavour) | Equivalent to API call [Disassemble](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Disassemble)
    34  dump_cancel() | Equivalent to API call [DumpCancel](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.DumpCancel)
    35  dump_start(Destination) | Equivalent to API call [DumpStart](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.DumpStart)
    36  dump_wait(Wait) | Equivalent to API call [DumpWait](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.DumpWait)
    37  eval(Scope, Expr, Cfg) | Equivalent to API call [Eval](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Eval)
    38  examine_memory(Address, Length) | Equivalent to API call [ExamineMemory](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ExamineMemory)
    39  find_location(Scope, Loc, IncludeNonExecutableLines, SubstitutePathRules) | Equivalent to API call [FindLocation](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.FindLocation)
    40  function_return_locations(FnName) | Equivalent to API call [FunctionReturnLocations](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.FunctionReturnLocations)
    41  get_breakpoint(Id, Name) | Equivalent to API call [GetBreakpoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.GetBreakpoint)
    42  get_buffered_tracepoints() | Equivalent to API call [GetBufferedTracepoints](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.GetBufferedTracepoints)
    43  get_thread(Id) | Equivalent to API call [GetThread](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.GetThread)
    44  is_multiclient() | Equivalent to API call [IsMulticlient](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.IsMulticlient)
    45  last_modified() | Equivalent to API call [LastModified](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.LastModified)
    46  breakpoints(All) | Equivalent to API call [ListBreakpoints](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListBreakpoints)
    47  checkpoints() | Equivalent to API call [ListCheckpoints](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListCheckpoints)
    48  dynamic_libraries() | Equivalent to API call [ListDynamicLibraries](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListDynamicLibraries)
    49  function_args(Scope, Cfg) | Equivalent to API call [ListFunctionArgs](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListFunctionArgs)
    50  functions(Filter) | Equivalent to API call [ListFunctions](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListFunctions)
    51  goroutines(Start, Count, Filters, GoroutineGroupingOptions) | Equivalent to API call [ListGoroutines](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListGoroutines)
    52  local_vars(Scope, Cfg) | Equivalent to API call [ListLocalVars](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListLocalVars)
    53  package_vars(Filter, Cfg) | Equivalent to API call [ListPackageVars](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListPackageVars)
    54  packages_build_info(IncludeFiles) | Equivalent to API call [ListPackagesBuildInfo](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListPackagesBuildInfo)
    55  registers(ThreadID, IncludeFp, Scope) | Equivalent to API call [ListRegisters](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListRegisters)
    56  sources(Filter) | Equivalent to API call [ListSources](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListSources)
    57  threads() | Equivalent to API call [ListThreads](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListThreads)
    58  types(Filter) | Equivalent to API call [ListTypes](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ListTypes)
    59  process_pid() | Equivalent to API call [ProcessPid](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ProcessPid)
    60  recorded() | Equivalent to API call [Recorded](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Recorded)
    61  restart(Position, ResetArgs, NewArgs, Rerecord, Rebuild, NewRedirects) | Equivalent to API call [Restart](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Restart)
    62  set_expr(Scope, Symbol, Value) | Equivalent to API call [Set](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Set)
    63  stacktrace(Id, Depth, Full, Defers, Opts, Cfg) | Equivalent to API call [Stacktrace](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.Stacktrace)
    64  state(NonBlocking) | Equivalent to API call [State](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.State)
    65  toggle_breakpoint(Id, Name) | Equivalent to API call [ToggleBreakpoint](https://godoc.org/github.com/go-delve/delve/service/rpc2#RPCServer.ToggleBreakpoint)
    66  dlv_command(command) | Executes the specified command as if typed at the dlv_prompt
    67  read_file(path) | Reads the file as a string
    68  write_file(path, contents) | Writes string to a file
    69  cur_scope() | Returns the current evaluation scope
    70  default_load_config() | Returns the current default load configuration
    71  <!-- END MAPPING TABLE -->
    72  
    73  ## Should I use raw_command or dlv_command?
    74  
    75  There are two ways to resume the execution of the target program:
    76  
    77  	raw_command("continue")
    78  	dlv_command("continue")
    79  
    80  The first one maps to the API call [Command](https://godoc.org/github.com/derekparker/delve/service/rpc2#RPCServer.Command). As such all the caveats explained in the [Client HowTo](../api/ClientHowto.md).
    81  
    82  The latter is equivalent to typing `continue` to the `(dlv)` command line and should do what you expect.
    83  
    84  In general `dlv_command("continue")` should be preferred, unless the behavior you wish to produces diverges significantly from that of the command line's `continue`.
    85  
    86  # Creating new commands
    87  
    88  Any global function with a name starting with `command_` will be made available as a command line command. If the function has a single argument named `args` all arguments passed on the command line will be passed to the function as a single string. 
    89  
    90  Otherwise arguments passed on the command line are interpreted as starlark expressions. See the [expression arguments](#expression-arguments) example. 
    91  
    92  If the command function has a doc string it will be used as a help message.
    93  
    94  # Working with variables
    95  
    96  Variables of the target program can be accessed using `local_vars`, `function_args` or the `eval` functions. Each variable will be returned as a [Variable](https://godoc.org/github.com/go-delve/delve/service/api#Variable) struct, with one special field: `Value`.
    97  
    98  ## Variable.Value
    99  
   100  The `Value` field will return the value of the target variable converted to a starlark value:
   101  
   102  * integers, floating point numbers and strings are represented by equivalent starlark values
   103  * structs are represented as starlark dictionaries
   104  * slices and arrays are represented by starlark lists
   105  * maps are represented by starlark dicts
   106  * pointers and interfaces are represented by a one-element starlark list containing the value they point to
   107  
   108  For example, given this variable in the target program:
   109  
   110  ```
   111  type astruct struct {
   112  	A int
   113  	B int
   114  }
   115  
   116  s2 := []astruct{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}, {13, 14}, {15, 16}}
   117  ```
   118  
   119  The following is possible:
   120  
   121  ```
   122  >>> s2 = eval(None, "s2").Variable
   123  >>> s2.Value[0]                                     # access of a slice item by index
   124  main.astruct {A: 1, B: 2}
   125  >>> a = s2.Value[1]
   126  >>> a.Value.A                                       # access to a struct field
   127  3
   128  >>> a.Value.A + 10                            # arithmetic on the value of s2[1].X
   129  13
   130  >>> a.Value["B"]                                    # access to a struct field, using dictionary syntax
   131  4
   132  ```
   133  
   134  For more examples see the [linked list example](#Print-all-elements-of-a-linked-list) below.
   135  
   136  # Examples
   137  
   138  ## Listing goroutines and making custom commands
   139  
   140  Create a `goroutine_start_line` command that prints the starting line of each goroutine, sets `gsl` as an alias:
   141  
   142  ```
   143  def command_goroutine_start_line(args):
   144  	gs = goroutines().Goroutines
   145  	for g in gs:
   146  		line = read_file(g.StartLoc.File).splitlines()[g.StartLoc.Line-1].strip()
   147  		print(g.ID, "\t", g.StartLoc.File + ":" + str(g.StartLoc.Line), "\t", line)
   148  
   149  def main():
   150  	dlv_command("config alias goroutine_start_line gsl")
   151  ```
   152  
   153  Use it like this:
   154  
   155  ```
   156  (dlv) source goroutine_start_line.star
   157  (dlv) goroutine_start_line
   158  1 	 /usr/local/go/src/runtime/proc.go:110 	 func main() {
   159  2 	 /usr/local/go/src/runtime/proc.go:242 	 func forcegchelper() {
   160  17 	 /usr/local/go/src/runtime/mgcsweep.go:64 	 func bgsweep(c chan int) {
   161  18 	 /usr/local/go/src/runtime/mfinal.go:161 	 func runfinq() {
   162  (dlv) gsl
   163  1 	 /usr/local/go/src/runtime/proc.go:110 	 func main() {
   164  2 	 /usr/local/go/src/runtime/proc.go:242 	 func forcegchelper() {
   165  17 	 /usr/local/go/src/runtime/mgcsweep.go:64 	 func bgsweep(c chan int) {
   166  18 	 /usr/local/go/src/runtime/mfinal.go:161 	 func runfinq() {
   167  ```
   168  
   169  ## Expression arguments
   170  
   171  After evaluating this script:
   172  
   173  ```
   174  def command_echo(args):
   175  	print(args)
   176  
   177  def command_echo_expr(a, b, c):
   178  	print("a", a, "b", b, "c", c)
   179  ```
   180  
   181  The first commnad, `echo`, takes its arguments as a single string, while for `echo_expr` it will be possible to pass starlark expression as arguments:
   182  
   183  ```
   184  (dlv) echo 2+2, 2-1, 2*3
   185  "2+2, 2-1, 2*3"
   186  (dlv) echo_expr 2+2, 2-1, 2*3
   187  a 4 b 1 c 6
   188  ```
   189  
   190  ## Creating breakpoints
   191  
   192  Set a breakpoint on all private methods of package `main`:
   193  
   194  ```
   195  def main():
   196  	for f in functions().Funcs:
   197  		v = f.split('.')
   198  		if len(v) != 2:
   199  			continue
   200  		if v[0] != "main":
   201  			continue
   202  		if v[1][0] >= 'a' and v[1][0] <= 'z':
   203  			create_breakpoint({ "FunctionName": f, "Line": -1 }) # see documentation of RPCServer.CreateBreakpoint
   204  ```
   205  
   206  ## Switching goroutines
   207  
   208  Create a command, `switch_to_main_goroutine`, that searches for a goroutine running a function in the main package and switches to it:
   209  
   210  ```
   211  def command_switch_to_main_goroutine(args):
   212  	for g in goroutines().Goroutines:
   213  		if g.currentLoc.function != None and g.currentLoc.function.name.startswith("main."):
   214  			print("switching to:", g.id)
   215  			raw_command("switchGoroutine", GoroutineID=g.id)
   216  			break
   217  ```
   218  
   219  ## Listing goroutines
   220  
   221  Create a command, "goexcl", that lists all goroutines excluding the ones stopped on a specified function.
   222  
   223  ```
   224  def command_goexcl(args):
   225  	"""Prints all goroutines not stopped in the function passed as argument."""
   226  	excluded = 0
   227  	start = 0
   228  	while start >= 0:
   229  		gr = goroutines(start, 10)
   230  		start = gr.Nextg
   231  		for g in gr.Goroutines:
   232  			fn = g.UserCurrentLoc.Function
   233  			if fn == None:
   234  				print("Goroutine", g.ID, "User:", g.UserCurrentLoc.File, g.UserCurrentLoc.Line)
   235  			elif fn.Name_ != args:
   236  				print("Goroutine", g.ID, "User:", g.UserCurrentLoc.File, g.UserCurrentLoc.Line, fn.Name_)
   237  			else:
   238  				excluded = excluded + 1
   239  	print("Excluded", excluded, "goroutines")
   240  ```
   241  
   242  Usage:
   243  
   244  ```
   245  (dlv) goexcl main.somefunc
   246  ```
   247  
   248  prints all goroutines that are not stopped inside `main.somefunc`.
   249  
   250  ## Repeatedly executing the target until a breakpoint is hit.
   251  
   252  Repeatedly call continue and restart until the target hits a breakpoint.
   253  
   254  ```
   255  def command_flaky(args):
   256  	"Repeatedly runs program until a breakpoint is hit"
   257  	while True:
   258  		if dlv_command("continue") == None:
   259  			break
   260  		dlv_command("restart")
   261  ```
   262  
   263  ## Print all elements of a linked list
   264  
   265  ```
   266  def command_linked_list(args):
   267  	"""Prints the contents of a linked list.
   268  	
   269  	linked_list <var_name> <next_field_name> <max_depth>
   270  
   271  Prints up to max_depth elements of the linked list variable 'var_name' using 'next_field_name' as the name of the link field.
   272  """
   273  	var_name, next_field_name, max_depth = args.split(" ")
   274  	max_depth = int(max_depth)
   275  	next_name = var_name
   276  	v = eval(None, var_name).Variable.Value
   277  	for i in range(0, max_depth):
   278  		print(str(i)+":",v)
   279  		if v[0] == None:
   280  			break
   281  		v = v[next_field_name]
   282  ```
   283  
   284  ## Find an array element matching a predicate
   285  
   286  ```
   287  def command_find_array(arr, pred):
   288  	"""Calls pred for each element of the array or slice 'arr' returns the index of the first element for which pred returns true.
   289  	
   290  	find_arr <arr> <pred>
   291  	
   292  Example use (find the first element of slice 's2' with field A equal to 5):
   293  	
   294  	find_arr "s2", lambda x: x.A == 5
   295  """
   296  	arrv = eval(None, arr).Variable
   297  	for i in range(0, arrv.Len):
   298  		v = arrv.Value[i]
   299  		if pred(v):
   300  			print("found", i)
   301  			return
   302  
   303  	print("not found")
   304  ```
   305  
   306  ## Rerunning a program until it fails or hits a breakpoint
   307  
   308  ```
   309  def command_flaky(args):
   310  	"Continues and restarts the target program repeatedly (re-recording it on the rr backend), until a breakpoint is hit"
   311  	count = 1
   312  	while True:
   313  		if dlv_command("continue") == None:
   314  			break
   315  		print("restarting", count, "...")
   316  		count = count+1
   317  		restart(Rerecord=True)
   318  
   319  ```