github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/Documentation/cli/expr.md (about)

     1  # Expressions
     2  
     3  Delve can evaluate a subset of go expression language, specifically the following features are supported:
     4  
     5  - All (binary and unary) on basic types except <-, ++ and --
     6  - Comparison operators on any type
     7  - Type casts between numeric types
     8  - Type casts of integer constants into any pointer type and vice versa
     9  - Type casts between string, []byte and []rune
    10  - Struct member access (i.e. `somevar.memberfield`)
    11  - Slicing and indexing operators on arrays, slices and strings
    12  - Map access
    13  - Pointer dereference
    14  - Calls to builtin functions: `cap`, `len`, `complex`, `imag` and `real`
    15  - Type assertion on interface variables (i.e. `somevar.(concretetype)`)
    16  
    17  # Special Variables
    18  
    19  Delve defines two special variables:
    20  
    21  * `runtime.curg` evaluates to the 'g' struct for the current goroutine, in particular `runtime.curg.goid` is the goroutine id of the current goroutine.
    22  * `runtime.frameoff` is the offset of the frame's base address from the bottom of the stack.
    23  
    24  # Nesting limit
    25  
    26  When delve evaluates a memory address it will automatically return the value of nested struct members, array and slice items and dereference pointers.
    27  However to limit the size of the output evaluation will be limited to two levels deep. Beyond two levels only the address of the item will be returned, for example:
    28  
    29  ```
    30  (dlv) print c1
    31  main.cstruct {
    32  	pb: *struct main.bstruct {
    33  		a: (*main.astruct)(0xc82000a430),
    34  	},
    35  	sa: []*main.astruct len: 3, cap: 3, [
    36  		*(*main.astruct)(0xc82000a440),
    37  		*(*main.astruct)(0xc82000a450),
    38  		*(*main.astruct)(0xc82000a460),
    39  	],
    40  }
    41  ```
    42  
    43  To see the contents of the first item of the slice `c1.sa` there are two possibilities:
    44  
    45  1. Execute `print c1.sa[0]`
    46  2. Use the address directly, executing: `print *(*main.astruct)(0xc82000a440)`
    47  
    48  # Elements limit
    49  
    50  For arrays, slices, strings and maps delve will only return a maximum of 64 elements at a time:
    51  
    52  ```
    53  (dlv) print ba
    54  []int len: 200, cap: 200, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...+136 more]
    55  ```
    56  
    57  To see more values use the slice operator:
    58  
    59  ```
    60  (dlv) print ba[64:]
    61  []int len: 136, cap: 136, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...+72 more]
    62  ```
    63  
    64  For this purpose delve allows use of the slice operator on maps, `m[64:]` will return the key/value pairs of map `m` that follow the first 64 key/value pairs (note that delve iterates over maps using a fixed ordering).
    65  
    66  These limits can be configured with `max-string-len` and `max-array-values`. See [config](https://github.com/go-delve/delve/tree/master/Documentation/cli#config) for usage.
    67  
    68  # Interfaces
    69  
    70  Interfaces will be printed using the following syntax:
    71  ```
    72  <interface name>(<concrete type>) <value>
    73  ```
    74  
    75  For example:
    76  
    77  ```
    78  (dlv) p iface1
    79  (dlv) p iface1
    80  interface {}(*struct main.astruct) *{A: 1, B: 2}
    81  (dlv) p iface2
    82  interface {}(*struct string) *"test"
    83  (dlv) p err1
    84  error(*struct main.astruct) *{A: 1, B: 2}
    85  ```
    86  
    87  To use the contents of an interface variable use a type assertion:
    88  
    89  ```
    90  (dlv) p iface1.(*main.astruct).B
    91  2
    92  ```
    93  
    94  Or just use the special `.(data)` type assertion:
    95  
    96  ```
    97  (dlv) p iface1.(data).B
    98  2
    99  ```
   100  
   101  If the contents of the interface variable are a struct or a pointer to struct the fields can also be accessed directly:
   102  
   103  ```
   104  (dlv) p iface1.B
   105  2
   106  ```
   107  
   108  # Specifying package paths
   109  
   110  Packages with the same name can be disambiguated by using the full package path. For example, if the application imports two packages, `some/package` and `some/other/package`, both defining a variable `A`, the two variables can be accessed using this syntax:
   111  
   112  ```
   113  (dlv) p "some/package".A
   114  (dlv) p "some/other/package".A
   115  ```
   116  
   117  # Pointers in Cgo
   118  
   119  Char pointers are always treated as NUL terminated strings, both indexing and the slice operator can be applied to them. Other C pointers can also be used similarly to Go slices, with indexing and the slice operator. In both of these cases it is up to the user to respect array bounds.
   120  
   121  
   122  # CPU Registers
   123  
   124  The name of a CPU register, in all uppercase letters, will resolve to the value of that CPU register in the current frame. For example on AMD64 the expression `RAX` will evaluate to the value of the RAX register. 
   125  
   126  Register names are shadowed by both local and global variables, so if a local variable called "RAX" exists, the `RAX` expression will evaluate to it instead of the CPU register.
   127  
   128  Register names can optionally be prefixed by any number of underscore characters, so `RAX`, `_RAX`, `__RAX`, etc... can all be used to refer to the same RAX register and, in absence of shadowing from other variables, will all evaluate to the same value.
   129  
   130  Registers of 64bits or less are returned as uint64 variables. Larger registers are returned as strings of hexadecimal digits.
   131  
   132  Because many architectures have SIMD registers that can be used by the application in different ways the following syntax is also available:
   133  
   134  * `REGNAME.intN` returns the register REGNAME as an array of intN elements.
   135  * `REGNAME.uintN` returns the register REGNAME as an array of uintN elements.
   136  * `REGNAME.floatN` returns the register REGNAME as an array of floatN elements.
   137  
   138  In all cases N must be a power of 2.
   139