about summary refs log tree commit diff stats
path: root/subx/056trace.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-06-08 12:55:44 -0700
committerKartik Agaram <vc@akkartik.com>2019-06-08 12:57:32 -0700
commitc437318df551a58319e1d596976cd593070f0d72 (patch)
tree7d7d4fce5ff7c0ab39e6e239cae1adeeaa12ba57 /subx/056trace.subx
parent35dd69f7ca0e2cec153822cc0856dd157e49fdad (diff)
downloadmu-c437318df551a58319e1d596976cd593070f0d72.tar.gz
snapshot of trace primitives
Diffstat (limited to 'subx/056trace.subx')
-rw-r--r--subx/056trace.subx346
1 files changed, 346 insertions, 0 deletions
diff --git a/subx/056trace.subx b/subx/056trace.subx
index 2f2fd0ea..af52da2a 100644
--- a/subx/056trace.subx
+++ b/subx/056trace.subx
@@ -45,6 +45,13 @@ _test-trace-stream:
 # . op          subop               mod             rm32          base        index         scale       r32
 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
 
+#? Entry:  # run a single test, while debugging
+#?     e8/call test-trace-single/disp32
+#?     # syscall(exit, Num-test-failures)
+#?     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
+#?     b8/copy-to-EAX  1/imm32/exit
+#?     cd/syscall  0x80/imm8
+
 # Allocate a new segment for the trace stream, initialize its length, and save its address to Trace-stream.
 # The Trace-stream segment will consist of variable-length lines separated by newlines (0x0a)
 initialize-trace-stream:  # n : int -> <void>
@@ -238,6 +245,345 @@ test-trace-empty-line:
     # end
     c3/return
 
+check-trace-contains:  # line : (address string), msg : (address string)
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # rewind-stream(*Trace-stream)
+    # . . push args
+    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Segment-size
+    # . . call
+    e8/call  rewind-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # check-trace-can-scan-to(line, msg)
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
+    # . . call
+    e8/call  check-trace-can-scan-to/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+$check-trace-contains:end:
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
+check-trace-scans-to:  # line : (address string), msg : (address string)
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # . save registers
+    50/push-EAX
+    # EAX = trace-scan(*Trace-stream, line)
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
+    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Segment-size
+    # . . call
+    e8/call  check-trace-can-scan-to/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # check-ints-equal(EAX, 1, msg)
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
+    68/push  1/imm32
+    50/push-EAX
+    # . . call
+    e8/call check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+$check-trace-scans-to:end:
+    # . restore registers
+    58/pop-to-EAX
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
+# Start scanning from Trace-stream->read for 'line'. If found, update Trace-stream->read and return true.
+trace-scan:  # line : string -> result/EAX : boolean
+    # pseudocode:
+    #   while true:
+    #     if Trace-stream->read >= Trace-stream->write
+    #       break
+    #     if next-line-matches?(Trace-stream, line)
+    #       skip-next-line(Trace-stream)
+    #       return true
+    #   return false
+    #
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # . save registers
+    51/push-ECX
+    56/push-ESI
+    # ESI = *Trace-stream
+    8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           6/r32/ESI   Trace-stream/disp32               # copy *Trace-stream to ESI
+    # ECX = Trace-stream->write
+$trace-scan:loop:
+    # if (Trace-stream->read >= Trace-stream->write) return false
+    39/compare                      1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   4/disp8         .                 # compare ECX with *(ESI+4)
+    7d/jump-if-greater-or-equal  $trace-scan:false/disp8
+    # EAX = next-line-matches?(Trace-stream, line)
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
+    56/push-ESI
+    # . . call
+    e8/call  next-line-matches?/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # if (EAX == 0) continue
+    3d/compare-EAX-and  0/imm32
+    74/jump-if-equal  $trace-scan:loop/disp8
+$trace-scan:true:
+    # skip-next-line(Trace-stream)
+    # . . push args
+    56/push-ESI
+    # . . call
+    e8/call  skip-next-line/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # return true
+    b8/copy-to-EAX  1/imm32/true
+    eb/jump  $trace-scan:end/disp8
+$trace-scan:false:
+    b8/copy-to-EAX  0/imm32/false
+$trace-scan:end:
+    # . restore registers
+    59/pop-to-ECX
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
+next-line-matches?:  # t : (address stream), line : string -> result/EAX : boolean
+    # pseudocode:
+    #   while true:
+    #     if (currl >= maxl) break
+    #     if (currt >= maxt) return false
+    #     if (*currt != *currl) return false
+    #     ++currt
+    #     ++currl
+    #   return *currt == '\n'
+    #
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # . save registers
+    # ESI = line
+    # maxl/ECX = line->data + line->size
+    # currl/ESI = line->data
+    # EDI = t
+    # maxt/EDX = t->data + t->write
+    # currt/EDI = t->data + t->read
+$next-line-matches?:loop:
+    # if (currl >= maxl) break
+    # if (currt >= maxt) return false
+    # if (*currt != *currl) return false
+    # ++currt
+    # ++currl
+$next-line-matches?:break:
+    # return *currt == '\n'
+$next-line-matches?:end:
+    # . restore registers
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
+skip-next-line:  # t : (address stream)
+    # pseudocode:
+    #   i = t->read
+    #   curr = t->data + t->read
+    #   max = t->data + t->write
+    #   while true
+    #     if (curr >= max) break
+    #     if (*curr == '\n') break
+    #     ++curr
+    #     ++i
+    #   t->read = i
+    #
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # . save registers
+    # i = t->read
+    # curr = t->data + t->read
+    # max = t->data + t->write
+$skip-next-line:loop:
+    # if (curr >= max) break
+    # if (*curr == '\n') break
+    # ++curr
+    # ++i
+$skip-next-line:end:
+    # t->read = i
+    # . restore registers
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
+test-trace-scan-first:
+    # setup
+    # . *Trace-stream = _test-trace-stream
+    b8/copy-to-EAX  _test-trace-stream/imm32
+    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Trace-stream/disp32               # copy EAX to *Trace-stream
+    # . clear-trace-stream()
+    e8/call  clear-trace-stream/disp32
+    # . trace("Ab")
+    # . . push args
+    68/push  "Ab"/imm32
+    # . . call
+    e8/call  trace/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # EAX = trace-scan("Ab")
+    # . . push args
+    68/push  "Ab"/imm32
+    # . . call
+    e8/call  trace-scan/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # check-ints-equal(EAX, 1, msg)
+    # . . push args
+    68/push  "F - test-trace-scan-first"/imm32
+    68/push  1/imm32
+    50/push-EAX
+    # . . call
+    e8/call check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . end
+    c3/return
+
+test-trace-scan-skips-lines-until-found:
+    # setup
+    # . *Trace-stream = _test-trace-stream
+    b8/copy-to-EAX  _test-trace-stream/imm32
+    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Trace-stream/disp32               # copy EAX to *Trace-stream
+    # . clear-trace-stream()
+    e8/call  clear-trace-stream/disp32
+    # . trace("Ab")
+    # . . push args
+    68/push  "Ab"/imm32
+    # . . call
+    e8/call  trace/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . trace("cd")
+    # . . push args
+    68/push  "cd"/imm32
+    # . . call
+    e8/call  trace/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # EAX = trace-scan("cd")
+    # . . push args
+    68/push  "cd"/imm32
+    # . . call
+    e8/call  trace-scan/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # check-ints-equal(EAX, 1, msg)
+    # . . push args
+    68/push  "F - test-trace-scan-skips-lines-until-found"/imm32
+    68/push  1/imm32
+    50/push-EAX
+    # . . call
+    e8/call check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . end
+    c3/return
+
+test-trace-second-scan-starts-where-first-left-off:
+    # setup
+    # . *Trace-stream = _test-trace-stream
+    b8/copy-to-EAX  _test-trace-stream/imm32
+    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Trace-stream/disp32               # copy EAX to *Trace-stream
+    # . clear-trace-stream()
+    e8/call  clear-trace-stream/disp32
+    # . trace("Ab")
+    # . . push args
+    68/push  "Ab"/imm32
+    # . . call
+    e8/call  trace/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . EAX = trace-scan("Ab")
+    # . . push args
+    68/push  "Ab"/imm32
+    # . . call
+    e8/call  trace-scan/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # second scan fails
+    # . EAX = trace-scan("Ab")
+    # . . push args
+    68/push  "Ab"/imm32
+    # . . call
+    e8/call  trace-scan/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # check-ints-equal(EAX, 0, msg)
+    # . . push args
+    68/push  "F - test-trace-second-scan-starts-where-first-left-off"/imm32
+    68/push  0/imm32
+    50/push-EAX
+    # . . call
+    e8/call check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . end
+    c3/return
+
+test-trace-scan-failure-leaves-read-index-untouched:
+    # setup
+    # . *Trace-stream = _test-trace-stream
+    b8/copy-to-EAX  _test-trace-stream/imm32
+    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Trace-stream/disp32               # copy EAX to *Trace-stream
+    # . clear-trace-stream()
+    e8/call  clear-trace-stream/disp32
+    # . trace("Ab")
+    # . . push args
+    68/push  "Ab"/imm32
+    # . . call
+    e8/call  trace/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . check-ints-equal(_test-trace-stream->read, 0, msg)
+    # . . push args
+    68/push  "F - test-trace-second-scan-starts-where-first-left-off/precondition-failure"/imm32
+    68/push  0/imm32
+    b8/copy-to-EAX  _test-trace-stream/imm32
+    ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         .                 # push *(EAX+4)
+    # . . call
+    e8/call check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # perform a failing scan
+    # . EAX = trace-scan("Ax")
+    # . . push args
+    68/push  "Ax"/imm32
+    # . . call
+    e8/call  trace-scan/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # no change in read index
+    # . check-ints-equal(_test-trace-stream->read, 0, msg)
+    # . . push args
+    68/push  "F - test-trace-second-scan-starts-where-first-left-off"/imm32
+    68/push  0/imm32
+    b8/copy-to-EAX  _test-trace-stream/imm32
+    ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         .                 # push *(EAX+4)
+    # . . call
+    e8/call check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . end
+    c3/return
+
 clear-trace-stream:
     # . prolog
     55/push-EBP