robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1" Tests for the Vim script debug commands 2 3source shared.vim 4source screendump.vim 5source check.vim 6 7func CheckCWD () 8" Check that the longer lines don't wrap due to the length of the script name 9" in cwd. Need to subtract by 1 since Vim will still wrap the message if it 10" just fits. 11let script_len = len ( getcwd () .. '/Xtest1.vim' ) 12let longest_line = len ( 'Breakpoint in "" line 1' ) 13if script_len > ( 75 - longest_line - 1 ) 14throw 'Skipped: Your CWD has too many characters' 15endif 16endfunc 17command ! -nargs = 0 -bar CheckCWD call CheckCWD() 18 19" "options" argument can contain: 20" 'msec' - time to wait for a match 21" 'match' - "pattern" to use "lines" as pattern instead of text 22func CheckDbgOutput ( buf , lines , options = {}) 23" Verify the expected output 24let lnum = 20 - len ( a: lines ) 25let msec = get ( a: options , 'msec' , 1000 ) 26for l in a: lines 27if get ( a: options , 'match' , 'equal' ) ==# 'pattern' 28call WaitForAssert ({ ->assert_match ( l , term_getline ( a: buf , lnum ))}, msec ) 29else 30call WaitForAssert ({ ->assert_equal ( l , term_getline ( a: buf , lnum ))}, msec ) 31endif 32let lnum += 1 33endfor 34endfunc 35 36" Run a Vim debugger command 37" If the expected output argument is supplied, then check for it. 38func s: RunDbgCmd ( buf , cmd , ...) 39call term_sendkeys ( a: buf , a: cmd . "\r" ) 40call TermWait ( a: buf ) 41 42if a: 0 != 0 43let options = #{ match : 'equal' } 44if a: 0 > 1 45call extend ( options , a: 2 ) 46endif 47call CheckDbgOutput ( a: buf , a: 1 , options ) 48endif 49endfunc 50 51" Debugger tests 52func Test_Debugger () 53CheckRunVimInTerminal 54 55" Create a Vim script with some functions 56let lines =<< trim END 57func Foo() 58let var1 = 1 59let var2 = Bar(var1) + 9 60return var2 61endfunc 62func Bar(var) 63let var1 = 2 + a:var 64let var2 = Bazz(var1) + 4 65return var2 66endfunc 67func Bazz(var) 68try 69let var1 = 3 + a:var 70let var3 = "another var" 71let var3 = "value2" 72catch 73let var4 = "exception" 74endtry 75return var1 76endfunc 77def Vim9Func() 78for cmd in ['confirm', 'xxxxxxx'] 79for _ in [1, 2] 80echo cmd 81endfor 82endfor 83enddef 84END 85call writefile ( lines , 'XtestDebug.vim' , 'D' ) 86 87" Start Vim in a terminal 88let buf = RunVimInTerminal ( '-S XtestDebug.vim' , {}) 89 90" Start the Vim debugger 91call s: RunDbgCmd ( buf , ':debug echo Foo()' , [ 'cmd: echo Foo()' ]) 92 93" Create a few stack frames by stepping through functions 94call s: RunDbgCmd ( buf , 'step' , [ 'line 1: let var1 = 1' ]) 95call s: RunDbgCmd ( buf , 'step' , [ 'line 2: let var2 = Bar(var1) + 9' ]) 96call s: RunDbgCmd ( buf , 'step' , [ 'line 1: let var1 = 2 + a:var' ]) 97call s: RunDbgCmd ( buf , 'step' , [ 'line 2: let var2 = Bazz(var1) + 4' ]) 98call s: RunDbgCmd ( buf , 'step' , [ 'line 1: try' ]) 99call s: RunDbgCmd ( buf , 'step' , [ 'line 2: let var1 = 3 + a:var' ]) 100call s: RunDbgCmd ( buf , 'step' , [ 'line 3: let var3 = "another var"' ]) 101 102" check backtrace 103call s: RunDbgCmd ( buf , 'backtrace' , [ 104 \' 2 function Foo[2]' , 105 \' 1 Bar[2]' , 106 \'->0 Bazz' , 107 \'line 3: let var3 = "another var"' ]) 108 109" Check variables in different stack frames 110call s: RunDbgCmd ( buf , 'echo var1' , [ '6' ]) 111 112call s: RunDbgCmd ( buf , 'up' ) 113call s: RunDbgCmd ( buf , 'back' , [ 114 \' 2 function Foo[2]' , 115 \'->1 Bar[2]' , 116 \' 0 Bazz' , 117 \'line 3: let var3 = "another var"' ]) 118call s: RunDbgCmd ( buf , 'echo var1' , [ '3' ]) 119 120call s: RunDbgCmd ( buf , 'u' ) 121call s: RunDbgCmd ( buf , 'bt' , [ 122 \'->2 function Foo[2]' , 123 \' 1 Bar[2]' , 124 \' 0 Bazz' , 125 \'line 3: let var3 = "another var"' ]) 126call s: RunDbgCmd ( buf , 'echo var1' , [ '1' ]) 127 128" Undefined variables 129call s: RunDbgCmd ( buf , 'step' ) 130call s: RunDbgCmd ( buf , 'frame 2' ) 131call s: RunDbgCmd ( buf , 'echo var3' , [ 132 \'Error in function Foo[2]..Bar[2]..Bazz:' , 133 \'line 4:' , 134 \'E121: Undefined variable: var3' ]) 135 136" var3 is defined in this level with some other value 137call s: RunDbgCmd ( buf , 'fr 0' ) 138call s: RunDbgCmd ( buf , 'echo var3' , [ 'another var' ]) 139 140call s: RunDbgCmd ( buf , 'step' ) 141call s: RunDbgCmd ( buf , '' ) 142call s: RunDbgCmd ( buf , '' ) 143call s: RunDbgCmd ( buf , '' ) 144call s: RunDbgCmd ( buf , '' ) 145call s: RunDbgCmd ( buf , 'step' , [ 146 \'function Foo[2]..Bar' , 147 \'line 3: End of function' ]) 148call s: RunDbgCmd ( buf , 'up' ) 149 150" Undefined var2 151call s: RunDbgCmd ( buf , 'echo var2' , [ 152 \'Error in function Foo[2]..Bar:' , 153 \'line 3:' , 154 \'E121: Undefined variable: var2' ]) 155 156" Var2 is defined with 10 157call s: RunDbgCmd ( buf , 'down' ) 158call s: RunDbgCmd ( buf , 'echo var2' , [ '10' ]) 159 160" Backtrace movements 161call s: RunDbgCmd ( buf , 'b' , [ 162 \' 1 function Foo[2]' , 163 \'->0 Bar' , 164 \'line 3: End of function' ]) 165 166" next command cannot go down, we are on bottom 167call s: RunDbgCmd ( buf , 'down' , [ 'frame is zero' ]) 168call s: RunDbgCmd ( buf , 'up' ) 169 170" next command cannot go up, we are on top 171call s: RunDbgCmd ( buf , 'up' , [ 'frame at highest level: 1' ]) 172call s: RunDbgCmd ( buf , 'where' , [ 173 \'->1 function Foo[2]' , 174 \' 0 Bar' , 175 \'line 3: End of function' ]) 176 177" fil is not frame or finish, it is file 178call s: RunDbgCmd ( buf , 'fil' , [ '"[No Name]" --No lines in buffer--' ]) 179 180" relative backtrace movement 181call s: RunDbgCmd ( buf , 'fr -1' ) 182call s: RunDbgCmd ( buf , 'frame' , [ 183 \' 1 function Foo[2]' , 184 \'->0 Bar' , 185 \'line 3: End of function' ]) 186 187call s: RunDbgCmd ( buf , 'fr +1' ) 188call s: RunDbgCmd ( buf , 'fram' , [ 189 \'->1 function Foo[2]' , 190 \' 0 Bar' , 191 \'line 3: End of function' ]) 192 193" go beyond limits does not crash 194call s: RunDbgCmd ( buf , 'fr 100' , [ 'frame at highest level: 1' ]) 195call s: RunDbgCmd ( buf , 'fra' , [ 196 \'->1 function Foo[2]' , 197 \' 0 Bar' , 198 \'line 3: End of function' ]) 199 200call s: RunDbgCmd ( buf , 'frame -40' , [ 'frame is zero' ]) 201call s: RunDbgCmd ( buf , 'fram' , [ 202 \' 1 function Foo[2]' , 203 \'->0 Bar' , 204 \'line 3: End of function' ]) 205 206" final result 19 207call s: RunDbgCmd ( buf , 'cont' , [ '19' ]) 208 209" breakpoints tests 210 211" Start a debug session, so that reading the last line from the terminal 212" works properly. 213call s: RunDbgCmd ( buf , ':debug echo Foo()' , [ 'cmd: echo Foo()' ]) 214 215" No breakpoints 216call s: RunDbgCmd ( buf , 'breakl' , [ 'No breakpoints defined' ]) 217 218" Place some breakpoints 219call s: RunDbgCmd ( buf , 'breaka func Bar' ) 220call s: RunDbgCmd ( buf , 'breaklis' , [ ' 1 func Bar line 1' ]) 221call s: RunDbgCmd ( buf , 'breakadd func 3 Bazz' ) 222call s: RunDbgCmd ( buf , 'breaklist' , [ ' 1 func Bar line 1' , 223 \' 2 func Bazz line 3' ]) 224 225" Check whether the breakpoints are hit 226call s: RunDbgCmd ( buf , 'cont' , [ 227 \'Breakpoint in "Bar" line 1' , 228 \'function Foo[2]..Bar' , 229 \'line 1: let var1 = 2 + a:var' ]) 230call s: RunDbgCmd ( buf , 'cont' , [ 231 \'Breakpoint in "Bazz" line 3' , 232 \'function Foo[2]..Bar[2]..Bazz' , 233 \'line 3: let var3 = "another var"' ]) 234 235" Delete the breakpoints 236call s: RunDbgCmd ( buf , 'breakd 1' ) 237call s: RunDbgCmd ( buf , 'breakli' , [ ' 2 func Bazz line 3' ]) 238call s: RunDbgCmd ( buf , 'breakdel func 3 Bazz' ) 239call s: RunDbgCmd ( buf , 'breakl' , [ 'No breakpoints defined' ]) 240 241call s: RunDbgCmd ( buf , 'cont' ) 242 243" Make sure the breakpoints are removed 244call s: RunDbgCmd ( buf , ':echo Foo()' , [ '19' ]) 245 246" Delete a non-existing breakpoint 247call s: RunDbgCmd ( buf , ':breakdel 2' , [ 'E161: Breakpoint not found: 2' ]) 248 249" Expression breakpoint 250call s: RunDbgCmd ( buf , ':breakadd func 2 Bazz' ) 251call s: RunDbgCmd ( buf , ':echo Bazz(1)' , [ 252 \'Entering Debug mode. Type "cont" to continue.' , 253 \'function Bazz' , 254 \'line 2: let var1 = 3 + a:var' ]) 255call s: RunDbgCmd ( buf , 'step' ) 256call s: RunDbgCmd ( buf , 'step' ) 257call s: RunDbgCmd ( buf , 'breaka expr var3' ) 258call s: RunDbgCmd ( buf , 'breakl' , [ ' 3 func Bazz line 2' , 259 \' 4 expr var3' ]) 260call s: RunDbgCmd ( buf , 'cont' , [ 'Breakpoint in "Bazz" line 5' , 261 \'Oldval = "''another var''"' , 262 \'Newval = "''value2''"' , 263 \'function Bazz' , 264 \'line 5: catch' ]) 265 266call s: RunDbgCmd ( buf , 'breakdel *' ) 267call s: RunDbgCmd ( buf , 'breakl' , [ 'No breakpoints defined' ]) 268 269" Check for error cases 270call s: RunDbgCmd ( buf , 'breakadd abcd' , [ 271 \'Error in function Bazz:' , 272 \'line 5:' , 273 \'E475: Invalid argument: abcd' ]) 274call s: RunDbgCmd ( buf , 'breakadd func' , [ 'E475: Invalid argument: func' ]) 275call s: RunDbgCmd ( buf , 'breakadd func 2' , [ 'E475: Invalid argument: func 2' ]) 276call s: RunDbgCmd ( buf , 'breaka func a()' , [ 'E475: Invalid argument: func a()' ]) 277call s: RunDbgCmd ( buf , 'breakd abcd' , [ 'E475: Invalid argument: abcd' ]) 278call s: RunDbgCmd ( buf , 'breakd func' , [ 'E475: Invalid argument: func' ]) 279call s: RunDbgCmd ( buf , 'breakd func a()' , [ 'E475: Invalid argument: func a()' ]) 280call s: RunDbgCmd ( buf , 'breakd func a' , [ 'E161: Breakpoint not found: func a' ]) 281call s: RunDbgCmd ( buf , 'breakd expr' , [ 'E475: Invalid argument: expr' ]) 282call s: RunDbgCmd ( buf , 'breakd expr x' , [ 'E161: Breakpoint not found: expr x' ]) 283 284" finish the current function 285call s: RunDbgCmd ( buf , 'finish' , [ 286 \'function Bazz' , 287 \'line 8: End of function' ]) 288call s: RunDbgCmd ( buf , 'cont' ) 289 290" Test for :next 291call s: RunDbgCmd ( buf , ':debug echo Bar(1)' ) 292call s: RunDbgCmd ( buf , 'step' ) 293call s: RunDbgCmd ( buf , 'next' ) 294call s: RunDbgCmd ( buf , '' , [ 295 \'function Bar' , 296 \'line 3: return var2' ]) 297call s: RunDbgCmd ( buf , 'c' ) 298 299" Test for :interrupt 300call s: RunDbgCmd ( buf , ':debug echo Bazz(1)' ) 301call s: RunDbgCmd ( buf , 'step' ) 302call s: RunDbgCmd ( buf , 'step' ) 303call s: RunDbgCmd ( buf , 'interrupt' , [ 304 \'Exception thrown: Vim:Interrupt' , 305 \'function Bazz' , 306 \'line 5: catch' ]) 307call s: RunDbgCmd ( buf , 'c' ) 308 309" Test showing local variable in :def function 310call s: RunDbgCmd ( buf , ':breakadd func 2 Vim9Func' ) 311call s: RunDbgCmd ( buf , ':call Vim9Func()' , [ 'line 2: for _ in [1, 2]' ]) 312call s: RunDbgCmd ( buf , 'next' , [ 'line 2: for _ in [1, 2]' ]) 313call s: RunDbgCmd ( buf , 'echo cmd' , [ 'confirm' ]) 314call s: RunDbgCmd ( buf , 'breakdel *' ) 315call s: RunDbgCmd ( buf , 'cont' ) 316 317" Test for :quit 318call s: RunDbgCmd ( buf , ':debug echo Foo()' ) 319call s: RunDbgCmd ( buf , 'breakdel *' ) 320call s: RunDbgCmd ( buf , 'breakadd func 3 Foo' ) 321call s: RunDbgCmd ( buf , 'breakadd func 3 Bazz' ) 322call s: RunDbgCmd ( buf , 'cont' , [ 323 \'Breakpoint in "Bazz" line 3' , 324 \'function Foo[2]..Bar[2]..Bazz' , 325 \'line 3: let var3 = "another var"' ]) 326call s: RunDbgCmd ( buf , 'quit' , [ 327 \'Breakpoint in "Foo" line 3' , 328 \'function Foo' , 329 \'line 3: return var2' ]) 330call s: RunDbgCmd ( buf , 'breakdel *' ) 331call s: RunDbgCmd ( buf , 'quit' ) 332call s: RunDbgCmd ( buf , 'enew! | only!' ) 333 334call StopVimInTerminal ( buf ) 335endfunc 336 337func Test_Debugger_breakadd () 338" Tests for :breakadd file and :breakadd here 339" Breakpoints should be set before sourcing the file 340CheckRunVimInTerminal 341 342let lines =<< trim END 343let var1 = 10 344let var2 = 20 345let var3 = 30 346let var4 = 40 347END 348call writefile ( lines , 'XdebugBreakadd.vim' , 'D' ) 349 350" Start Vim in a terminal 351let buf = RunVimInTerminal ( 'XdebugBreakadd.vim' , {}) 352call s: RunDbgCmd ( buf , ':breakadd file 2 XdebugBreakadd.vim' ) 353call s: RunDbgCmd ( buf , ':4 | breakadd here' ) 354call s: RunDbgCmd ( buf , ':source XdebugBreakadd.vim' , [ 'line 2: let var2 = 20' ]) 355call s: RunDbgCmd ( buf , 'cont' , [ 'line 4: let var4 = 40' ]) 356call s: RunDbgCmd ( buf , 'cont' ) 357 358call StopVimInTerminal ( buf ) 359 360 %bw ! 361 362call assert_fails ( 'breakadd here' , 'E32:' ) 363call assert_fails ( 'breakadd file Xtest.vim /\)/' , 'E55:' ) 364endfunc 365 366" Test for expression breakpoint set using ":breakadd expr <expr>" 367" FIXME: This doesn't seem to work as documented. The breakpoint is not 368" triggered until the next function call. 369func Test_Debugger_breakadd_expr () 370CheckRunVimInTerminal 371CheckCWD 372 373let lines =<< trim END 374func Foo() 375eval 1 376eval 2 377endfunc 378 379let g:Xtest_var += 1 380call Foo() 381let g:Xtest_var += 1 382call Foo() 383END 384call writefile ( lines , 'XbreakExpr.vim' , 'D' ) 385 386" Start Vim in a terminal 387let buf = RunVimInTerminal ( 'XbreakExpr.vim' , {}) 388call s: RunDbgCmd ( buf , ':let g:Xtest_var = 10' ) 389call s: RunDbgCmd ( buf , ':breakadd expr g:Xtest_var' ) 390let expected =<< trim eval END 391Oldval = "10" 392Newval = "11" 393{fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo 394line 1: eval 1 395END 396call s: RunDbgCmd ( buf , ':source %' , expected ) 397let expected =<< trim eval END 398Oldval = "11" 399Newval = "12" 400{fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo 401line 1: eval 1 402END 403call s: RunDbgCmd ( buf , 'cont' , expected ) 404call s: RunDbgCmd ( buf , 'cont' ) 405 406" Check the behavior without the g: prefix. 407" FIXME: The Oldval and Newval don't look right here. 408call s: RunDbgCmd ( buf , ':breakdel *' ) 409call s: RunDbgCmd ( buf , ':breakadd expr Xtest_var' ) 410let expected =<< trim eval END 411Oldval = "13" 412Newval = "(does not exist)" 413{fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo 414line 1: eval 1 415END 416call s: RunDbgCmd ( buf , ':source %' , expected ) 417let expected =<< trim eval END 418{fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo 419line 2: eval 2 420END 421call s: RunDbgCmd ( buf , 'cont' , expected ) 422let expected =<< trim eval END 423Oldval = "14" 424Newval = "(does not exist)" 425{fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo 426line 1: eval 1 427END 428call s: RunDbgCmd ( buf , 'cont' , expected ) 429let expected =<< trim eval END 430{fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo 431line 2: eval 2 432END 433call s: RunDbgCmd ( buf , 'cont' , expected ) 434call s: RunDbgCmd ( buf , 'cont' ) 435 436call StopVimInTerminal ( buf ) 437endfunc 438 439" def Test_Debugger_break_at_return() 440" var lines =<< trim END 441" vim9script 442" def g:GetNum(): number 443" return 1 444" + 2 445" + 3 446" enddef 447" breakadd func GetNum 448" END 449" writefile(lines, 'Xtest.vim') 450" 451" # Start Vim in a terminal 452" var buf = RunVimInTerminal('-S Xtest.vim', {wait_for_ruler: 0}) 453" call TermWait(buf) 454" 455" RunDbgCmd(buf, ':call GetNum()', 456" ['line 1: return 1 + 2 + 3'], {match: 'pattern'}) 457" 458" call StopVimInTerminal(buf) 459" call delete('Xtest.vim') 460" enddef 461 462func Test_Backtrace_Through_Source () 463CheckRunVimInTerminal 464CheckCWD 465let file1 =<< trim END 466func SourceAnotherFile() 467source Xtest2.vim 468endfunc 469 470func CallAFunction() 471call SourceAnotherFile() 472call File2Function() 473endfunc 474 475func GlobalFunction() 476call CallAFunction() 477endfunc 478END 479call writefile ( file1 , 'Xtest1.vim' , 'D' ) 480 481let file2 =<< trim END 482func DoAThing() 483echo "DoAThing" 484endfunc 485 486func File2Function() 487call DoAThing() 488endfunc 489 490call File2Function() 491END 492call writefile ( file2 , 'Xtest2.vim' , 'D' ) 493 494let buf = RunVimInTerminal ( '-S Xtest1.vim' , {}) 495 496call s: RunDbgCmd ( buf , 497 \':debug call GlobalFunction()' , 498 \[ 'cmd: call GlobalFunction()' ]) 499call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call CallAFunction()' ]) 500 501call s: RunDbgCmd ( buf , 'backtrace' , [ '>backtrace' , 502 \'->0 function GlobalFunction' , 503 \'line 1: call CallAFunction()' ]) 504 505call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call SourceAnotherFile()' ]) 506call s: RunDbgCmd ( buf , 'step' , [ 'line 1: source Xtest2.vim' ]) 507 508call s: RunDbgCmd ( buf , 'backtrace' , [ '>backtrace' , 509 \' 2 function GlobalFunction[1]' , 510 \' 1 CallAFunction[1]' , 511 \'->0 SourceAnotherFile' , 512 \'line 1: source Xtest2.vim' ]) 513 514" Step into the 'source' command. Note that we print the full trace all the 515" way though the source command. 516call s: RunDbgCmd ( buf , 'step' , [ 'line 1: func DoAThing()' ]) 517call s: RunDbgCmd ( buf , 'backtrace' , [ 518 \'>backtrace' , 519 \' 3 function GlobalFunction[1]' , 520 \' 2 CallAFunction[1]' , 521 \' 1 SourceAnotherFile[1]' , 522 \'->0 script ' .. getcwd () .. '/Xtest2.vim' , 523 \'line 1: func DoAThing()' ]) 524 525call s: RunDbgCmd ( buf , 'up' ) 526call s: RunDbgCmd ( buf , 'backtrace' , [ 527 \'>backtrace' , 528 \' 3 function GlobalFunction[1]' , 529 \' 2 CallAFunction[1]' , 530 \'->1 SourceAnotherFile[1]' , 531 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 532 \'line 1: func DoAThing()' ] ) 533 534call s: RunDbgCmd ( buf , 'up' ) 535call s: RunDbgCmd ( buf , 'backtrace' , [ 536 \'>backtrace' , 537 \' 3 function GlobalFunction[1]' , 538 \'->2 CallAFunction[1]' , 539 \' 1 SourceAnotherFile[1]' , 540 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 541 \'line 1: func DoAThing()' ] ) 542 543call s: RunDbgCmd ( buf , 'up' ) 544call s: RunDbgCmd ( buf , 'backtrace' , [ 545 \'>backtrace' , 546 \'->3 function GlobalFunction[1]' , 547 \' 2 CallAFunction[1]' , 548 \' 1 SourceAnotherFile[1]' , 549 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 550 \'line 1: func DoAThing()' ] ) 551 552call s: RunDbgCmd ( buf , 'up' , [ 'frame at highest level: 3' ] ) 553call s: RunDbgCmd ( buf , 'backtrace' , [ 554 \'>backtrace' , 555 \'->3 function GlobalFunction[1]' , 556 \' 2 CallAFunction[1]' , 557 \' 1 SourceAnotherFile[1]' , 558 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 559 \'line 1: func DoAThing()' ] ) 560 561call s: RunDbgCmd ( buf , 'down' ) 562call s: RunDbgCmd ( buf , 'backtrace' , [ 563 \'>backtrace' , 564 \' 3 function GlobalFunction[1]' , 565 \'->2 CallAFunction[1]' , 566 \' 1 SourceAnotherFile[1]' , 567 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 568 \'line 1: func DoAThing()' ] ) 569 570call s: RunDbgCmd ( buf , 'down' ) 571call s: RunDbgCmd ( buf , 'backtrace' , [ 572 \'>backtrace' , 573 \' 3 function GlobalFunction[1]' , 574 \' 2 CallAFunction[1]' , 575 \'->1 SourceAnotherFile[1]' , 576 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 577 \'line 1: func DoAThing()' ] ) 578 579call s: RunDbgCmd ( buf , 'down' ) 580call s: RunDbgCmd ( buf , 'backtrace' , [ 581 \'>backtrace' , 582 \' 3 function GlobalFunction[1]' , 583 \' 2 CallAFunction[1]' , 584 \' 1 SourceAnotherFile[1]' , 585 \'->0 script ' .. getcwd () .. '/Xtest2.vim' , 586 \'line 1: func DoAThing()' ] ) 587 588call s: RunDbgCmd ( buf , 'down' , [ 'frame is zero' ] ) 589 590" step until we have another meaningful trace 591call s: RunDbgCmd ( buf , 'step' , [ 'line 5: func File2Function()' ]) 592call s: RunDbgCmd ( buf , 'step' , [ 'line 9: call File2Function()' ]) 593call s: RunDbgCmd ( buf , 'backtrace' , [ 594 \'>backtrace' , 595 \' 3 function GlobalFunction[1]' , 596 \' 2 CallAFunction[1]' , 597 \' 1 SourceAnotherFile[1]' , 598 \'->0 script ' .. getcwd () .. '/Xtest2.vim' , 599 \'line 9: call File2Function()' ]) 600 601call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call DoAThing()' ]) 602call s: RunDbgCmd ( buf , 'step' , [ 'line 1: echo "DoAThing"' ]) 603call s: RunDbgCmd ( buf , 'backtrace' , [ 604 \'>backtrace' , 605 \' 5 function GlobalFunction[1]' , 606 \' 4 CallAFunction[1]' , 607 \' 3 SourceAnotherFile[1]' , 608 \' 2 script ' .. getcwd () .. '/Xtest2.vim[9]' , 609 \' 1 function File2Function[1]' , 610 \'->0 DoAThing' , 611 \'line 1: echo "DoAThing"' ]) 612 613" Now, step (back to Xfile1.vim), and call the function _in_ Xfile2.vim 614call s: RunDbgCmd ( buf , 'step' , [ 'line 1: End of function' ]) 615call s: RunDbgCmd ( buf , 'step' , [ 'line 1: End of function' ]) 616call s: RunDbgCmd ( buf , 'step' , [ 'line 10: End of sourced file' ]) 617call s: RunDbgCmd ( buf , 'step' , [ 'line 1: End of function' ]) 618call s: RunDbgCmd ( buf , 'step' , [ 'line 2: call File2Function()' ]) 619call s: RunDbgCmd ( buf , 'backtrace' , [ 620 \'>backtrace' , 621 \' 1 function GlobalFunction[1]' , 622 \'->0 CallAFunction' , 623 \'line 2: call File2Function()' ]) 624 625call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call DoAThing()' ]) 626call s: RunDbgCmd ( buf , 'backtrace' , [ 627 \'>backtrace' , 628 \' 2 function GlobalFunction[1]' , 629 \' 1 CallAFunction[2]' , 630 \'->0 File2Function' , 631 \'line 1: call DoAThing()' ]) 632 633call StopVimInTerminal ( buf ) 634endfunc 635 636func Test_Backtrace_Autocmd () 637CheckRunVimInTerminal 638CheckCWD 639let file1 =<< trim END 640func SourceAnotherFile() 641source Xtest2.vim 642endfunc 643 644func CallAFunction() 645call SourceAnotherFile() 646call File2Function() 647endfunc 648 649func GlobalFunction() 650call CallAFunction() 651endfunc 652 653au User TestGlobalFunction :call GlobalFunction() | echo "Done" 654END 655call writefile ( file1 , 'Xtest1.vim' , 'D' ) 656 657let file2 =<< trim END 658func DoAThing() 659echo "DoAThing" 660endfunc 661 662func File2Function() 663call DoAThing() 664endfunc 665 666call File2Function() 667END 668call writefile ( file2 , 'Xtest2.vim' , 'D' ) 669 670let buf = RunVimInTerminal ( '-S Xtest1.vim' , {}) 671 672call s: RunDbgCmd ( buf , 673 \':debug doautocmd User TestGlobalFunction' , 674 \[ 'cmd: doautocmd User TestGlobalFunction' ]) 675call s: RunDbgCmd ( buf , 'step' , [ 'cmd: call GlobalFunction() | echo "Done"' ]) 676 677" At this point the only thing in the stack is the autocommand 678call s: RunDbgCmd ( buf , 'backtrace' , [ 679 \'>backtrace' , 680 \'->0 User Autocommands for "TestGlobalFunction"' , 681 \'cmd: call GlobalFunction() | echo "Done"' ]) 682 683" And now we're back into the call stack 684call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call CallAFunction()' ]) 685call s: RunDbgCmd ( buf , 'backtrace' , [ 686 \'>backtrace' , 687 \' 1 User Autocommands for "TestGlobalFunction"' , 688 \'->0 function GlobalFunction' , 689 \'line 1: call CallAFunction()' ]) 690 691call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call SourceAnotherFile()' ]) 692call s: RunDbgCmd ( buf , 'step' , [ 'line 1: source Xtest2.vim' ]) 693 694call s: RunDbgCmd ( buf , 'backtrace' , [ 695 \'>backtrace' , 696 \' 3 User Autocommands for "TestGlobalFunction"' , 697 \' 2 function GlobalFunction[1]' , 698 \' 1 CallAFunction[1]' , 699 \'->0 SourceAnotherFile' , 700 \'line 1: source Xtest2.vim' ]) 701 702" Step into the 'source' command. Note that we print the full trace all the 703" way though the source command. 704call s: RunDbgCmd ( buf , 'step' , [ 'line 1: func DoAThing()' ]) 705call s: RunDbgCmd ( buf , 'backtrace' , [ 706 \'>backtrace' , 707 \' 4 User Autocommands for "TestGlobalFunction"' , 708 \' 3 function GlobalFunction[1]' , 709 \' 2 CallAFunction[1]' , 710 \' 1 SourceAnotherFile[1]' , 711 \'->0 script ' .. getcwd () .. '/Xtest2.vim' , 712 \'line 1: func DoAThing()' ]) 713 714call s: RunDbgCmd ( buf , 'up' ) 715call s: RunDbgCmd ( buf , 'backtrace' , [ 716 \'>backtrace' , 717 \' 4 User Autocommands for "TestGlobalFunction"' , 718 \' 3 function GlobalFunction[1]' , 719 \' 2 CallAFunction[1]' , 720 \'->1 SourceAnotherFile[1]' , 721 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 722 \'line 1: func DoAThing()' ] ) 723 724call s: RunDbgCmd ( buf , 'up' ) 725call s: RunDbgCmd ( buf , 'backtrace' , [ 726 \'>backtrace' , 727 \' 4 User Autocommands for "TestGlobalFunction"' , 728 \' 3 function GlobalFunction[1]' , 729 \'->2 CallAFunction[1]' , 730 \' 1 SourceAnotherFile[1]' , 731 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 732 \'line 1: func DoAThing()' ] ) 733 734call s: RunDbgCmd ( buf , 'up' ) 735call s: RunDbgCmd ( buf , 'backtrace' , [ 736 \'>backtrace' , 737 \' 4 User Autocommands for "TestGlobalFunction"' , 738 \'->3 function GlobalFunction[1]' , 739 \' 2 CallAFunction[1]' , 740 \' 1 SourceAnotherFile[1]' , 741 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 742 \'line 1: func DoAThing()' ] ) 743 744call s: RunDbgCmd ( buf , 'up' ) 745call s: RunDbgCmd ( buf , 'backtrace' , [ 746 \'>backtrace' , 747 \'->4 User Autocommands for "TestGlobalFunction"' , 748 \' 3 function GlobalFunction[1]' , 749 \' 2 CallAFunction[1]' , 750 \' 1 SourceAnotherFile[1]' , 751 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 752 \'line 1: func DoAThing()' ] ) 753 754call s: RunDbgCmd ( buf , 'up' , [ 'frame at highest level: 4' ] ) 755call s: RunDbgCmd ( buf , 'backtrace' , [ 756 \'>backtrace' , 757 \'->4 User Autocommands for "TestGlobalFunction"' , 758 \' 3 function GlobalFunction[1]' , 759 \' 2 CallAFunction[1]' , 760 \' 1 SourceAnotherFile[1]' , 761 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 762 \'line 1: func DoAThing()' ] ) 763 764call s: RunDbgCmd ( buf , 'down' ) 765call s: RunDbgCmd ( buf , 'backtrace' , [ 766 \'>backtrace' , 767 \' 4 User Autocommands for "TestGlobalFunction"' , 768 \'->3 function GlobalFunction[1]' , 769 \' 2 CallAFunction[1]' , 770 \' 1 SourceAnotherFile[1]' , 771 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 772 \'line 1: func DoAThing()' ] ) 773 774 775call s: RunDbgCmd ( buf , 'down' ) 776call s: RunDbgCmd ( buf , 'backtrace' , [ 777 \'>backtrace' , 778 \' 4 User Autocommands for "TestGlobalFunction"' , 779 \' 3 function GlobalFunction[1]' , 780 \'->2 CallAFunction[1]' , 781 \' 1 SourceAnotherFile[1]' , 782 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 783 \'line 1: func DoAThing()' ] ) 784 785call s: RunDbgCmd ( buf , 'down' ) 786call s: RunDbgCmd ( buf , 'backtrace' , [ 787 \'>backtrace' , 788 \' 4 User Autocommands for "TestGlobalFunction"' , 789 \' 3 function GlobalFunction[1]' , 790 \' 2 CallAFunction[1]' , 791 \'->1 SourceAnotherFile[1]' , 792 \' 0 script ' .. getcwd () .. '/Xtest2.vim' , 793 \'line 1: func DoAThing()' ] ) 794 795call s: RunDbgCmd ( buf , 'down' ) 796call s: RunDbgCmd ( buf , 'backtrace' , [ 797 \'>backtrace' , 798 \' 4 User Autocommands for "TestGlobalFunction"' , 799 \' 3 function GlobalFunction[1]' , 800 \' 2 CallAFunction[1]' , 801 \' 1 SourceAnotherFile[1]' , 802 \'->0 script ' .. getcwd () .. '/Xtest2.vim' , 803 \'line 1: func DoAThing()' ] ) 804 805call s: RunDbgCmd ( buf , 'down' , [ 'frame is zero' ] ) 806 807" step until we have another meaningful trace 808call s: RunDbgCmd ( buf , 'step' , [ 'line 5: func File2Function()' ]) 809call s: RunDbgCmd ( buf , 'step' , [ 'line 9: call File2Function()' ]) 810call s: RunDbgCmd ( buf , 'backtrace' , [ 811 \'>backtrace' , 812 \' 4 User Autocommands for "TestGlobalFunction"' , 813 \' 3 function GlobalFunction[1]' , 814 \' 2 CallAFunction[1]' , 815 \' 1 SourceAnotherFile[1]' , 816 \'->0 script ' .. getcwd () .. '/Xtest2.vim' , 817 \'line 9: call File2Function()' ]) 818 819call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call DoAThing()' ]) 820call s: RunDbgCmd ( buf , 'step' , [ 'line 1: echo "DoAThing"' ]) 821call s: RunDbgCmd ( buf , 'backtrace' , [ 822 \'>backtrace' , 823 \' 6 User Autocommands for "TestGlobalFunction"' , 824 \' 5 function GlobalFunction[1]' , 825 \' 4 CallAFunction[1]' , 826 \' 3 SourceAnotherFile[1]' , 827 \' 2 script ' .. getcwd () .. '/Xtest2.vim[9]' , 828 \' 1 function File2Function[1]' , 829 \'->0 DoAThing' , 830 \'line 1: echo "DoAThing"' ]) 831 832" Now, step (back to Xfile1.vim), and call the function _in_ Xfile2.vim 833call s: RunDbgCmd ( buf , 'step' , [ 'line 1: End of function' ]) 834call s: RunDbgCmd ( buf , 'step' , [ 'line 1: End of function' ]) 835call s: RunDbgCmd ( buf , 'step' , [ 'line 10: End of sourced file' ]) 836call s: RunDbgCmd ( buf , 'step' , [ 'line 1: End of function' ]) 837call s: RunDbgCmd ( buf , 'step' , [ 'line 2: call File2Function()' ]) 838call s: RunDbgCmd ( buf , 'backtrace' , [ 839 \'>backtrace' , 840 \' 2 User Autocommands for "TestGlobalFunction"' , 841 \' 1 function GlobalFunction[1]' , 842 \'->0 CallAFunction' , 843 \'line 2: call File2Function()' ]) 844 845call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call DoAThing()' ]) 846call s: RunDbgCmd ( buf , 'backtrace' , [ 847 \'>backtrace' , 848 \' 3 User Autocommands for "TestGlobalFunction"' , 849 \' 2 function GlobalFunction[1]' , 850 \' 1 CallAFunction[2]' , 851 \'->0 File2Function' , 852 \'line 1: call DoAThing()' ]) 853 854 855" Now unwind so that we get back to the original autocommand (and the second 856" cmd echo "Done") 857call s: RunDbgCmd ( buf , 'finish' , [ 'line 1: End of function' ]) 858call s: RunDbgCmd ( buf , 'backtrace' , [ 859 \'>backtrace' , 860 \' 3 User Autocommands for "TestGlobalFunction"' , 861 \' 2 function GlobalFunction[1]' , 862 \' 1 CallAFunction[2]' , 863 \'->0 File2Function' , 864 \'line 1: End of function' ]) 865 866call s: RunDbgCmd ( buf , 'finish' , [ 'line 2: End of function' ]) 867call s: RunDbgCmd ( buf , 'backtrace' , [ 868 \'>backtrace' , 869 \' 2 User Autocommands for "TestGlobalFunction"' , 870 \' 1 function GlobalFunction[1]' , 871 \'->0 CallAFunction' , 872 \'line 2: End of function' ]) 873 874call s: RunDbgCmd ( buf , 'finish' , [ 'line 1: End of function' ]) 875call s: RunDbgCmd ( buf , 'backtrace' , [ 876 \'>backtrace' , 877 \' 1 User Autocommands for "TestGlobalFunction"' , 878 \'->0 function GlobalFunction' , 879 \'line 1: End of function' ]) 880 881call s: RunDbgCmd ( buf , 'step' , [ 'cmd: echo "Done"' ]) 882call s: RunDbgCmd ( buf , 'backtrace' , [ 883 \'>backtrace' , 884 \'->0 User Autocommands for "TestGlobalFunction"' , 885 \'cmd: echo "Done"' ]) 886 887call StopVimInTerminal ( buf ) 888endfunc 889 890func Test_Backtrace_CmdLine () 891CheckRunVimInTerminal 892CheckCWD 893let file1 =<< trim END 894func SourceAnotherFile() 895source Xtest2.vim 896endfunc 897 898func CallAFunction() 899call SourceAnotherFile() 900call File2Function() 901endfunc 902 903func GlobalFunction() 904call CallAFunction() 905endfunc 906 907au User TestGlobalFunction :call GlobalFunction() | echo "Done" 908END 909call writefile ( file1 , 'Xtest1.vim' , 'D' ) 910 911let file2 =<< trim END 912func DoAThing() 913echo "DoAThing" 914endfunc 915 916func File2Function() 917call DoAThing() 918endfunc 919 920call File2Function() 921END 922call writefile ( file2 , 'Xtest2.vim' , 'D' ) 923 924let buf = RunVimInTerminal ( 925 \'-S Xtest1.vim -c "debug call GlobalFunction()"' , 926 \{ 'wait_for_ruler' : 0 }) 927 928" Need to wait for the vim-in-terminal to be ready. 929" With valgrind this can take quite long. 930call CheckDbgOutput ( buf , [ 'command line' , 931 \'cmd: call GlobalFunction()' ], #{ msec : 5000 }) 932 933" At this point the only thing in the stack is the cmdline 934call s: RunDbgCmd ( buf , 'backtrace' , [ 935 \'>backtrace' , 936 \'->0 command line' , 937 \'cmd: call GlobalFunction()' ]) 938 939" And now we're back into the call stack 940call s: RunDbgCmd ( buf , 'step' , [ 'line 1: call CallAFunction()' ]) 941call s: RunDbgCmd ( buf , 'backtrace' , [ 942 \'>backtrace' , 943 \' 1 command line' , 944 \'->0 function GlobalFunction' , 945 \'line 1: call CallAFunction()' ]) 946 947call StopVimInTerminal ( buf ) 948endfunc 949 950func Test_Backtrace_DefFunction () 951CheckRunVimInTerminal 952CheckCWD 953let file1 =<< trim END 954vim9script 955import './Xtest2.vim' as imp 956 957def SourceAnotherFile() 958source Xtest2.vim 959enddef 960 961def CallAFunction() 962SourceAnotherFile() 963imp.File2Function() 964enddef 965 966def g:GlobalFunction() 967var some = "some var" 968CallAFunction() 969enddef 970 971defcompile 972END 973call writefile ( file1 , 'Xtest1.vim' , 'D' ) 974 975let file2 =<< trim END 976vim9script 977 978def DoAThing(): number 979var a = 100 * 2 980a += 3 981return a 982enddef 983 984export def File2Function() 985DoAThing() 986enddef 987 988defcompile 989File2Function() 990END 991call writefile ( file2 , 'Xtest2.vim' , 'D' ) 992 993let buf = RunVimInTerminal ( '-S Xtest1.vim' , {}) 994 995call s: RunDbgCmd ( buf , 996 \':debug call GlobalFunction()' , 997 \[ 'cmd: call GlobalFunction()' ]) 998 999call s: RunDbgCmd ( buf , 'step' , [ 'line 1: var some = "some var"' ]) 1000call s: RunDbgCmd ( buf , 'step' , [ 'line 2: CallAFunction()' ]) 1001call s: RunDbgCmd ( buf , 'echo some' , [ 'some var' ]) 1002 1003call s: RunDbgCmd ( buf , 'backtrace' , [ 1004 \'\V>backtrace' , 1005 \'\V->0 function GlobalFunction' , 1006 \'\Vline 2: CallAFunction()' , 1007 \], 1008 \#{ match : 'pattern' }) 1009 1010call s: RunDbgCmd ( buf , 'step' , [ 'line 1: SourceAnotherFile()' ]) 1011call s: RunDbgCmd ( buf , 'step' , [ 'line 1: source Xtest2.vim' ]) 1012" Repeated line, because we fist are in the compiled function before the 1013" EXEC and then in do_cmdline() before the :source command. 1014call s: RunDbgCmd ( buf , 'step' , [ 'line 1: source Xtest2.vim' ]) 1015call s: RunDbgCmd ( buf , 'step' , [ 'line 1: vim9script' ]) 1016call s: RunDbgCmd ( buf , 'step' , [ 'line 3: def DoAThing(): number' ]) 1017call s: RunDbgCmd ( buf , 'step' , [ 'line 9: export def File2Function()' ]) 1018call s: RunDbgCmd ( buf , 'step' , [ 'line 13: defcompile' ]) 1019call s: RunDbgCmd ( buf , 'step' , [ 'line 14: File2Function()' ]) 1020call s: RunDbgCmd ( buf , 'backtrace' , [ 1021 \'\V>backtrace' , 1022 \'\V 3 function GlobalFunction[2]' , 1023 \'\V 2 <SNR>\.\*_CallAFunction[1]' , 1024 \'\V 1 <SNR>\.\*_SourceAnotherFile[1]' , 1025 \'\V->0 script ' .. getcwd () .. '/Xtest2.vim' , 1026 \'\Vline 14: File2Function()' ], 1027 \#{ match : 'pattern' }) 1028 1029" Don't step into compiled functions... 1030call s: RunDbgCmd ( buf , 'next' , [ 'line 15: End of sourced file' ]) 1031call s: RunDbgCmd ( buf , 'backtrace' , [ 1032 \'\V>backtrace' , 1033 \'\V 3 function GlobalFunction[2]' , 1034 \'\V 2 <SNR>\.\*_CallAFunction[1]' , 1035 \'\V 1 <SNR>\.\*_SourceAnotherFile[1]' , 1036 \'\V->0 script ' .. getcwd () .. '/Xtest2.vim' , 1037 \'\Vline 15: End of sourced file' ], 1038 \#{ match : 'pattern' }) 1039 1040call StopVimInTerminal ( buf ) 1041endfunc 1042 1043func Test_DefFunction_expr () 1044CheckRunVimInTerminal 1045CheckCWD 1046let file3 =<< trim END 1047vim9script 1048g:someVar = "foo" 1049def g:ChangeVar() 1050g:someVar = "bar" 1051echo "changed" 1052enddef 1053defcompile 1054END 1055call writefile ( file3 , 'Xtest3.vim' , 'D' ) 1056let buf = RunVimInTerminal ( '-S Xtest3.vim' , {}) 1057 1058call s: RunDbgCmd ( buf , ':breakadd expr g:someVar' ) 1059call s: RunDbgCmd ( buf , ':call g:ChangeVar()' , [ 'Oldval = "''foo''"' , 'Newval = "''bar''"' , 'function ChangeVar' , 'line 2: echo "changed"' ]) 1060 1061call StopVimInTerminal ( buf ) 1062endfunc 1063 1064func Test_debug_def_and_legacy_function () 1065CheckRunVimInTerminal 1066CheckCWD 1067let file =<< trim END 1068vim9script 1069def g:SomeFunc() 1070echo "here" 1071echo "and" 1072echo "there" 1073breakadd func 2 LocalFunc 1074LocalFunc() 1075enddef 1076 1077def LocalFunc() 1078echo "first" 1079echo "second" 1080breakadd func LegacyFunc 1081LegacyFunc() 1082enddef 1083 1084func LegacyFunc() 1085echo "legone" 1086echo "legtwo" 1087endfunc 1088 1089breakadd func 2 g:SomeFunc 1090END 1091call writefile ( file , 'XtestDebug.vim' , 'D' ) 1092 1093let buf = RunVimInTerminal ( '-S XtestDebug.vim' , {}) 1094 1095call s: RunDbgCmd ( buf , ':call SomeFunc()' , [ 'line 2: echo "and"' ]) 1096call s: RunDbgCmd ( buf , 'next' , [ 'line 3: echo "there"' ]) 1097call s: RunDbgCmd ( buf , 'next' , [ 'line 4: breakadd func 2 LocalFunc' ]) 1098 1099" continue, next breakpoint is in LocalFunc() 1100call s: RunDbgCmd ( buf , 'cont' , [ 'line 2: echo "second"' ]) 1101 1102" continue, next breakpoint is in LegacyFunc() 1103call s: RunDbgCmd ( buf , 'cont' , [ 'line 1: echo "legone"' ]) 1104 1105call s: RunDbgCmd ( buf , 'cont' ) 1106 1107call StopVimInTerminal ( buf ) 1108endfunc 1109 1110" Test for using "up" in the debugger to select the frame of a :def function 1111" and then evaluating a variable name 1112func Test_debug_backtrace_up_def_function () 1113CheckRunVimInTerminal 1114CheckCWD 1115let file =<< trim END 1116vim9script 1117def g:DefFunc() 1118var defvar = 'in def' 1119g:LegacyFunc('arg') 1120enddef 1121 1122func g:LegacyFunc(text) 1123let legacyvar = 'in legacy' 1124echo legacyvar 1125endfunc 1126 1127breakadd func 2 g:LegacyFunc 1128END 1129call writefile ( file , 'XtestDebugUp.vim' , 'D' ) 1130 1131let buf = RunVimInTerminal ( '-S XtestDebugUp.vim' , {}) 1132 1133call s: RunDbgCmd ( buf , ':call DefFunc()' , [ 'line 2: echo legacyvar' ]) 1134call s: RunDbgCmd ( buf , 'echo legacyvar' , [ 'in legacy' ]) 1135call s: RunDbgCmd ( buf , 'echo l:legacyvar' , [ 'in legacy' ]) 1136call s: RunDbgCmd ( buf , 'echo a:text' , [ 'arg' ]) 1137 1138" Move up to the frame of the :def function, the local variables of the 1139" legacy function are not available there. This used to crash. 1140call s: RunDbgCmd ( buf , 'up' ) 1141call s: RunDbgCmd ( buf , 'echo legacyvar' , 1142 \[ 'E121: Undefined variable: legacyvar' ]) 1143call s: RunDbgCmd ( buf , 'echo l:legacyvar' , 1144 \[ 'E121: Undefined variable: l:legacyvar' ]) 1145call s: RunDbgCmd ( buf , 'echo a:text' , [ 'E121: Undefined variable: a:text' ]) 1146 1147" Back in the frame of the legacy function the variables are found again. 1148call s: RunDbgCmd ( buf , 'down' ) 1149call s: RunDbgCmd ( buf , 'echo legacyvar' , [ 'in legacy' ]) 1150 1151call s: RunDbgCmd ( buf , 'cont' ) 1152call StopVimInTerminal ( buf ) 1153endfunc 1154 1155func Test_debug_def_function () 1156CheckRunVimInTerminal 1157CheckCWD 1158let file =<< trim END 1159vim9script 1160def g:Func() 1161var n: number 1162def Closure(): number 1163return n + 3 1164enddef 1165n += Closure() 1166echo 'result: ' .. n 1167enddef 1168 1169def g:FuncWithArgs(text: string, nr: number, ...items: list<number>) 1170echo text .. nr 1171for it in items 1172echo it 1173endfor 1174echo "done" 1175enddef 1176 1177def g:FuncWithDict() 1178var d = { 1179a: 1, 1180b: 2, 1181} 1182# comment 1183def Inner() 1184eval 1 + 2 1185enddef 1186enddef 1187 1188def g:FuncComment() 1189# comment 1190echo "first" 1191.. "one" 1192# comment 1193echo "second" 1194enddef 1195 1196def g:FuncForLoop() 1197eval 1 + 2 1198for i in [11, 22, 33] 1199eval i + 2 1200endfor 1201echo "done" 1202enddef 1203 1204def g:FuncWithSplitLine() 1205eval 1 + 2 1206| eval 2 + 3 1207enddef 1208END 1209call writefile ( file , 'Xtest.vim' , 'D' ) 1210 1211let buf = RunVimInTerminal ( '-S Xtest.vim' , {}) 1212 1213call s: RunDbgCmd ( buf , 1214 \':debug call Func()' , 1215 \[ 'cmd: call Func()' ]) 1216call s: RunDbgCmd ( buf , 'next' , [ 'result: 3' ]) 1217call term_sendkeys ( buf , "\r" ) 1218call s: RunDbgCmd ( buf , 'cont' ) 1219 1220call s: RunDbgCmd ( buf , 1221 \':debug call FuncWithArgs("asdf", 42, 1, 2, 3)' , 1222 \[ 'cmd: call FuncWithArgs("asdf", 42, 1, 2, 3)' ]) 1223call s: RunDbgCmd ( buf , 'step' , [ 'line 1: echo text .. nr' ]) 1224call s: RunDbgCmd ( buf , 'echo text' , [ 'asdf' ]) 1225call s: RunDbgCmd ( buf , 'echo nr' , [ '42' ]) 1226call s: RunDbgCmd ( buf , 'echo items' , [ '[1, 2, 3]' ]) 1227call s: RunDbgCmd ( buf , 'step' , [ 'asdf42' , 'function FuncWithArgs' , 'line 2: for it in items' ]) 1228call s: RunDbgCmd ( buf , 'step' , [ 'function FuncWithArgs' , 'line 2: for it in items' ]) 1229call s: RunDbgCmd ( buf , 'echo it' , [ '0' ]) 1230call s: RunDbgCmd ( buf , 'step' , [ 'line 3: echo it' ]) 1231call s: RunDbgCmd ( buf , 'echo it' , [ '1' ]) 1232call s: RunDbgCmd ( buf , 'step' , [ '1' , 'function FuncWithArgs' , 'line 4: endfor' ]) 1233call s: RunDbgCmd ( buf , 'step' , [ 'line 2: for it in items' ]) 1234call s: RunDbgCmd ( buf , 'echo it' , [ '1' ]) 1235call s: RunDbgCmd ( buf , 'step' , [ 'line 3: echo it' ]) 1236call s: RunDbgCmd ( buf , 'step' , [ '2' , 'function FuncWithArgs' , 'line 4: endfor' ]) 1237call s: RunDbgCmd ( buf , 'step' , [ 'line 2: for it in items' ]) 1238call s: RunDbgCmd ( buf , 'echo it' , [ '2' ]) 1239call s: RunDbgCmd ( buf , 'step' , [ 'line 3: echo it' ]) 1240call s: RunDbgCmd ( buf , 'step' , [ '3' , 'function FuncWithArgs' , 'line 4: endfor' ]) 1241call s: RunDbgCmd ( buf , 'step' , [ 'line 2: for it in items' ]) 1242call s: RunDbgCmd ( buf , 'step' , [ 'line 5: echo "done"' ]) 1243call s: RunDbgCmd ( buf , 'cont' ) 1244 1245call s: RunDbgCmd ( buf , 1246 \':debug call FuncWithDict()' , 1247 \[ 'cmd: call FuncWithDict()' ]) 1248call s: RunDbgCmd ( buf , 'step' , [ 'line 1: var d = { a: 1, b: 2, }' ]) 1249call s: RunDbgCmd ( buf , 'step' , [ 'line 6: def Inner()' ]) 1250call s: RunDbgCmd ( buf , 'cont' ) 1251 1252call s: RunDbgCmd ( buf , ':breakadd func 1 FuncComment' ) 1253call s: RunDbgCmd ( buf , ':call FuncComment()' , [ 'function FuncComment' , 'line 2: echo "first" .. "one"' ]) 1254call s: RunDbgCmd ( buf , ':breakadd func 3 FuncComment' ) 1255call s: RunDbgCmd ( buf , 'cont' , [ 'function FuncComment' , 'line 5: echo "second"' ]) 1256call s: RunDbgCmd ( buf , 'cont' ) 1257 1258call s: RunDbgCmd ( buf , ':breakadd func 2 FuncForLoop' ) 1259call s: RunDbgCmd ( buf , ':call FuncForLoop()' , [ 'function FuncForLoop' , 'line 2: for i in [11, 22, 33]' ]) 1260call s: RunDbgCmd ( buf , 'step' , [ 'line 2: for i in [11, 22, 33]' ]) 1261call s: RunDbgCmd ( buf , 'next' , [ 'function FuncForLoop' , 'line 3: eval i + 2' ]) 1262call s: RunDbgCmd ( buf , 'echo i' , [ '11' ]) 1263call s: RunDbgCmd ( buf , 'next' , [ 'function FuncForLoop' , 'line 4: endfor' ]) 1264call s: RunDbgCmd ( buf , 'next' , [ 'function FuncForLoop' , 'line 2: for i in [11, 22, 33]' ]) 1265call s: RunDbgCmd ( buf , 'next' , [ 'line 3: eval i + 2' ]) 1266call s: RunDbgCmd ( buf , 'echo i' , [ '22' ]) 1267 1268call s: RunDbgCmd ( buf , 'breakdel *' ) 1269call s: RunDbgCmd ( buf , 'cont' ) 1270 1271call s: RunDbgCmd ( buf , ':breakadd func FuncWithSplitLine' ) 1272call s: RunDbgCmd ( buf , ':call FuncWithSplitLine()' , [ 'function FuncWithSplitLine' , 'line 1: eval 1 + 2 | eval 2 + 3' ]) 1273 1274call s: RunDbgCmd ( buf , 'cont' ) 1275call StopVimInTerminal ( buf ) 1276endfunc 1277 1278func Test_debug_def_function_with_lambda () 1279CheckRunVimInTerminal 1280CheckCWD 1281let lines =<< trim END 1282vim9script 1283def g:Func() 1284var s = 'a' 1285['b']->map((_, v) => s) 1286echo "done" 1287enddef 1288breakadd func 2 g:Func 1289END 1290call writefile ( lines , 'XtestLambda.vim' , 'D' ) 1291 1292let buf = RunVimInTerminal ( '-S XtestLambda.vim' , {}) 1293 1294call s: RunDbgCmd ( buf , 1295 \':call g:Func()' , 1296 \[ 'function Func' , 'line 2: [''b'']->map((_, v) => s)' ]) 1297call s: RunDbgCmd ( buf , 1298 \'next' , 1299 \[ 'function Func' , 'line 3: echo "done"' ]) 1300 1301call s: RunDbgCmd ( buf , 'cont' ) 1302call StopVimInTerminal ( buf ) 1303endfunc 1304 1305func Test_debug_backtrace_level () 1306CheckRunVimInTerminal 1307CheckCWD 1308let lines =<< trim END 1309let s:file1_var = 'file1' 1310let g:global_var = 'global' 1311 1312func s:File1Func( arg ) 1313let s:file1_var .= a:arg 1314let local_var = s:file1_var .. ' test1' 1315let g:global_var .= local_var 1316source Xtest2.vim 1317endfunc 1318 1319call s:File1Func( 'arg1' ) 1320END 1321call writefile ( lines , 'Xtest1.vim' , 'D' ) 1322 1323let lines =<< trim END 1324let s:file2_var = 'file2' 1325 1326func s:File2Func( arg ) 1327let s:file2_var .= a:arg 1328let local_var = s:file2_var .. ' test2' 1329let g:global_var .= local_var 1330endfunc 1331 1332call s:File2Func( 'arg2' ) 1333END 1334call writefile ( lines , 'Xtest2.vim' , 'D' ) 1335 1336let file1 = getcwd () .. '/Xtest1.vim' 1337let file2 = getcwd () .. '/Xtest2.vim' 1338 1339" set a breakpoint and source file1.vim 1340let buf = RunVimInTerminal ( 1341 \'-c "breakadd file 1 Xtest1.vim" -S Xtest1.vim' , 1342 \#{ wait_for_ruler : 0 }) 1343 1344call CheckDbgOutput ( buf , [ 1345 \'Breakpoint in "' .. file1 .. '" line 1' , 1346 \'Entering Debug mode. Type "cont" to continue.' , 1347 \'command line..script ' .. file1 , 1348 \'line 1: let s:file1_var = ''file1''' 1349 \], #{ msec : 5000 }) 1350 1351" step through the initial declarations 1352call s: RunDbgCmd ( buf , 'step' , [ 'line 2: let g:global_var = ''global''' ] ) 1353call s: RunDbgCmd ( buf , 'step' , [ 'line 4: func s:File1Func( arg )' ] ) 1354call s: RunDbgCmd ( buf , 'echo s:file1_var' , [ 'file1' ] ) 1355call s: RunDbgCmd ( buf , 'echo g:global_var' , [ 'global' ] ) 1356call s: RunDbgCmd ( buf , 'echo global_var' , [ 'global' ] ) 1357 1358" step in to the first function 1359call s: RunDbgCmd ( buf , 'step' , [ 'line 11: call s:File1Func( ''arg1'' )' ] ) 1360call s: RunDbgCmd ( buf , 'step' , [ 'line 1: let s:file1_var .= a:arg' ] ) 1361call s: RunDbgCmd ( buf , 'echo a:arg' , [ 'arg1' ] ) 1362call s: RunDbgCmd ( buf , 'echo s:file1_var' , [ 'file1' ] ) 1363call s: RunDbgCmd ( buf , 'echo g:global_var' , [ 'global' ] ) 1364call s: RunDbgCmd ( buf , 1365 \'echo global_var' , 1366 \[ 'E121: Undefined variable: global_var' ] ) 1367call s: RunDbgCmd ( buf , 1368 \'echo local_var' , 1369 \[ 'E121: Undefined variable: local_var' ] ) 1370call s: RunDbgCmd ( buf , 1371 \'echo l:local_var' , 1372 \[ 'E121: Undefined variable: l:local_var' ] ) 1373 1374" backtrace up 1375call s: RunDbgCmd ( buf , 'backtrace' , [ 1376 \'\V>backtrace' , 1377 \'\V 2 command line' , 1378 \'\V 1 script ' .. file1 .. '[11]' , 1379 \'\V->0 function <SNR>\.\*_File1Func' , 1380 \'\Vline 1: let s:file1_var .= a:arg' , 1381 \], 1382 \#{ match : 'pattern' } ) 1383call s: RunDbgCmd ( buf , 'up' , [ '>up' ] ) 1384 1385call s: RunDbgCmd ( buf , 'backtrace' , [ 1386 \'\V>backtrace' , 1387 \'\V 2 command line' , 1388 \'\V->1 script ' .. file1 .. '[11]' , 1389 \'\V 0 function <SNR>\.\*_File1Func' , 1390 \'\Vline 1: let s:file1_var .= a:arg' , 1391 \], 1392 \#{ match : 'pattern' } ) 1393 1394" Expression evaluation in the script frame (not the function frame) 1395" FIXME: Unexpected in this scope (a: should not be visible) 1396call s: RunDbgCmd ( buf , 'echo a:arg' , [ 'arg1' ] ) 1397call s: RunDbgCmd ( buf , 'echo s:file1_var' , [ 'file1' ] ) 1398call s: RunDbgCmd ( buf , 'echo g:global_var' , [ 'global' ] ) 1399" FIXME: Unexpected in this scope (global should be found) 1400call s: RunDbgCmd ( buf , 1401 \'echo global_var' , 1402 \[ 'E121: Undefined variable: global_var' ] ) 1403call s: RunDbgCmd ( buf , 1404 \'echo local_var' , 1405 \[ 'E121: Undefined variable: local_var' ] ) 1406call s: RunDbgCmd ( buf , 1407 \'echo l:local_var' , 1408 \[ 'E121: Undefined variable: l:local_var' ] ) 1409 1410 1411" step while backtraced jumps to the latest frame 1412call s: RunDbgCmd ( buf , 'step' , [ 1413 \'line 2: let local_var = s:file1_var .. '' test1''' ] ) 1414call s: RunDbgCmd ( buf , 'backtrace' , [ 1415 \'\V>backtrace' , 1416 \'\V 2 command line' , 1417 \'\V 1 script ' .. file1 .. '[11]' , 1418 \'\V->0 function <SNR>\.\*_File1Func' , 1419 \'\Vline 2: let local_var = s:file1_var .. '' test1''' , 1420 \], 1421 \#{ match : 'pattern' } ) 1422 1423call s: RunDbgCmd ( buf , 'step' , [ 'line 3: let g:global_var .= local_var' ] ) 1424call s: RunDbgCmd ( buf , 'echo local_var' , [ 'file1arg1 test1' ] ) 1425call s: RunDbgCmd ( buf , 'echo l:local_var' , [ 'file1arg1 test1' ] ) 1426 1427call s: RunDbgCmd ( buf , 'step' , [ 'line 4: source Xtest2.vim' ] ) 1428call s: RunDbgCmd ( buf , 'step' , [ 'line 1: let s:file2_var = ''file2''' ] ) 1429call s: RunDbgCmd ( buf , 'backtrace' , [ 1430 \'\V>backtrace' , 1431 \'\V 3 command line' , 1432 \'\V 2 script ' .. file1 .. '[11]' , 1433 \'\V 1 function <SNR>\.\*_File1Func[4]' , 1434 \'\V->0 script ' .. file2 , 1435 \'\Vline 1: let s:file2_var = ''file2''' , 1436 \], 1437 \#{ match : 'pattern' } ) 1438 1439" Expression evaluation in the script frame file2 (not the function frame) 1440call s: RunDbgCmd ( buf , 'echo a:arg' , [ 'E121: Undefined variable: a:arg' ] ) 1441call s: RunDbgCmd ( buf , 1442 \'echo s:file1_var' , 1443 \[ 'E121: Undefined variable: s:file1_var' ] ) 1444call s: RunDbgCmd ( buf , 'echo g:global_var' , [ 'globalfile1arg1 test1' ] ) 1445call s: RunDbgCmd ( buf , 'echo global_var' , [ 'globalfile1arg1 test1' ] ) 1446call s: RunDbgCmd ( buf , 1447 \'echo local_var' , 1448 \[ 'E121: Undefined variable: local_var' ] ) 1449call s: RunDbgCmd ( buf , 1450 \'echo l:local_var' , 1451 \[ 'E121: Undefined variable: l:local_var' ] ) 1452call s: RunDbgCmd ( buf , 1453 \'echo s:file2_var' , 1454 \[ 'E121: Undefined variable: s:file2_var' ] ) 1455 1456call s: RunDbgCmd ( buf , 'step' , [ 'line 3: func s:File2Func( arg )' ] ) 1457call s: RunDbgCmd ( buf , 'echo s:file2_var' , [ 'file2' ] ) 1458 1459" Up the stack to the other script context 1460call s: RunDbgCmd ( buf , 'up' ) 1461call s: RunDbgCmd ( buf , 'backtrace' , [ 1462 \'\V>backtrace' , 1463 \'\V 3 command line' , 1464 \'\V 2 script ' .. file1 .. '[11]' , 1465 \'\V->1 function <SNR>\.\*_File1Func[4]' , 1466 \'\V 0 script ' .. file2 , 1467 \'\Vline 3: func s:File2Func( arg )' , 1468 \], 1469 \#{ match : 'pattern' } ) 1470" FIXME: Unexpected. Should see the a: and l: dicts from File1Func 1471call s: RunDbgCmd ( buf , 'echo a:arg' , [ 'E121: Undefined variable: a:arg' ] ) 1472call s: RunDbgCmd ( buf , 1473 \'echo l:local_var' , 1474 \[ 'E121: Undefined variable: l:local_var' ] ) 1475 1476call s: RunDbgCmd ( buf , 'up' ) 1477call s: RunDbgCmd ( buf , 'backtrace' , [ 1478 \'\V>backtrace' , 1479 \'\V 3 command line' , 1480 \'\V->2 script ' .. file1 .. '[11]' , 1481 \'\V 1 function <SNR>\.\*_File1Func[4]' , 1482 \'\V 0 script ' .. file2 , 1483 \'\Vline 3: func s:File2Func( arg )' , 1484 \], 1485 \#{ match : 'pattern' } ) 1486 1487" FIXME: Unexpected (wrong script vars are used) 1488call s: RunDbgCmd ( buf , 1489 \'echo s:file1_var' , 1490 \[ 'E121: Undefined variable: s:file1_var' ] ) 1491call s: RunDbgCmd ( buf , 'echo s:file2_var' , [ 'file2' ] ) 1492 1493call s: RunDbgCmd ( buf , 'cont' ) 1494call StopVimInTerminal ( buf ) 1495endfunc 1496 1497" Test for setting a breakpoint on a :endif where the :if condition is false 1498" and then quit the script. This should generate an interrupt. 1499func Test_breakpt_endif_intr () 1500func F () 1501let g: Xpath ..= 'a' 1502if v: false 1503let g: Xpath ..= 'b' 1504endif 1505invalid _command 1506endfunc 1507 1508let g: Xpath = '' 1509breakadd func 4 F 1510try 1511let caught_intr = 0 1512debuggreedy 1513call feedkeys ( ":call F()\<CR>quit\<CR>" , "xt" ) 1514catch /^Vim:Interrupt$/ 1515call assert_match ( '\.F, line 4' , v: throwpoint ) 1516let caught_intr = 1 1517endtry 15180 debuggreedy 1519call assert_equal ( 1 , caught_intr ) 1520call assert_equal ( 'a' , g: Xpath ) 1521breakdel * 1522delfunc F 1523endfunc 1524 1525" Test for setting a breakpoint on a :else where the :if condition is false 1526" and then quit the script. This should generate an interrupt. 1527func Test_breakpt_else_intr () 1528func F () 1529let g: Xpath ..= 'a' 1530if v: false 1531let g: Xpath ..= 'b' 1532else 1533invalid _command 1534endif 1535invalid _command 1536endfunc 1537 1538let g: Xpath = '' 1539breakadd func 4 F 1540try 1541let caught_intr = 0 1542debuggreedy 1543call feedkeys ( ":call F()\<CR>quit\<CR>" , "xt" ) 1544catch /^Vim:Interrupt$/ 1545call assert_match ( '\.F, line 4' , v: throwpoint ) 1546let caught_intr = 1 1547endtry 15480 debuggreedy 1549call assert_equal ( 1 , caught_intr ) 1550call assert_equal ( 'a' , g: Xpath ) 1551breakdel * 1552delfunc F 1553endfunc 1554 1555" Test for setting a breakpoint on a :endwhile where the :while condition is 1556" false and then quit the script. This should generate an interrupt. 1557func Test_breakpt_endwhile_intr () 1558func F () 1559let g: Xpath ..= 'a' 1560while v: false 1561let g: Xpath ..= 'b' 1562endwhile 1563invalid _command 1564endfunc 1565 1566let g: Xpath = '' 1567breakadd func 4 F 1568try 1569let caught_intr = 0 1570debuggreedy 1571call feedkeys ( ":call F()\<CR>quit\<CR>" , "xt" ) 1572catch /^Vim:Interrupt$/ 1573call assert_match ( '\.F, line 4' , v: throwpoint ) 1574let caught_intr = 1 1575endtry 15760 debuggreedy 1577call assert_equal ( 1 , caught_intr ) 1578call assert_equal ( 'a' , g: Xpath ) 1579breakdel * 1580delfunc F 1581endfunc 1582 1583" Test for setting a breakpoint on a script local function 1584func Test_breakpt_scriptlocal_func () 1585let g: Xpath = '' 1586func s: G () 1587let g: Xpath ..= 'a' 1588endfunc 1589 1590let funcname = expand ( "<SID>" ) .. "G" 1591exe "breakadd func 1 " .. funcname 1592debuggreedy 1593redir => output 1594call feedkeys ( ":call " .. funcname .. "()\<CR>c\<CR>" , "xt" ) 1595redir END 15960 debuggreedy 1597call assert_match ( 'Breakpoint in "' .. funcname .. '" line 1' , output ) 1598call assert_equal ( 'a' , g: Xpath ) 1599breakdel * 1600exe "delfunc " .. funcname 1601endfunc 1602 1603" vim: shiftwidth=2 sts=2 expandtab