NAME=spawn + attach and suspend
FILE=bins/mach0/hello-macos-arm64
ARGS=-d
CMDS=<<EOF
# when spawning the process ourselves, it should be
# suspended right away and stop at the entry in dyld.
# This is a bit hard to check automatically, so let's
# just make sure pc is in a map of dyld even after sleeping.
!sleep 0.5
dm @ pc~*~[9]
EOF
EXPECT=<<EOF
/usr/lib/dyld
EOF
RUN

NAME=dcu + read
FILE=bins/mach0/hello-macos-arm64
ARGS=-d
CMDS=<<EOF
dcu main
pi 8 @ pc
EOF
EXPECT=<<EOF
sub sp, sp, 0x50
stp fp, lr, [sp, 0x40]
add fp, sp, 0x40
adrp x8, reloc.NSLog
add x8, x8, 0x180
stur w0, [fp, -4]
stur x1, [fp, -0x10]
str x8, [sp, 0x10]
EOF
REGEXP_FILTER_ERR=(((Continue\suntil)|(hit\sbreakpoint\sat:)|[0-9a-f][0-9a-f][0-9a-f]\n))
EXPECT_ERR=<<EOF
Continue until
c88

hit breakpoint at:
c88

EOF
RUN

NAME=maps
FILE=bins/mach0/hello-macos-arm64
ARGS=-d
CMDS=<<EOF
dm~hello~[3-9]
EOF
REGEXP_FILTER_OUT=([a-zA-Z0-9_-]+[\.rwx_-]*\s+)
EXPECT=<<EOF
- 
usr 
16K 
u 
r-x 
hello-macos-arm64 
hello-macos-arm64

- 
usr 
16K 
u 
rw- 
hello-macos-arm64 
hello-macos-arm64

- 
usr 
16K 
u 
rw- 
hello-macos-arm64 
hello-macos-arm64

- 
usr 
16K 
u 
r-- 
hello-macos-arm64 
hello-macos-arm64
EOF
RUN

NAME=maps as flags
FILE=bins/mach0/hello-macos-arm64
ARGS=-d
CMDS=<<EOF
fl@F:maps~hello~[1-]
echo --
dm*~hello~[0-2]
EOF
REGEXP_FILTER_OUT=([0-9]+ [a-zA-Z0-9_-]+\.[\.rwx_]+|[f-].+\n)
EXPECT=<<EOF
16384 hello_macos_arm64.r_x
16384 hello_macos_arm64.rw
16384 hello_macos_arm64.rw.
16384 hello_macos_arm64.r
--

f+ map.hello_macos_arm64.r_x 0x00004000

f+ map.hello_macos_arm64.rw 0x00004000

f+ map.hello_macos_arm64.rw 0x00004000

f+ map.hello_macos_arm64.r 0x00004000
EOF
RUN

NAME=step and check pc
FILE=bins/mach0/calculate-macos-arm64
ARGS=-d
CMDS=<<EOF
dcu main
5 ds
dr pc
dr x8
echo --
ds
dr pc
dr x8
EOF
REGEXP_FILTER_OUT=((x8\s=\s0x.*)|(pc\s=)|(f44)|(f48))
EXPECT=<<EOF
pc =
f44
x8 = 0x0000000000000000
pc =
f48
x8 = 0x0000000000000064
EOF
REGEXP_FILTER_ERR=(((Continue\suntil)|(hit\sbreakpoint\sat:)|[0-9a-f][0-9a-f][0-9a-f]\n))
EXPECT_ERR=<<EOF
Continue until
f30

hit breakpoint at:
f30

EOF
RUN

NAME=break multiple times and check regs
FILE=bins/mach0/calculate-macos-arm64
ARGS=-d
CMDS=<<EOF
db @ main
db @ main + 20
db @ main + 24
dc
dr pc
echo --
dc
dr pc
dr x8
echo --
dc
dr pc
dr x8
EOF
REGEXP_FILTER_OUT=((x8\s=\s0x.*)|(pc\s=)|(f30)|(f44)|(f48))
EXPECT=<<EOF
pc =
f30
pc =
f44
x8 = 0x0000000000000000
pc =
f48
x8 = 0x0000000000000064
EOF
REGEXP_FILTER_ERR=(((hit\sbreakpoint\sat:)|[0-9a-f][0-9a-f][0-9a-f]\n))
EXPECT_ERR=<<EOF
hit breakpoint at:
f30

hit breakpoint at:
f44

hit breakpoint at:
f48

EOF
RUN

NAME=write gpr
FILE=bins/mach0/calculate-macos-arm64
ARGS=-d
CMDS=<<EOF
db @ main + 64
dc
dr x8
dr x8 = 1337
dc
EOF
EXPECT=<<EOF
x8 = 0x000000000000002a
Result = 1337
EOF
REGEXP_FILTER_ERR=((hit\sbreakpoint\sat:)|(f70\n))
EXPECT_ERR=<<EOF
hit breakpoint at:
f70
EOF
RUN

NAME=break multiple times and check regs (fat)
FILE=bins/mach0/calculate-macos-fat-arm64-x86_64
ARGS=-d
CMDS=<<EOF
db @ main
db @ main + 20
db @ main + 24
dc
dr pc
echo --
dc
dr pc
dr x8
echo --
dc
dr pc
dr x8
EOF
REGEXP_FILTER_OUT=((x8\s=\s0x.*)|(pc\s=)|(f30)|(f44)|(f48))
EXPECT=<<EOF
pc =
f30
pc =
f44
x8 = 0x0000000000000000
pc =
f48
x8 = 0x0000000000000064
EOF
REGEXP_FILTER_ERR=(((hit\sbreakpoint\sat:)|[0-9a-f][0-9a-f][0-9a-f]\n))
EXPECT_ERR=<<EOF
hit breakpoint at:
f30

hit breakpoint at:
f44

hit breakpoint at:
f48

EOF
RUN
