2015-03-20 21:11:11 +00:00
package terminal
2014-05-20 21:28:24 +00:00
2014-05-20 23:09:34 +00:00
import (
2022-01-27 21:18:25 +00:00
"bytes"
2017-02-10 14:11:40 +00:00
"flag"
2015-05-04 22:31:13 +00:00
"fmt"
2016-02-13 20:00:21 +00:00
"net"
2017-05-08 18:16:14 +00:00
"net/http"
2016-02-13 20:00:21 +00:00
"os"
2015-09-29 16:40:12 +00:00
"path/filepath"
2021-07-22 17:16:42 +00:00
"reflect"
2017-04-19 13:45:23 +00:00
"regexp"
2018-05-11 12:51:15 +00:00
"runtime"
2016-02-23 14:12:04 +00:00
"strconv"
2016-02-13 20:00:21 +00:00
"strings"
2014-05-20 23:09:34 +00:00
"testing"
2017-05-08 18:16:14 +00:00
"time"
2015-09-29 16:40:12 +00:00
2019-01-04 18:39:25 +00:00
"github.com/go-delve/delve/pkg/config"
"github.com/go-delve/delve/pkg/goversion"
2019-07-02 17:55:27 +00:00
"github.com/go-delve/delve/pkg/logflags"
2019-01-04 18:39:25 +00:00
"github.com/go-delve/delve/pkg/proc/test"
"github.com/go-delve/delve/service"
"github.com/go-delve/delve/service/api"
2020-04-13 18:07:15 +00:00
"github.com/go-delve/delve/service/debugger"
2019-01-04 18:39:25 +00:00
"github.com/go-delve/delve/service/rpc2"
"github.com/go-delve/delve/service/rpccommon"
2014-05-20 23:09:34 +00:00
)
2014-05-20 21:28:24 +00:00
2018-05-29 15:01:51 +00:00
var testBackend , buildMode string
2017-02-10 14:11:40 +00:00
func TestMain ( m * testing . M ) {
flag . StringVar ( & testBackend , "backend" , "" , "selects backend" )
2018-05-29 15:01:51 +00:00
flag . StringVar ( & buildMode , "test-buildmode" , "" , "selects build mode" )
2019-07-02 17:55:27 +00:00
var logConf string
flag . StringVar ( & logConf , "log" , "" , "configures logging" )
2017-02-10 14:11:40 +00:00
flag . Parse ( )
2018-10-01 08:19:06 +00:00
test . DefaultTestBackend ( & testBackend )
2018-05-29 15:01:51 +00:00
if buildMode != "" && buildMode != "pie" {
fmt . Fprintf ( os . Stderr , "unknown build mode %q" , buildMode )
os . Exit ( 1 )
}
2019-07-02 17:55:27 +00:00
logflags . Setup ( logConf != "" , logConf , "" )
2024-11-12 16:03:50 +00:00
test . RunTestsWithFixtures ( m )
2017-02-10 14:11:40 +00:00
}
2016-02-13 20:00:21 +00:00
type FakeTerminal struct {
2016-02-27 23:02:55 +00:00
* Term
2016-02-29 00:42:48 +00:00
t testing . TB
2016-02-13 20:00:21 +00:00
}
2017-05-08 18:16:14 +00:00
const logCommandOutput = false
2016-02-27 23:02:55 +00:00
func ( ft * FakeTerminal ) Exec ( cmdstr string ) ( outstr string , err error ) {
2022-01-27 21:18:25 +00:00
var buf bytes . Buffer
2022-08-17 07:02:53 +00:00
ft . Term . stdout . pw . w = & buf
2022-01-27 21:18:25 +00:00
ft . Term . starlarkEnv . Redirect ( ft . Term . stdout )
2017-04-19 10:17:21 +00:00
err = ft . cmds . Call ( cmdstr , ft . Term )
2022-01-27 21:18:25 +00:00
outstr = buf . String ( )
if logCommandOutput {
ft . t . Logf ( "command %q -> %q" , cmdstr , outstr )
}
ft . Term . stdout . Flush ( )
2016-02-13 20:00:21 +00:00
return
}
2019-07-02 17:55:27 +00:00
func ( ft * FakeTerminal ) ExecStarlark ( starlarkProgram string ) ( outstr string , err error ) {
2022-01-27 21:18:25 +00:00
var buf bytes . Buffer
2022-08-17 07:02:53 +00:00
ft . Term . stdout . pw . w = & buf
2022-01-27 21:18:25 +00:00
ft . Term . starlarkEnv . Redirect ( ft . Term . stdout )
2019-07-02 17:55:27 +00:00
_ , err = ft . Term . starlarkEnv . Execute ( "<stdin>" , starlarkProgram , "main" , nil )
2022-01-27 21:18:25 +00:00
outstr = buf . String ( )
if logCommandOutput {
ft . t . Logf ( "command %q -> %q" , starlarkProgram , outstr )
}
ft . Term . stdout . Flush ( )
2019-07-02 17:55:27 +00:00
return
}
2016-02-27 23:02:55 +00:00
func ( ft * FakeTerminal ) MustExec ( cmdstr string ) string {
2023-02-22 17:26:28 +00:00
ft . t . Helper ( )
2016-02-27 23:02:55 +00:00
outstr , err := ft . Exec ( cmdstr )
2016-02-13 20:00:21 +00:00
if err != nil {
2019-07-02 17:55:27 +00:00
ft . t . Errorf ( "output of %q: %q" , cmdstr , outstr )
2016-02-27 23:02:55 +00:00
ft . t . Fatalf ( "Error executing <%s>: %v" , cmdstr , err )
2016-02-13 20:00:21 +00:00
}
return outstr
}
2019-07-02 17:55:27 +00:00
func ( ft * FakeTerminal ) MustExecStarlark ( starlarkProgram string ) string {
outstr , err := ft . ExecStarlark ( starlarkProgram )
if err != nil {
ft . t . Errorf ( "output of %q: %q" , starlarkProgram , outstr )
ft . t . Fatalf ( "Error executing <%s>: %v" , starlarkProgram , err )
}
return outstr
}
2016-02-23 14:12:04 +00:00
func ( ft * FakeTerminal ) AssertExec ( cmdstr , tgt string ) {
out := ft . MustExec ( cmdstr )
if out != tgt {
ft . t . Fatalf ( "Error executing %q, expected %q got %q" , cmdstr , tgt , out )
}
}
func ( ft * FakeTerminal ) AssertExecError ( cmdstr , tgterr string ) {
_ , err := ft . Exec ( cmdstr )
if err == nil {
2016-02-29 00:42:48 +00:00
ft . t . Fatalf ( "Expected error executing %q" , cmdstr )
2016-02-23 14:12:04 +00:00
}
if err . Error ( ) != tgterr {
ft . t . Fatalf ( "Expected error %q executing %q, got error %q" , tgterr , cmdstr , err . Error ( ) )
}
}
2016-02-13 20:00:21 +00:00
func withTestTerminal ( name string , t testing . TB , fn func ( * FakeTerminal ) ) {
2018-06-20 08:06:12 +00:00
withTestTerminalBuildFlags ( name , t , 0 , fn )
}
func withTestTerminalBuildFlags ( name string , t testing . TB , buildFlags test . BuildFlags , fn func ( * FakeTerminal ) ) {
2017-05-05 22:17:52 +00:00
if testBackend == "rr" {
test . MustHaveRecordingAllowed ( t )
}
2023-09-19 16:29:47 +00:00
t . Setenv ( "TERM" , "dumb" )
2019-07-30 15:38:25 +00:00
listener , err := net . Listen ( "tcp" , "127.0.0.1:0" )
2016-02-13 20:00:21 +00:00
if err != nil {
t . Fatalf ( "couldn't start listener: %s\n" , err )
}
defer listener . Close ( )
2018-05-29 15:01:51 +00:00
if buildMode == "pie" {
buildFlags |= test . BuildModePIE
}
2016-06-19 06:43:29 +00:00
server := rpccommon . NewServer ( & service . Config {
2016-02-13 20:00:21 +00:00
Listener : listener ,
2018-06-20 08:06:12 +00:00
ProcessArgs : [ ] string { test . BuildFixture ( name , buildFlags ) . Path } ,
2020-04-13 18:07:15 +00:00
Debugger : debugger . Config {
Backend : testBackend ,
} ,
2018-06-14 18:12:11 +00:00
} )
2016-02-13 20:00:21 +00:00
if err := server . Run ( ) ; err != nil {
t . Fatal ( err )
}
2016-04-18 19:20:20 +00:00
client := rpc2 . NewClient ( listener . Addr ( ) . String ( ) )
2016-02-13 20:00:21 +00:00
defer func ( ) {
client . Detach ( true )
} ( )
2016-02-23 14:12:04 +00:00
2016-02-27 23:02:55 +00:00
ft := & FakeTerminal {
2016-02-29 00:42:48 +00:00
t : t ,
2017-07-29 04:11:06 +00:00
Term : New ( client , & config . Config { } ) ,
2016-02-27 23:02:55 +00:00
}
fn ( ft )
2016-02-13 20:00:21 +00:00
}
2014-05-20 21:28:24 +00:00
func TestCommandDefault ( t * testing . T ) {
var (
2014-11-14 05:51:32 +00:00
cmds = Commands { }
2022-03-23 15:10:00 +00:00
cmd = cmds . Find ( "non-existent-command" , noPrefix ) . cmdFn
2014-05-20 21:28:24 +00:00
)
2016-02-23 14:12:04 +00:00
err := cmd ( nil , callContext { } , "" )
2014-05-20 21:28:24 +00:00
if err == nil {
t . Fatal ( "cmd() did not default" )
}
if err . Error ( ) != "command not available" {
t . Fatal ( "wrong command output" )
}
}
2014-05-20 23:09:34 +00:00
2014-05-21 15:15:58 +00:00
func TestCommandReplayWithoutPreviousCommand ( t * testing . T ) {
var (
2015-03-20 21:11:11 +00:00
cmds = DebugCommands ( nil )
2021-08-05 17:55:27 +00:00
cmd = cmds . Find ( "" , noPrefix ) . cmdFn
2016-02-23 14:12:04 +00:00
err = cmd ( nil , callContext { } , "" )
2014-05-21 15:15:58 +00:00
)
if err != nil {
t . Error ( "Null command not returned" , err )
}
}
2015-05-30 07:23:41 +00:00
func TestCommandThread ( t * testing . T ) {
var (
cmds = DebugCommands ( nil )
2021-08-05 17:55:27 +00:00
cmd = cmds . Find ( "thread" , noPrefix ) . cmdFn
2015-05-30 07:23:41 +00:00
)
2016-02-23 14:12:04 +00:00
err := cmd ( nil , callContext { } , "" )
2015-05-30 07:23:41 +00:00
if err == nil {
t . Fatal ( "thread terminal command did not default" )
}
if err . Error ( ) != "you must specify a thread" {
t . Fatal ( "wrong command output: " , err . Error ( ) )
}
}
2015-09-29 16:40:12 +00:00
func TestExecuteFile ( t * testing . T ) {
breakCount := 0
traceCount := 0
c := & Commands {
client : nil ,
cmds : [ ] command {
2016-02-23 14:12:04 +00:00
{ aliases : [ ] string { "trace" } , cmdFn : func ( t * Term , ctx callContext , args string ) error {
2015-09-29 16:40:12 +00:00
traceCount ++
return nil
} } ,
2016-02-23 14:12:04 +00:00
{ aliases : [ ] string { "break" } , cmdFn : func ( t * Term , ctx callContext , args string ) error {
2015-09-29 16:40:12 +00:00
breakCount ++
return nil
} } ,
} ,
}
fixturesDir := test . FindFixturesDir ( )
err := c . executeFile ( nil , filepath . Join ( fixturesDir , "bpfile" ) )
if err != nil {
t . Fatalf ( "executeFile: %v" , err )
}
if breakCount != 1 || traceCount != 1 {
t . Fatalf ( "Wrong counts break: %d trace: %d\n" , breakCount , traceCount )
}
}
2016-01-27 12:33:37 +00:00
func TestIssue354 ( t * testing . T ) {
2020-10-26 12:36:52 +00:00
printStack ( & Term { } , os . Stdout , [ ] api . Stackframe { } , "" , false )
printStack ( & Term { } , os . Stdout , [ ] api . Stackframe {
2020-03-18 16:25:32 +00:00
{ Location : api . Location { PC : 0 , File : "irrelevant.go" , Line : 10 , Function : nil } ,
Bottom : true } } , "" , false )
2016-01-27 12:33:37 +00:00
}
2016-02-13 20:00:21 +00:00
func TestIssue411 ( t * testing . T ) {
2017-05-05 22:17:52 +00:00
test . AllowRecording ( t )
2016-02-13 20:00:21 +00:00
withTestTerminal ( "math" , t , func ( term * FakeTerminal ) {
2020-02-11 01:31:54 +00:00
term . MustExec ( "break _fixtures/math.go:8" )
term . MustExec ( "trace _fixtures/math.go:9" )
2016-02-13 20:00:21 +00:00
term . MustExec ( "continue" )
out := term . MustExec ( "next" )
2020-05-13 06:38:10 +00:00
if ! strings . HasPrefix ( out , "> goroutine(1): main.main()" ) {
2016-02-13 20:00:21 +00:00
t . Fatalf ( "Wrong output for next: <%s>" , out )
}
} )
}
2016-02-23 14:12:04 +00:00
2020-05-13 06:38:10 +00:00
func TestTrace ( t * testing . T ) {
test . AllowRecording ( t )
withTestTerminal ( "issue573" , t , func ( term * FakeTerminal ) {
term . MustExec ( "trace foo" )
out , _ := term . Exec ( "continue" )
// The output here is a little strange, but we don't filter stdout vs stderr so it gets jumbled.
// Therefore we assert about the call and return values separately.
if ! strings . Contains ( out , "> goroutine(1): main.foo(99, 9801)" ) {
t . Fatalf ( "Wrong output for tracepoint: %s" , out )
}
if ! strings . Contains ( out , "=> (9900)" ) {
t . Fatalf ( "Wrong output for tracepoint return value: %s" , out )
}
} )
}
func TestTraceWithName ( t * testing . T ) {
test . AllowRecording ( t )
withTestTerminal ( "issue573" , t , func ( term * FakeTerminal ) {
term . MustExec ( "trace foobar foo" )
out , _ := term . Exec ( "continue" )
// The output here is a little strange, but we don't filter stdout vs stderr so it gets jumbled.
// Therefore we assert about the call and return values separately.
if ! strings . Contains ( out , "> goroutine(1): [foobar] main.foo(99, 9801)" ) {
t . Fatalf ( "Wrong output for tracepoint: %s" , out )
}
if ! strings . Contains ( out , "=> (9900)" ) {
t . Fatalf ( "Wrong output for tracepoint return value: %s" , out )
}
} )
}
func TestTraceOnNonFunctionEntry ( t * testing . T ) {
test . AllowRecording ( t )
withTestTerminal ( "issue573" , t , func ( term * FakeTerminal ) {
term . MustExec ( "trace foobar issue573.go:19" )
out , _ := term . Exec ( "continue" )
if ! strings . Contains ( out , "> goroutine(1): [foobar] main.foo(99, 9801)" ) {
t . Fatalf ( "Wrong output for tracepoint: %s" , out )
}
if strings . Contains ( out , "=> (9900)" ) {
t . Fatalf ( "Tracepoint on non-function locspec should not have return value:\n%s" , out )
}
} )
}
2019-12-02 16:16:18 +00:00
func TestExitStatus ( t * testing . T ) {
withTestTerminal ( "continuetestprog" , t , func ( term * FakeTerminal ) {
term . Exec ( "continue" )
status , err := term . handleExit ( )
if err != nil {
t . Fatal ( err )
}
if status != 0 {
t . Fatalf ( "incorrect exit status, expected 0, got %d" , status )
}
} )
}
2016-02-23 14:12:04 +00:00
func TestScopePrefix ( t * testing . T ) {
2023-07-07 16:30:38 +00:00
if runtime . GOARCH == "ppc64le" && buildMode == "pie" {
t . Skip ( "pie mode broken on ppc64le" )
}
2016-02-23 14:12:04 +00:00
const goroutinesLinePrefix = " Goroutine "
const goroutinesCurLinePrefix = "* Goroutine "
2017-05-05 22:17:52 +00:00
test . AllowRecording ( t )
2020-08-18 00:17:39 +00:00
2021-10-13 15:43:54 +00:00
lenient := 0
if runtime . GOOS == "windows" {
lenient = 1
}
2016-02-23 14:12:04 +00:00
withTestTerminal ( "goroutinestackprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "b stacktraceme" )
term . MustExec ( "continue" )
goroutinesOut := strings . Split ( term . MustExec ( "goroutines" ) , "\n" )
agoroutines := [ ] int { }
2017-02-16 13:16:00 +00:00
nonagoroutines := [ ] int { }
2016-02-23 14:12:04 +00:00
curgid := - 1
for _ , line := range goroutinesOut {
iscur := strings . HasPrefix ( line , goroutinesCurLinePrefix )
if ! iscur && ! strings . HasPrefix ( line , goroutinesLinePrefix ) {
continue
}
dash := strings . Index ( line , " - " )
if dash < 0 {
continue
}
gid , err := strconv . Atoi ( line [ len ( goroutinesLinePrefix ) : dash ] )
if err != nil {
continue
}
if iscur {
curgid = gid
}
if idx := strings . Index ( line , " main.agoroutine " ) ; idx < 0 {
2017-02-16 13:16:00 +00:00
nonagoroutines = append ( nonagoroutines , gid )
2016-02-23 14:12:04 +00:00
continue
}
agoroutines = append ( agoroutines , gid )
}
2017-02-16 13:16:00 +00:00
if len ( agoroutines ) > 10 {
t . Fatalf ( "Output of goroutines did not have 10 goroutines stopped on main.agoroutine (%d found): %q" , len ( agoroutines ) , goroutinesOut )
}
if len ( agoroutines ) < 10 {
extraAgoroutines := 0
for _ , gid := range nonagoroutines {
stackOut := strings . Split ( term . MustExec ( fmt . Sprintf ( "goroutine %d stack" , gid ) ) , "\n" )
for _ , line := range stackOut {
if strings . HasSuffix ( line , " main.agoroutine" ) {
extraAgoroutines ++
break
}
}
}
2021-10-13 15:43:54 +00:00
if len ( agoroutines ) + extraAgoroutines < 10 - lenient {
2017-02-16 13:16:00 +00:00
t . Fatalf ( "Output of goroutines did not have 10 goroutines stopped on main.agoroutine (%d+%d found): %q" , len ( agoroutines ) , extraAgoroutines , goroutinesOut )
}
2016-02-23 14:12:04 +00:00
}
if curgid < 0 {
t . Fatalf ( "Could not find current goroutine in output of goroutines: %q" , goroutinesOut )
}
seen := make ( [ ] bool , 10 )
for _ , gid := range agoroutines {
stackOut := strings . Split ( term . MustExec ( fmt . Sprintf ( "goroutine %d stack" , gid ) ) , "\n" )
fid := - 1
for _ , line := range stackOut {
2021-10-18 20:17:47 +00:00
line = strings . TrimLeft ( line , " " )
2016-02-23 14:12:04 +00:00
space := strings . Index ( line , " " )
if space < 0 {
continue
}
curfid , err := strconv . Atoi ( line [ : space ] )
if err != nil {
continue
}
if idx := strings . Index ( line , " main.agoroutine" ) ; idx >= 0 {
fid = curfid
break
}
}
if fid < 0 {
2021-10-18 20:17:47 +00:00
t . Fatalf ( "Could not find frame for goroutine %d: %q" , gid , stackOut )
2016-02-23 14:12:04 +00:00
}
2017-04-19 10:17:21 +00:00
term . AssertExec ( fmt . Sprintf ( "goroutine %d frame %d locals" , gid , fid ) , "(no locals)\n" )
2016-02-23 14:12:04 +00:00
argsOut := strings . Split ( term . MustExec ( fmt . Sprintf ( "goroutine %d frame %d args" , gid , fid ) ) , "\n" )
if len ( argsOut ) != 4 || argsOut [ 3 ] != "" {
t . Fatalf ( "Wrong number of arguments in goroutine %d frame %d: %v" , gid , fid , argsOut )
}
out := term . MustExec ( fmt . Sprintf ( "goroutine %d frame %d p i" , gid , fid ) )
ival , err := strconv . Atoi ( out [ : len ( out ) - 1 ] )
if err != nil {
t . Fatalf ( "could not parse value %q of i for goroutine %d frame %d: %v" , out , gid , fid , err )
}
seen [ ival ] = true
}
for i := range seen {
2020-11-09 19:28:40 +00:00
if ! seen [ i ] {
2021-10-13 15:43:54 +00:00
if lenient > 0 {
lenient --
} else {
t . Fatalf ( "goroutine %d not found" , i )
}
2016-02-23 14:12:04 +00:00
}
}
term . MustExec ( "c" )
term . AssertExecError ( "frame" , "not enough arguments" )
term . AssertExecError ( fmt . Sprintf ( "goroutine %d frame 10 locals" , curgid ) , fmt . Sprintf ( "Frame 10 does not exist in goroutine %d" , curgid ) )
2020-03-23 17:57:01 +00:00
term . AssertExecError ( "goroutine 9000 locals" , "unknown goroutine 9000" )
2016-02-23 14:12:04 +00:00
term . AssertExecError ( "print n" , "could not find symbol value for n" )
term . AssertExec ( "frame 1 print n" , "3\n" )
term . AssertExec ( "frame 2 print n" , "2\n" )
term . AssertExec ( "frame 3 print n" , "1\n" )
term . AssertExec ( "frame 4 print n" , "0\n" )
term . AssertExecError ( "frame 5 print n" , "could not find symbol value for n" )
2018-03-22 17:02:15 +00:00
term . MustExec ( "frame 2" )
term . AssertExec ( "print n" , "2\n" )
term . MustExec ( "frame 4" )
term . AssertExec ( "print n" , "0\n" )
term . MustExec ( "down" )
term . AssertExec ( "print n" , "1\n" )
term . MustExec ( "down 2" )
term . AssertExec ( "print n" , "3\n" )
term . AssertExecError ( "down 2" , "Invalid frame -1" )
term . AssertExec ( "print n" , "3\n" )
term . MustExec ( "up 2" )
term . AssertExec ( "print n" , "1\n" )
term . AssertExecError ( "up 100" , "Invalid frame 103" )
term . AssertExec ( "print n" , "1\n" )
term . MustExec ( "step" )
term . AssertExecError ( "print n" , "could not find symbol value for n" )
term . MustExec ( "frame 2" )
term . AssertExec ( "print n" , "2\n" )
2016-02-23 14:12:04 +00:00
} )
}
func TestOnPrefix ( t * testing . T ) {
const prefix = "\ti: "
2017-05-05 22:17:52 +00:00
test . AllowRecording ( t )
2021-10-13 15:43:54 +00:00
lenient := false
if runtime . GOOS == "windows" {
lenient = true
}
2016-02-23 14:12:04 +00:00
withTestTerminal ( "goroutinestackprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "b agobp main.agoroutine" )
term . MustExec ( "on agobp print i" )
seen := make ( [ ] bool , 10 )
for {
outstr , err := term . Exec ( "continue" )
if err != nil {
2017-06-29 18:15:59 +00:00
if ! strings . Contains ( err . Error ( ) , "exited" ) {
2016-02-23 14:12:04 +00:00
t . Fatalf ( "Unexpected error executing 'continue': %v" , err )
}
break
}
out := strings . Split ( outstr , "\n" )
for i := range out {
2019-07-13 01:28:04 +00:00
if ! strings . HasPrefix ( out [ i ] , prefix ) {
2016-02-23 14:12:04 +00:00
continue
}
id , err := strconv . Atoi ( out [ i ] [ len ( prefix ) : ] )
if err != nil {
continue
}
if seen [ id ] {
t . Fatalf ( "Goroutine %d seen twice\n" , id )
}
seen [ id ] = true
}
}
for i := range seen {
2020-11-09 19:28:40 +00:00
if ! seen [ i ] {
2021-10-13 15:43:54 +00:00
if lenient {
lenient = false
} else {
t . Fatalf ( "Goroutine %d not seen\n" , i )
}
2016-02-23 14:12:04 +00:00
}
}
} )
}
2016-04-06 16:26:10 +00:00
func TestNoVars ( t * testing . T ) {
2017-05-05 22:17:52 +00:00
test . AllowRecording ( t )
2016-04-06 16:26:10 +00:00
withTestTerminal ( "locationsUpperCase" , t , func ( term * FakeTerminal ) {
term . MustExec ( "b main.main" )
term . MustExec ( "continue" )
term . AssertExec ( "args" , "(no args)\n" )
term . AssertExec ( "locals" , "(no locals)\n" )
term . AssertExec ( "vars filterThatMatchesNothing" , "(no vars)\n" )
} )
}
2016-04-24 17:15:39 +00:00
func TestOnPrefixLocals ( t * testing . T ) {
const prefix = "\ti: "
2017-05-05 22:17:52 +00:00
test . AllowRecording ( t )
2016-04-24 17:15:39 +00:00
withTestTerminal ( "goroutinestackprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "b agobp main.agoroutine" )
term . MustExec ( "on agobp args -v" )
seen := make ( [ ] bool , 10 )
for {
outstr , err := term . Exec ( "continue" )
if err != nil {
2017-06-29 18:15:59 +00:00
if ! strings . Contains ( err . Error ( ) , "exited" ) {
2016-04-24 17:15:39 +00:00
t . Fatalf ( "Unexpected error executing 'continue': %v" , err )
}
break
}
out := strings . Split ( outstr , "\n" )
for i := range out {
2019-07-13 01:28:04 +00:00
if ! strings . HasPrefix ( out [ i ] , prefix ) {
2016-04-24 17:15:39 +00:00
continue
}
id , err := strconv . Atoi ( out [ i ] [ len ( prefix ) : ] )
if err != nil {
continue
}
if seen [ id ] {
t . Fatalf ( "Goroutine %d seen twice\n" , id )
}
seen [ id ] = true
}
}
for i := range seen {
2020-11-09 19:28:40 +00:00
if ! seen [ i ] {
2016-04-24 17:15:39 +00:00
t . Fatalf ( "Goroutine %d not seen\n" , i )
}
}
} )
}
2016-04-24 23:20:02 +00:00
2017-04-19 13:45:23 +00:00
func listIsAt ( t * testing . T , term * FakeTerminal , listcmd string , cur , start , end int ) {
2020-11-12 23:24:31 +00:00
t . Helper ( )
2017-04-19 13:45:23 +00:00
outstr := term . MustExec ( listcmd )
lines := strings . Split ( outstr , "\n" )
t . Logf ( "%q: %q" , listcmd , outstr )
2019-10-25 16:59:18 +00:00
if cur >= 0 && ! strings . Contains ( lines [ 0 ] , fmt . Sprintf ( ":%d" , cur ) ) {
2017-04-19 13:45:23 +00:00
t . Fatalf ( "Could not find current line number in first output line: %q" , lines [ 0 ] )
}
re := regexp . MustCompile ( ` (=>)?\s+(\d+): ` )
outStart , outEnd := 0 , 0
2017-05-05 22:17:52 +00:00
for _ , line := range lines [ 1 : ] {
2017-04-19 13:45:23 +00:00
if line == "" {
continue
}
v := re . FindStringSubmatch ( line )
if len ( v ) != 3 {
2017-05-05 22:17:52 +00:00
continue
2017-04-19 13:45:23 +00:00
}
curline , _ := strconv . Atoi ( v [ 2 ] )
if v [ 1 ] == "=>" {
if cur != curline {
t . Fatalf ( "Wrong current line, got %d expected %d" , curline , cur )
}
}
if outStart == 0 {
outStart = curline
}
outEnd = curline
}
2017-05-05 22:17:52 +00:00
if start != - 1 || end != - 1 {
if outStart != start || outEnd != end {
t . Fatalf ( "Wrong output range, got %d:%d expected %d:%d" , outStart , outEnd , start , end )
}
2017-04-19 13:45:23 +00:00
}
}
func TestListCmd ( t * testing . T ) {
withTestTerminal ( "testvariables" , t , func ( term * FakeTerminal ) {
term . MustExec ( "continue" )
term . MustExec ( "continue" )
2020-11-12 23:24:31 +00:00
listIsAt ( t , term , "list" , 27 , 22 , 32 )
2021-03-08 17:41:47 +00:00
listIsAt ( t , term , "list 69" , 69 , 64 , 74 )
listIsAt ( t , term , "frame 1 list" , 66 , 61 , 71 )
listIsAt ( t , term , "frame 1 list 69" , 69 , 64 , 74 )
2017-04-19 13:45:23 +00:00
_ , err := term . Exec ( "frame 50 list" )
if err == nil {
t . Fatalf ( "Expected error requesting 50th frame" )
}
2019-10-25 16:59:18 +00:00
listIsAt ( t , term , "list testvariables.go:1" , - 1 , 1 , 6 )
listIsAt ( t , term , "list testvariables.go:10000" , - 1 , 0 , 0 )
2017-04-19 13:45:23 +00:00
} )
}
2017-05-05 22:17:52 +00:00
func TestReverseContinue ( t * testing . T ) {
test . AllowRecording ( t )
if testBackend != "rr" {
return
}
withTestTerminal ( "continuetestprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
term . MustExec ( "break main.sayhi" )
listIsAt ( t , term , "continue" , 16 , - 1 , - 1 )
listIsAt ( t , term , "continue" , 12 , - 1 , - 1 )
listIsAt ( t , term , "rewind" , 16 , - 1 , - 1 )
} )
}
func TestCheckpoints ( t * testing . T ) {
test . AllowRecording ( t )
if testBackend != "rr" {
return
}
withTestTerminal ( "continuetestprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
listIsAt ( t , term , "continue" , 16 , - 1 , - 1 )
term . MustExec ( "checkpoint" )
term . MustExec ( "checkpoints" )
listIsAt ( t , term , "next" , 17 , - 1 , - 1 )
listIsAt ( t , term , "next" , 18 , - 1 , - 1 )
2020-02-11 01:31:54 +00:00
term . MustExec ( "restart c1" )
term . MustExec ( "goroutine 1" )
listIsAt ( t , term , "list" , 16 , - 1 , - 1 )
2017-05-05 22:17:52 +00:00
} )
}
2017-05-08 18:16:14 +00:00
2019-07-30 01:04:26 +00:00
func TestNextWithCount ( t * testing . T ) {
test . AllowRecording ( t )
withTestTerminal ( "nextcond" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
listIsAt ( t , term , "continue" , 8 , - 1 , - 1 )
listIsAt ( t , term , "next 2" , 10 , - 1 , - 1 )
} )
}
2018-01-18 22:16:11 +00:00
func TestRestart ( t * testing . T ) {
withTestTerminal ( "restartargs" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.printArgs" )
term . MustExec ( "continue" )
if out := term . MustExec ( "print main.args" ) ; ! strings . Contains ( out , ", []" ) {
t . Fatalf ( "wrong args: %q" , out )
}
// Reset the arg list
term . MustExec ( "restart hello" )
term . MustExec ( "continue" )
if out := term . MustExec ( "print main.args" ) ; ! strings . Contains ( out , ", [\"hello\"]" ) {
t . Fatalf ( "wrong args: %q " , out )
}
// Restart w/o arg should retain the current args.
term . MustExec ( "restart" )
term . MustExec ( "continue" )
if out := term . MustExec ( "print main.args" ) ; ! strings . Contains ( out , ", [\"hello\"]" ) {
t . Fatalf ( "wrong args: %q " , out )
}
// Empty arg list
term . MustExec ( "restart -noargs" )
term . MustExec ( "continue" )
if out := term . MustExec ( "print main.args" ) ; ! strings . Contains ( out , ", []" ) {
t . Fatalf ( "wrong args: %q " , out )
}
} )
}
2017-05-08 18:16:14 +00:00
func TestIssue827 ( t * testing . T ) {
// switching goroutines when the current thread isn't running any goroutine
// causes nil pointer dereference.
withTestTerminal ( "notify-v2" , t , func ( term * FakeTerminal ) {
go func ( ) {
time . Sleep ( 1 * time . Second )
2023-05-17 10:01:26 +00:00
resp , err := http . Get ( "http://127.0.0.1:8888/test" )
if err == nil {
resp . Body . Close ( )
}
2017-05-08 18:16:14 +00:00
time . Sleep ( 1 * time . Second )
term . client . Halt ( )
} ( )
term . MustExec ( "continue" )
term . MustExec ( "goroutine 1" )
} )
}
2017-07-29 04:11:06 +00:00
func findCmdName ( c * Commands , cmdstr string , prefix cmdPrefix ) string {
for _ , v := range c . cmds {
if v . match ( cmdstr ) {
if prefix != noPrefix && v . allowedPrefixes & prefix == 0 {
continue
}
return v . aliases [ 0 ]
}
}
return ""
}
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
func assertNoError ( t * testing . T , err error , str string ) {
t . Helper ( )
if err != nil {
t . Fatalf ( "%s: %v" , str , err )
}
}
func assertNoErrorConfigureCmd ( t * testing . T , term * Term , cmdstr string ) {
t . Helper ( )
err := configureCmd ( term , callContext { } , cmdstr )
assertNoError ( t , err , fmt . Sprintf ( "error executing configureCmd(%s)" , cmdstr ) )
}
func assertSubstitutePath ( t * testing . T , sp config . SubstitutePathRules , v ... string ) {
t . Helper ( )
if len ( sp ) != len ( v ) / 2 {
t . Fatalf ( "wrong number of substitute path rules (expected: %d): %#v" , len ( v ) / 2 , sp )
}
for i := range sp {
if sp [ i ] . From != v [ i * 2 ] || sp [ i ] . To != v [ i * 2 + 1 ] {
t . Fatalf ( "wrong substitute path rule %#v expected (from: %q to %q)" , sp [ i ] , v [ i * 2 ] , v [ i * 2 + 1 ] )
}
}
}
func assertDebugInfoDirs ( t * testing . T , got [ ] string , tgt ... string ) {
if len ( got ) != len ( tgt ) {
t . Fatalf ( "wrong number of debug info directories (got %d expected %d)" , len ( got ) , len ( tgt ) )
}
for i := range got {
if got [ i ] != tgt [ i ] {
t . Fatalf ( "debug info directories mismatch got: %v expected: %v" , got , tgt )
}
}
}
2017-07-29 04:11:06 +00:00
func TestConfig ( t * testing . T ) {
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
var buf bytes . Buffer
2017-07-29 04:11:06 +00:00
var term Term
term . conf = & config . Config { }
term . cmds = DebugCommands ( nil )
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
term . stdout = & transcriptWriter { pw : & pagingWriter { w : & buf } }
2017-07-29 04:11:06 +00:00
err := configureCmd ( & term , callContext { } , "nonexistent-parameter 10" )
if err == nil {
t . Fatalf ( "expected error executing configureCmd(nonexistent-parameter)" )
}
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
assertNoErrorConfigureCmd ( t , & term , "max-string-len 10" )
2017-07-29 04:11:06 +00:00
if term . conf . MaxStringLen == nil {
t . Fatalf ( "expected MaxStringLen 10, got nil" )
}
if * term . conf . MaxStringLen != 10 {
t . Fatalf ( "expected MaxStringLen 10, got: %d" , * term . conf . MaxStringLen )
}
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
assertNoErrorConfigureCmd ( t , & term , "show-location-expr true" )
2020-01-19 09:55:58 +00:00
if term . conf . ShowLocationExpr != true {
t . Fatalf ( "expected ShowLocationExpr true, got false" )
}
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
assertNoErrorConfigureCmd ( t , & term , "max-variable-recurse 4" )
2019-07-23 21:40:35 +00:00
if term . conf . MaxVariableRecurse == nil {
t . Fatalf ( "expected MaxVariableRecurse 4, got nil" )
}
if * term . conf . MaxVariableRecurse != 4 {
t . Fatalf ( "expected MaxVariableRecurse 4, got: %d" , * term . conf . MaxVariableRecurse )
}
2017-07-29 04:11:06 +00:00
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
assertNoErrorConfigureCmd ( t , & term , "substitute-path a b" )
assertSubstitutePath ( t , term . conf . SubstitutePath , "a" , "b" )
2017-07-29 04:11:06 +00:00
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
assertNoErrorConfigureCmd ( t , & term , "substitute-path a" )
assertSubstitutePath ( t , term . conf . SubstitutePath )
2017-07-29 04:11:06 +00:00
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
assertNoErrorConfigureCmd ( t , & term , "alias print blah" )
2017-07-29 04:11:06 +00:00
if len ( term . conf . Aliases [ "print" ] ) != 1 {
t . Fatalf ( "aliases not changed after configure command %v" , term . conf . Aliases )
}
if findCmdName ( term . cmds , "blah" , noPrefix ) != "print" {
t . Fatalf ( "new alias not found" )
}
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
assertNoErrorConfigureCmd ( t , & term , "alias blah" )
2017-07-29 04:11:06 +00:00
if len ( term . conf . Aliases [ "print" ] ) != 0 {
t . Fatalf ( "alias not removed after configure command %v" , term . conf . Aliases )
}
if findCmdName ( term . cmds , "blah" , noPrefix ) != "" {
t . Fatalf ( "new alias found after delete" )
}
*: misc improvements to config command and substitute-path rules (#3335)
A series of interconnected changes to both the terminal command
'config', DAP command 'dlv config', quality of life improvements to how
substitute-path works, and better documentation.
- Let 'config substitute-path' show the current substitute path rules
- Add a -clear command to 'config substitute-path'
- Support 'config-debug-info-directories'
- rewrite SubstitutePath to be platform independent (see below)
- document path substitution more
Regarding the rewrite of SubstitutePath: the previous version used
runtime.GOOS and filepath.IsAbs to determine which filepath separator to use
and if matching should be case insensitive. This is wrong in all situations
where the client and server run on different OSes, when examining core files
and when cross-compilation is involved.
The new version of SubstitutePath checks the rules and the input path to
determine if Windows is involved in the process, if it looks like it is it
switches to case-insensitive matching. It uses a lax version of
filepath.IsAbs to determine if a path is absolute and tries to avoid having
to select a path separator as much as possible
Fixes #2891, #2890, #2889, #3179, #3332, #3343
2023-05-02 19:23:59 +00:00
err = configureCmd ( & term , callContext { } , "show-location-expr" )
if err == nil {
t . Fatalf ( "no error form configureCmd(show-location-expr)" )
}
if ! term . conf . ShowLocationExpr {
t . Fatalf ( "ShowLocationExpr not set to true" )
}
assertNoErrorConfigureCmd ( t , & term , "show-location-expr false" )
if term . conf . ShowLocationExpr {
t . Fatalf ( "ShowLocationExpr set to true" )
}
assertNoErrorConfigureCmd ( t , & term , "substitute-path a b" )
assertNoErrorConfigureCmd ( t , & term , "substitute-path c d" )
assertSubstitutePath ( t , term . conf . SubstitutePath , "a" , "b" , "c" , "d" )
buf . Reset ( )
assertNoErrorConfigureCmd ( t , & term , "substitute-path" )
t . Logf ( "current substitute-path: %q" , buf . String ( ) )
if buf . String ( ) != "\"a\" → \"b\"\n\"c\" → \"d\"\n" {
t . Fatalf ( "wrong substitute-path value" )
}
assertNoErrorConfigureCmd ( t , & term , "substitute-path -clear c" )
assertSubstitutePath ( t , term . conf . SubstitutePath , "a" , "b" )
assertNoErrorConfigureCmd ( t , & term , "substitute-path -clear" )
assertSubstitutePath ( t , term . conf . SubstitutePath )
assertNoErrorConfigureCmd ( t , & term , "substitute-path \"\" something" )
assertSubstitutePath ( t , term . conf . SubstitutePath , "" , "something" )
assertNoErrorConfigureCmd ( t , & term , "substitute-path somethingelse \"\"" )
assertSubstitutePath ( t , term . conf . SubstitutePath , "" , "something" , "somethingelse" , "" )
assertDebugInfoDirs ( t , term . conf . DebugInfoDirectories )
assertNoErrorConfigureCmd ( t , & term , "debug-info-directories -add a" )
assertDebugInfoDirs ( t , term . conf . DebugInfoDirectories , "a" )
assertNoErrorConfigureCmd ( t , & term , "debug-info-directories -add b" )
assertDebugInfoDirs ( t , term . conf . DebugInfoDirectories , "a" , "b" )
assertNoErrorConfigureCmd ( t , & term , "debug-info-directories -add c" )
assertDebugInfoDirs ( t , term . conf . DebugInfoDirectories , "a" , "b" , "c" )
assertNoErrorConfigureCmd ( t , & term , "debug-info-directories -rm b" )
assertDebugInfoDirs ( t , term . conf . DebugInfoDirectories , "a" , "c" )
assertNoErrorConfigureCmd ( t , & term , "debug-info-directories -clear" )
assertDebugInfoDirs ( t , term . conf . DebugInfoDirectories )
2017-07-29 04:11:06 +00:00
}
2017-11-28 14:32:25 +00:00
2018-01-26 14:48:47 +00:00
func TestIssue1090 ( t * testing . T ) {
// Exit while executing 'next' should report the "Process exited" error
// message instead of crashing.
withTestTerminal ( "math" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
term . MustExec ( "continue" )
for {
_ , err := term . Exec ( "next" )
if err != nil && strings . Contains ( err . Error ( ) , " has exited with status " ) {
break
}
}
} )
}
2018-04-08 10:41:47 +00:00
func TestPrintContextParkedGoroutine ( t * testing . T ) {
2023-07-07 16:30:38 +00:00
if runtime . GOARCH == "ppc64le" && buildMode == "pie" {
t . Skip ( "pie mode broken on ppc64le" )
}
2018-04-08 10:41:47 +00:00
withTestTerminal ( "goroutinestackprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break stacktraceme" )
term . MustExec ( "continue" )
// pick a goroutine that isn't running on a thread
gid := ""
gout := strings . Split ( term . MustExec ( "goroutines" ) , "\n" )
t . Logf ( "goroutines -> %q" , gout )
for _ , gline := range gout {
if ! strings . Contains ( gline , "thread " ) && strings . Contains ( gline , "agoroutine" ) {
if dash := strings . Index ( gline , " - " ) ; dash > 0 {
gid = gline [ len ( " Goroutine " ) : dash ]
break
}
}
}
t . Logf ( "picked %q" , gid )
term . MustExec ( fmt . Sprintf ( "goroutine %s" , gid ) )
frameout := strings . Split ( term . MustExec ( "frame 0" ) , "\n" )
t . Logf ( "frame 0 -> %q" , frameout )
if strings . Contains ( frameout [ 0 ] , "stacktraceme" ) {
2024-06-25 13:16:58 +00:00
t . Fatal ( "bad output for `frame 0` command on a parked goroutine" )
2018-04-08 10:41:47 +00:00
}
listout := strings . Split ( term . MustExec ( "list" ) , "\n" )
t . Logf ( "list -> %q" , listout )
if strings . Contains ( listout [ 0 ] , "stacktraceme" ) {
t . Fatal ( "bad output for list command on a parked goroutine" )
}
} )
}
2018-05-11 12:51:15 +00:00
func TestStepOutReturn ( t * testing . T ) {
ver , _ := goversion . Parse ( runtime . Version ( ) )
2020-03-18 16:25:32 +00:00
if ver . Major >= 0 && ! ver . AfterOrEqual ( goversion . GoVersion { Major : 1 , Minor : 10 , Rev : - 1 } ) {
2018-05-11 12:51:15 +00:00
t . Skip ( "return variables aren't marked on 1.9 or earlier" )
}
withTestTerminal ( "stepoutret" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.stepout" )
term . MustExec ( "continue" )
out := term . MustExec ( "stepout" )
t . Logf ( "output: %q" , out )
if ! strings . Contains ( out , "num: " ) || ! strings . Contains ( out , "str: " ) {
t . Fatal ( "could not find parameter" )
}
} )
}
2018-06-20 08:06:12 +00:00
func TestOptimizationCheck ( t * testing . T ) {
withTestTerminal ( "continuetestprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
out := term . MustExec ( "continue" )
t . Logf ( "output %q" , out )
if strings . Contains ( out , optimizedFunctionWarning ) {
t . Fatal ( "optimized function warning" )
}
} )
if goversion . VersionAfterOrEqual ( runtime . Version ( ) , 1 , 10 ) {
withTestTerminalBuildFlags ( "continuetestprog" , t , test . EnableOptimization | test . EnableInlining , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
out := term . MustExec ( "continue" )
t . Logf ( "output %q" , out )
if ! strings . Contains ( out , optimizedFunctionWarning ) {
t . Fatal ( "optimized function warning missing" )
}
} )
}
}
2018-08-18 10:12:39 +00:00
func TestTruncateStacktrace ( t * testing . T ) {
2023-07-07 16:30:38 +00:00
if runtime . GOARCH == "ppc64le" && buildMode == "pie" {
t . Skip ( "pie mode broken on ppc64le" )
}
2021-06-16 20:05:17 +00:00
const stacktraceTruncatedMessage = "(truncated)"
2018-08-18 10:12:39 +00:00
withTestTerminal ( "stacktraceprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.stacktraceme" )
term . MustExec ( "continue" )
out1 := term . MustExec ( "stack" )
t . Logf ( "untruncated output %q" , out1 )
if strings . Contains ( out1 , stacktraceTruncatedMessage ) {
t . Fatalf ( "stacktrace was truncated" )
}
out2 := term . MustExec ( "stack 1" )
t . Logf ( "truncated output %q" , out2 )
if ! strings . Contains ( out2 , stacktraceTruncatedMessage ) {
t . Fatalf ( "stacktrace was not truncated" )
}
} )
}
2019-02-26 16:53:45 +00:00
func TestIssue1493 ( t * testing . T ) {
// The 'regs' command without the '-a' option should only return
// general purpose registers.
2023-07-07 16:30:38 +00:00
if runtime . GOARCH == "ppc64le" {
t . Skip ( "skipping, some registers such as vector registers are currently not loaded" )
}
2019-02-26 16:53:45 +00:00
withTestTerminal ( "continuetestprog" , t , func ( term * FakeTerminal ) {
r := term . MustExec ( "regs" )
nr := len ( strings . Split ( r , "\n" ) )
t . Logf ( "regs: %s" , r )
ra := term . MustExec ( "regs -a" )
nra := len ( strings . Split ( ra , "\n" ) )
t . Logf ( "regs -a: %s" , ra )
2020-02-24 18:47:02 +00:00
if nr > nra / 2 + 1 {
2019-02-26 16:53:45 +00:00
t . Fatalf ( "'regs' returned too many registers (%d) compared to 'regs -a' (%d)" , nr , nra )
}
} )
}
2019-07-02 17:55:27 +00:00
func findStarFile ( name string ) string {
return filepath . Join ( test . FindFixturesDir ( ) , name + ".star" )
}
2019-07-08 17:27:31 +00:00
func TestIssue1598 ( t * testing . T ) {
2023-09-21 17:39:57 +00:00
if buildMode == "pie" && runtime . GOARCH == "ppc64le" {
t . Skip ( "Debug function call Test broken in PIE mode" )
}
2019-07-08 17:27:31 +00:00
test . MustSupportFunctionCalls ( t , testBackend )
withTestTerminal ( "issue1598" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break issue1598.go:5" )
term . MustExec ( "continue" )
term . MustExec ( "config max-string-len 500" )
r := term . MustExec ( "call x()" )
t . Logf ( "result %q" , r )
if ! strings . Contains ( r , "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut \\nlabore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut" ) {
t . Fatalf ( "wrong value returned" )
}
} )
}
2020-02-13 17:29:21 +00:00
func TestExamineMemoryCmd ( t * testing . T ) {
withTestTerminal ( "examinememory" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break examinememory.go:19" )
term . MustExec ( "break examinememory.go:24" )
term . MustExec ( "continue" )
addressStr := strings . TrimSpace ( term . MustExec ( "p bspUintptr" ) )
address , err := strconv . ParseInt ( addressStr , 0 , 64 )
if err != nil {
t . Fatalf ( "could convert %s into int64, err %s" , addressStr , err )
}
2020-09-11 06:21:11 +00:00
res := term . MustExec ( "examinemem -count 52 -fmt hex " + addressStr )
2020-02-13 17:29:21 +00:00
t . Logf ( "the result of examining memory \n%s" , res )
// check first line
2020-02-27 06:53:09 +00:00
firstLine := fmt . Sprintf ( "%#x: 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11" , address )
2020-02-13 17:29:21 +00:00
if ! strings . Contains ( res , firstLine ) {
t . Fatalf ( "expected first line: %s" , firstLine )
}
// check last line
2020-02-27 06:53:09 +00:00
lastLine := fmt . Sprintf ( "%#x: 0x3a 0x3b 0x3c 0x00" , address + 6 * 8 )
2020-02-13 17:29:21 +00:00
if ! strings . Contains ( res , lastLine ) {
t . Fatalf ( "expected last line: %s" , lastLine )
}
// second examining memory
term . MustExec ( "continue" )
2020-09-11 06:21:11 +00:00
res = term . MustExec ( "x -count 52 -fmt bin " + addressStr )
2020-02-13 17:29:21 +00:00
t . Logf ( "the second result of examining memory result \n%s" , res )
// check first line
2020-02-27 06:53:09 +00:00
firstLine = fmt . Sprintf ( "%#x: 11111111 00001011 00001100 00001101" , address )
2020-02-13 17:29:21 +00:00
if ! strings . Contains ( res , firstLine ) {
t . Fatalf ( "expected first line: %s" , firstLine )
}
2021-04-26 17:36:24 +00:00
// third examining memory: -x addr
res = term . MustExec ( "examinemem -x " + addressStr )
t . Logf ( "the third result of examining memory result \n%s" , res )
firstLine = fmt . Sprintf ( "%#x: 0xff" , address )
if ! strings . Contains ( res , firstLine ) {
t . Fatalf ( "expected first line: %s" , firstLine )
}
// fourth examining memory: -x addr + offset
res = term . MustExec ( "examinemem -x " + addressStr + " + 8" )
t . Logf ( "the fourth result of examining memory result \n%s" , res )
firstLine = fmt . Sprintf ( "%#x: 0x12" , address + 8 )
if ! strings . Contains ( res , firstLine ) {
t . Fatalf ( "expected first line: %s" , firstLine )
}
// fifth examining memory: -x &var
res = term . MustExec ( "examinemem -x &bs[0]" )
t . Logf ( "the fifth result of examining memory result \n%s" , res )
firstLine = fmt . Sprintf ( "%#x: 0xff" , address )
if ! strings . Contains ( res , firstLine ) {
t . Fatalf ( "expected first line: %s" , firstLine )
}
// sixth examining memory: -fmt and double spaces
res = term . MustExec ( "examinemem -fmt hex -x &bs[0]" )
t . Logf ( "the sixth result of examining memory result \n%s" , res )
firstLine = fmt . Sprintf ( "%#x: 0xff" , address )
if ! strings . Contains ( res , firstLine ) {
t . Fatalf ( "expected first line: %s" , firstLine )
}
} )
withTestTerminal ( "testvariables2" , t , func ( term * FakeTerminal ) {
tests := [ ] struct {
Expr string
Want int
} {
{ Expr : "&i1" , Want : 1 } ,
{ Expr : "&i2" , Want : 2 } ,
{ Expr : "p1" , Want : 1 } ,
{ Expr : "*pp1" , Want : 1 } ,
{ Expr : "&str1[1]" , Want : '1' } ,
{ Expr : "c1.pb" , Want : 1 } ,
{ Expr : "&c1.pb.a" , Want : 1 } ,
{ Expr : "&c1.pb.a.A" , Want : 1 } ,
{ Expr : "&c1.pb.a.B" , Want : 2 } ,
}
term . MustExec ( "continue" )
for _ , test := range tests {
res := term . MustExec ( "examinemem -fmt dec -x " + test . Expr )
// strip addr from output, e.g. "0xc0000160b8: 023" -> "023"
res = strings . TrimSpace ( strings . Split ( res , ":" ) [ 1 ] )
got , err := strconv . Atoi ( res )
if err != nil {
t . Fatalf ( "expr=%q err=%s" , test . Expr , err )
} else if got != test . Want {
t . Errorf ( "expr=%q got=%d want=%d" , test . Expr , got , test . Want )
}
}
2020-02-13 17:29:21 +00:00
} )
}
2020-08-05 16:38:53 +00:00
func TestPrintOnTracepoint ( t * testing . T ) {
withTestTerminal ( "increment" , t , func ( term * FakeTerminal ) {
term . MustExec ( "trace main.Increment" )
term . MustExec ( "on 1 print y+1" )
out , _ := term . Exec ( "continue" )
if ! strings . Contains ( out , "y+1: 4" ) || ! strings . Contains ( out , "y+1: 2" ) || ! strings . Contains ( out , "y+1: 1" ) {
t . Errorf ( "output did not contain breakpoint information: %q" , out )
}
} )
}
2020-08-24 20:37:06 +00:00
func TestPrintCastToInterface ( t * testing . T ) {
withTestTerminal ( "testvariables2" , t , func ( term * FakeTerminal ) {
term . MustExec ( "continue" )
out := term . MustExec ( ` p (*"interface { }")(uintptr(&iface2)) ` )
t . Logf ( "%q" , out )
} )
}
2020-08-21 14:14:02 +00:00
func TestParseNewArgv ( t * testing . T ) {
testCases := [ ] struct {
in string
tgtargs string
tgtredir string
tgterr string
} {
{ "-noargs" , "" , " | | " , "" } ,
{ "-noargs arg1" , "" , "" , "too many arguments to restart" } ,
{ "arg1 arg2" , "arg1 | arg2" , " | | " , "" } ,
{ "arg1 arg2 <input.txt" , "arg1 | arg2" , "input.txt | | " , "" } ,
{ "arg1 arg2 < input.txt" , "arg1 | arg2" , "input.txt | | " , "" } ,
{ "<input.txt" , "" , "input.txt | | " , "" } ,
{ "< input.txt" , "" , "input.txt | | " , "" } ,
{ "arg1 < input.txt > output.txt 2> error.txt" , "arg1" , "input.txt | output.txt | error.txt" , "" } ,
{ "< input.txt > output.txt 2> error.txt" , "" , "input.txt | output.txt | error.txt" , "" } ,
{ "arg1 <input.txt >output.txt 2>error.txt" , "arg1" , "input.txt | output.txt | error.txt" , "" } ,
{ "<input.txt >output.txt 2>error.txt" , "" , "input.txt | output.txt | error.txt" , "" } ,
{ "<input.txt <input2.txt" , "" , "" , "redirect error: stdin redirected twice" } ,
}
for _ , tc := range testCases {
resetArgs , newArgv , newRedirects , err := parseNewArgv ( tc . in )
t . Logf ( "%q -> %q %q %v\n" , tc . in , newArgv , newRedirects , err )
if tc . tgterr != "" {
if err == nil {
t . Errorf ( "Expected error %q, got no error" , tc . tgterr )
} else if errstr := err . Error ( ) ; errstr != tc . tgterr {
t . Errorf ( "Expected error %q, got error %q" , tc . tgterr , errstr )
}
} else {
if ! resetArgs {
t . Errorf ( "parse error, resetArgs is false" )
continue
}
argvstr := strings . Join ( newArgv , " | " )
if argvstr != tc . tgtargs {
t . Errorf ( "Expected new arguments %q, got %q" , tc . tgtargs , argvstr )
}
redirstr := strings . Join ( newRedirects [ : ] , " | " )
if redirstr != tc . tgtredir {
t . Errorf ( "Expected new redirects %q, got %q" , tc . tgtredir , redirstr )
}
}
}
}
2021-03-11 21:27:29 +00:00
func TestContinueUntil ( t * testing . T ) {
withTestTerminal ( "continuetestprog" , t , func ( term * FakeTerminal ) {
2021-03-15 16:33:13 +00:00
if runtime . GOARCH != "386" {
listIsAt ( t , term , "continue main.main" , 16 , - 1 , - 1 )
} else {
listIsAt ( t , term , "continue main.main" , 17 , - 1 , - 1 )
2021-07-28 11:13:32 +00:00
}
listIsAt ( t , term , "continue main.sayhi" , 12 , - 1 , - 1 )
} )
}
func TestContinueUntilExistingBreakpoint ( t * testing . T ) {
withTestTerminal ( "continuetestprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
if runtime . GOARCH != "386" {
listIsAt ( t , term , "continue main.main" , 16 , - 1 , - 1 )
} else {
listIsAt ( t , term , "continue main.main" , 17 , - 1 , - 1 )
2021-03-15 16:33:13 +00:00
}
2021-03-11 21:27:29 +00:00
listIsAt ( t , term , "continue main.sayhi" , 12 , - 1 , - 1 )
} )
}
2021-03-25 16:45:30 +00:00
func TestPrintFormat ( t * testing . T ) {
withTestTerminal ( "testvariables2" , t , func ( term * FakeTerminal ) {
term . MustExec ( "continue" )
out := term . MustExec ( "print %#x m2[1].B" )
if ! strings . Contains ( out , "0xb\n" ) {
t . Fatalf ( "output did not contain '0xb': %q" , out )
}
} )
}
2021-06-02 20:47:32 +00:00
func TestHitCondBreakpoint ( t * testing . T ) {
withTestTerminal ( "break" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break bp1 main.main:4" )
term . MustExec ( "condition -hitcount bp1 > 2" )
listIsAt ( t , term , "continue" , 7 , - 1 , - 1 )
out := term . MustExec ( "print i" )
t . Logf ( "%q" , out )
if ! strings . Contains ( out , "3\n" ) {
t . Fatalf ( "wrong value of i" )
}
} )
2022-07-12 08:31:34 +00:00
withTestTerminal ( "condperghitcount" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break bp1 main.main:8" )
term . MustExec ( "condition -per-g-hitcount bp1 == 2" )
listIsAt ( t , term , "continue" , 16 , - 1 , - 1 )
// first g hit
out := term . MustExec ( "print j" )
t . Logf ( "%q" , out )
if ! strings . Contains ( out , "2\n" ) {
t . Fatalf ( "wrong value of j" )
}
listIsAt ( t , term , "continue" , 16 , - 1 , - 1 )
// second g hit
out = term . MustExec ( "print j" )
t . Logf ( "%q" , out )
if ! strings . Contains ( out , "2\n" ) {
t . Fatalf ( "wrong value of j" )
}
} )
2021-06-02 20:47:32 +00:00
}
2021-07-22 17:16:42 +00:00
2023-10-24 16:57:39 +00:00
func TestCondBreakpointWithFrame ( t * testing . T ) {
withTestTerminal ( "condframe" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break bp1 callme2" )
term . MustExec ( "condition bp1 runtime.frame(1).i == 3" )
term . MustExec ( "continue" )
out := term . MustExec ( "frame 1 print i" )
t . Logf ( "%q" , out )
if ! strings . Contains ( out , "3\n" ) {
t . Fatalf ( "wrong value of i" )
}
} )
}
2022-02-15 17:41:23 +00:00
func TestClearCondBreakpoint ( t * testing . T ) {
withTestTerminal ( "break" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main:4" )
term . MustExec ( "condition 1 i%3==2" )
listIsAt ( t , term , "continue" , 7 , - 1 , - 1 )
out := term . MustExec ( "print i" )
t . Logf ( "%q" , out )
if ! strings . Contains ( out , "2\n" ) {
t . Fatalf ( "wrong value of i" )
}
term . MustExec ( "condition -clear 1" )
listIsAt ( t , term , "continue" , 7 , - 1 , - 1 )
out = term . MustExec ( "print i" )
t . Logf ( "%q" , out )
if ! strings . Contains ( out , "3\n" ) {
t . Fatalf ( "wrong value of i" )
}
} )
}
2021-07-22 17:16:42 +00:00
func TestBreakpointEditing ( t * testing . T ) {
term := & FakeTerminal {
t : t ,
Term : New ( nil , & config . Config { } ) ,
}
_ = term
var testCases = [ ] struct {
inBp * api . Breakpoint
inBpStr string
edit string
outBp * api . Breakpoint
} {
{ // tracepoint -> breakpoint
& api . Breakpoint { Tracepoint : true } ,
"trace" ,
"" ,
& api . Breakpoint { } } ,
{ // breakpoint -> tracepoint
& api . Breakpoint { Variables : [ ] string { "a" } } ,
"print a" ,
"print a\ntrace" ,
& api . Breakpoint { Tracepoint : true , Variables : [ ] string { "a" } } } ,
{ // add print var
& api . Breakpoint { Variables : [ ] string { "a" } } ,
"print a" ,
"print b\nprint a\n" ,
& api . Breakpoint { Variables : [ ] string { "b" , "a" } } } ,
{ // add goroutine flag
& api . Breakpoint { } ,
"" ,
"goroutine" ,
& api . Breakpoint { Goroutine : true } } ,
{ // remove goroutine flag
& api . Breakpoint { Goroutine : true } ,
"goroutine" ,
"" ,
& api . Breakpoint { } } ,
{ // add stack directive
& api . Breakpoint { } ,
"" ,
"stack 10" ,
& api . Breakpoint { Stacktrace : 10 } } ,
{ // remove stack directive
& api . Breakpoint { Stacktrace : 20 } ,
"stack 20" ,
"print a" ,
& api . Breakpoint { Variables : [ ] string { "a" } } } ,
{ // add condition
& api . Breakpoint { Variables : [ ] string { "a" } } ,
"print a" ,
"print a\ncond a < b" ,
& api . Breakpoint { Variables : [ ] string { "a" } , Cond : "a < b" } } ,
{ // remove condition
& api . Breakpoint { Cond : "a < b" } ,
"cond a < b" ,
"" ,
& api . Breakpoint { } } ,
{ // change condition
& api . Breakpoint { Cond : "a < b" } ,
"cond a < b" ,
"cond a < 5" ,
& api . Breakpoint { Cond : "a < 5" } } ,
{ // change hitcount condition
& api . Breakpoint { HitCond : "% 2" } ,
"cond -hitcount % 2" ,
"cond -hitcount = 2" ,
& api . Breakpoint { HitCond : "= 2" } } ,
}
for _ , tc := range testCases {
bp := * tc . inBp
bpStr := strings . Join ( formatBreakpointAttrs ( "" , & bp , true ) , "\n" )
if bpStr != tc . inBpStr {
t . Errorf ( "Expected %q got %q for:\n%#v" , tc . inBpStr , bpStr , tc . inBp )
}
ctx := callContext { Prefix : onPrefix , Scope : api . EvalScope { GoroutineID : - 1 , Frame : 0 , DeferredCall : 0 } , Breakpoint : & bp }
err := term . cmds . parseBreakpointAttrs ( nil , ctx , strings . NewReader ( tc . edit ) )
if err != nil {
t . Errorf ( "Unexpected error during edit %q" , tc . edit )
}
if ! reflect . DeepEqual ( bp , * tc . outBp ) {
t . Errorf ( "mismatch after edit\nexpected: %#v\ngot: %#v" , tc . outBp , bp )
}
}
}
2022-01-27 21:18:25 +00:00
func TestTranscript ( t * testing . T ) {
withTestTerminal ( "math" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
out := term . MustExec ( "continue" )
2024-02-28 14:49:33 +00:00
if ! strings . HasPrefix ( out , "> [Breakpoint 1] main.main()" ) {
2022-01-27 21:18:25 +00:00
t . Fatalf ( "Wrong output for next: <%s>" , out )
}
2023-09-25 18:41:59 +00:00
fh , err := os . CreateTemp ( "" , "test-transcript-*" )
2022-01-27 21:18:25 +00:00
if err != nil {
2023-09-25 18:41:59 +00:00
t . Fatalf ( "CreateTemp: %v" , err )
2022-01-27 21:18:25 +00:00
}
name := fh . Name ( )
fh . Close ( )
t . Logf ( "output to %q" , name )
slurp := func ( ) string {
2023-09-25 18:41:59 +00:00
b , err := os . ReadFile ( name )
2022-01-27 21:18:25 +00:00
if err != nil {
t . Fatalf ( "could not read transcript file: %v" , err )
}
return string ( b )
}
term . MustExec ( fmt . Sprintf ( "transcript %s" , name ) )
out = term . MustExec ( "list" )
//term.MustExec("transcript -off")
if out != slurp ( ) {
t . Logf ( "output of list %s" , out )
t . Logf ( "contents of transcript: %s" , slurp ( ) )
t . Errorf ( "transcript and command out differ" )
}
term . MustExec ( fmt . Sprintf ( "transcript -t -x %s" , name ) )
out = term . MustExec ( ` print "hello" ` )
if out != "" {
t . Errorf ( "output of print is %q but should have been suppressed by transcript" , out )
}
if slurp ( ) != "\"hello\"\n" {
t . Errorf ( "wrong contents of transcript: %q" , slurp ( ) )
}
os . Remove ( name )
} )
}
2022-07-14 21:08:47 +00:00
func TestDisassPosCmd ( t * testing . T ) {
2023-07-07 16:30:38 +00:00
if runtime . GOARCH == "ppc64le" && buildMode == "pie" {
t . Skip ( "pie mode broken on ppc64le" )
}
2022-07-14 21:08:47 +00:00
withTestTerminal ( "testvariables2" , t , func ( term * FakeTerminal ) {
term . MustExec ( "continue" )
out := term . MustExec ( "step-instruction" )
t . Logf ( "%q\n" , out )
if ! strings . Contains ( out , "call $runtime.Breakpoint" ) && ! strings . Contains ( out , "CALL runtime.Breakpoint(SB)" ) {
t . Errorf ( "output doesn't look like disassembly" )
}
} )
}
2022-11-16 17:31:33 +00:00
func TestCreateBreakpointByLocExpr ( t * testing . T ) {
withTestTerminal ( "math" , t , func ( term * FakeTerminal ) {
out := term . MustExec ( "break main.main" )
position1 := strings . Split ( out , " set at " ) [ 1 ]
term . MustExec ( "continue" )
term . MustExec ( "clear 1" )
out = term . MustExec ( "break +0" )
position2 := strings . Split ( out , " set at " ) [ 1 ]
if position1 != position2 {
t . Fatalf ( "mismatched positions %q and %q\n" , position1 , position2 )
}
} )
}
2023-07-20 10:29:59 +00:00
2024-04-09 13:15:38 +00:00
func TestCreateBreakpointWithCondition ( t * testing . T ) {
withTestTerminal ( "break" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break bp1 main.main:4 if i == 3" )
2024-04-15 08:07:36 +00:00
out := term . MustExec ( "breakpoints" )
if ! strings . Contains ( out , "Breakpoint bp1" ) {
t . Fatal ( "incorrect breakpoint name" )
}
2024-04-09 13:15:38 +00:00
listIsAt ( t , term , "continue" , 7 , - 1 , - 1 )
2024-04-15 08:07:36 +00:00
out = term . MustExec ( "print i" )
2024-04-09 13:15:38 +00:00
t . Logf ( "%q" , out )
if ! strings . Contains ( out , "3\n" ) {
t . Fatalf ( "wrong value of i" )
}
} )
}
func TestCreateBreakpointWithCondition2 ( t * testing . T ) {
withTestTerminal ( "break" , t , func ( term * FakeTerminal ) {
term . MustExec ( "continue main.main:4" )
term . MustExec ( "break if i == 3" )
2024-04-15 08:07:36 +00:00
out := term . MustExec ( "breakpoints" )
if strings . Contains ( out , "Breakpoint if" ) {
t . Fatal ( "incorrect breakpoint name, should be ID" )
}
2024-04-09 13:15:38 +00:00
listIsAt ( t , term , "continue" , 7 , - 1 , - 1 )
2024-04-15 08:07:36 +00:00
out = term . MustExec ( "print i" )
2024-04-09 13:15:38 +00:00
t . Logf ( "%q" , out )
if ! strings . Contains ( out , "3\n" ) {
t . Fatalf ( "wrong value of i" )
}
} )
}
func TestCreateBreakpointWithCondition3 ( t * testing . T ) {
withTestTerminal ( "test if path/main" , t , func ( term * FakeTerminal ) {
// We should not attempt to parse this as a condition.
term . MustExec ( ` break _fixtures/test if path/main.go:4 ` )
listIsAt ( t , term , "continue" , 4 , - 1 , - 1 )
} )
}
2023-07-20 10:29:59 +00:00
func TestRestartBreakpoints ( t * testing . T ) {
// Tests that breakpoints set using just a line number and with a line
// offset are preserved after restart. See issue #3423.
withTestTerminal ( "continuetestprog" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
term . MustExec ( "continue" )
term . MustExec ( "break 9" )
term . MustExec ( "break +1" )
out := term . MustExec ( "breakpoints" )
t . Log ( "breakpoints before:\n" , out )
term . MustExec ( "restart" )
out = term . MustExec ( "breakpoints" )
t . Log ( "breakpoints after:\n" , out )
bps , err := term . client . ListBreakpoints ( false )
assertNoError ( t , err , "ListBreakpoints" )
for _ , bp := range bps {
if bp . ID < 0 {
continue
}
if bp . Addr == 0 {
t . Fatalf ( "breakpoint %d has address 0" , bp . ID )
}
}
} )
}
2023-09-18 15:59:58 +00:00
func TestListPackages ( t * testing . T ) {
test . AllowRecording ( t )
withTestTerminal ( "goroutinestackprog" , t , func ( term * FakeTerminal ) {
out := term . MustExec ( "packages" )
t . Logf ( "> packages\n%s" , out )
seen := map [ string ] bool { }
for _ , p := range strings . Split ( strings . TrimSpace ( out ) , "\n" ) {
seen [ p ] = true
}
if ! seen [ "main" ] || ! seen [ "runtime" ] {
t . Error ( "output omits 'main' and 'runtime'" )
}
out = term . MustExec ( "packages runtime" )
t . Logf ( "> packages runtime\n%s" , out )
for _ , p := range strings . Split ( strings . TrimSpace ( out ) , "\n" ) {
if ! strings . Contains ( p , "runtime" ) {
t . Errorf ( "output includes unexpected %q" , p )
}
seen [ p ] = true
}
if ! seen [ "runtime" ] {
t . Error ( "output omits 'runtime'" )
}
} )
}
2023-11-16 18:43:23 +00:00
func TestSubstitutePathAndList ( t * testing . T ) {
// checks that substitute path rules do not remain cached after a -clear.
// See issue #3565.
if runtime . GOOS == "windows" {
t . Skip ( "test is not valid on windows due to path separators" )
}
withTestTerminal ( "math" , t , func ( term * FakeTerminal ) {
term . MustExec ( "break main.main" )
term . MustExec ( "continue" )
fixturesDir , _ := filepath . Abs ( test . FindFixturesDir ( ) )
term . MustExec ( "config substitute-path " + fixturesDir + " /blah" )
out , _ := term . Exec ( "list" )
t . Logf ( "list output %s" , out )
if ! strings . Contains ( out , "/blah/math.go" ) {
t . Fatalf ( "bad output" )
}
term . MustExec ( "config substitute-path -clear" )
term . MustExec ( "config substitute-path " + fixturesDir + " /blah2" )
out , _ = term . Exec ( "list" )
t . Logf ( "list output %s" , out )
if ! strings . Contains ( out , "/blah2/math.go" ) {
t . Fatalf ( "bad output" )
}
} )
}
2023-12-12 19:43:41 +00:00
func TestDisplay ( t * testing . T ) {
// Tests that display command works. See issue #3595.
type testCase struct { in , tgt string }
withTestTerminal ( "testvariables2" , t , func ( term * FakeTerminal ) {
term . MustExec ( "continue" )
for _ , tc := range [ ] testCase {
{ "string(byteslice)" , ` 0: string(byteslice) = "tèst" ` } ,
{ "string(byteslice[1:])" , ` 0: string(byteslice[1:]) = "èst" ` } ,
{ "%s string(byteslice)" , ` 0: string(byteslice) = tèst ` } ,
} {
out := term . MustExec ( "display -a " + tc . in )
t . Logf ( "%q -> %q" , tc . in , out )
if ! strings . Contains ( out , tc . tgt ) {
t . Errorf ( "wrong output for 'display -a %s':\n\tgot: %q\n\texpected: %q" , tc . in , out , tc . tgt )
}
term . MustExec ( "display -d 0" )
}
} )
}