hoglet67 / PiTubeDirect

Bare-metal Raspberry Pi project that attaches to the Acorn TUBE interface and emulates many BBC Micro Co Processors
GNU General Public License v3.0
187 stars 23 forks source link

Debugger: Implement a search command #190

Closed hoglet67 closed 11 months ago

hoglet67 commented 1 year ago

Ref: private email thread with Ed titled "feature idea for pitubedirect debugger"

hoglet67 commented 12 months ago

Implemented in f433b51a

It supports a mix of strings (with escape codes) and values (parsed according to the the current width and base). Each value can be followed by a optional mask indicating which bits are significant. There is also a reasonable amount of error checking. Internally the pattern is stored as an array of 8-bit value bytes and an array of 8-bit mask bytes. The search loop is very simple at the moment:

   // Brute force search
   unsigned int len = pattern.len;
   for (unsigned int addr = start; addr <= end - len; addr++) {
      unsigned int i = 0;
      while (i < len && (((uint8_t) cpu->memread(addr + i)) & pattern.masks[i]) == pattern.values[i]) {
         i++;
      }
      if (i == len) {
         printf("Match at: %s\r\n", format_addr(addr));
      }
   }

One question how the matching should be done when the user has specified a width of 32? i.e should be search loop be stepping a word at a time, rather than a byte at at time?

Anyway, here's a sample session:

>> search
Missing parameter(s)

>> help search
  search <start> <end> <pattern>

>> mem fc0a00 fc0b00
fc0a00  13 01 01 ff 23 26 11 00 ef 00 d0 0e 63 02 06 02  ....#&......c...
fc0a10  37 06 fc 00 13 06 46 ea 23 20 b6 00 e7 80 05 00  7.....F.# ......
fc0a20  13 05 00 00 83 20 c1 00 13 01 01 01 67 80 00 00  ..... ......g...
fc0a30  b7 08 ac 00 93 88 f8 00 73 00 00 00 fc 00 00 00  ........s.......
fc0a40  42 61 64 20 61 64 64 72 65 73 73 00 13 01 01 ff  Bad address.....
fc0a50  23 26 11 00 17 05 00 00 13 05 c5 01 ef 00 10 19  #&..............
fc0a60  13 05 10 00 83 20 c1 00 13 01 01 01 67 80 00 00  ..... ......g...
fc0a70  52 49 53 43 2d 56 20 30 2e 39 30 0a 0d 00 00 00  RISC-V 0.90.....
fc0a80  13 01 01 ff 23 26 11 00 ef 00 d0 06 e3 02 06 fa  ....#&..........
fc0a90  13 85 05 00 ef 00 c0 70 13 05 00 02 b7 08 ac 00  .......p........
fc0aa0  93 88 48 00 73 00 00 00 13 85 05 00 ef 00 c0 76  ..H.s..........v
fc0ab0  b7 08 ac 00 93 88 58 00 73 00 00 00 13 05 00 00  ......X.s.......
fc0ac0  83 20 c1 00 13 01 01 01 67 80 00 00 13 01 01 ff  . ......g.......
fc0ad0  23 26 11 00 23 24 b1 00 13 01 01 ff ef 00 d0 07  #&..#$..........
fc0ae0  63 08 06 08 23 24 b1 00 13 05 20 00 93 05 01 00  c...#$.... .....
fc0af0  23 a0 05 00 23 a2 05 00 b7 08 ac 00 93 88 38 00  #...#.........8.

>> search fc0a00 fc0b00 "addr"
Pattern bytes (in hex): 61 64 64 72
Match at: fc0a44

>> search fc0a00 fc0b00 "RISC\*V"
Pattern bytes (in hex): 52 49 53 43 ** 56
Match at: fc0a70

>> search fc0a00 fc0b00 93 88 48 00
Pattern bytes (in hex): 93 88 48 00
Match at: fc0aa0

>> search fc0a00 fc0b00 "0.90" 0a 0d
Pattern bytes (in hex): 30 2e 39 30 0a 0d
Match at: fc0a77

>> search fc0a00 fc0b00 "0.90" 0d 0a
Pattern bytes (in hex): 30 2e 39 30 0d 0a

>> width 32
Setting data width to 32 bits (4 bytes)

>> mem fc0a00 fc0b00
fc0a00  ff010113 00112623 0ed000ef 02060263  ....#&......c...
fc0a10  00fc0637 ea460613 00b62023 000580e7  7.....F.# ......
fc0a20  00000513 00c12083 01010113 00008067  ..... ......g...
fc0a30  00ac08b7 00f88893 00000073 000000fc  ........s.......
fc0a40  20646142 72646461 00737365 ff010113  Bad address.....
fc0a50  00112623 00000517 01c50513 191000ef  #&..............
fc0a60  00100513 00c12083 01010113 00008067  ..... ......g...
fc0a70  43534952 3020562d 0a30392e 0000000d  RISC-V 0.90.....
fc0a80  ff010113 00112623 06d000ef fa0602e3  ....#&..........
fc0a90  00058513 70c000ef 02000513 00ac08b7  .......p........
fc0aa0  00488893 00000073 00058513 76c000ef  ..H.s..........v
fc0ab0  00ac08b7 00588893 00000073 00000513  ......X.s.......
fc0ac0  00c12083 01010113 00008067 ff010113  . ......g.......
fc0ad0  00112623 00b12423 ff010113 07d000ef  #&..#$..........
fc0ae0  08060863 00b12423 00200513 00010593  c...#$.... .....
fc0af0  0005a023 0005a223 00ac08b7 00388893  #...#.........8.

>> search fc0a00 fc0b00 00000073
Pattern bytes (in hex): 73 00 00 00
Match at: fc0a38
Match at: fc0aa4
Match at: fc0ab8

>> search fc0a00 fc0b00 ff010113/ff00ffff
Pattern bytes (in hex): 13 01 ** ff
Match at: fc0a00
Match at: fc0a4c
Match at: fc0a80
Match at: fc0acc
Match at: fc0ad8

>> search fc0a00 fc0b00 ff010113/f00ffff
Pattern bytes (in hex): 13 01 ** *f
Match at: fc0a00
Match at: fc0a4c
Match at: fc0a80
Match at: fc0acc
Match at: fc0ad8

>> search fc0a00 fc0b00 ff010113/f80ffff
Pattern bytes (in hex): 13 01 00/80 *f
Match at: fc0a00
Match at: fc0a4c
Match at: fc0a80
Match at: fc0acc
Match at: fc0ad8

>> search fc0a00 fc0b00 *
Pattern contains only wildcards

>> search fc0a00 fc0b00 * * *
Pattern contains only wildcards

>> search fc0a00 fc0b00 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Pattern too long, max length is 32 bytes

>> search fc0a00 fc0b00
Missing pattern

>> width 8
Setting data width to  8 bits (1 byte)

>> search fc0a00 fc0b00 12345678
Pattern syntax error: Value too big

>> search fc0a00 fc0b00 12 34 56 78
Pattern bytes (in hex): 12 34 56 78

>> search fc0a00 fc0b00 "hello
Pattern syntax error: Missing closing quote

>> search fc0a00 fc0b00 "hello\
Pattern syntax error: Missing closing quote

>> search fc0a00 fc0b00 "hello\x
Pattern syntax error: Bad escape sequence

>> search fc0a00 fc0b00 xxxx
Pattern syntax error: Malformed value

>> search fc0a00 fc0b00 00/xxxx
Pattern syntax error: Malformed mask
hoglet67 commented 11 months ago

Fixed in indigo alpha3