github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/_fixtures/fncall.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "runtime" 7 "strings" 8 ) 9 10 var call = "this is a variable named `call`" 11 12 func callstacktrace() (stacktrace string) { 13 for skip := 0; ; skip++ { 14 pc, file, line, ok := runtime.Caller(skip) 15 if !ok { 16 break 17 } 18 fn := runtime.FuncForPC(pc) 19 stacktrace += fmt.Sprintf("in %s at %s:%d\n", fn.Name(), file, line) 20 } 21 return stacktrace 22 } 23 24 func call0(a, b int) { 25 fmt.Printf("call0: first: %d second: %d\n", a, b) 26 } 27 28 func call1(a, b int) int { 29 fmt.Printf("first: %d second: %d\n", a, b) 30 return a + b 31 } 32 33 func call2(a, b int) (int, int) { 34 fmt.Printf("call2: first: %d second: %d\n", a, b) 35 return a, b 36 } 37 38 func callexit() { 39 fmt.Printf("about to exit\n") 40 os.Exit(0) 41 } 42 43 func callpanic() { 44 fmt.Printf("about to panic\n") 45 panic("callpanic panicked") 46 } 47 48 func callbreak() { 49 fmt.Printf("about to break") 50 runtime.Breakpoint() 51 } 52 53 func stringsJoin(v []string, sep string) string { 54 // This is needed because strings.Join is in an optimized package and 55 // because of a bug in the compiler arguments of optimized functions don't 56 // have a location. 57 return strings.Join(v, sep) 58 } 59 60 type astruct struct { 61 X int 62 } 63 64 type a2struct struct { 65 Y int 66 } 67 68 func (a astruct) VRcvr(x int) string { 69 return fmt.Sprintf("%d + %d = %d", x, a.X, x+a.X) 70 } 71 72 func (pa *astruct) PRcvr(x int) string { 73 return fmt.Sprintf("%d - %d = %d", x, pa.X, x-pa.X) 74 } 75 76 type PRcvrable interface { 77 PRcvr(int) string 78 } 79 80 type VRcvrable interface { 81 VRcvr(int) string 82 } 83 84 var zero = 0 85 86 func makeclos(pa *astruct) func(int) string { 87 i := 0 88 return func(x int) string { 89 i++ 90 return fmt.Sprintf("%d + %d + %d = %d", i, pa.X, x, i+pa.X+x) 91 } 92 } 93 94 var ga astruct 95 var globalPA2 *a2struct 96 97 func escapeArg(pa2 *a2struct) { 98 globalPA2 = pa2 99 } 100 101 func square(x int) int { 102 return x * x 103 } 104 105 func intcallpanic(a int) int { 106 if a == 0 { 107 panic("panic requested") 108 } 109 return a 110 } 111 112 func onetwothree(n int) []int { 113 return []int{n + 1, n + 2, n + 3} 114 } 115 116 func curriedAdd(n int) func(int) int { 117 return func(m int) int { 118 return n + m 119 } 120 } 121 122 func getAStruct(n int) astruct { 123 return astruct{X: n} 124 } 125 126 func getAStructPtr(n int) *astruct { 127 return &astruct{X: n} 128 } 129 130 func getVRcvrableFromAStruct(n int) VRcvrable { 131 return astruct{X: n} 132 } 133 134 func getPRcvrableFromAStructPtr(n int) PRcvrable { 135 return &astruct{X: n} 136 } 137 138 func getVRcvrableFromAStructPtr(n int) VRcvrable { 139 return &astruct{X: n} 140 } 141 142 func noreturncall(n int) { 143 return 144 } 145 146 type Base struct { 147 y int 148 } 149 150 type Derived struct { 151 x int 152 Base 153 } 154 155 func (b *Base) Method() int { 156 return b.y 157 } 158 159 type X int 160 161 func (_ X) CallMe() { 162 println("foo") 163 } 164 165 type X2 int 166 167 func (_ X2) CallMe(i int) int { 168 return i * i 169 } 170 171 func regabistacktest(s1, s2, s3, s4, s5 string, n uint8) (string, string, string, string, string, uint8) { 172 return s1 + s2, s2 + s3, s3 + s4, s4 + s5, s5 + s1, 2 * n 173 } 174 175 func regabistacktest2(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10 int) (int, int, int, int, int, int, int, int, int, int) { 176 return n1 + n2, n2 + n3, n3 + n4, n4 + n5, n5 + n6, n6 + n7, n7 + n8, n8 + n9, n9 + n10, n10 + n1 177 } 178 179 func regabistacktest3(sargs [10]string, n uint8) (r [10]string, m uint8) { 180 for i := range sargs { 181 r[i] = sargs[i] + sargs[(i+1)%len(sargs)] 182 } 183 m = n * 3 184 return 185 } 186 187 func floatsum(a, b float64) float64 { 188 return a + b 189 } 190 191 type Issue2698 struct { 192 a uint32 193 b uint8 194 c uint8 195 d uintptr 196 } 197 198 func (i Issue2698) String() string { 199 return fmt.Sprintf("%d %d %d %d", i.a, i.b, i.c, i.d) 200 } 201 202 type Issue3364 struct { 203 a int 204 b uint32 205 } 206 207 func (i Issue3364) String() string { 208 return fmt.Sprintf("%d %d", i.a, i.b) 209 } 210 211 func main() { 212 one, two := 1, 2 213 intslice := []int{1, 2, 3} 214 stringslice := []string{"one", "two", "three"} 215 comma := "," 216 a := astruct{X: 3} 217 pa := &astruct{X: 6} 218 a2 := a2struct{Y: 7} 219 var pa2 *astruct 220 var str string = "old string value" 221 longstrs := []string{"very long string 0123456789a0123456789b0123456789c0123456789d0123456789e0123456789f0123456789g012345678h90123456789i0123456789j0123456789"} 222 rast3 := [10]string{"one", "two", "three", "four", "five", "six", "seven", "height", "nine", "ten"} 223 var vable_a VRcvrable = a 224 var vable_pa VRcvrable = pa 225 var pable_pa PRcvrable = pa 226 var x X = 2 227 var x2 X2 = 2 228 issue2698 := Issue2698{ 229 a: 1, 230 b: 2, 231 c: 3, 232 d: 4, 233 } 234 issue3364 := Issue3364{ 235 a: 1, 236 b: 2, 237 } 238 239 fn2clos := makeclos(pa) 240 fn2glob := call1 241 fn2valmeth := pa.VRcvr 242 fn2ptrmeth := pa.PRcvr 243 var fn2nil func() 244 245 d := &Derived{3, Base{4}} 246 247 var ref strings.Builder 248 fmt.Fprintf(&ref, "blah") 249 250 runtime.Breakpoint() // breakpoint here 251 call1(one, two) 252 fn2clos(2) 253 strings.LastIndexByte(stringslice[1], 'w') 254 d.Method() 255 d.Base.Method() 256 x.CallMe() 257 fmt.Println(one, two, zero, call, call0, call2, callexit, callpanic, callbreak, callstacktrace, stringsJoin, intslice, stringslice, comma, a.VRcvr, a.PRcvr, pa, vable_a, vable_pa, pable_pa, fn2clos, fn2glob, fn2valmeth, fn2ptrmeth, fn2nil, ga, escapeArg, a2, square, intcallpanic, onetwothree, curriedAdd, getAStruct, getAStructPtr, getVRcvrableFromAStruct, getPRcvrableFromAStructPtr, getVRcvrableFromAStructPtr, pa2, noreturncall, str, d, x, x2.CallMe(5), longstrs, regabistacktest, regabistacktest2, issue2698.String(), issue3364.String(), regabistacktest3, rast3, floatsum, ref) 258 }