diff --git a/.gitignore b/.gitignore index e539b476d3..99aebfcf37 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,9 @@ CMakeFiles/ **/capstone-config-version.cmake **/capstone-config.cmake -**/simeng-fileio-test.txt \ No newline at end of file +**/simeng-fileio-test.txt +build.sh +test.c +*.elf +sme_matmul_fp32 +stream-gcc10.3.0-armv8.4-a+sve diff --git a/CMakeLists.txt b/CMakeLists.txt index 542049f2f8..f1d043fdd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,7 @@ option(SIMENG_SANITIZE "Enable compiler sanitizers" OFF) option(SIMENG_OPTIMIZE "Enable Extra Compiler Optimizations" OFF) option(SIMENG_ENABLE_SST "Compile SimEng SST Wrapper" OFF) option(SIMENG_ENABLE_SST_TESTS "Enable testing for SST" OFF) +option(SIMENG_ENABLE_GDB "Enable GDB for SimEng build" OFF) # Set CXX flag for Apple Mac so that `binary_function` and `unary_function` types that are used in SST can be recognised. # They were deprecated in C++11 and removed in C++17, and Apple Clang v15 no longer supports these types without the following flag diff --git a/LICENSE_SPECS.txt b/LICENSE_SPECS.txt new file mode 100644 index 0000000000..a7ef6bb527 --- /dev/null +++ b/LICENSE_SPECS.txt @@ -0,0 +1,6 @@ +Copyright (C) 2009-2024 Free Software Foundation, Inc. +Contributed by ARM Ltd. + +Copying and distribution of this file, with or without modification, +are permitted in any medium without royalty provided the copyright +notice and this notice are preserved. \ No newline at end of file diff --git a/LICENSE_TINYXML2.txt b/LICENSE_TINYXML2.txt new file mode 100644 index 0000000000..e10eb82e7e --- /dev/null +++ b/LICENSE_TINYXML2.txt @@ -0,0 +1,18 @@ +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. \ No newline at end of file diff --git a/configs/a64fx_SME_gdb.yaml b/configs/a64fx_SME_gdb.yaml new file mode 100644 index 0000000000..1f6e8b9fc1 --- /dev/null +++ b/configs/a64fx_SME_gdb.yaml @@ -0,0 +1,409 @@ +# The following resources where utilised to create the config file and naming schemes: +# https://github.com/fujitsu/A64FX + +Core: + Simulation-Mode: emulation + ISA: AArch64 + Clock-Frequency-GHz: 1.8 + Timer-Frequency-MHz: 100 + Micro-Operations: True + Vector-Length: 512 + Streaming-Vector-Length: 512 +Fetch: + Fetch-Block-Size: 32 + Loop-Buffer-Size: 48 + Loop-Detection-Threshold: 4 +Process-Image: + Heap-Size: 1073741824 + Stack-Size: 1048576 +Register-Set: + GeneralPurpose-Count: 96 + FloatingPoint/SVE-Count: 128 + Predicate-Count: 48 + Conditional-Count: 128 + Matrix-Count: 2 +Pipeline-Widths: + Commit: 4 + FrontEnd: 4 + LSQ-Completion: 2 +Queue-Sizes: + ROB: 128 + Load: 40 + Store: 24 +Branch-Predictor: + Type: "Perceptron" + BTB-Tag-Bits: 11 + Global-History-Length: 19 + RAS-entries: 8 +L1-Data-Memory: + Interface-Type: Flat +L1-Instruction-Memory: + Interface-Type: Flat +LSQ-L1-Interface: + Access-Latency: 5 + Exclusive: True + Load-Bandwidth: 128 + Store-Bandwidth: 64 + Permitted-Requests-Per-Cycle: 2 + Permitted-Loads-Per-Cycle: 2 + Permitted-Stores-Per-Cycle: 1 +Ports: + 0: + Portname: FLA + Instruction-Group-Support: + - FP + - SVE + 1: + Portname: PR + Instruction-Group-Support: + - PREDICATE + 2: + Portname: EXA + Instruction-Group-Support: + - INT_SIMPLE + - INT_MUL + - STORE_DATA + 3: + Portname: FLB + Instruction-Group-Support: + - FP_SIMPLE + - FP_MUL + - SVE_SIMPLE + - SVE_MUL + 4: + Portname: EXB + Instruction-Group-Support: + - INT_SIMPLE + - INT_DIV_OR_SQRT + 5: + Portname: EAGA + Instruction-Support: + - LOAD + - STORE_ADDRESS + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CMP + 6: + Portname: EAGB + Instruction-Support: + - LOAD + - STORE_ADDRESS + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CMP + 7: + Portname: BR + Instruction-Group-Support: + - BRANCH + 8: + Portname: SME + Instruction-Group-Support: + - SME +Reservation-Stations: + 0: + Size: 20 + Dispatch-Rate: 2 + Ports: + - FLA + - PR + - EXA + 1: + Size: 20 + Dispatch-Rate: 2 + Ports: + - FLB + - EXB + 2: + Size: 10 + Dispatch-Rate: 1 + Ports: + - EAGA + 3: + Size: 10 + Dispatch-Rate: 1 + Ports: + - EAGB + 4: + Size: 19 + Dispatch-Rate: 1 + Ports: + - BR + 5: + Size: 20 + Dispatch-Rate: 1 + Ports: + - SME +Execution-Units: + 0: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 1: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 2: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 3: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 4: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 5: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 6: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 7: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 8: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT +Latencies: + 0: + Instruction-Groups: + - INT + Execution-Latency: 2 + Execution-Throughput: 2 + 1: + Instruction-Groups: + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CVT + Execution-Latency: 1 + Execution-Throughput: 1 + 2: + Instruction-Groups: + - INT_MUL + Execution-Latency: 5 + Execution-Throughput: 1 + 3: + Instruction-Groups: + - INT_DIV_OR_SQRT + Execution-Latency: 41 + Execution-Throughput: 41 + 4: + Instruction-Groups: + - SCALAR_SIMPLE + - VECTOR_SIMPLE_LOGICAL + - SVE_SIMPLE_LOGICAL + - SME_SIMPLE_LOGICAL + - VECTOR_SIMPLE_CMP + - SVE_SIMPLE_CMP + - SME_SIMPLE_CMP + Execution-Latency: 4 + Execution-Throughput: 1 + 5: + Instruction-Groups: + - FP_DIV_OR_SQRT + Execution-Latency: 29 + Execution-Throughput: 29 + 6: + Instruction-Groups: + - SCALAR_SIMPLE_CVT + - VECTOR_SIMPLE + - SVE_SIMPLE + - SME_SIMPLE + - FP_MUL + - SVE_MUL + - SME_MUL + Execution-Latency: 9 + Execution-Throughput: 1 + 7: + Instruction-Groups: + - SVE_DIV_OR_SQRT + - SME_DIV_OR_SQRT + Execution-Latency: 98 + Execution-Throughput: 98 + 8: + Instruction-Groups: + - PREDICATE + Execution-Latency: 3 + Execution-Throughput: 1 + 9: + Instruction-Groups: + - LOAD_SCALAR + - LOAD_VECTOR + - STORE_ADDRESS_SCALAR + - STORE_ADDRESS_VECTOR + Execution-Latency: 3 + Execution-Throughput: 1 + 10: + Instruction-Groups: + - LOAD_SVE + - LOAD_SME + - STORE_ADDRESS_SVE + - STORE_ADDRESS_SME + Execution-Latency: 6 + Execution-Throughput: 1 +# Indexed FMLA instructions split into 2 dependent µops. Latency increased to 15 to mimic such behaviour +# NOTE: Any changes to the capstone opcode list could invalidate the mapping between ARM instructions and the values below + 11: + Instruction-Opcodes: + - 1922 + - 1924 + - 1926 + - 2359 + - 2360 + - 2361 + - 2364 + - 2365 + - 2368 + - 2369 + - 2371 + - 2390 + - 2391 + - 2392 + - 2395 + - 2396 + - 2399 + - 2400 + - 2402 + - 2445 + - 2446 + - 2447 + - 2450 + - 2451 + - 2454 + - 2455 + - 2457 + - 2470 + - 2471 + - 2472 + - 2475 + - 2476 + - 2479 + - 2480 + - 2482 + - 3627 + - 3629 + - 3631 + - 3633 + - 3644 + - 3646 + - 3648 + - 3650 + - 3709 + - 3711 + - 3713 + - 3715 + - 4306 + - 4308 + - 4310 + - 4312 + - 4326 + - 4328 + - 4330 + - 4332 + - 4372 + - 4374 + - 4376 + - 4378 + - 4468 + - 4469 + - 4470 + - 4472 + - 4474 + - 4476 + - 4493 + - 4494 + - 4495 + - 4497 + - 4499 + - 4501 + - 4511 + - 4513 + - 4515 + - 4517 + - 4519 + - 4521 + - 4534 + - 4535 + - 4536 + - 4538 + - 4540 + - 4542 + - 4594 + - 4595 + - 4599 + - 4601 + - 4603 + - 4605 + - 4613 + - 4614 + - 4618 + - 4620 + - 4622 + - 4624 + - 4633 + - 4635 + - 4637 + - 4639 + - 4641 + - 4643 + - 5760 + - 5762 + - 5764 + - 5766 + - 5780 + - 5782 + - 5784 + - 5786 + - 5824 + - 5826 + - 5828 + - 5830 + Execution-Latency: 15 + Execution-Throughput: 1 +# CPU-Info mainly used to generate a replica of the special (or system) file directory +# structure +CPU-Info: + # Set Generate-Special-Dir to True to generate the special files directory, or to False to not. + # (Not generating the special files directory may require the user to copy over files manually) + Generate-Special-Dir: True + # Core-Count MUST be 1 as multi-core is not supported at this time. (A64FX true value is 48) + Core-Count: 1 + # Socket-Count MUST be 1 as multi-socket simulations are not supported at this time. (A64FX true value is 1) + Socket-Count: 1 + # SMT MUST be 1 as Simultanious-Multi-Threading is not supported at this time. (A64FX true value is 1) + SMT: 1 + # Below are the values needed to generate /proc/cpuinfo + BogoMIPS: 200.00 + Features: fp asimd evtstrm sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm fcma dcpop sve + CPU-Implementer: "0x46" + CPU-Architecture: 8 + CPU-Variant: "0x1" + CPU-Part: "0x001" + CPU-Revision: 0 + # Package-Count is used to generate + # /sys/devices/system/cpu/cpu{0..Core-Count}/topology/{physical_package_id, core_id} + Package-Count: 1 diff --git a/configs/a64fx_SME_gdb_ooo.yaml b/configs/a64fx_SME_gdb_ooo.yaml new file mode 100644 index 0000000000..8c9fdb7692 --- /dev/null +++ b/configs/a64fx_SME_gdb_ooo.yaml @@ -0,0 +1,409 @@ +# The following resources where utilised to create the config file and naming schemes: +# https://github.com/fujitsu/A64FX + +Core: + Simulation-Mode: outoforder + ISA: AArch64 + Clock-Frequency-GHz: 1.8 + Timer-Frequency-MHz: 100 + Micro-Operations: True + Vector-Length: 512 + Streaming-Vector-Length: 512 +Fetch: + Fetch-Block-Size: 32 + Loop-Buffer-Size: 48 + Loop-Detection-Threshold: 4 +Process-Image: + Heap-Size: 1073741824 + Stack-Size: 1048576 +Register-Set: + GeneralPurpose-Count: 96 + FloatingPoint/SVE-Count: 128 + Predicate-Count: 48 + Conditional-Count: 128 + Matrix-Count: 2 +Pipeline-Widths: + Commit: 4 + FrontEnd: 4 + LSQ-Completion: 2 +Queue-Sizes: + ROB: 128 + Load: 40 + Store: 24 +Branch-Predictor: + Type: "Perceptron" + BTB-Tag-Bits: 11 + Global-History-Length: 19 + RAS-entries: 8 +L1-Data-Memory: + Interface-Type: Flat +L1-Instruction-Memory: + Interface-Type: Flat +LSQ-L1-Interface: + Access-Latency: 5 + Exclusive: True + Load-Bandwidth: 128 + Store-Bandwidth: 64 + Permitted-Requests-Per-Cycle: 2 + Permitted-Loads-Per-Cycle: 2 + Permitted-Stores-Per-Cycle: 1 +Ports: + 0: + Portname: FLA + Instruction-Group-Support: + - FP + - SVE + 1: + Portname: PR + Instruction-Group-Support: + - PREDICATE + 2: + Portname: EXA + Instruction-Group-Support: + - INT_SIMPLE + - INT_MUL + - STORE_DATA + 3: + Portname: FLB + Instruction-Group-Support: + - FP_SIMPLE + - FP_MUL + - SVE_SIMPLE + - SVE_MUL + 4: + Portname: EXB + Instruction-Group-Support: + - INT_SIMPLE + - INT_DIV_OR_SQRT + 5: + Portname: EAGA + Instruction-Support: + - LOAD + - STORE_ADDRESS + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CMP + 6: + Portname: EAGB + Instruction-Support: + - LOAD + - STORE_ADDRESS + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CMP + 7: + Portname: BR + Instruction-Group-Support: + - BRANCH + 8: + Portname: SME + Instruction-Group-Support: + - SME +Reservation-Stations: + 0: + Size: 20 + Dispatch-Rate: 2 + Ports: + - FLA + - PR + - EXA + 1: + Size: 20 + Dispatch-Rate: 2 + Ports: + - FLB + - EXB + 2: + Size: 10 + Dispatch-Rate: 1 + Ports: + - EAGA + 3: + Size: 10 + Dispatch-Rate: 1 + Ports: + - EAGB + 4: + Size: 19 + Dispatch-Rate: 1 + Ports: + - BR + 5: + Size: 20 + Dispatch-Rate: 1 + Ports: + - SME +Execution-Units: + 0: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 1: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 2: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 3: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 4: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 5: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 6: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 7: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 8: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT +Latencies: + 0: + Instruction-Groups: + - INT + Execution-Latency: 2 + Execution-Throughput: 2 + 1: + Instruction-Groups: + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CVT + Execution-Latency: 1 + Execution-Throughput: 1 + 2: + Instruction-Groups: + - INT_MUL + Execution-Latency: 5 + Execution-Throughput: 1 + 3: + Instruction-Groups: + - INT_DIV_OR_SQRT + Execution-Latency: 41 + Execution-Throughput: 41 + 4: + Instruction-Groups: + - SCALAR_SIMPLE + - VECTOR_SIMPLE_LOGICAL + - SVE_SIMPLE_LOGICAL + - SME_SIMPLE_LOGICAL + - VECTOR_SIMPLE_CMP + - SVE_SIMPLE_CMP + - SME_SIMPLE_CMP + Execution-Latency: 4 + Execution-Throughput: 1 + 5: + Instruction-Groups: + - FP_DIV_OR_SQRT + Execution-Latency: 29 + Execution-Throughput: 29 + 6: + Instruction-Groups: + - SCALAR_SIMPLE_CVT + - VECTOR_SIMPLE + - SVE_SIMPLE + - SME_SIMPLE + - FP_MUL + - SVE_MUL + - SME_MUL + Execution-Latency: 9 + Execution-Throughput: 1 + 7: + Instruction-Groups: + - SVE_DIV_OR_SQRT + - SME_DIV_OR_SQRT + Execution-Latency: 98 + Execution-Throughput: 98 + 8: + Instruction-Groups: + - PREDICATE + Execution-Latency: 3 + Execution-Throughput: 1 + 9: + Instruction-Groups: + - LOAD_SCALAR + - LOAD_VECTOR + - STORE_ADDRESS_SCALAR + - STORE_ADDRESS_VECTOR + Execution-Latency: 3 + Execution-Throughput: 1 + 10: + Instruction-Groups: + - LOAD_SVE + - LOAD_SME + - STORE_ADDRESS_SVE + - STORE_ADDRESS_SME + Execution-Latency: 6 + Execution-Throughput: 1 +# Indexed FMLA instructions split into 2 dependent µops. Latency increased to 15 to mimic such behaviour +# NOTE: Any changes to the capstone opcode list could invalidate the mapping between ARM instructions and the values below + 11: + Instruction-Opcodes: + - 1922 + - 1924 + - 1926 + - 2359 + - 2360 + - 2361 + - 2364 + - 2365 + - 2368 + - 2369 + - 2371 + - 2390 + - 2391 + - 2392 + - 2395 + - 2396 + - 2399 + - 2400 + - 2402 + - 2445 + - 2446 + - 2447 + - 2450 + - 2451 + - 2454 + - 2455 + - 2457 + - 2470 + - 2471 + - 2472 + - 2475 + - 2476 + - 2479 + - 2480 + - 2482 + - 3627 + - 3629 + - 3631 + - 3633 + - 3644 + - 3646 + - 3648 + - 3650 + - 3709 + - 3711 + - 3713 + - 3715 + - 4306 + - 4308 + - 4310 + - 4312 + - 4326 + - 4328 + - 4330 + - 4332 + - 4372 + - 4374 + - 4376 + - 4378 + - 4468 + - 4469 + - 4470 + - 4472 + - 4474 + - 4476 + - 4493 + - 4494 + - 4495 + - 4497 + - 4499 + - 4501 + - 4511 + - 4513 + - 4515 + - 4517 + - 4519 + - 4521 + - 4534 + - 4535 + - 4536 + - 4538 + - 4540 + - 4542 + - 4594 + - 4595 + - 4599 + - 4601 + - 4603 + - 4605 + - 4613 + - 4614 + - 4618 + - 4620 + - 4622 + - 4624 + - 4633 + - 4635 + - 4637 + - 4639 + - 4641 + - 4643 + - 5760 + - 5762 + - 5764 + - 5766 + - 5780 + - 5782 + - 5784 + - 5786 + - 5824 + - 5826 + - 5828 + - 5830 + Execution-Latency: 15 + Execution-Throughput: 1 +# CPU-Info mainly used to generate a replica of the special (or system) file directory +# structure +CPU-Info: + # Set Generate-Special-Dir to True to generate the special files directory, or to False to not. + # (Not generating the special files directory may require the user to copy over files manually) + Generate-Special-Dir: True + # Core-Count MUST be 1 as multi-core is not supported at this time. (A64FX true value is 48) + Core-Count: 1 + # Socket-Count MUST be 1 as multi-socket simulations are not supported at this time. (A64FX true value is 1) + Socket-Count: 1 + # SMT MUST be 1 as Simultanious-Multi-Threading is not supported at this time. (A64FX true value is 1) + SMT: 1 + # Below are the values needed to generate /proc/cpuinfo + BogoMIPS: 200.00 + Features: fp asimd evtstrm sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm fcma dcpop sve + CPU-Implementer: "0x46" + CPU-Architecture: 8 + CPU-Variant: "0x1" + CPU-Part: "0x001" + CPU-Revision: 0 + # Package-Count is used to generate + # /sys/devices/system/cpu/cpu{0..Core-Count}/topology/{physical_package_id, core_id} + Package-Count: 1 diff --git a/configs/a64fx_gdb.yaml b/configs/a64fx_gdb.yaml new file mode 100644 index 0000000000..f780ae63e4 --- /dev/null +++ b/configs/a64fx_gdb.yaml @@ -0,0 +1,385 @@ +# The following resources where utilised to create the config file and naming schemes: +# https://github.com/fujitsu/A64FX + +Core: + ISA: AArch64 + Simulation-Mode: emulation + Clock-Frequency-GHz: 1.8 + Timer-Frequency-MHz: 100 + Micro-Operations: True + Vector-Length: 512 +Fetch: + Fetch-Block-Size: 32 + Loop-Buffer-Size: 48 + Loop-Detection-Threshold: 4 +Process-Image: + Heap-Size: 1073741824 + Stack-Size: 1048576 +Register-Set: + GeneralPurpose-Count: 96 + FloatingPoint/SVE-Count: 128 + Predicate-Count: 48 + Conditional-Count: 128 +Pipeline-Widths: + Commit: 4 + FrontEnd: 4 + LSQ-Completion: 2 +Queue-Sizes: + ROB: 128 + Load: 40 + Store: 24 +Branch-Predictor: + Type: "Perceptron" + BTB-Tag-Bits: 11 + Global-History-Length: 19 + RAS-entries: 8 +L1-Data-Memory: + Interface-Type: Flat +L1-Instruction-Memory: + Interface-Type: Flat +LSQ-L1-Interface: + Access-Latency: 5 + Exclusive: True + Load-Bandwidth: 128 + Store-Bandwidth: 64 + Permitted-Requests-Per-Cycle: 2 + Permitted-Loads-Per-Cycle: 2 + Permitted-Stores-Per-Cycle: 1 +Ports: + 0: + Portname: FLA + Instruction-Group-Support: + - FP + - SVE + 1: + Portname: PR + Instruction-Group-Support: + - PREDICATE + 2: + Portname: EXA + Instruction-Group-Support: + - INT_SIMPLE + - INT_MUL + - STORE_DATA + 3: + Portname: FLB + Instruction-Group-Support: + - FP_SIMPLE + - FP_MUL + - SVE_SIMPLE + - SVE_MUL + 4: + Portname: EXB + Instruction-Group-Support: + - INT_SIMPLE + - INT_DIV_OR_SQRT + 5: + Portname: EAGA + Instruction-Group-Support: + - LOAD + - STORE_ADDRESS + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CMP + 6: + Portname: EAGB + Instruction-Group-Support: + - LOAD + - STORE_ADDRESS + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CMP + 7: + Portname: BR + Instruction-Group-Support: + - BRANCH +Reservation-Stations: + 0: + Size: 20 + Dispatch-Rate: 2 + Ports: + - FLA + - PR + - EXA + 1: + Size: 20 + Dispatch-Rate: 2 + Ports: + - FLB + - EXB + 2: + Size: 10 + Dispatch-Rate: 1 + Ports: + - EAGA + 3: + Size: 10 + Dispatch-Rate: 1 + Ports: + - EAGB + 4: + Size: 19 + Dispatch-Rate: 1 + Ports: + - BR +Execution-Units: + 0: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 1: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 2: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 3: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 4: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 5: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 6: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT + 7: + Pipelined: True + Blocking-Groups: + - INT_DIV_OR_SQRT + - FP_DIV_OR_SQRT + - SVE_DIV_OR_SQRT +Latencies: + 0: + Instruction-Groups: + - INT + Execution-Latency: 2 + Execution-Throughput: 2 + 1: + Instruction-Groups: + - INT_SIMPLE_ARTH_NOSHIFT + - INT_SIMPLE_LOGICAL_NOSHIFT + - INT_SIMPLE_CVT + Execution-Latency: 1 + Execution-Throughput: 1 + 2: + Instruction-Groups: + - INT_MUL + Execution-Latency: 5 + Execution-Throughput: 1 + 3: + Instruction-Groups: + - INT_DIV_OR_SQRT + Execution-Latency: 41 + Execution-Throughput: 41 + 4: + Instruction-Groups: + - SCALAR_SIMPLE + - VECTOR_SIMPLE_LOGICAL + - SVE_SIMPLE_LOGICAL + - VECTOR_SIMPLE_CMP + - SVE_SIMPLE_CMP + Execution-Latency: 4 + Execution-Throughput: 1 + 5: + Instruction-Groups: + - FP_DIV_OR_SQRT + Execution-Latency: 29 + Execution-Throughput: 29 + 6: + Instruction-Groups: + - VECTOR_SIMPLE + - SVE_SIMPLE + - SCALAR_SIMPLE_CVT + - FP_MUL + - SVE_MUL + Execution-Latency: 9 + Execution-Throughput: 1 + 7: + Instruction-Groups: + - SVE_DIV_OR_SQRT + Execution-Latency: 98 + Execution-Throughput: 98 + 8: + Instruction-Groups: + - PREDICATE + Execution-Latency: 3 + Execution-Throughput: 1 + 9: + Instruction-Groups: + - LOAD_SCALAR + - LOAD_VECTOR + - STORE_ADDRESS_SCALAR + - STORE_ADDRESS_VECTOR + Execution-Latency: 3 + Execution-Throughput: 1 + 10: + Instruction-Groups: + - LOAD_SVE + - STORE_ADDRESS_SVE + Execution-Latency: 6 + Execution-Throughput: 1 +# Indexed FMLA instructions split into 2 dependent µops. Latency increased to 15 to mimic such behaviour +# NOTE: Any changes to the capstone opcode list could invalidate the mapping between ARM instructions and the values below + 11: + Instruction-Opcodes: + - 1922 + - 1924 + - 1926 + - 2359 + - 2360 + - 2361 + - 2364 + - 2365 + - 2368 + - 2369 + - 2371 + - 2390 + - 2391 + - 2392 + - 2395 + - 2396 + - 2399 + - 2400 + - 2402 + - 2445 + - 2446 + - 2447 + - 2450 + - 2451 + - 2454 + - 2455 + - 2457 + - 2470 + - 2471 + - 2472 + - 2475 + - 2476 + - 2479 + - 2480 + - 2482 + - 3627 + - 3629 + - 3631 + - 3633 + - 3644 + - 3646 + - 3648 + - 3650 + - 3709 + - 3711 + - 3713 + - 3715 + - 4306 + - 4308 + - 4310 + - 4312 + - 4326 + - 4328 + - 4330 + - 4332 + - 4372 + - 4374 + - 4376 + - 4378 + - 4468 + - 4469 + - 4470 + - 4472 + - 4474 + - 4476 + - 4493 + - 4494 + - 4495 + - 4497 + - 4499 + - 4501 + - 4511 + - 4513 + - 4515 + - 4517 + - 4519 + - 4521 + - 4534 + - 4535 + - 4536 + - 4538 + - 4540 + - 4542 + - 4594 + - 4595 + - 4599 + - 4601 + - 4603 + - 4605 + - 4613 + - 4614 + - 4618 + - 4620 + - 4622 + - 4624 + - 4633 + - 4635 + - 4637 + - 4639 + - 4641 + - 4643 + - 5760 + - 5762 + - 5764 + - 5766 + - 5780 + - 5782 + - 5784 + - 5786 + - 5824 + - 5826 + - 5828 + - 5830 + Execution-Latency: 15 + Execution-Throughput: 1 +# CPU-Info mainly used to generate a replica of the special (or system) file directory +# structure +CPU-Info: + # Set Generate-Special-Dir to True to generate the special files directory, or to False to not. + # (Not generating the special files directory may require the user to copy over files manually) + Generate-Special-Dir: True + # Core-Count MUST be 1 as multi-core is not supported at this time. (A64FX true value is 48) + Core-Count: 1 + # Socket-Count MUST be 1 as multi-socket simulations are not supported at this time. (A64FX true value is 1) + Socket-Count: 1 + # SMT MUST be 1 as Simultanious-Multi-Threading is not supported at this time. (A64FX true value is 1) + SMT: 1 + # Below are the values needed to generate /proc/cpuinfo + BogoMIPS: 200.00 + Features: fp asimd evtstrm sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm fcma dcpop sve + CPU-Implementer: "0x46" + CPU-Architecture: 8 + CPU-Variant: "0x1" + CPU-Part: "0x001" + CPU-Revision: 0 + # Package-Count is used to generate + # /sys/devices/system/cpu/cpu{0..Core-Count}/topology/{physical_package_id, core_id} + Package-Count: 1 \ No newline at end of file diff --git a/docs/sphinx/index.rst b/docs/sphinx/index.rst index ac7026f9a9..cab2319445 100644 --- a/docs/sphinx/index.rst +++ b/docs/sphinx/index.rst @@ -60,6 +60,8 @@ The main component provided by the simulator is a discrete processor core model, A future release of SimEng will support multi-core simulation by integrating with the SST. +A basic GDB stub is provided, for debugging Arm-based programs using GDB. + Talks and presentations ----------------------- @@ -94,6 +96,7 @@ While we have tried to minimise SimEng's dependencies to keep it as simple as po - `Rapid YAML `_ - Parsing YAML configuration files - `GoogleTest `_ - Framework for the test suites - `LLVM `_ - Generation of binaries for use in the regression test suite +- `TinyXML2 `_ - Generation of XML description files in the GDB stub Contributors @@ -127,6 +130,7 @@ Additional Contributors: - Tom Hepworth - Tom Lin - Will Robinson +- Jay Parker Funding ------- diff --git a/docs/sphinx/user/building_simeng.rst b/docs/sphinx/user/building_simeng.rst index 1a5cd41123..f1f0e1987f 100644 --- a/docs/sphinx/user/building_simeng.rst +++ b/docs/sphinx/user/building_simeng.rst @@ -43,7 +43,7 @@ With this configuration, the build files will be generated in a directory called .. Note:: LLVM versions greater than 14 or less than 8 are not supported. We'd recommend using LLVM 14.0.5 where possible as this has been verified by us to work correctly. - b. Two additional flags are available when building SimEng. Firstly is ``-DSIMENG_SANITIZE={ON, OFF}`` which adds a selection of sanitisation compilation flags (primarily used during the development of the framework). Secondly is ``-SIMENG_OPTIMIZE={ON, OFF}`` which attempts to optimise the framework's compilation for the host machine through a set of compiler flags and options. + b. Three additional flags are available when building SimEng. Firstly is ``-DSIMENG_SANITIZE={ON, OFF}`` which adds a selection of sanitisation compilation flags (primarily used during the development of the framework). Secondly is ``-DSIMENG_OPTIMIZE={ON, OFF}`` which attempts to optimise the framework's compilation for the host machine through a set of compiler flags and options. Thirdly is ``-DSIMENG_ENABLE_GDB={ON, OFF}`` which enables or disables the GDB stub and related code. We recommend using the `Ninja `_ build system for faster builds, especially if not using pre-built LLVM libraries. After installation, it can be enabled through the addition of the ``-GNinja`` flag in the above CMake build command. diff --git a/docs/sphinx/user/running_simeng.rst b/docs/sphinx/user/running_simeng.rst index 92a9682032..f8e1d1097d 100644 --- a/docs/sphinx/user/running_simeng.rst +++ b/docs/sphinx/user/running_simeng.rst @@ -11,6 +11,11 @@ If no arguments are passed to SimEng, default options are used. The default conf Whilst a configuration file can be specified without a program (will use default program), a specified program must be accompanied by a configuration file. +Using GDB +--------- + +If SimEng has been compiled with GDB support, additional arguments can be passed to the program in order to start the GDB stub and manipulate its functionality. Run SimEng with the flag ``--help`` to view these arguments; for most use cases it suffices to use the ``--use-gdb`` flag (or, optionally, its short form ``-g``) to set up the stub with default settings. + Simulation Output ----------------- diff --git a/src/include/simeng/Core.hh b/src/include/simeng/Core.hh index cba2df9b97..fd45287483 100644 --- a/src/include/simeng/Core.hh +++ b/src/include/simeng/Core.hh @@ -2,6 +2,7 @@ #include #include +#include #include #include "simeng/ArchitecturalRegisterFileSet.hh" @@ -11,6 +12,23 @@ namespace simeng { +struct BreakReason { + enum { + Break, + Write, + Read, + Access, + SyscallEntry, + SyscallReturn, + } reason; + uint64_t info; + uint64_t pc; + + bool isWatch() const { + return (reason == Write) || (reason == Read) || (reason == Access); + } +}; + namespace arch { // Forward declare Architecture and ExceptionHandler classes. class Architecture; @@ -53,7 +71,32 @@ class Core { return (ticks_ / clockFrequency_); } - protected: + /** Retrieve the program counter. */ + virtual const uint64_t getProgramCounter() const = 0; + + /** Set the program counter. */ + virtual void setProgramCounter(uint64_t pc) = 0; + + /** Retrieve the ISA instance. */ + const arch::Architecture& getISA() const { return isa_; } + + /** Prepare the necessary breakpoint state for the following run. */ + virtual void prepareBreakpoints( + const std::optional* step_from = nullptr, + const std::vector* bp = nullptr, + const std::vector* wp = nullptr, + const std::vector* rp = nullptr, + const std::vector* ap = nullptr, + const std::optional>* syscalls = nullptr) {} + + /** Retrieve the reason for a break, if any. */ + virtual const std::optional getBreakReason() const = 0; + + /** Retrieve the exit code. Result only valid after exit() syscall has been entered. */ + virtual uint64_t getExitCode() const { + return 0; + } + /** Apply changes to the process state. */ void applyStateChange(const arch::ProcessStateChange& change) const { auto& regFile = const_cast( @@ -95,6 +138,7 @@ class Core { } } + protected: /** A memory interface to access data. */ memory::MemoryInterface& dataMemory_; diff --git a/src/include/simeng/Instruction.hh b/src/include/simeng/Instruction.hh index 9a126460de..21e9c2d100 100644 --- a/src/include/simeng/Instruction.hh +++ b/src/include/simeng/Instruction.hh @@ -29,7 +29,7 @@ struct ExecutionInfo { * Each supported ISA should provide a derived implementation of this class. */ class Instruction { public: - virtual ~Instruction(){}; + virtual ~Instruction() {}; /** Retrieve the source registers this instruction reads. */ virtual const span getSourceRegisters() const = 0; @@ -116,6 +116,9 @@ class Instruction { * latency and throughput, and the set of ports which support it. */ virtual void setExecutionInfo(const ExecutionInfo& info) = 0; + /** Get whether this instruction is a syscall. */ + virtual bool isSyscall() const = 0; + /** Set this instruction's sequence ID. */ void setSequenceId(uint64_t seqId) { sequenceId_ = seqId; } @@ -136,6 +139,14 @@ class Instruction { /** Get this instruction's instruction memory address. */ uint64_t getInstructionAddress() const { return instructionAddress_; } + /** Set this instruction's next instruction memory address. */ + void setNextInstructionAddress(uint64_t address) { + nextInstructionAddress_ = address; + } + + /** Get this instruction's next instruction memory address. */ + uint64_t getNextInstructionAddress() const { return nextInstructionAddress_; } + /** Supply a branch prediction. */ void setBranchPrediction(BranchPrediction prediction) { prediction_ = prediction; @@ -248,6 +259,9 @@ class Instruction { /** The location in memory of this instruction was decoded at. */ uint64_t instructionAddress_ = 0; + /** The location in memory of the next instruction following this. */ + uint64_t nextInstructionAddress_ = 0; + // Execution /** Whether or not this instruction has been executed. */ bool executed_ = false; diff --git a/src/include/simeng/Register.hh b/src/include/simeng/Register.hh index 4b60772e60..27e1ab863a 100644 --- a/src/include/simeng/Register.hh +++ b/src/include/simeng/Register.hh @@ -1,5 +1,6 @@ #pragma once #include +#include namespace simeng { diff --git a/src/include/simeng/arch/Architecture.hh b/src/include/simeng/arch/Architecture.hh index 70782c079c..b4b16ddffd 100644 --- a/src/include/simeng/arch/Architecture.hh +++ b/src/include/simeng/arch/Architecture.hh @@ -30,7 +30,7 @@ struct ExceptionResult { * cycle until complete. */ class ExceptionHandler { public: - virtual ~ExceptionHandler(){}; + virtual ~ExceptionHandler() {}; /** Tick the exception handler to progress handling of the exception. Should * return `false` if the exception requires further handling, or `true` once * complete. */ @@ -46,7 +46,7 @@ class Architecture { public: Architecture(kernel::Linux& kernel) : linux_(kernel) {} - virtual ~Architecture(){}; + virtual ~Architecture() {}; /** Attempt to pre-decode from `bytesAvailable` bytes of instruction memory. * Writes into the supplied macro-op vector, and returns the number of bytes @@ -81,6 +81,16 @@ class Architecture { virtual void updateSystemTimerRegisters(RegisterFileSet* regFile, const uint64_t iterations) const = 0; + /** Get the architecture-specific vector size (currently only for (S)VL on + * AArch64) */ + virtual const std::tuple getVectorSize() const = 0; + + /** Get the register which holds the syscall ID in this ISA. */ + virtual const Register getSyscallIDReg() const = 0; + + /** Get the register which holds the exit code in this ISA. */ + virtual const Register getExitCodeReg() const = 0; + protected: /** A Capstone decoding library handle, for decoding instructions. */ csh capstoneHandle_; diff --git a/src/include/simeng/arch/aarch64/Architecture.hh b/src/include/simeng/arch/aarch64/Architecture.hh index 8d4939c991..1d3d9505bb 100644 --- a/src/include/simeng/arch/aarch64/Architecture.hh +++ b/src/include/simeng/arch/aarch64/Architecture.hh @@ -54,6 +54,22 @@ class Architecture : public arch::Architecture { void updateSystemTimerRegisters(RegisterFileSet* regFile, const uint64_t iterations) const override; + /** Get the architecture-specific vector size (currently only for (S)VL on + * AArch64) */ + const std::tuple getVectorSize() const override { + return std::make_tuple(getVectorLength(), getStreamingVectorLength()); + } + + /** Get the register which holds the syscall ID in this ISA. */ + const Register getSyscallIDReg() const override { + return {RegisterType::GENERAL, 8}; + } + + /** Get the register which holds the exit code in this ISA. */ + const Register getExitCodeReg() const override { + return {RegisterType::GENERAL, 0}; + }; + /** Retrieve an ExecutionInfo object for the requested instruction. If a * opcode-based override has been defined for the latency and/or * port information, return that instead of the group-defined execution diff --git a/src/include/simeng/arch/aarch64/Instruction.hh b/src/include/simeng/arch/aarch64/Instruction.hh index 315a555a00..ba8b974560 100644 --- a/src/include/simeng/arch/aarch64/Instruction.hh +++ b/src/include/simeng/arch/aarch64/Instruction.hh @@ -360,6 +360,11 @@ class Instruction : public simeng::Instruction { * latency and throughput, and the set of ports which support it. */ void setExecutionInfo(const ExecutionInfo& info) override; + /** Get whether this instruction is a syscall. */ + bool isSyscall() const override { + return getException() == InstructionException::SupervisorCall; + }; + /** Retrieve the instruction's metadata. */ const InstructionMetadata& getMetadata() const; diff --git a/src/include/simeng/arch/riscv/Architecture.hh b/src/include/simeng/arch/riscv/Architecture.hh index b156832e18..8d83868738 100644 --- a/src/include/simeng/arch/riscv/Architecture.hh +++ b/src/include/simeng/arch/riscv/Architecture.hh @@ -52,6 +52,22 @@ class Architecture : public arch::Architecture { void updateSystemTimerRegisters(RegisterFileSet* regFile, const uint64_t iterations) const override; + /** Get the architecture-specific vector size (currently only for (S)VL on + * AArch64) */ + const std::tuple getVectorSize() const override { + return std::make_tuple(0, 0); + } + + /** Get the register which holds the syscall ID in this ISA. */ + const Register getSyscallIDReg() const override { + return {RegisterType::GENERAL, 17}; + } + + /** Get the register which holds the exit code in this ISA. */ + const Register getExitCodeReg() const override { + return {RegisterType::GENERAL, 10}; + }; + private: /** Retrieve an ExecutionInfo object for the requested instruction. If a * opcode-based override has been defined for the latency and/or diff --git a/src/include/simeng/arch/riscv/Instruction.hh b/src/include/simeng/arch/riscv/Instruction.hh index 888900ba18..04e5e032e8 100644 --- a/src/include/simeng/arch/riscv/Instruction.hh +++ b/src/include/simeng/arch/riscv/Instruction.hh @@ -175,6 +175,11 @@ class Instruction : public simeng::Instruction { * latency and throughput, and the set of ports which support it. */ void setExecutionInfo(const ExecutionInfo& info) override; + /** Get whether this instruction is a syscall. */ + bool isSyscall() const override { + return getException() == InstructionException::SupervisorCall; + }; + /** Retrieve the instruction's metadata. */ const InstructionMetadata& getMetadata() const; diff --git a/src/include/simeng/gdb/GDBStub.hh b/src/include/simeng/gdb/GDBStub.hh new file mode 100644 index 0000000000..ddcd85d056 --- /dev/null +++ b/src/include/simeng/gdb/GDBStub.hh @@ -0,0 +1,164 @@ +#pragma once + +#include + +#include "simeng/CoreInstance.hh" + +enum AckMode { + Enabled, + Transition, + Disabled, +}; + +enum BreakpointType { + SoftwareBP, + HardwareBP, + WriteWP, + ReadWP, + AccessWP, + SWStepBP, + HWStepBP +}; + +struct Breakpoint { + BreakpointType type; + uint64_t addr; + unsigned int kind; +}; + +enum ParseState { + ExpectStart, + Packet, + Escape, + Checksum, + Done, + ExtraData, +}; + +struct ParseResult { + ParseState state; + std::string packet; + uint8_t calculatedChecksum; + uint8_t receivedChecksum; + unsigned int checksumRemaining; + + bool done() const { return (state == Done) || (state == ExtraData); } + + bool valid() const { return calculatedChecksum == receivedChecksum; } +}; + +namespace simeng { +/** A GDB server stub, allowing for remote connections from a GDB client via + * GDB's Remote Serial Protocol (RSP) in order to debug programs running on + * the simulator. This class is only compiled when the GDB_ENABLED build + * option is set. */ +class GDBStub { + public: + /** Construct a GDBStub with a reference to a CoreInstance. */ + GDBStub(simeng::CoreInstance& coreInstance, bool verbose, uint16_t port); + + /** Run the GDBStub using the CoreInstance. This hands over execution to the + * stub, allowing it to control the emulation core, ready for a GDB client + * to send it commands via the provided port. */ + uint64_t run(); + + /** Read single register */ + std::string handleReadRegister(const std::string& reg); + + private: + /** The CoreInstance used for the simulation. */ + simeng::CoreInstance& coreInstance_; + + /** Whether to print verbose messages or not */ + bool verbose_; + + /** The port to listen on */ + uint16_t port_; + + /** Enum for whether to send and handle acknowledgements. */ + AckMode ack_mode = Enabled; + + /** The last response sent to the client, in case it needs retransmitting. + */ + std::string lastResponse = ""; + + /** File descriptor for the connection to the client. */ + int connection; + + /** Number of ticks executed */ + uint64_t iterations; + + /** Currently active breakpoints */ + std::vector breakpoints; + + /** Breakpoints for a step operation */ + std::vector step_breakpoints; + + /** Syscalls to catch */ + std::optional> syscalls_ = std::nullopt; + + /** Last known PC (may be fake) */ + uint64_t pc_; + + /** Run until a breakpoint or end-of-program is reached */ + std::string runUntilStop( + const std::optional& step_from = std::nullopt); + + /** Handle a ? query */ + std::string handleHaltReason(); + + /** Continue program */ + std::string handleContinue(const std::string& addr); + + /** Read all registers */ + std::string handleReadRegisters(); + + /** Write single register */ + std::string handleWriteRegister(const std::string& register_value); + + /** Write all registers */ + std::string handleWriteRegisters(const std::string& register_values); + + /** Read memory */ + std::string handleReadMemory(const std::string& raw_params); + + /** Write memory */ + std::string handleWriteMemory(const std::string& raw_params); + + /** Handle general query packets, e.g. qSupported */ + std::string handleQuery(const std::string& query); + + /** Handle general set packets, e.g. QStartNoAckMode */ + std::string handleSet(const std::string& set); + + /** Single step */ + std::string handleStep(const std::string& addr); + + /** Handle removing a breakpoint */ + std::string handleRemoveBreakpoint(const std::string& raw_params); + + /** Handle adding a breakpoint */ + std::string handleAddBreakpoint(const std::string& raw_params); + + /** Handle Xfer:features query */ + std::string queryFeatures(const std::vector& params); + + /** Decode a packet, handling escape sequences and verifying the checksum */ + std::optional decodePacket(const std::string& encodedPacket, + ParseResult result); + + /** Encode a packet, handling escape sequences and calculating the checksum */ + std::string encodePacket(const std::string& response); + + /** Send a response to the client, storing it in case it needs + * retransmitting. + */ + void sendResponse(const std::string& response); + + /** Create a socket and listen on the port number provided. + * Socket handling code modified from: + * https://ncona.com/2019/04/building-a-simple-server-with-cpp/. */ + static int openSocket(const uint16_t port); +}; + +} // namespace simeng diff --git a/src/include/simeng/memory/FixedLatencyMemoryInterface.hh b/src/include/simeng/memory/FixedLatencyMemoryInterface.hh index 44cbf7adcc..0e82649a32 100644 --- a/src/include/simeng/memory/FixedLatencyMemoryInterface.hh +++ b/src/include/simeng/memory/FixedLatencyMemoryInterface.hh @@ -62,6 +62,12 @@ class FixedLatencyMemoryInterface : public MemoryInterface { /** Returns true if there are any outstanding memory requests in-flight. */ bool hasPendingRequests() const override; + /** Read from memory directly. */ + void rawRead(void* dest, uint64_t src, uint16_t size) const override; + + /** Write to memory directly. */ + void rawWrite(uint64_t dest, void* src, uint16_t size) override; + /** Tick the memory model to process the request queue. */ void tick() override; diff --git a/src/include/simeng/memory/FlatMemoryInterface.hh b/src/include/simeng/memory/FlatMemoryInterface.hh index a1cb1ff8d4..e26d1ebf48 100644 --- a/src/include/simeng/memory/FlatMemoryInterface.hh +++ b/src/include/simeng/memory/FlatMemoryInterface.hh @@ -32,6 +32,12 @@ class FlatMemoryInterface : public MemoryInterface { /** Returns true if there are any outstanding memory requests in-flight. */ bool hasPendingRequests() const override; + /** Read from memory directly. */ + void rawRead(void* dest, uint64_t src, uint16_t size) const override; + + /** Write to memory directly. */ + void rawWrite(uint64_t dest, void* src, uint16_t size) override; + /** Tick: do nothing */ void tick() override; diff --git a/src/include/simeng/memory/MemoryAccessTarget.hh b/src/include/simeng/memory/MemoryAccessTarget.hh index 4962f7567f..db658029e5 100644 --- a/src/include/simeng/memory/MemoryAccessTarget.hh +++ b/src/include/simeng/memory/MemoryAccessTarget.hh @@ -20,6 +20,12 @@ struct MemoryAccessTarget { bool operator!=(const MemoryAccessTarget& other) const { return !(other == *this); } + + bool overlaps(const MemoryAccessTarget& other) const { + return ((address <= other.address) && ((address + size) > other.address)) || + ((other.address <= address) && + ((other.address + other.size) > address)); + } }; } // namespace memory diff --git a/src/include/simeng/memory/MemoryInterface.hh b/src/include/simeng/memory/MemoryInterface.hh index dd39d34c3b..9eecf56e74 100644 --- a/src/include/simeng/memory/MemoryInterface.hh +++ b/src/include/simeng/memory/MemoryInterface.hh @@ -41,6 +41,12 @@ class MemoryInterface { /** Returns true if there are any outstanding memory requests in-flight. */ virtual bool hasPendingRequests() const = 0; + /** Read from memory directly. */ + virtual void rawRead(void* dest, uint64_t src, uint16_t size) const = 0; + + /** Write to memory directly. */ + virtual void rawWrite(uint64_t dest, void* src, uint16_t size) = 0; + /** Tick the memory interface to allow it to process internal tasks. * * TODO: Move ticking out of the memory interface and into a central "memory diff --git a/src/include/simeng/models/emulation/Core.hh b/src/include/simeng/models/emulation/Core.hh index f1e38d7022..95e93c7071 100644 --- a/src/include/simeng/models/emulation/Core.hh +++ b/src/include/simeng/models/emulation/Core.hh @@ -39,12 +39,34 @@ class Core : public simeng::Core { /** Retrieve a map of statistics to report. */ std::map getStats() const override; + /** Retrieve the program counter. */ + const uint64_t getProgramCounter() const override; + + /** Set the program counter. */ + void setProgramCounter(uint64_t pc) override; + + /** Prepare the necessary breakpoint state for the following run. */ + void prepareBreakpoints( + const std::optional* step_from = nullptr, + const std::vector* bp = nullptr, + const std::vector* wp = nullptr, + const std::vector* rp = nullptr, + const std::vector* ap = nullptr, + const std::optional>* syscalls = nullptr) override; + + /** Retrieve the reason for a break, if any. */ + const std::optional getBreakReason() const override; + + /** Retrieve the exit code. Result only valid after exit() syscall has been + * entered. */ + uint64_t getExitCode() const override { return exit_code_; } + private: /** Execute an instruction. */ - void execute(std::shared_ptr& uop); + bool execute(std::shared_ptr& uop); /** Handle an encountered exception. */ - void handleException(const std::shared_ptr& instruction); + bool handleException(const std::shared_ptr& instruction); /** Process an active exception handler. */ void processExceptionHandler(); @@ -73,6 +95,36 @@ class Core : public simeng::Core { /** The number of branches executed. */ uint64_t branchesExecuted_ = 0; + + /** If present, break when the program counter doesn't match this value. */ + const std::optional* step_from_ = nullptr; + + /** If present, break when the program counter matches any of these values. */ + const std::vector* bp_ = nullptr; + + /** If present, break when writing to any of these addresses. */ + const std::vector* wp_ = nullptr; + + /** If present, break when reading from any of these addresses. */ + const std::vector* rp_ = nullptr; + + /** If present, break when accessing any of these addresses. */ + const std::vector* ap_ = nullptr; + + /** If present, the syscalls to catch. */ + const std::optional>* syscalls_ = nullptr; + + /** The last reason for which a break occurred. */ + std::optional br_ = std::nullopt; + + /** The next reason to break. */ + std::optional brn_ = std::nullopt; + + /** If present, the syscall currently being caught. */ + std::optional current_syscall_ = std::nullopt; + + /** Exit code. Only valid after exit() syscall has been entered. */ + uint64_t exit_code_; }; } // namespace emulation diff --git a/src/include/simeng/models/inorder/Core.hh b/src/include/simeng/models/inorder/Core.hh index 7b4b74f713..e74db0c1bd 100644 --- a/src/include/simeng/models/inorder/Core.hh +++ b/src/include/simeng/models/inorder/Core.hh @@ -43,6 +43,15 @@ class Core : public simeng::Core { /** Generate a map of statistics to report. */ std::map getStats() const override; + /** Retrieve the program counter. */ + const uint64_t getProgramCounter() const override; + + /** Set the program counter. */ + void setProgramCounter(uint64_t pc) override; + + /** Retrieve the reason for a break, if any. */ + const std::optional getBreakReason() const override; + private: /** Raise an exception to the core, providing the generating instruction. */ void raiseException(const std::shared_ptr& instruction); diff --git a/src/include/simeng/models/outoforder/Core.hh b/src/include/simeng/models/outoforder/Core.hh index 2b1b16f8a0..a5f97f31d8 100644 --- a/src/include/simeng/models/outoforder/Core.hh +++ b/src/include/simeng/models/outoforder/Core.hh @@ -49,6 +49,28 @@ class Core : public simeng::Core { /** Generate a map of statistics to report. */ std::map getStats() const override; + /** Retrieve the program counter. */ + const uint64_t getProgramCounter() const override; + + /** Set the program counter. */ + void setProgramCounter(uint64_t pc) override; + + /** Prepare the necessary breakpoint state for the following run. */ + void prepareBreakpoints( + const std::optional* step_from = nullptr, + const std::vector* bp = nullptr, + const std::vector* wp = nullptr, + const std::vector* rp = nullptr, + const std::vector* ap = nullptr, + const std::optional>* syscalls = nullptr) override; + + /** Retrieve the reason for a break, if any. */ + const std::optional getBreakReason() const override; + + /** Retrieve the exit code. Result only valid after exit() syscall has been + * entered. */ + uint64_t getExitCode() const override { return exit_code_; } + private: /** Raise an exception to the core, providing the generating instruction. */ void raiseException(const std::shared_ptr& instruction); @@ -134,6 +156,15 @@ class Core : public simeng::Core { /** A pointer to the instruction responsible for generating the exception. */ std::shared_ptr exceptionGeneratingInstruction_; + + /** If present, the syscalls to catch. */ + const std::optional>* syscalls_ = nullptr; + + /** If present, the syscall currently being caught. */ + std::optional current_syscall_ = std::nullopt; + + /** Exit code. Only valid after exit() syscall has been entered. */ + uint64_t exit_code_; }; } // namespace outoforder diff --git a/src/include/simeng/pipeline/FetchUnit.hh b/src/include/simeng/pipeline/FetchUnit.hh index 09e8bb31f6..cda72ada5a 100644 --- a/src/include/simeng/pipeline/FetchUnit.hh +++ b/src/include/simeng/pipeline/FetchUnit.hh @@ -69,6 +69,9 @@ class FetchUnit { /** Clear the loop buffer. */ void flushLoopBuffer(); + /** Retrieve the current program counter value. */ + const uint64_t getPC() const; + private: /** An output buffer connecting this unit to the decode unit. */ PipelineBuffer& output_; diff --git a/src/include/simeng/pipeline/RegisterAliasTable.hh b/src/include/simeng/pipeline/RegisterAliasTable.hh index 43b8e0db4c..9c0e920b7a 100644 --- a/src/include/simeng/pipeline/RegisterAliasTable.hh +++ b/src/include/simeng/pipeline/RegisterAliasTable.hh @@ -21,6 +21,10 @@ class RegisterAliasTable { * architectural register. */ Register getMapping(Register architectural) const; + /** Retrieve the committed physical register assigned to the provided + * architectural register. */ + Register getCommittedMapping(Register architectural) const; + /** Determine whether it's possible to allocate `quantity` physical registers * of type `type` this cycle. */ bool canAllocate(uint8_t type, unsigned int quantity) const; @@ -48,6 +52,9 @@ class RegisterAliasTable { * register mappings for each register type. */ std::vector> mappingTable_; + /** TODO: comment here */ + std::vector> commitTable_; + /** The register history tables. Each table holds an entry for each physical * register, recording the physical register formerly assigned to its * architectural register; one table is available per register type. */ diff --git a/src/include/simeng/pipeline/ReorderBuffer.hh b/src/include/simeng/pipeline/ReorderBuffer.hh index 278a0174ca..7de33fbad6 100644 --- a/src/include/simeng/pipeline/ReorderBuffer.hh +++ b/src/include/simeng/pipeline/ReorderBuffer.hh @@ -2,7 +2,9 @@ #include #include +#include +#include "simeng/Core.hh" #include "simeng/Instruction.hh" #include "simeng/pipeline/LoadStoreQueue.hh" #include "simeng/pipeline/RegisterAliasTable.hh" @@ -85,6 +87,26 @@ class ReorderBuffer { /** Get the number of speculated loads which violated load-store ordering. */ uint64_t getViolatingLoadsCount() const; + /** Clobber all instructions after a certain sequence ID. */ + void clobberAfter(uint64_t id, uint64_t pc); + + /** Prepare the necessary breakpoint state for the following run. */ + void prepareBreakpoints( + const std::optional* step_from, const std::vector* bp, + const std::vector* wp, + const std::vector* rp, + const std::vector* ap); + + /** Retrieve the reason for a break, if any. */ + const std::optional getBreakReason() const; + + /** Set the break reasons. */ + void setBreakReasons(std::optional reason, + std::optional next_reason); + + /** Retrieve the current program counter value. */ + const uint64_t getPC() const; + private: /** A reference to the register alias table. */ RegisterAliasTable& rat_; @@ -144,6 +166,27 @@ class ReorderBuffer { /** The number of speculative loads which violated load-store ordering. */ uint64_t loadViolations_ = 0; + + /** If present, break when the program counter doesn't match this value. */ + const std::optional* step_from_ = nullptr; + + /** If present, break when the program counter matches any of these values. */ + const std::vector* bp_ = nullptr; + + /** If present, break when writing to any of these addresses. */ + const std::vector* wp_ = nullptr; + + /** If present, break when reading from any of these addresses. */ + const std::vector* rp_ = nullptr; + + /** If present, break when accessing any of these addresses. */ + const std::vector* ap_ = nullptr; + + /** The last reason for which a break occurred. */ + std::optional br_ = std::nullopt; + + /** The next reason to break. */ + std::optional brn_ = std::nullopt; }; } // namespace pipeline diff --git a/src/include/simeng/version.hh.in b/src/include/simeng/version.hh.in index 5f1e8f410b..2ec7766cd4 100644 --- a/src/include/simeng/version.hh.in +++ b/src/include/simeng/version.hh.in @@ -9,5 +9,6 @@ #define SIMENG_LLVM_VERSION @SIMENG_LLVM_VERSION@ #define SIMENG_ENABLE_TESTS "${SIMENG_ENABLE_TESTS}" #define SIMENG_BUILD_DIR "${CMAKE_BINARY_DIR}" +#define SIMENG_ENABLE_GDB "${SIMENG_ENABLE_GDB}" #endif \ No newline at end of file diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index ffabd8bbca..314c684c6f 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -47,6 +47,10 @@ set(SIMENG_SOURCES SpecialFileDirGen.cc ) +if (SIMENG_ENABLE_GDB) + list(APPEND SIMENG_SOURCES gdb/GDBStub.cc gdb/tinyxml2.cpp) +endif() + configure_file(${capstone_SOURCE_DIR}/arch/AArch64/AArch64GenInstrInfo.inc AArch64GenInstrInfo.inc COPYONLY) configure_file(${capstone_SOURCE_DIR}/arch/RISCV/RISCVGenInstrInfo.inc RISCVGenInstrInfo.inc COPYONLY) diff --git a/src/lib/arch/aarch64/Architecture.cc b/src/lib/arch/aarch64/Architecture.cc index e27ea8f0f9..734092c9bb 100644 --- a/src/lib/arch/aarch64/Architecture.cc +++ b/src/lib/arch/aarch64/Architecture.cc @@ -148,6 +148,7 @@ uint8_t Architecture::predecode(const uint8_t* ptr, uint16_t bytesAvailable, uop = std::make_shared(*this, metadataCache_.front(), InstructionException::MisalignedPC); uop->setInstructionAddress(instructionAddress); + uop->setNextInstructionAddress(instructionAddress + 1); // Return non-zero value to avoid fatal error return 1; } @@ -197,6 +198,7 @@ uint8_t Architecture::predecode(const uint8_t* ptr, uint16_t bytesAvailable, // Set instruction address and branch prediction for each micro-op generated for (int i = 0; i < num_ops; i++) { output[i]->setInstructionAddress(instructionAddress); + output[i]->setNextInstructionAddress(instructionAddress + 4); } return 4; diff --git a/src/lib/arch/aarch64/ExceptionHandler.cc b/src/lib/arch/aarch64/ExceptionHandler.cc index 36aff03781..842fdf7c69 100644 --- a/src/lib/arch/aarch64/ExceptionHandler.cc +++ b/src/lib/arch/aarch64/ExceptionHandler.cc @@ -29,10 +29,10 @@ bool ExceptionHandler::init() { InstructionException exception = instruction_.getException(); const auto& registerFileSet = core_.getArchitecturalRegisterFileSet(); - if (exception == InstructionException::SupervisorCall) { + if (instruction_.isSyscall()) { // Retrieve syscall ID held in register x8 auto syscallId = - registerFileSet.get({RegisterType::GENERAL, 8}).get(); + registerFileSet.get(core_.getISA().getSyscallIDReg()).get(); ProcessStateChange stateChange; switch (syscallId) { @@ -859,7 +859,7 @@ bool ExceptionHandler::readBufferThen(uint64_t ptr, uint64_t length, } bool ExceptionHandler::concludeSyscall(ProcessStateChange& stateChange) { - uint64_t nextInstructionAddress = instruction_.getInstructionAddress() + 4; + uint64_t nextInstructionAddress = instruction_.getNextInstructionAddress(); result_ = {false, nextInstructionAddress, stateChange}; return true; } diff --git a/src/lib/arch/riscv/Architecture.cc b/src/lib/arch/riscv/Architecture.cc index c600c285d4..4357d1b97a 100644 --- a/src/lib/arch/riscv/Architecture.cc +++ b/src/lib/arch/riscv/Architecture.cc @@ -169,6 +169,7 @@ uint8_t Architecture::predecode(const uint8_t* ptr, uint16_t bytesAvailable, uop = std::make_shared(*this, metadataCache_.front(), InstructionException::MisalignedPC); uop->setInstructionAddress(instructionAddress); + uop->setNextInstructionAddress(instructionAddress + 1); // Return non-zero value to avoid fatal error return 1; } @@ -245,9 +246,11 @@ uint8_t Architecture::predecode(const uint8_t* ptr, uint16_t bytesAvailable, iter = decodeCache_.insert({insnEncoding, newInsn}).first; } - assert(((insnEncoding & 0b11) != 0b11 - ? iter->second.getMetadata().getInsnLength() == 2 - : iter->second.getMetadata().getInsnLength() == 4) && + assert((insnSize == iter->second.getMetadata().getInsnLength()) && "That's not good!"); + + insnSize = iter->second.getMetadata().getInsnLength(); + + assert(((insnEncoding & 0b11) != 0b11 ? insnSize == 2 : insnSize == 4) && "Predicted number of bytes don't match disassembled number of bytes"); output.resize(1); @@ -257,8 +260,9 @@ uint8_t Architecture::predecode(const uint8_t* ptr, uint16_t bytesAvailable, uop = std::make_shared(iter->second); uop->setInstructionAddress(instructionAddress); + uop->setNextInstructionAddress(instructionAddress + insnSize); - return iter->second.getMetadata().getInsnLength(); + return insnSize; } int32_t Architecture::getSystemRegisterTag(uint16_t reg) const { diff --git a/src/lib/arch/riscv/ExceptionHandler.cc b/src/lib/arch/riscv/ExceptionHandler.cc index 15a5518c64..f0bcfedbc1 100644 --- a/src/lib/arch/riscv/ExceptionHandler.cc +++ b/src/lib/arch/riscv/ExceptionHandler.cc @@ -27,10 +27,10 @@ bool ExceptionHandler::init() { InstructionException exception = instruction_.getException(); const auto& registerFileSet = core_.getArchitecturalRegisterFileSet(); - if (exception == InstructionException::SupervisorCall) { + if (instruction_.isSyscall()) { // Retrieve syscall ID held in register a7 auto syscallId = - registerFileSet.get({RegisterType::GENERAL, 17}).get(); + registerFileSet.get(core_.getISA().getSyscallIDReg()).get(); ProcessStateChange stateChange; switch (syscallId) { @@ -850,7 +850,7 @@ bool ExceptionHandler::readBufferThen(uint64_t ptr, uint64_t length, } bool ExceptionHandler::concludeSyscall(ProcessStateChange& stateChange) { - uint64_t nextInstructionAddress = instruction_.getInstructionAddress() + 4; + uint64_t nextInstructionAddress = instruction_.getNextInstructionAddress(); result_ = {false, nextInstructionAddress, stateChange}; return true; } diff --git a/src/lib/gdb/GDBStub.cc b/src/lib/gdb/GDBStub.cc new file mode 100644 index 0000000000..83c1400d26 --- /dev/null +++ b/src/lib/gdb/GDBStub.cc @@ -0,0 +1,2235 @@ +#include "simeng/gdb/GDBStub.hh" + +#include +#include +#include +#include + +#include +#include + +#include "simeng/arch/Architecture.hh" +#include "tinyxml2.h" + +// there are many things about this implementation that i am unsatisfied with - +// particularly, the reliance on strings and the lack of good testing +// infrastructure - but it's workable and mostly functional +// +// for information about the protocol, see: +// https://sourceware.org/gdb/current/onlinedocs/gdb.html/Remote-Protocol.html +// +// this implementation supports arm and seems to work pretty well, but i have no +// doubt that there are a ton of subtle bugs +// +// good places to do more work: +// - adding risc-v support +// - refactoring +// - checking whether memory and register writes are sound (i think they might +// not be, particularly out-of-order pc writes) +// - lldb support (lldb *claims* to support the gdb remote protocol, but in +// reality every implementation is slightly different and i wrote this one +// targeting gdb itself, so it doesn't work) + +// read buffer size +#define BUF_SIZE (1000) + +// number of requests to queue +#define NUM_REQUESTS (1) + +// colour codes for pretty printing +#define RESET "\033[0m" +#define CYAN "\033[36m" +#define GREEN "\033[32m" +#define RED "\033[31m" + +struct SupportedFeature { + std::string name; + std::optional value; + + std::string format() const; +}; + +std::string SupportedFeature::format() const { + if (value) { + return name + '=' + *value; + } else { + return name + '+'; + } +} + +// functions for converting between integers and hexadecimal strings, used in +// various places to interface with the gdb protocol +template +std::string int_to_hex(T i) { + char* ptr; + const auto bytesWritten = + asprintf(&ptr, "%0*x", static_cast(sizeof(T) * 2), i); + const auto str = std::string{ptr, static_cast(bytesWritten)}; + free(ptr); + return str; +} + +template <> +std::string int_to_hex(uint64_t i) { + return int_to_hex((uint32_t)(i >> 32)) + int_to_hex((uint32_t)i); +} + +template <> +std::string int_to_hex(unsigned long long i) { + return int_to_hex((uint64_t)i); +} + +template +std::string int_to_hex_ne(T i) { + uint8_t bytes[sizeof(T)]; + memcpy(bytes, &i, sizeof(T)); + std::string rv; + for (const auto byte : bytes) { + rv += int_to_hex(byte); + } + return rv; +} + +template +std::optional hex_to_int_ne(const std::string& str) { + if (str.size() != sizeof(T) * 2) { + return std::nullopt; + } + + uint8_t bytes[sizeof(T)]; + try { + for (size_t i = 0; i < sizeof(T); i++) { + bytes[i] = std::stoi(str.substr(i * 2, 2), nullptr, 16); + } + } catch (const std::exception& e) { + return std::nullopt; + } + + T rv; + + memcpy(&rv, bytes, sizeof(T)); + + return rv; +} + +template +std::optional> hex_to_vect_ne(const std::string& str) { + if (str.size() != N * 2) { + return std::nullopt; + } + + char bytes[N]; + try { + for (size_t i = 0; i < N; i++) { + bytes[i] = std::stoi(str.substr(i * 2, 2), nullptr, 16); + } + } catch (const std::exception& e) { + return std::nullopt; + } + + std::array rv; + std::copy(std::begin(bytes), std::end(bytes), rv.begin()); + + return rv; +} + +// features of the gdb protocol that we support +const SupportedFeature supported_features[] = { + { + "QStartNoAckMode", + std::nullopt, + }, + {"PacketSize", int_to_hex(BUF_SIZE - 10)}, + {"hwbreak", std::nullopt}, + {"swbreak", std::nullopt}, + {"QCatchSyscalls", std::nullopt}, + {"qXfer:features:read", std::nullopt}}; + +// we manipulate and store gdb protocol values and state as strings, because +// doing it """properly""" would require a fairly complex data structure that i +// wasn't comfortable writing in c++, so there are a few functions for +// formatting other data to fit the protocol +// +// stripping out this infrastructure and replacing it with a proper type would +// be a great first step in continuing this codebase +template +std::string formatSignal( + uint8_t signal, + const std::vector, std::string>>& + values) { + if (values.empty()) { + return 'S' + int_to_hex(signal); + } else { + std::string rv; + + rv += 'T'; + rv += int_to_hex(signal); + + for (const auto& param : values) { + const auto& type = std::get<0>(param); + const auto& value = std::get<1>(param); + + switch (type.index()) { + case 0: { + // string + rv += std::get<0>(type); + break; + } + case 1: { + // T + rv += int_to_hex(std::get<1>(type)); + break; + } + } + + rv += ':'; + + rv += value; + + rv += ';'; + } + + return rv; + } +} + +std::string formatError(const std::string& textual_error) { + return "E." + textual_error; +} + +std::string formatError(uint8_t error_num) { + return "E" + int_to_hex(error_num); +} + +std::string formatExit(uint8_t status) { return "W" + int_to_hex(status); } + +// this function really ought to be in the c++ standard library, but it doesn't +// seem to be +std::vector splitBy(const std::string& param_string, + const char by) { + std::vector rv; + std::string cur; + + for (const auto& c : param_string) { + if (c == by) { + rv.push_back(cur); + cur = ""; + } else { + cur += c; + } + } + + rv.push_back(cur); + + return rv; +} + +/*std::tuple, uint8_t> +getCurrentInstruction( const simeng::CoreInstance& coreInstance) { const auto +core = coreInstance.getCore(); const auto instructionMemory = +coreInstance.getInstructionMemory(); const auto pc = core->getProgramCounter(); + const auto& isa = core->getISA(); + + const auto ptr = instructionMemory->getMemoryPointer(); + const auto size = isa.getMaxInstructionSize(); + + uint8_t buffer[size]; + memcpy(buffer, ptr + pc, size); + + simeng::MacroOp macroOp; + auto bytesRead = isa.predecode(buffer, size, pc, macroOp); + + // TODO: is this always valid? + return std::make_tuple(macroOp[0], bytesRead); +}*/ + +// a few different operations act differently depending on what type of register +// is being used, so "size" isn't strictly accurate +enum RegSize { + Byte, + Short, + Word, + ByteWord, + DoubleWord, + Double, + Vector, + Predicate, + PC, + VG, + SVG, + ZA, +}; + +unsigned int getBitsize(RegSize size) { + switch (size) { + case Byte: + return 8; + case Short: + return 16; + case Word: + return 32; + case ByteWord: + return 32; + case DoubleWord: + return 32; + case Double: + return 64; + case Vector: + return 2048; + case Predicate: + return 256; + case PC: + return 64; + case VG: + return 64; + case SVG: + return 64; + case ZA: // this register is more complex to handle and doesn't necessarily + // have a defined bit size + return 0; + default: + return 0; + } +} + +using RegList = std::vector>; + +// we build an abstract representation of the processor's registers to be able +// to generically interface with gdb +struct TargetSpec { + std::string spec; + RegList regs; +}; + +RegList::value_type makeReg(uint8_t type, uint16_t tag, RegSize size) { + const simeng::Register reg = {type, tag}; + return std::make_tuple(reg, size); +} + +// while building the abstract representation, we build a target description xml +// to send to gdb +// +// see: +// https://sourceware.org/gdb/current/onlinedocs/gdb.html/Target-Description-Format.html +void addReg( + tinyxml2::XMLPrinter& printer, RegList& regs, uint8_t type, uint16_t tag, + const std::string& name, RegSize size, + const std::optional& data_type = std::nullopt, + const std::optional& override_bitsize = std::nullopt) { + printer.OpenElement("reg", true); + + printer.PushAttribute("name", name.c_str()); + printer.PushAttribute( + "bitsize", (override_bitsize) ? *override_bitsize : getBitsize(size)); + if (data_type) { + printer.PushAttribute("type", data_type->c_str()); + } + + printer.CloseElement(true); + + regs.push_back(makeReg(type, tag, size)); +} + +struct FlagsField { + std::string name; + uint8_t start; + uint8_t end; +}; +void addFlags(tinyxml2::XMLPrinter& printer, const std::string& name, + uint8_t size, const std::vector& fields) { + printer.OpenElement("flags"); + + printer.PushAttribute("id", name.c_str()); + printer.PushAttribute("size", size); + + for (const auto& [name, start, end] : fields) { + printer.OpenElement("field", true); + + printer.PushAttribute("name", name.c_str()); + printer.PushAttribute("start", start); + printer.PushAttribute("end", end); + + printer.CloseElement(true); + } + + printer.CloseElement(); +} + +struct UnionField { + std::string name; + std::string type; +}; +void addUnion(tinyxml2::XMLPrinter& printer, const std::string& name, + const std::vector& fields) { + printer.OpenElement("union"); + + printer.PushAttribute("id", name.c_str()); + + for (const auto& [name, type] : fields) { + printer.OpenElement("field", true); + + printer.PushAttribute("name", name.c_str()); + printer.PushAttribute("type", type.c_str()); + + printer.CloseElement(true); + } + + printer.CloseElement(); +} + +void addVector(tinyxml2::XMLPrinter& printer, const std::string& name, + const std::string& type, uint16_t count) { + printer.OpenElement("vector", true); + + printer.PushAttribute("id", name.c_str()); + printer.PushAttribute("type", type.c_str()); + printer.PushAttribute("count", count); + + printer.CloseElement(true); +} + +// add the arm base registers +void deriveCore(tinyxml2::XMLPrinter& printer, RegList& regs) { + printer.OpenElement("feature"); + + printer.PushAttribute("name", "org.gnu.gdb.aarch64.core"); + + for (auto i = 0; i < 32; i++) { + const auto name = "x" + std::to_string(i); + + addReg(printer, regs, 0, i, (i == 31) ? "sp" : ("x" + std::to_string(i)), + Double, (i == 31) ? std::optional("data_ptr") : std::nullopt); + } + + addReg(printer, regs, 0, 0, "pc", PC, "code_ptr"); + + addFlags( + printer, "cpsr_flags", 4, + { + {"SP", 0, 0}, {"EL", 2, 3}, {"nRW", 4, 4}, {"F", 6, 6}, + {"I", 7, 7}, {"A", 8, 8}, {"D", 9, 9}, {"BTYPE", 10, 11}, + {"SSBS", 12, 12}, {"IL", 20, 20}, {"SS", 21, 21}, {"PAN", 22, 22}, + {"UAO", 23, 23}, {"DIT", 24, 24}, {"TCO", 25, 25}, {"V", 28, 28}, + {"C", 29, 29}, {"Z", 30, 30}, {"N", 31, 31}, + }); + + addReg(printer, regs, 3, 0, "cpsr", ByteWord, "cpsr_flags"); + + printer.CloseElement(); +} + +// add the sve extension registers +void deriveSVE(tinyxml2::XMLPrinter& printer, RegList& regs) { + printer.OpenElement("feature"); + + printer.PushAttribute("name", "org.gnu.gdb.aarch64.sve"); + + addVector(printer, "svevqu", "uint128", 16); + addVector(printer, "svevqs", "int128", 16); + addVector(printer, "svevdf", "ieee_double", 32); + addVector(printer, "svevdu", "uint64", 32); + addVector(printer, "svevds", "int64", 32); + addVector(printer, "svevsf", "ieee_single", 64); + addVector(printer, "svevsu", "uint32", 64); + addVector(printer, "svevss", "int32", 64); + addVector(printer, "svevhf", "ieee_half", 128); + addVector(printer, "svevhu", "uint16", 128); + addVector(printer, "svevhs", "int16", 128); + addVector(printer, "svevbu", "uint8", 256); + addVector(printer, "svevbs", "int8", 256); + addVector(printer, "svep", "uint8", 32); + + addUnion(printer, "svevnq", + { + {"u", "svevqu"}, + {"s", "svevqs"}, + }); + addUnion(printer, "svevnd", + { + {"f", "svevdf"}, + {"u", "svevdu"}, + {"s", "svevds"}, + }); + addUnion(printer, "svevns", + { + {"f", "svevsf"}, + {"u", "svevsu"}, + {"s", "svevss"}, + }); + addUnion(printer, "svevnh", + { + {"f", "svevhf"}, + {"u", "svevhu"}, + {"s", "svevhs"}, + }); + addUnion(printer, "svevnb", + { + {"u", "svevbu"}, + {"s", "svevbs"}, + }); + addUnion(printer, "svev", + { + {"q", "svevnq"}, + {"d", "svevnd"}, + {"s", "svevns"}, + {"h", "svevnh"}, + {"b", "svevnb"}, + }); + + addFlags(printer, "fpsr_flags", 4, + { + {"IOC", 0, 0}, + {"DZC", 1, 1}, + {"OFC", 2, 2}, + {"UFC", 3, 3}, + {"IXC", 4, 4}, + {"IDC", 7, 7}, + {"QC", 27, 27}, + {"V", 28, 28}, + {"C", 29, 29}, + {"Z", 30, 30}, + {"N", 31, 31}, + }); + addFlags(printer, "fpcr_flags", 4, + { + {"FIZ", 0, 0}, + {"AH", 1, 1}, + {"NEP", 2, 2}, + {"IOE", 8, 8}, + {"DZE", 9, 9}, + {"OFE", 10, 10}, + {"UFE", 11, 11}, + {"IXE", 12, 12}, + {"EBF", 13, 13}, + {"IDE", 15, 15}, + {"Len", 16, 18}, + {"FZ16", 19, 19}, + {"Stride", 20, 21}, + {"RMode", 22, 23}, + {"FZ", 24, 24}, + {"DN", 25, 25}, + {"AHP", 26, 26}, + }); + + for (auto i = 0; i < 32; i++) { + addReg(printer, regs, 1, i, "z" + std::to_string(i), Vector, "svev"); + } + + addReg(printer, regs, 4, 2, "fpsr", DoubleWord, "fpsr_flags"); + addReg(printer, regs, 4, 1, "fpcr", DoubleWord, "fpcr_flags"); + + for (auto i = 0; i < 16; i++) { + addReg(printer, regs, 2, i, "p" + std::to_string(i), Predicate, "svep"); + } + + addReg(printer, regs, 2, 16, "ffr", Predicate, "svep"); + addReg(printer, regs, 0, 0, "vg", VG, "int"); + + printer.CloseElement(); +} + +// add the sme extension registers +void deriveSME(tinyxml2::XMLPrinter& printer, RegList& regs, + unsigned int rows) { + printer.OpenElement("feature"); + + printer.PushAttribute("name", "org.gnu.gdb.aarch64.sme"); + + addReg(printer, regs, 0, 0, "svg", SVG, "int"); + + addFlags(printer, "svcr_flags", 8, + { + {"SM", 0, 0}, + {"ZA", 1, 1}, + }); + + addReg(printer, regs, 4, 7, "svcr", Double, "svcr_flags"); + + addVector(printer, "sme_bv", "uint8", 256); + addVector(printer, "sme_bvv", "sme_bv", rows); + + addReg(printer, regs, 0, 0, "za", ZA, "sme_bvv", rows * 2048); + + printer.CloseElement(); +} + +// build the spec for the arm chip we emulate using the above functions +TargetSpec deriveSpec() { + TargetSpec rv; + + tinyxml2::XMLPrinter printer; + + printer.OpenElement("target"); + printer.PushAttribute("version", "1.0"); + + printer.OpenElement("architecture"); + printer.PushText("aarch64"); + printer.CloseElement(); + + deriveCore(printer, rv.regs); + + deriveSVE(printer, rv.regs); + + // this version of the code assumes that za is always of size 256 - you might + // want to pass this in as a parameter + deriveSME(printer, rv.regs, 256); + + printer.CloseElement(); + + rv.spec = std::string(printer.CStr(), printer.CStrSize() - 1); + + return rv; +} + +TargetSpec target_spec; + +void checkSpec(const simeng::CoreInstance& coreInstance) { + if (target_spec.spec.empty()) { + // const auto core = coreInstance.getCore(); + // const auto& isa = core->getISA(); + + // const auto [_vl, svl] = isa.getVectorSize(); + + // this is where you would check which isa is being used, and derive the + // spec for risc-v instead of arm + target_spec = deriveSpec(); + + /*std::ofstream out("spec.xml"); + out << target_spec.spec; + out.close();*/ + } +} + +// read a register from the core and format it as a string for gdb +std::string readRegister(const RegList::value_type& which, + const simeng::ArchitecturalRegisterFileSet& registers, + uint64_t pc, uint64_t vl, uint64_t svl) { + const auto& [reg, size] = which; + + std::string rv; + + switch (size) { + case Byte: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case Short: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case Word: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case ByteWord: { + rv += int_to_hex_ne(registers.get(reg).zeroExtend(1, 4).get()); + break; + } + + case DoubleWord: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case Double: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case Vector: { + const auto vect = registers.get(reg).getAsVector(); + for (auto j = 0; j < 256; j++) { + rv += int_to_hex(vect[j]); + } + break; + } + + case Predicate: { + const auto vect = registers.get(reg).getAsVector(); + for (auto j = 0; j < 32; j++) { + rv += int_to_hex(vect[j]); + } + break; + } + + case PC: { + rv += int_to_hex_ne(pc); + break; + } + + case VG: { + rv += int_to_hex_ne(vl / 64); + break; + } + + case SVG: { + rv += int_to_hex_ne(svl / 64); + break; + } + + case ZA: { + for (uint16_t i = 0; i < 256; i++) { + if (i < (svl / 8)) { + const auto vect = registers.get({5, i}).getAsVector(); + for (auto j = 0; j < 256; j++) { + rv += int_to_hex(vect[j]); + } + } else { + for (auto j = 0; j < 256; j++) { + rv += "00"; + } + } + } + break; + } + } + + return rv; +} + +// parse a string from gdb to a RegisterValue +std::optional parseRegister( + const RegList::value_type& which, const std::string& str) { + const auto& [reg, size] = which; + + switch (size) { + case Byte: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(*value); + } else { + return std::nullopt; + } + } + + case Short: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(*value); + } else { + return std::nullopt; + } + } + + case Word: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(*value); + } else { + return std::nullopt; + } + } + + case ByteWord: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(static_cast(*value)); + } else { + return std::nullopt; + } + } + + case DoubleWord: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(static_cast(*value)); + } else { + return std::nullopt; + } + } + + case Double: + case PC: + case VG: + case SVG: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(*value); + } else { + return std::nullopt; + } + } + + case Vector: { + const auto value = hex_to_vect_ne<256>(str); + if (value) { + const auto arr = *value; + return simeng::RegisterValue(arr.data(), arr.size()); + } else { + return std::nullopt; + } + } + + case Predicate: { + const auto value = hex_to_vect_ne<32>(str); + if (value) { + const auto arr = *value; + return simeng::RegisterValue(arr.data(), arr.size()); + } else { + return std::nullopt; + } + } + + case ZA: { + return std::nullopt; + } + } + + return std::nullopt; +} + +namespace simeng { +GDBStub::GDBStub(simeng::CoreInstance& coreInstance, bool verbose, + uint16_t port) + : coreInstance_(coreInstance), + verbose_(verbose), + port_(port), + pc_(coreInstance.getCore()->getProgramCounter()) {} + +uint64_t GDBStub::run() { + iterations = 0; + + connection = openSocket(port_); + std::cout << "[SimEng:GDBStub] Connection to GDB client established, " + "debugging in progress\n" + << std::endl; + + char buffer[BUF_SIZE]; + + auto running = true; + + // various shenanigans to be able to handle split packets + // + // this would be a good part to rewrite + while (running) { + auto packet = ParseResult{ExpectStart, "", 0, 0, 2}; + + while (!packet.done()) { + const ssize_t bytesRead = read(connection, buffer, sizeof(buffer)); + + if (bytesRead == 0) { + std::cout << "[SimEng:GDBStub] Client disconnected (read EOF from " + "connection)." + << std::endl; + break; + } else if (bytesRead < 0) { + std::cerr + << RED + << "[SimEng:GDBStub] An error occurred while reading from the " + "connection. errno: " + << errno << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + + // safety: we've already checked whether bytesRead is less than 0, so we + // know it's positive and less than SIZE_T_MAX + auto bufferString = std::string{buffer, static_cast(bytesRead)}; + + if (verbose_) { + std::cout << CYAN << "[SimEng:GDBStub] <- Raw packet: '" << bufferString + << "' (" << bufferString.size() << ")" << RESET << std::endl; + } + + // transmission error + if (bufferString[0] == '-') { + sendResponse(lastResponse); + continue; + } + + // etx + if (bufferString[0] == '\3') { + sendResponse(encodePacket(formatSignal(SIGTRAP, {}))); + continue; + } + + if (ack_mode != Disabled) { + // '+' is an acknowledgement of successful receipt of message + // '-' is a request for retransmission + + // this is very janky + while (!bufferString.empty()) { + if (bufferString[0] == '+') { + if (verbose_) { + std::cout + << CYAN + << "[SimEng:GDBStub] <- Received message acknowledgement" + << RESET << std::endl; + } + bufferString = bufferString.substr(1); + continue; + } + break; + } + + sendResponse("+"); + + if (ack_mode == Transition) { + ack_mode = Disabled; + } + } + + // if the packet was just an acknowledgement and nothing else, + // bufferString is now empty + if (bufferString.empty()) { + continue; + } + + const auto next_packet = decodePacket(bufferString, packet); + if (next_packet) { + packet = *next_packet; + } else { + sendResponse("-"); + } + } + + if (!packet.valid()) { + if (verbose_) { + std::cerr + << RED << std::hex + << "[SimEng:GDBStub] Packet checksum does not match expected; " + "received " + << int_to_hex(packet.receivedChecksum) << ", calculated " + << int_to_hex(packet.calculatedChecksum) << std::dec << RESET + << std::endl; + } + sendResponse("-"); + continue; + } + + const auto command = packet.packet; + + if (verbose_) { + std::cout << GREEN << "[SimEng:GDBStub] <- " << command << RESET + << std::endl; + } + + if (command.size() < 1) { + sendResponse("-"); + continue; + } + + std::string rawResponse; + + // safety: we've already checked whether the size was less than 1, so + // there must be at least one character in the string + const auto commandType = command[0]; + const auto commandParams = command.substr(1); + + if (verbose_) { + std::cout << "[SimEng:GDBStub] <- Command " << commandType << ", params " + << commandParams << std::endl; + } + + // most of the basic gdb protocol is fundamentally single-character + // commands, but many of the more advanced and interesting features are not, + // so we start by looking at the first character and then parse further if + // needed + switch (command[0]) { + case '?': { + rawResponse = handleHaltReason(); + break; + } + + case 'c': { + rawResponse = handleContinue(commandParams); + break; + } + + case 'g': { + rawResponse = handleReadRegisters(); + break; + } + + case 'G': { + rawResponse = handleWriteRegisters(commandParams); + break; + } + + case 'k': { + if (verbose_) { + std::cout << CYAN + << "[SimEng:GDBStub] Received kill request from " + "client, exiting" + << RESET << std::endl; + } + running = false; + continue; + } + + case 'm': { + rawResponse = handleReadMemory(commandParams); + break; + } + + case 'M': { + rawResponse = handleWriteMemory(commandParams); + break; + } + + case 'p': { + rawResponse = handleReadRegister(commandParams); + break; + } + + case 'P': { + rawResponse = handleWriteRegister(commandParams); + break; + } + + case 'q': { + rawResponse = handleQuery(commandParams); + break; + } + + case 'Q': { + rawResponse = handleSet(commandParams); + break; + } + + case 's': { + rawResponse = handleStep(commandParams); + break; + } + + case 'z': { + rawResponse = handleRemoveBreakpoint(commandParams); + break; + } + + case 'Z': { + rawResponse = handleAddBreakpoint(commandParams); + break; + } + + default: { + // unsupported + rawResponse = ""; + break; + } + } + + sendResponse(encodePacket(rawResponse)); + } + + return iterations; +} + +// step the core until we hit a breakpoint, watchpoint, exit or have +// single-stepped +// +// pass an address as the step_from parameter to single-step - we'll tick the +// core until the program counter does not match the value of step_from +// +// pass nullopt to run until breakpoint etc. +std::string GDBStub::runUntilStop(const std::optional& step_from) { + auto core = coreInstance_.getCore(); + const auto dataMemory = coreInstance_.getDataMemory(); + + std::vector bp; + std::vector wp, rp, ap; + // split out the breakpoints list to breakpoints and the various kinds of + // watchpoint + for (const auto [type, addr, kind] : breakpoints) { + switch (type) { + case HardwareBP: + bp.push_back(addr); + break; + + case WriteWP: + wp.push_back(simeng::memory::MemoryAccessTarget{ + addr, static_cast(kind)}); + break; + + case ReadWP: + rp.push_back(simeng::memory::MemoryAccessTarget{ + addr, static_cast(kind)}); + break; + + case AccessWP: + ap.push_back(simeng::memory::MemoryAccessTarget{ + addr, static_cast(kind)}); + break; + + default: + // do nothing + break; + } + } + + // TODO: currently, must manually ensure these are reset to nullptr after exit + // + // the infrastructure for setting the core's breakpoints is a bit cursed but + // seems to work + core->prepareBreakpoints(&step_from, &bp, &wp, &rp, &ap, &syscalls_); + + while (!core->hasHalted() || dataMemory->hasPendingRequests()) { + iterations++; + + // TODO: exception to clear the pipeline on breakpoint in OoO? + + core->tick(); + dataMemory->tick(); + + /* + const auto pc = core->getProgramCounter(); + + // only check breakpoints if we're not single-stepping + if (step_from) { + if (pc != *step_from) { + core->prepareBreakpoints(); + return formatSignal(SIGTRAP, {std::make_tuple("hwbreak", "")}); + } + } else { + for (const auto [type, addr, kind] : breakpoints) { + if (type == HardwareBP) { + if (addr == pc) { + core->prepareBreakpoints(); + return formatSignal(SIGTRAP, {std::make_tuple("hwbreak", "")}); + } + } + } + } + */ + + // if nothing happened, this will be nullopt + const auto reason = core->getBreakReason(); + + if (reason) { + core->prepareBreakpoints(); + + pc_ = reason->pc; + + switch (reason->reason) { + case BreakReason::Break: + return formatSignal(SIGTRAP, {std::make_tuple("hwbreak", "")}); + + case BreakReason::Write: + return formatSignal( + SIGTRAP, {std::make_tuple("watch", int_to_hex(reason->info))}); + + case BreakReason::Read: + return formatSignal( + SIGTRAP, {std::make_tuple("rwatch", int_to_hex(reason->info))}); + + case BreakReason::Access: + return formatSignal( + SIGTRAP, {std::make_tuple("awatch", int_to_hex(reason->info))}); + + case BreakReason::SyscallEntry: + return formatSignal( + SIGTRAP, + {std::make_tuple("syscall_entry", int_to_hex(reason->info))}); + + case BreakReason::SyscallReturn: + return formatSignal( + SIGTRAP, + {std::make_tuple("syscall_return", int_to_hex(reason->info))}); + } + } + } + + // clean up the breakpoints (important!) + core->prepareBreakpoints(); + + return formatExit(core->getExitCode()); +} + +// '?' +std::string GDBStub::handleHaltReason() { + // for now, assume breakpoint + return formatSignal(SIGTRAP, {std::make_tuple("hwbreak", "")}); +} + +// 'c' +std::string GDBStub::handleContinue(const std::string& addr) { + return runUntilStop(); +} + +// 'p' +std::string GDBStub::handleReadRegister(const std::string& reg) { + const auto core = coreInstance_.getCore(); + const auto& registers = core->getArchitecturalRegisterFileSet(); + const auto& isa = core->getISA(); + + // const auto pc = core->getProgramCounter(); + const auto [vl, svl] = isa.getVectorSize(); + + int reg_num; + try { + reg_num = std::stoi(reg); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register number: " << reg + << RESET << std::endl; + } + return formatError("invalid single register number"); + } + + if ((reg_num < 0) || ((unsigned int)reg_num >= target_spec.regs.size())) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Reg num of range: " << reg_num + << RESET << std::endl; + } + return formatError("single register number out of range"); + } + + // because we generated the target spec and the target description together, + // the index gdb provides will match the target spec index + return readRegister(target_spec.regs[reg_num], registers, pc_, vl, svl); +} + +// 'g' +std::string GDBStub::handleReadRegisters() { + const auto core = coreInstance_.getCore(); + const auto& registers = core->getArchitecturalRegisterFileSet(); + const auto& isa = core->getISA(); + + // const auto pc = core->getProgramCounter(); + const auto [vl, svl] = isa.getVectorSize(); + + checkSpec(coreInstance_); + + std::string rv; + + for (const auto& reg : target_spec.regs) { + rv += readRegister(reg, registers, pc_, vl, svl); + } + + return rv; +} + +// 'P' +std::string GDBStub::handleWriteRegister( + const std::string& raw_register_value) { + auto core = coreInstance_.getCore(); + // auto& registers = core->getArchitecturalRegisterFileSet(); + const auto& isa = core->getISA(); + const auto [vl, svl] = isa.getVectorSize(); + + const auto register_value = splitBy(raw_register_value, '='); + + if (register_value.size() != 2) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a register write" + << RESET << std::endl; + } + return formatError("invalid number of parameters for register write"); + } + + int reg_num; + try { + reg_num = std::stoi(register_value[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register number: " + << register_value[0] << RESET << std::endl; + } + return formatError("invalid single register number"); + } + + if ((reg_num < 0) || ((unsigned int)reg_num >= target_spec.regs.size())) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Reg num of range: " << reg_num + << RESET << std::endl; + } + return formatError("single register number out of range"); + } + + const auto reg_size = target_spec.regs[reg_num]; + + const auto [reg, size] = reg_size; + + // special-case the registers that are a bit of a pain to deal with + switch (size) { + case PC: { + const auto value = hex_to_int_ne(register_value[1]); + if (!value) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register data" << RESET + << std::endl; + } + return formatError("single register data invalid (pc)"); + } + core->setProgramCounter(*value); + break; + } + + case VG: { + // cannot write VG + break; + } + + case SVG: { + // cannot write SVG + break; + } + + case ZA: { + std::vector regs; + std::vector vals; + + const auto value = hex_to_vect_ne<256 * 256>(register_value[1]); + if (!value) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register data" << RESET + << std::endl; + } + return formatError("single register data invalid (za)"); + } + + for (uint16_t i = 0; i < 256; i++) { + if (i < (svl / 8)) { + regs.push_back(simeng::Register{5, i}); + + vals.push_back(simeng::RegisterValue(value->data() + (i * 256), 256)); + } + } + + core->applyStateChange(simeng::arch::ProcessStateChange{ + simeng::arch::ChangeType::REPLACEMENT, regs, vals, {}, {}}); + break; + } + + default: { + const auto value = parseRegister(reg_size, register_value[1]); + if (!value) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register data" << RESET + << std::endl; + } + return formatError("single register data invalid"); + } + + core->applyStateChange(simeng::arch::ProcessStateChange{ + simeng::arch::ChangeType::REPLACEMENT, {reg}, {*value}, {}, {}}); + break; + } + } + + return "OK"; +} + +// 'G' +std::string GDBStub::handleWriteRegisters(const std::string& register_values) { + auto core = coreInstance_.getCore(); + // auto& registers = core->getArchitecturalRegisterFileSet(); + const auto& isa = core->getISA(); + + // const auto& reg_layout = simeng::config::SimInfo::getArchRegStruct(); + + const auto error = [&] { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register set write" << RESET + << std::endl; + } + return formatError("invalid register set write"); + }; + + const auto [vl, svl] = isa.getVectorSize(); + + checkSpec(coreInstance_); + + size_t string_offset = 0; + std::vector regs; + std::vector vals; + + for (const auto& reg_size : target_spec.regs) { + const auto [reg, size] = reg_size; + + switch (size) { + case PC: { + const auto value = hex_to_int_ne( + register_values.substr(string_offset, 2 * sizeof(uint64_t))); + string_offset += 2 * sizeof(uint64_t); + if (!value) { + return error(); + } + core->setProgramCounter(*value); + break; + } + + case VG: { + // cannot write VG + string_offset += 2 * sizeof(uint64_t); + break; + } + + case SVG: { + // cannot write SVG + string_offset += 2 * sizeof(uint64_t); + break; + } + + case ZA: { + const auto value = hex_to_vect_ne<256 * 256>( + register_values.substr(string_offset, 2 * 256 * 256)); + string_offset += 2 * 256 * 256; + if (!value) { + return error(); + } + + for (uint16_t i = 0; i < 256; i++) { + if (i < (svl / 8)) { + regs.push_back(simeng::Register{5, i}); + + vals.push_back( + simeng::RegisterValue(value->data() + (i * 256), 256)); + } + } + break; + } + + default: { + const auto value = parseRegister( + reg_size, + register_values.substr(string_offset, (getBitsize(size) / 8) * 2)); + string_offset += (getBitsize(size) / 8) * 2; + if (!value) { + return error(); + } + + regs.push_back(reg); + vals.push_back(*value); + break; + } + } + } + + core->applyStateChange(simeng::arch::ProcessStateChange{ + simeng::arch::ChangeType::REPLACEMENT, regs, vals, {}, {}}); + + return "OK"; +} + +// 'm' +std::string GDBStub::handleReadMemory(const std::string& raw_params) { + const auto params = splitBy(raw_params, ','); + + if (params.size() != 2) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a memory read" + << RESET << std::endl; + } + return formatError(0); + } + + unsigned long long startAddress; + unsigned long long numberOfBytes; + + try { + startAddress = std::stoull(params[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory read address invalid" + << RESET << std::endl; + } + return formatError(1); + }; + + try { + numberOfBytes = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory read length invalid" << RESET + << std::endl; + } + return formatError(2); + }; + + const auto buffer = new uint8_t[numberOfBytes]; + + if (verbose_) { + std::cout << "[SimEng:GDBStub] Reading " << numberOfBytes + << " bytes from memory address " << int_to_hex(startAddress) + << std::endl; + } + + // don't go through the access latency infrastructure + coreInstance_.getDataMemory()->rawRead(buffer, startAddress, numberOfBytes); + + std::string rv; + for (unsigned int i = 0; i < numberOfBytes; i++) { + rv += int_to_hex(buffer[i]); + } + + delete[] buffer; + + return rv; +} + +// 'M' +std::string GDBStub::handleWriteMemory(const std::string& raw_params) { + auto core = coreInstance_.getCore(); + const auto dataMemory = coreInstance_.getDataMemory(); + + const auto data = splitBy(raw_params, ':'); + + if (data.size() != 2) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a memory write" + << RESET << std::endl; + } + return formatError(0); + } + + const auto params = splitBy(data[0], ','); + + if (params.size() != 2) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a memory write" + << RESET << std::endl; + } + return formatError(1); + } + + unsigned long long startAddress; + unsigned long long numberOfBytes; + + try { + startAddress = std::stoull(params[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory write address invalid" + << RESET << std::endl; + } + return formatError(2); + }; + + try { + numberOfBytes = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory write length invalid" + << RESET << std::endl; + } + return formatError(3); + }; + + if (data[1].size() != numberOfBytes * 2) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory write data length invalid" + << RESET << std::endl; + } + return formatError(4); + } + + const auto buffer = new uint8_t[numberOfBytes]; + + try { + for (size_t i = 0; i < numberOfBytes; i++) { + buffer[i] = std::stoi(data[1].substr(i * 2, 2), nullptr, 16); + } + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory write data invalid" << RESET + << std::endl; + } + return formatError(5); + } + + if (verbose_) { + std::cout << "[SimEng:GDBStub] Writing " << numberOfBytes + << " bytes to memory address " << int_to_hex(startAddress) + << std::endl; + } + + std::vector targets; + std::vector values; + + for (unsigned long long i = 0; (unsigned)i < numberOfBytes; i += UINT16_MAX) { + const auto remaining = numberOfBytes - i; + const auto len = static_cast( + (remaining <= UINT16_MAX) ? remaining : UINT16_MAX); + dataMemory->rawWrite(startAddress + i, buffer + i, len); + } + + delete[] buffer; + + return "OK"; +} + +// 'q' +std::string GDBStub::handleQuery(const std::string& query) { + // parse out the query + // any number of any character except :, optionally followed by : and then any + // number of any character + const std::regex query_regex("^([^:]+)(?::(.*))?$"); + std::smatch query_match; + + if (regex_match(query, query_match, query_regex)) { + // safety: we know that the match succeeded and the first capture group is + // non-optional + const auto query_type = query_match[1].str(); + const auto query_params = (query_match.size() == 3) + ? std::optional{query_match[2].str()} + : std::nullopt; + + if (query_type == "Supported") { + if (query_params) { + const auto params = splitBy(*query_params, ';'); + + for (const auto& param : params) { + // here's where we would handle the features GDB supports, if we cared + // at all + if (verbose_) { + std::cout << param << std::endl; + } + } + + std::string features; + for (const auto& feature : supported_features) { + features += feature.format(); + features += ';'; + } + + if (features.back() == ';') { + features.pop_back(); + } + + return features; + } else { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] 'Supported' query requires parameters" + << RESET << std::endl; + } + return ""; + } + } else if (query_type == "Xfer") { + if (query_params) { + const auto params = splitBy(*query_params, ':'); + + const auto transfer_type = params[0]; + + if (transfer_type == "features") { + return queryFeatures(params); + } + + return "l"; + } else { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] 'Supported' query requires parameters" + << RESET << std::endl; + } + return ""; + } + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Unsupported query type '" + << query_type << "'" << RESET << std::endl; + } + return ""; + } + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid query '" << query << "'" + << RESET << std::endl; + } + return ""; + } +} + +// 'Q' +std::string GDBStub::handleSet(const std::string& set) { + // parse out the set + // any number of any character except :, optionally followed by : and then any + // number of any character + const std::regex set_regex("^([^:]+)(?::(.*))?$"); + std::smatch set_match; + + if (regex_match(set, set_match, set_regex)) { + // safety: we know that the match succeeded and the first capture group is + // non-optional + const auto set_type = set_match[1].str(); + const auto set_params = (set_match.size() == 3) + ? std::optional{set_match[2].str()} + : std::nullopt; + + if (set_type == "StartNoAckMode") { + ack_mode = Transition; + return "OK"; + } else if (set_type == "CatchSyscalls") { + if (set_params) { + const auto params = splitBy(*set_params, ';'); + + if (params.size() == 1) { + if (params[0] == "0") { + syscalls_ = std::nullopt; + } else if (params[0] == "1") { + syscalls_ = {}; + } else { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid enable option in set " + "'CatchSyscalls'" + << RESET << std::endl; + } + return ""; + } + } else { + std::vector calls; + + for (auto it = params.cbegin() + 1; it != params.cend(); it++) { + uint64_t num; + + try { + num = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid syscall number in set " + "'CatchSyscalls'" + << RESET << std::endl; + } + return ""; + }; + + calls.push_back(num); + } + + syscalls_ = calls; + } + + return "OK"; + } else { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] 'CatchSyscalls' set requires parameters" + << RESET << std::endl; + } + return ""; + } + return ""; + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Unsupported set type '" + << set_type << "'" << RESET << std::endl; + } + return ""; + } + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid set '" << set << "'" + << RESET << std::endl; + } + return ""; + } +} + +// 's' +std::string GDBStub::handleStep(const std::string& addr) { + // auto [op, size] = getCurrentInstruction(coreInstance_); + + const auto core = coreInstance_.getCore(); + // const auto pc = core->getProgramCounter(); + + // single-step from the current pc + return runUntilStop(pc_); +} + +// 'z' +std::string GDBStub::handleRemoveBreakpoint(const std::string& raw_params) { + const auto params = splitBy(raw_params, ','); + + if (params.size() != 3) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid number of parameters to a " + "breakpoint remove" + << RESET << std::endl; + } + return formatError("invalid number of parameters"); + } + + unsigned int type; + unsigned long long address; + unsigned int kind; + + try { + type = std::stoi(params[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint type invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint type"); + }; + + try { + address = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint address invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint address"); + }; + + try { + kind = std::stoi(params[2], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint kind invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint kind"); + }; + + if (type < SWStepBP) { + if (type == SoftwareBP) { + // TODO: convince GDB we really don't support software breakpoints + type = HardwareBP; + } + + bool found = false; + for (auto it = breakpoints.begin(); it < breakpoints.end(); it++) { + // if (*it == Breakpoint{static_cast(type), address, + // kind}) { + if ((it->addr == address) && (it->kind == kind) && (it->type == type)) { + breakpoints.erase(it); + + found = true; + + // avoid deleting twice + break; + } + } + + if (found) { + return "OK"; + } else { + return ""; + } + } else { + return ""; + } +} + +// 'Z' +std::string GDBStub::handleAddBreakpoint(const std::string& raw_params) { + const auto params = splitBy(raw_params, ','); + + if (params.size() != 3) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a breakpoint set" + << RESET << std::endl; + } + return formatError("invalid number of parameters"); + } + + unsigned int type; + unsigned long long address; + unsigned int kind; + + try { + type = std::stoi(params[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint type invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint type"); + }; + + try { + address = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint address invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint address"); + }; + + try { + kind = std::stoi(params[2], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint kind invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint kind"); + }; + + if (type < SWStepBP) { + if (type == SoftwareBP) { + // TODO: convince GDB we really don't support software breakpoints + type = HardwareBP; + } + + breakpoints.push_back( + Breakpoint{static_cast(type), address, kind}); + + return "OK"; + } else { + return ""; + } +} + +std::string GDBStub::queryFeatures(const std::vector& params) { + if (params.size() != 4) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Received transfer query with incorrect " + "number of parameters" + << RESET << std::endl; + } + return formatError( + "invalid number of parameters to features transfer query"); + } + + const auto& type = params[1]; + const auto& annex = params[2]; + const auto& where = splitBy(params[3], ','); + if (where.size() != 2) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Received transfer query with incorrect " + "offset/length info" + << RESET << std::endl; + } + return formatError( + "invalid offset/length info in features transfer request"); + } + int offset; + int length; + try { + offset = std::stoi(where[0], nullptr, 16); + length = std::stoi(where[1], nullptr, 16); + + // TODO: awful + if ((offset < 0) || (length < 0)) { + throw std::exception(); + } + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid offset or length parameters to " + "transfer query" + << RESET << std::endl; + } + return formatError( + "invalid offset or length parameters in transfer request"); + } + + checkSpec(coreInstance_); + + const unsigned int max_len = target_spec.spec.size() - offset; + if ((unsigned int)length > max_len) { + length = max_len; + } + + if (type != "read") { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Received unsupported non-read transfer query" + << RESET << std::endl; + } + return formatError("invalid non-read features transfer query"); + } + + if (annex == "target.xml") { + if (length > 1) { + return "m" + target_spec.spec.substr(offset, length); + } else { + return "l"; + } + } else { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Received unsupported read transfer query " + "of file '" + << annex << "'" << RESET << std::endl; + } + return formatError("invalid file for features transfer query"); + } +} + +// packets are encoded in a simple ASCII-based format so that they can be +// transferred over 7-bit connections, except for some later command types that +// just shove binary data over the connection and hope for the best +// +// parsing it kind of sucks, especially since we have to handle partial packets +// that can be resumed later +// +// see: +// https://sourceware.org/gdb/current/onlinedocs/gdb.html/Overview.html#Overview +std::optional GDBStub::decodePacket( + const std::string& encodedPacket, ParseResult result) { + for (const auto& c : encodedPacket) { + switch (result.state) { + case ExpectStart: { + switch (c) { + case '$': { + result.state = Packet; + break; + } + + case '-': + case '+': { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Unexpected acknowledgement in " + "packet (should have been handled already)" + << RESET << std::endl; + } + break; + } + + default: { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Unexpected character '" << c + << "' in packet" << RESET << std::endl; + } + break; + } + } + + break; + } + case Packet: { + switch (c) { + case '}': { + result.state = Escape; + break; + } + + case '#': { + result.state = Checksum; + + // do not add the hash to the checksum + continue; + } + + case '$': { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid character '$' in packet" + << RESET << std::endl; + } + + // return error + return std::nullopt; + } + + default: { + result.packet += c; + break; + } + } + + result.calculatedChecksum += c; + + break; + } + case Escape: { + result.packet += c ^ 0x20; + result.calculatedChecksum += c; + + result.state = Packet; + + break; + } + case Checksum: { + result.receivedChecksum <<= 4; + if ((c >= '0') && (c <= '9')) { + result.receivedChecksum |= c - '0'; + } else if ((c >= 'A') && (c <= 'F')) { + result.receivedChecksum |= c - 'A' + 10; + } else if ((c >= 'a') && (c <= 'f')) { + result.receivedChecksum |= c - 'a' + 10; + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid character '" << c + << "' in checksum" << RESET << std::endl; + } + + // return error + return std::nullopt; + } + + result.checksumRemaining--; + if (result.checksumRemaining <= 0) { + result.state = Done; + } + + break; + } + case Done: { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] More data follows after packet, ignoring" + << RESET << std::endl; + } + result.state = ExtraData; + + break; + } + + case ExtraData: { + // do nothing + break; + } + } + } + + return result; +} + +// encode a string into the same format, with run-length encoding to save on +// size (incoming packets from gdb will never use rle, and it's optional +// for outgoing packets) +std::string GDBStub::encodePacket(const std::string& response) { + uint8_t checksum = 0; + std::string rv; + + auto calcChar = [&](const auto c) { + checksum += c; + rv += c; + }; + + rv += '$'; + + auto rle_char = '*'; + + for (size_t i = 0; i < response.length(); i++) { + const auto c = response[i]; + + switch (c) { + case '#': + case '$': + case '}': + case '*': { + calcChar('}'); + calcChar(c ^ 0x20); + rle_char = '*'; + break; + } + + default: { + if (((i + 2) < response.length()) && + std::all_of(response.cbegin() + i, response.cbegin() + i + 3, + [&](const auto c) { return c == rle_char; })) { + // worth doing RLE + + uint8_t n = 3; + while (((i + n) < response.length()) && + (response[i + n] == rle_char) && (n < 97)) { + n++; + } + + if ((n == 6) || (n == 7)) { + // cannot do RLE + n = 5; + } + + calcChar('*'); + calcChar(n + 29); + rle_char = '*'; + + i += n - 1; + } else { + calcChar(c); + + // comment out this line to disable rle + rle_char = c; + } + } + } + } + + rv += '#'; + + rv += int_to_hex(checksum); + + return rv; +} + +// send a response to gdb +void GDBStub::sendResponse(const std::string& response) { + if (verbose_) { + std::cout << GREEN << "[SimEng:GDBStub] -> " << response << RESET + << std::endl; + } + + const auto bytesSent = send(connection, response.data(), response.size(), 0); + if (bytesSent < 0) { + std::cerr << RED << "[SimEng:GDBStub] Error retransmitting packet. errno: " + << errno << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + lastResponse = response; +} + +// this is a static function +int GDBStub::openSocket(const uint16_t port) { + // Create an INET stream socket, picking the protocol automatically + const auto sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + std::cerr << RED + << "[SimEng:GDBStub] Failed to create socket. errno: " << errno + << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + + sockaddr_in sockaddr; + sockaddr.sin_family = AF_INET; + sockaddr.sin_addr.s_addr = INADDR_ANY; + sockaddr.sin_port = + htons(port); // convert the native integer to network byte order + + const auto addrlen = sizeof(sockaddr); + + if (bind(sockfd, (struct sockaddr*)&sockaddr, addrlen) < 0) { + std::cerr << RED << "[SimEng:GDBStub] Failed to bind to port " << port + << ". errno: " << errno << " (" << strerror(errno) << ")" << RESET + << std::endl; + exit(EXIT_FAILURE); + } else { + std::cout << "[SimEng:GDBStub] Started listening on port " << port + << std::endl; + } + + // Start listening + if (listen(sockfd, NUM_REQUESTS) < 0) { + std::cerr << RED + << "[SimEng:GDBStub] Failed to listen on socket. errno: " + << errno << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + + // safety assumption: addrlen fits into a socklen_t + // this should always hold, since sizeof(sockaddr) is pretty small (~16) in + // all sane cases + auto realAddrLen = (socklen_t)addrlen; + + // Grab a connection from the queue + const auto connection = + accept(sockfd, (struct sockaddr*)&sockaddr, &realAddrLen); + if (connection < 0) { + std::cerr << RED << "[SimEng:GDBStub] Failed to grab connection. errno: " + << errno << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + + return connection; +} + +} // namespace simeng \ No newline at end of file diff --git a/src/lib/gdb/tinyxml2.cpp b/src/lib/gdb/tinyxml2.cpp new file mode 100644 index 0000000000..deadfb0e8c --- /dev/null +++ b/src/lib/gdb/tinyxml2.cpp @@ -0,0 +1,3027 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#include "tinyxml2.h" + +#include // yes, this one new style header, is in the Android SDK. +#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__) +# include +# include +#else +# include +# include +#endif + +#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE) + // Microsoft Visual Studio, version 2005 and higher. Not WinCE. + /*int _snprintf_s( + char *buffer, + size_t sizeOfBuffer, + size_t count, + const char *format [, + argument] ... + );*/ + static inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) + { + va_list va; + va_start( va, format ); + const int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); + va_end( va ); + return result; + } + + static inline int TIXML_VSNPRINTF( char* buffer, size_t size, const char* format, va_list va ) + { + const int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); + return result; + } + + #define TIXML_VSCPRINTF _vscprintf + #define TIXML_SSCANF sscanf_s +#elif defined _MSC_VER + // Microsoft Visual Studio 2003 and earlier or WinCE + #define TIXML_SNPRINTF _snprintf + #define TIXML_VSNPRINTF _vsnprintf + #define TIXML_SSCANF sscanf + #if (_MSC_VER < 1400 ) && (!defined WINCE) + // Microsoft Visual Studio 2003 and not WinCE. + #define TIXML_VSCPRINTF _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have. + #else + // Microsoft Visual Studio 2003 and earlier or WinCE. + static inline int TIXML_VSCPRINTF( const char* format, va_list va ) + { + int len = 512; + for (;;) { + len = len*2; + char* str = new char[len](); + const int required = _vsnprintf(str, len, format, va); + delete[] str; + if ( required != -1 ) { + TIXMLASSERT( required >= 0 ); + len = required; + break; + } + } + TIXMLASSERT( len >= 0 ); + return len; + } + #endif +#else + // GCC version 3 and higher + //#warning( "Using sn* functions." ) + #define TIXML_SNPRINTF snprintf + #define TIXML_VSNPRINTF vsnprintf + static inline int TIXML_VSCPRINTF( const char* format, va_list va ) + { + int len = vsnprintf( 0, 0, format, va ); + TIXMLASSERT( len >= 0 ); + return len; + } + #define TIXML_SSCANF sscanf +#endif + +#if defined(_WIN64) + #define TIXML_FSEEK _fseeki64 + #define TIXML_FTELL _ftelli64 +#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__CYGWIN__) + #define TIXML_FSEEK fseeko + #define TIXML_FTELL ftello +#elif defined(__ANDROID__) && __ANDROID_API__ > 24 + #define TIXML_FSEEK fseeko64 + #define TIXML_FTELL ftello64 +#else + #define TIXML_FSEEK fseek + #define TIXML_FTELL ftell +#endif + + +static const char LINE_FEED = static_cast(0x0a); // all line endings are normalized to LF +static const char LF = LINE_FEED; +static const char CARRIAGE_RETURN = static_cast(0x0d); // CR gets filtered out +static const char CR = CARRIAGE_RETURN; +static const char SINGLE_QUOTE = '\''; +static const char DOUBLE_QUOTE = '\"'; + +// Bunch of unicode info at: +// http://www.unicode.org/faq/utf_bom.html +// ef bb bf (Microsoft "lead bytes") - designates UTF-8 + +static const unsigned char TIXML_UTF_LEAD_0 = 0xefU; +static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU; +static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; + +namespace tinyxml2 +{ + +struct Entity { + const char* pattern; + int length; + char value; +}; + +static const int NUM_ENTITIES = 5; +static const Entity entities[NUM_ENTITIES] = { + { "quot", 4, DOUBLE_QUOTE }, + { "amp", 3, '&' }, + { "apos", 4, SINGLE_QUOTE }, + { "lt", 2, '<' }, + { "gt", 2, '>' } +}; + + +StrPair::~StrPair() +{ + Reset(); +} + + +void StrPair::TransferTo( StrPair* other ) +{ + if ( this == other ) { + return; + } + // This in effect implements the assignment operator by "moving" + // ownership (as in auto_ptr). + + TIXMLASSERT( other != 0 ); + TIXMLASSERT( other->_flags == 0 ); + TIXMLASSERT( other->_start == 0 ); + TIXMLASSERT( other->_end == 0 ); + + other->Reset(); + + other->_flags = _flags; + other->_start = _start; + other->_end = _end; + + _flags = 0; + _start = 0; + _end = 0; +} + + +void StrPair::Reset() +{ + if ( _flags & NEEDS_DELETE ) { + delete [] _start; + } + _flags = 0; + _start = 0; + _end = 0; +} + + +void StrPair::SetStr( const char* str, int flags ) +{ + TIXMLASSERT( str ); + Reset(); + size_t len = strlen( str ); + TIXMLASSERT( _start == 0 ); + _start = new char[ len+1 ]; + memcpy( _start, str, len+1 ); + _end = _start + len; + _flags = flags | NEEDS_DELETE; +} + + +char* StrPair::ParseText( char* p, const char* endTag, int strFlags, int* curLineNumPtr ) +{ + TIXMLASSERT( p ); + TIXMLASSERT( endTag && *endTag ); + TIXMLASSERT(curLineNumPtr); + + char* start = p; + const char endChar = *endTag; + size_t length = strlen( endTag ); + + // Inner loop of text parsing. + while ( *p ) { + if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) { + Set( start, p, strFlags ); + return p + length; + } else if (*p == '\n') { + ++(*curLineNumPtr); + } + ++p; + TIXMLASSERT( p ); + } + return 0; +} + + +char* StrPair::ParseName( char* p ) +{ + if ( !p || !(*p) ) { + return 0; + } + if ( !XMLUtil::IsNameStartChar( (unsigned char) *p ) ) { + return 0; + } + + char* const start = p; + ++p; + while ( *p && XMLUtil::IsNameChar( (unsigned char) *p ) ) { + ++p; + } + + Set( start, p, 0 ); + return p; +} + + +void StrPair::CollapseWhitespace() +{ + // Adjusting _start would cause undefined behavior on delete[] + TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 ); + // Trim leading space. + _start = XMLUtil::SkipWhiteSpace( _start, 0 ); + + if ( *_start ) { + const char* p = _start; // the read pointer + char* q = _start; // the write pointer + + while( *p ) { + if ( XMLUtil::IsWhiteSpace( *p )) { + p = XMLUtil::SkipWhiteSpace( p, 0 ); + if ( *p == 0 ) { + break; // don't write to q; this trims the trailing space. + } + *q = ' '; + ++q; + } + *q = *p; + ++q; + ++p; + } + *q = 0; + } +} + + +const char* StrPair::GetStr() +{ + TIXMLASSERT( _start ); + TIXMLASSERT( _end ); + if ( _flags & NEEDS_FLUSH ) { + *_end = 0; + _flags ^= NEEDS_FLUSH; + + if ( _flags ) { + const char* p = _start; // the read pointer + char* q = _start; // the write pointer + + while( p < _end ) { + if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) { + // CR-LF pair becomes LF + // CR alone becomes LF + // LF-CR becomes LF + if ( *(p+1) == LF ) { + p += 2; + } + else { + ++p; + } + *q = LF; + ++q; + } + else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) { + if ( *(p+1) == CR ) { + p += 2; + } + else { + ++p; + } + *q = LF; + ++q; + } + else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) { + // Entities handled by tinyXML2: + // - special entities in the entity table [in/out] + // - numeric character reference [in] + // 中 or 中 + + if ( *(p+1) == '#' ) { + const int buflen = 10; + char buf[buflen] = { 0 }; + int len = 0; + const char* adjusted = const_cast( XMLUtil::GetCharacterRef( p, buf, &len ) ); + if ( adjusted == 0 ) { + *q = *p; + ++p; + ++q; + } + else { + TIXMLASSERT( 0 <= len && len <= buflen ); + TIXMLASSERT( q + len <= adjusted ); + p = adjusted; + memcpy( q, buf, len ); + q += len; + } + } + else { + bool entityFound = false; + for( int i = 0; i < NUM_ENTITIES; ++i ) { + const Entity& entity = entities[i]; + if ( strncmp( p + 1, entity.pattern, entity.length ) == 0 + && *( p + entity.length + 1 ) == ';' ) { + // Found an entity - convert. + *q = entity.value; + ++q; + p += entity.length + 2; + entityFound = true; + break; + } + } + if ( !entityFound ) { + // fixme: treat as error? + ++p; + ++q; + } + } + } + else { + *q = *p; + ++p; + ++q; + } + } + *q = 0; + } + // The loop below has plenty going on, and this + // is a less useful mode. Break it out. + if ( _flags & NEEDS_WHITESPACE_COLLAPSING ) { + CollapseWhitespace(); + } + _flags = (_flags & NEEDS_DELETE); + } + TIXMLASSERT( _start ); + return _start; +} + + + + +// --------- XMLUtil ----------- // + +const char* XMLUtil::writeBoolTrue = "true"; +const char* XMLUtil::writeBoolFalse = "false"; + +void XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse) +{ + static const char* defTrue = "true"; + static const char* defFalse = "false"; + + writeBoolTrue = (writeTrue) ? writeTrue : defTrue; + writeBoolFalse = (writeFalse) ? writeFalse : defFalse; +} + + +const char* XMLUtil::ReadBOM( const char* p, bool* bom ) +{ + TIXMLASSERT( p ); + TIXMLASSERT( bom ); + *bom = false; + const unsigned char* pu = reinterpret_cast(p); + // Check for BOM: + if ( *(pu+0) == TIXML_UTF_LEAD_0 + && *(pu+1) == TIXML_UTF_LEAD_1 + && *(pu+2) == TIXML_UTF_LEAD_2 ) { + *bom = true; + p += 3; + } + TIXMLASSERT( p ); + return p; +} + + +void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ) +{ + const unsigned long BYTE_MASK = 0xBF; + const unsigned long BYTE_MARK = 0x80; + const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; + + if (input < 0x80) { + *length = 1; + } + else if ( input < 0x800 ) { + *length = 2; + } + else if ( input < 0x10000 ) { + *length = 3; + } + else if ( input < 0x200000 ) { + *length = 4; + } + else { + *length = 0; // This code won't convert this correctly anyway. + return; + } + + output += *length; + + // Scary scary fall throughs are annotated with carefully designed comments + // to suppress compiler warnings such as -Wimplicit-fallthrough in gcc + switch (*length) { + case 4: + --output; + *output = static_cast((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + //fall through + case 3: + --output; + *output = static_cast((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + //fall through + case 2: + --output; + *output = static_cast((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + //fall through + case 1: + --output; + *output = static_cast(input | FIRST_BYTE_MARK[*length]); + break; + default: + TIXMLASSERT( false ); + } +} + + +const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length ) +{ + // Presume an entity, and pull it out. + *length = 0; + + if ( *(p+1) == '#' && *(p+2) ) { + unsigned long ucs = 0; + TIXMLASSERT( sizeof( ucs ) >= 4 ); + ptrdiff_t delta = 0; + unsigned mult = 1; + static const char SEMICOLON = ';'; + + if ( *(p+2) == 'x' ) { + // Hexadecimal. + const char* q = p+3; + if ( !(*q) ) { + return 0; + } + + q = strchr( q, SEMICOLON ); + + if ( !q ) { + return 0; + } + TIXMLASSERT( *q == SEMICOLON ); + + delta = q-p; + --q; + + while ( *q != 'x' ) { + unsigned int digit = 0; + + if ( *q >= '0' && *q <= '9' ) { + digit = *q - '0'; + } + else if ( *q >= 'a' && *q <= 'f' ) { + digit = *q - 'a' + 10; + } + else if ( *q >= 'A' && *q <= 'F' ) { + digit = *q - 'A' + 10; + } + else { + return 0; + } + TIXMLASSERT( digit < 16 ); + TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit ); + const unsigned int digitScaled = mult * digit; + TIXMLASSERT( ucs <= ULONG_MAX - digitScaled ); + ucs += digitScaled; + TIXMLASSERT( mult <= UINT_MAX / 16 ); + mult *= 16; + --q; + } + } + else { + // Decimal. + const char* q = p+2; + if ( !(*q) ) { + return 0; + } + + q = strchr( q, SEMICOLON ); + + if ( !q ) { + return 0; + } + TIXMLASSERT( *q == SEMICOLON ); + + delta = q-p; + --q; + + while ( *q != '#' ) { + if ( *q >= '0' && *q <= '9' ) { + const unsigned int digit = *q - '0'; + TIXMLASSERT( digit < 10 ); + TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit ); + const unsigned int digitScaled = mult * digit; + TIXMLASSERT( ucs <= ULONG_MAX - digitScaled ); + ucs += digitScaled; + } + else { + return 0; + } + TIXMLASSERT( mult <= UINT_MAX / 10 ); + mult *= 10; + --q; + } + } + // convert the UCS to UTF-8 + ConvertUTF32ToUTF8( ucs, value, length ); + return p + delta + 1; + } + return p+1; +} + + +void XMLUtil::ToStr( int v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%d", v ); +} + + +void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%u", v ); +} + + +void XMLUtil::ToStr( bool v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? writeBoolTrue : writeBoolFalse); +} + +/* + ToStr() of a number is a very tricky topic. + https://github.com/leethomason/tinyxml2/issues/106 +*/ +void XMLUtil::ToStr( float v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v ); +} + + +void XMLUtil::ToStr( double v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v ); +} + + +void XMLUtil::ToStr( int64_t v, char* buffer, int bufferSize ) +{ + // horrible syntax trick to make the compiler happy about %lld + TIXML_SNPRINTF(buffer, bufferSize, "%lld", static_cast(v)); +} + +void XMLUtil::ToStr( uint64_t v, char* buffer, int bufferSize ) +{ + // horrible syntax trick to make the compiler happy about %llu + TIXML_SNPRINTF(buffer, bufferSize, "%llu", static_cast(v)); +} + +bool XMLUtil::ToInt(const char* str, int* value) +{ + if (IsPrefixHex(str)) { + unsigned v; + if (TIXML_SSCANF(str, "%x", &v) == 1) { + *value = static_cast(v); + return true; + } + } + else { + if (TIXML_SSCANF(str, "%d", value) == 1) { + return true; + } + } + return false; +} + +bool XMLUtil::ToUnsigned(const char* str, unsigned* value) +{ + if (TIXML_SSCANF(str, IsPrefixHex(str) ? "%x" : "%u", value) == 1) { + return true; + } + return false; +} + +bool XMLUtil::ToBool( const char* str, bool* value ) +{ + int ival = 0; + if ( ToInt( str, &ival )) { + *value = (ival==0) ? false : true; + return true; + } + static const char* TRUE_VALS[] = { "true", "True", "TRUE", 0 }; + static const char* FALSE_VALS[] = { "false", "False", "FALSE", 0 }; + + for (int i = 0; TRUE_VALS[i]; ++i) { + if (StringEqual(str, TRUE_VALS[i])) { + *value = true; + return true; + } + } + for (int i = 0; FALSE_VALS[i]; ++i) { + if (StringEqual(str, FALSE_VALS[i])) { + *value = false; + return true; + } + } + return false; +} + + +bool XMLUtil::ToFloat( const char* str, float* value ) +{ + if ( TIXML_SSCANF( str, "%f", value ) == 1 ) { + return true; + } + return false; +} + + +bool XMLUtil::ToDouble( const char* str, double* value ) +{ + if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) { + return true; + } + return false; +} + + +bool XMLUtil::ToInt64(const char* str, int64_t* value) +{ + if (IsPrefixHex(str)) { + unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llx + if (TIXML_SSCANF(str, "%llx", &v) == 1) { + *value = static_cast(v); + return true; + } + } + else { + long long v = 0; // horrible syntax trick to make the compiler happy about %lld + if (TIXML_SSCANF(str, "%lld", &v) == 1) { + *value = static_cast(v); + return true; + } + } + return false; +} + + +bool XMLUtil::ToUnsigned64(const char* str, uint64_t* value) { + unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llu + if(TIXML_SSCANF(str, IsPrefixHex(str) ? "%llx" : "%llu", &v) == 1) { + *value = (uint64_t)v; + return true; + } + return false; +} + + +char* XMLDocument::Identify( char* p, XMLNode** node, bool first ) +{ + TIXMLASSERT( node ); + TIXMLASSERT( p ); + char* const start = p; + int const startLine = _parseCurLineNum; + p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum ); + if( !*p ) { + *node = 0; + TIXMLASSERT( p ); + return p; + } + + // These strings define the matching patterns: + static const char* xmlHeader = { "( _commentPool ); + returnNode->_parseLineNum = _parseCurLineNum; + p += xmlHeaderLen; + } + else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) { + returnNode = CreateUnlinkedNode( _commentPool ); + returnNode->_parseLineNum = _parseCurLineNum; + p += commentHeaderLen; + } + else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) { + XMLText* text = CreateUnlinkedNode( _textPool ); + returnNode = text; + returnNode->_parseLineNum = _parseCurLineNum; + p += cdataHeaderLen; + text->SetCData( true ); + } + else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) { + returnNode = CreateUnlinkedNode( _commentPool ); + returnNode->_parseLineNum = _parseCurLineNum; + p += dtdHeaderLen; + } + else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) { + + // Preserve whitespace pedantically before closing tag, when it's immediately after opening tag + if (WhitespaceMode() == PEDANTIC_WHITESPACE && first && p != start && *(p + elementHeaderLen) == '/') { + returnNode = CreateUnlinkedNode(_textPool); + returnNode->_parseLineNum = startLine; + p = start; // Back it up, all the text counts. + _parseCurLineNum = startLine; + } + else { + returnNode = CreateUnlinkedNode(_elementPool); + returnNode->_parseLineNum = _parseCurLineNum; + p += elementHeaderLen; + } + } + else { + returnNode = CreateUnlinkedNode( _textPool ); + returnNode->_parseLineNum = _parseCurLineNum; // Report line of first non-whitespace character + p = start; // Back it up, all the text counts. + _parseCurLineNum = startLine; + } + + TIXMLASSERT( returnNode ); + TIXMLASSERT( p ); + *node = returnNode; + return p; +} + + +bool XMLDocument::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + if ( visitor->VisitEnter( *this ) ) { + for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) { + if ( !node->Accept( visitor ) ) { + break; + } + } + } + return visitor->VisitExit( *this ); +} + + +// --------- XMLNode ----------- // + +XMLNode::XMLNode( XMLDocument* doc ) : + _document( doc ), + _parent( 0 ), + _value(), + _parseLineNum( 0 ), + _firstChild( 0 ), _lastChild( 0 ), + _prev( 0 ), _next( 0 ), + _userData( 0 ), + _memPool( 0 ) +{ +} + + +XMLNode::~XMLNode() +{ + DeleteChildren(); + if ( _parent ) { + _parent->Unlink( this ); + } +} + +// ChildElementCount was originally suggested by msteiger on the sourceforge page for TinyXML and modified by KB1SPH for TinyXML-2. + +int XMLNode::ChildElementCount(const char *value) const { + int count = 0; + + const XMLElement *e = FirstChildElement(value); + + while (e) { + e = e->NextSiblingElement(value); + count++; + } + + return count; +} + +int XMLNode::ChildElementCount() const { + int count = 0; + + const XMLElement *e = FirstChildElement(); + + while (e) { + e = e->NextSiblingElement(); + count++; + } + + return count; +} + +const char* XMLNode::Value() const +{ + // Edge case: XMLDocuments don't have a Value. Return null. + if ( this->ToDocument() ) + return 0; + return _value.GetStr(); +} + +void XMLNode::SetValue( const char* str, bool staticMem ) +{ + if ( staticMem ) { + _value.SetInternedStr( str ); + } + else { + _value.SetStr( str ); + } +} + +XMLNode* XMLNode::DeepClone(XMLDocument* target) const +{ + XMLNode* clone = this->ShallowClone(target); + if (!clone) return 0; + + for (const XMLNode* child = this->FirstChild(); child; child = child->NextSibling()) { + XMLNode* childClone = child->DeepClone(target); + TIXMLASSERT(childClone); + clone->InsertEndChild(childClone); + } + return clone; +} + +void XMLNode::DeleteChildren() +{ + while( _firstChild ) { + TIXMLASSERT( _lastChild ); + DeleteChild( _firstChild ); + } + _firstChild = _lastChild = 0; +} + + +void XMLNode::Unlink( XMLNode* child ) +{ + TIXMLASSERT( child ); + TIXMLASSERT( child->_document == _document ); + TIXMLASSERT( child->_parent == this ); + if ( child == _firstChild ) { + _firstChild = _firstChild->_next; + } + if ( child == _lastChild ) { + _lastChild = _lastChild->_prev; + } + + if ( child->_prev ) { + child->_prev->_next = child->_next; + } + if ( child->_next ) { + child->_next->_prev = child->_prev; + } + child->_next = 0; + child->_prev = 0; + child->_parent = 0; +} + + +void XMLNode::DeleteChild( XMLNode* node ) +{ + TIXMLASSERT( node ); + TIXMLASSERT( node->_document == _document ); + TIXMLASSERT( node->_parent == this ); + Unlink( node ); + TIXMLASSERT(node->_prev == 0); + TIXMLASSERT(node->_next == 0); + TIXMLASSERT(node->_parent == 0); + DeleteNode( node ); +} + + +XMLNode* XMLNode::InsertEndChild( XMLNode* addThis ) +{ + TIXMLASSERT( addThis ); + if ( addThis->_document != _document ) { + TIXMLASSERT( false ); + return 0; + } + InsertChildPreamble( addThis ); + + if ( _lastChild ) { + TIXMLASSERT( _firstChild ); + TIXMLASSERT( _lastChild->_next == 0 ); + _lastChild->_next = addThis; + addThis->_prev = _lastChild; + _lastChild = addThis; + + addThis->_next = 0; + } + else { + TIXMLASSERT( _firstChild == 0 ); + _firstChild = _lastChild = addThis; + + addThis->_prev = 0; + addThis->_next = 0; + } + addThis->_parent = this; + return addThis; +} + + +XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis ) +{ + TIXMLASSERT( addThis ); + if ( addThis->_document != _document ) { + TIXMLASSERT( false ); + return 0; + } + InsertChildPreamble( addThis ); + + if ( _firstChild ) { + TIXMLASSERT( _lastChild ); + TIXMLASSERT( _firstChild->_prev == 0 ); + + _firstChild->_prev = addThis; + addThis->_next = _firstChild; + _firstChild = addThis; + + addThis->_prev = 0; + } + else { + TIXMLASSERT( _lastChild == 0 ); + _firstChild = _lastChild = addThis; + + addThis->_prev = 0; + addThis->_next = 0; + } + addThis->_parent = this; + return addThis; +} + + +XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ) +{ + TIXMLASSERT( addThis ); + if ( addThis->_document != _document ) { + TIXMLASSERT( false ); + return 0; + } + + TIXMLASSERT( afterThis ); + + if ( afterThis->_parent != this ) { + TIXMLASSERT( false ); + return 0; + } + if ( afterThis == addThis ) { + // Current state: BeforeThis -> AddThis -> OneAfterAddThis + // Now AddThis must disappear from it's location and then + // reappear between BeforeThis and OneAfterAddThis. + // So just leave it where it is. + return addThis; + } + + if ( afterThis->_next == 0 ) { + // The last node or the only node. + return InsertEndChild( addThis ); + } + InsertChildPreamble( addThis ); + addThis->_prev = afterThis; + addThis->_next = afterThis->_next; + afterThis->_next->_prev = addThis; + afterThis->_next = addThis; + addThis->_parent = this; + return addThis; +} + + + + +const XMLElement* XMLNode::FirstChildElement( const char* name ) const +{ + for( const XMLNode* node = _firstChild; node; node = node->_next ) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { + return element; + } + } + return 0; +} + + +const XMLElement* XMLNode::LastChildElement( const char* name ) const +{ + for( const XMLNode* node = _lastChild; node; node = node->_prev ) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { + return element; + } + } + return 0; +} + + +const XMLElement* XMLNode::NextSiblingElement( const char* name ) const +{ + for( const XMLNode* node = _next; node; node = node->_next ) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { + return element; + } + } + return 0; +} + + +const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const +{ + for( const XMLNode* node = _prev; node; node = node->_prev ) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { + return element; + } + } + return 0; +} + + +char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) +{ + // This is a recursive method, but thinking about it "at the current level" + // it is a pretty simple flat list: + // + // + // + // With a special case: + // + // + // + // + // Where the closing element (/foo) *must* be the next thing after the opening + // element, and the names must match. BUT the tricky bit is that the closing + // element will be read by the child. + // + // 'endTag' is the end tag for this node, it is returned by a call to a child. + // 'parentEnd' is the end tag for the parent, which is filled in and returned. + + XMLDocument::DepthTracker tracker(_document); + if (_document->Error()) + return 0; + + bool first = true; + while( p && *p ) { + XMLNode* node = 0; + + p = _document->Identify( p, &node, first ); + TIXMLASSERT( p ); + if ( node == 0 ) { + break; + } + first = false; + + const int initialLineNum = node->_parseLineNum; + + StrPair endTag; + p = node->ParseDeep( p, &endTag, curLineNumPtr ); + if ( !p ) { + _document->DeleteNode( node ); + if ( !_document->Error() ) { + _document->SetError( XML_ERROR_PARSING, initialLineNum, 0); + } + break; + } + + const XMLDeclaration* const decl = node->ToDeclaration(); + if ( decl ) { + // Declarations are only allowed at document level + // + // Multiple declarations are allowed but all declarations + // must occur before anything else. + // + // Optimized due to a security test case. If the first node is + // a declaration, and the last node is a declaration, then only + // declarations have so far been added. + bool wellLocated = false; + + if (ToDocument()) { + if (FirstChild()) { + wellLocated = + FirstChild() && + FirstChild()->ToDeclaration() && + LastChild() && + LastChild()->ToDeclaration(); + } + else { + wellLocated = true; + } + } + if ( !wellLocated ) { + _document->SetError( XML_ERROR_PARSING_DECLARATION, initialLineNum, "XMLDeclaration value=%s", decl->Value()); + _document->DeleteNode( node ); + break; + } + } + + XMLElement* ele = node->ToElement(); + if ( ele ) { + // We read the end tag. Return it to the parent. + if ( ele->ClosingType() == XMLElement::CLOSING ) { + if ( parentEndTag ) { + ele->_value.TransferTo( parentEndTag ); + } + node->_memPool->SetTracked(); // created and then immediately deleted. + DeleteNode( node ); + return p; + } + + // Handle an end tag returned to this level. + // And handle a bunch of annoying errors. + bool mismatch = false; + if ( endTag.Empty() ) { + if ( ele->ClosingType() == XMLElement::OPEN ) { + mismatch = true; + } + } + else { + if ( ele->ClosingType() != XMLElement::OPEN ) { + mismatch = true; + } + else if ( !XMLUtil::StringEqual( endTag.GetStr(), ele->Name() ) ) { + mismatch = true; + } + } + if ( mismatch ) { + _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, initialLineNum, "XMLElement name=%s", ele->Name()); + _document->DeleteNode( node ); + break; + } + } + InsertEndChild( node ); + } + return 0; +} + +/*static*/ void XMLNode::DeleteNode( XMLNode* node ) +{ + if ( node == 0 ) { + return; + } + TIXMLASSERT(node->_document); + if (!node->ToDocument()) { + node->_document->MarkInUse(node); + } + + MemPool* pool = node->_memPool; + node->~XMLNode(); + pool->Free( node ); +} + +void XMLNode::InsertChildPreamble( XMLNode* insertThis ) const +{ + TIXMLASSERT( insertThis ); + TIXMLASSERT( insertThis->_document == _document ); + + if (insertThis->_parent) { + insertThis->_parent->Unlink( insertThis ); + } + else { + insertThis->_document->MarkInUse(insertThis); + insertThis->_memPool->SetTracked(); + } +} + +const XMLElement* XMLNode::ToElementWithName( const char* name ) const +{ + const XMLElement* element = this->ToElement(); + if ( element == 0 ) { + return 0; + } + if ( name == 0 ) { + return element; + } + if ( XMLUtil::StringEqual( element->Name(), name ) ) { + return element; + } + return 0; +} + +// --------- XMLText ---------- // +char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) +{ + if ( this->CData() ) { + p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_CDATA, _parseLineNum, 0 ); + } + return p; + } + else { + int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES; + if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) { + flags |= StrPair::NEEDS_WHITESPACE_COLLAPSING; + } + + p = _value.ParseText( p, "<", flags, curLineNumPtr ); + if ( p && *p ) { + return p-1; + } + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_TEXT, _parseLineNum, 0 ); + } + } + return 0; +} + + +XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern? + text->SetCData( this->CData() ); + return text; +} + + +bool XMLText::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLText* text = compare->ToText(); + return ( text && XMLUtil::StringEqual( text->Value(), Value() ) ); +} + + +bool XMLText::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + return visitor->Visit( *this ); +} + + +// --------- XMLComment ---------- // + +XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLComment::~XMLComment() +{ +} + + +char* XMLComment::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) +{ + // Comment parses as text. + p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNumPtr ); + if ( p == 0 ) { + _document->SetError( XML_ERROR_PARSING_COMMENT, _parseLineNum, 0 ); + } + return p; +} + + +XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern? + return comment; +} + + +bool XMLComment::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLComment* comment = compare->ToComment(); + return ( comment && XMLUtil::StringEqual( comment->Value(), Value() )); +} + + +bool XMLComment::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + return visitor->Visit( *this ); +} + + +// --------- XMLDeclaration ---------- // + +XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLDeclaration::~XMLDeclaration() +{ + //printf( "~XMLDeclaration\n" ); +} + + +char* XMLDeclaration::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) +{ + // Declaration parses as text. + p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); + if ( p == 0 ) { + _document->SetError( XML_ERROR_PARSING_DECLARATION, _parseLineNum, 0 ); + } + return p; +} + + +XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern? + return dec; +} + + +bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLDeclaration* declaration = compare->ToDeclaration(); + return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() )); +} + + + +bool XMLDeclaration::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + return visitor->Visit( *this ); +} + +// --------- XMLUnknown ---------- // + +XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLUnknown::~XMLUnknown() +{ +} + + +char* XMLUnknown::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) +{ + // Unknown parses as text. + p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_UNKNOWN, _parseLineNum, 0 ); + } + return p; +} + + +XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern? + return text; +} + + +bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLUnknown* unknown = compare->ToUnknown(); + return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() )); +} + + +bool XMLUnknown::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + return visitor->Visit( *this ); +} + +// --------- XMLAttribute ---------- // + +const char* XMLAttribute::Name() const +{ + return _name.GetStr(); +} + +const char* XMLAttribute::Value() const +{ + return _value.GetStr(); +} + +char* XMLAttribute::ParseDeep( char* p, bool processEntities, int* curLineNumPtr ) +{ + // Parse using the name rules: bug fix, was using ParseText before + p = _name.ParseName( p ); + if ( !p || !*p ) { + return 0; + } + + // Skip white space before = + p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); + if ( *p != '=' ) { + return 0; + } + + ++p; // move up to opening quote + p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); + if ( *p != '\"' && *p != '\'' ) { + return 0; + } + + const char endTag[2] = { *p, 0 }; + ++p; // move past opening quote + + p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES, curLineNumPtr ); + return p; +} + + +void XMLAttribute::SetName( const char* n ) +{ + _name.SetStr( n ); +} + + +XMLError XMLAttribute::QueryIntValue( int* value ) const +{ + if ( XMLUtil::ToInt( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const +{ + if ( XMLUtil::ToUnsigned( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryInt64Value(int64_t* value) const +{ + if (XMLUtil::ToInt64(Value(), value)) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryUnsigned64Value(uint64_t* value) const +{ + if(XMLUtil::ToUnsigned64(Value(), value)) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryBoolValue( bool* value ) const +{ + if ( XMLUtil::ToBool( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryFloatValue( float* value ) const +{ + if ( XMLUtil::ToFloat( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryDoubleValue( double* value ) const +{ + if ( XMLUtil::ToDouble( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +void XMLAttribute::SetAttribute( const char* v ) +{ + _value.SetStr( v ); +} + + +void XMLAttribute::SetAttribute( int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +void XMLAttribute::SetAttribute( unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +void XMLAttribute::SetAttribute(int64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + _value.SetStr(buf); +} + +void XMLAttribute::SetAttribute(uint64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + _value.SetStr(buf); +} + + +void XMLAttribute::SetAttribute( bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + +void XMLAttribute::SetAttribute( double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + +void XMLAttribute::SetAttribute( float v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +// --------- XMLElement ---------- // +XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), + _closingType( OPEN ), + _rootAttribute( 0 ) +{ +} + + +XMLElement::~XMLElement() +{ + while( _rootAttribute ) { + XMLAttribute* next = _rootAttribute->_next; + DeleteAttribute( _rootAttribute ); + _rootAttribute = next; + } +} + + +const XMLAttribute* XMLElement::FindAttribute( const char* name ) const +{ + for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) { + if ( XMLUtil::StringEqual( a->Name(), name ) ) { + return a; + } + } + return 0; +} + + +const char* XMLElement::Attribute( const char* name, const char* value ) const +{ + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return 0; + } + if ( !value || XMLUtil::StringEqual( a->Value(), value )) { + return a->Value(); + } + return 0; +} + +int XMLElement::IntAttribute(const char* name, int defaultValue) const +{ + int i = defaultValue; + QueryIntAttribute(name, &i); + return i; +} + +unsigned XMLElement::UnsignedAttribute(const char* name, unsigned defaultValue) const +{ + unsigned i = defaultValue; + QueryUnsignedAttribute(name, &i); + return i; +} + +int64_t XMLElement::Int64Attribute(const char* name, int64_t defaultValue) const +{ + int64_t i = defaultValue; + QueryInt64Attribute(name, &i); + return i; +} + +uint64_t XMLElement::Unsigned64Attribute(const char* name, uint64_t defaultValue) const +{ + uint64_t i = defaultValue; + QueryUnsigned64Attribute(name, &i); + return i; +} + +bool XMLElement::BoolAttribute(const char* name, bool defaultValue) const +{ + bool b = defaultValue; + QueryBoolAttribute(name, &b); + return b; +} + +double XMLElement::DoubleAttribute(const char* name, double defaultValue) const +{ + double d = defaultValue; + QueryDoubleAttribute(name, &d); + return d; +} + +float XMLElement::FloatAttribute(const char* name, float defaultValue) const +{ + float f = defaultValue; + QueryFloatAttribute(name, &f); + return f; +} + +const char* XMLElement::GetText() const +{ + /* skip comment node */ + const XMLNode* node = FirstChild(); + while (node) { + if (node->ToComment()) { + node = node->NextSibling(); + continue; + } + break; + } + + if ( node && node->ToText() ) { + return node->Value(); + } + return 0; +} + + +void XMLElement::SetText( const char* inText ) +{ + if ( FirstChild() && FirstChild()->ToText() ) + FirstChild()->SetValue( inText ); + else { + XMLText* theText = GetDocument()->NewText( inText ); + InsertFirstChild( theText ); + } +} + + +void XMLElement::SetText( int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText(int64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + SetText(buf); +} + +void XMLElement::SetText(uint64_t v) { + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + SetText(buf); +} + + +void XMLElement::SetText( bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( float v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +XMLError XMLElement::QueryIntText( int* ival ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToInt( t, ival ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToUnsigned( t, uval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryInt64Text(int64_t* ival) const +{ + if (FirstChild() && FirstChild()->ToText()) { + const char* t = FirstChild()->Value(); + if (XMLUtil::ToInt64(t, ival)) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryUnsigned64Text(uint64_t* uval) const +{ + if(FirstChild() && FirstChild()->ToText()) { + const char* t = FirstChild()->Value(); + if(XMLUtil::ToUnsigned64(t, uval)) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryBoolText( bool* bval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToBool( t, bval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryDoubleText( double* dval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToDouble( t, dval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryFloatText( float* fval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToFloat( t, fval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + +int XMLElement::IntText(int defaultValue) const +{ + int i = defaultValue; + QueryIntText(&i); + return i; +} + +unsigned XMLElement::UnsignedText(unsigned defaultValue) const +{ + unsigned i = defaultValue; + QueryUnsignedText(&i); + return i; +} + +int64_t XMLElement::Int64Text(int64_t defaultValue) const +{ + int64_t i = defaultValue; + QueryInt64Text(&i); + return i; +} + +uint64_t XMLElement::Unsigned64Text(uint64_t defaultValue) const +{ + uint64_t i = defaultValue; + QueryUnsigned64Text(&i); + return i; +} + +bool XMLElement::BoolText(bool defaultValue) const +{ + bool b = defaultValue; + QueryBoolText(&b); + return b; +} + +double XMLElement::DoubleText(double defaultValue) const +{ + double d = defaultValue; + QueryDoubleText(&d); + return d; +} + +float XMLElement::FloatText(float defaultValue) const +{ + float f = defaultValue; + QueryFloatText(&f); + return f; +} + + +XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) +{ + XMLAttribute* last = 0; + XMLAttribute* attrib = 0; + for( attrib = _rootAttribute; + attrib; + last = attrib, attrib = attrib->_next ) { + if ( XMLUtil::StringEqual( attrib->Name(), name ) ) { + break; + } + } + if ( !attrib ) { + attrib = CreateAttribute(); + TIXMLASSERT( attrib ); + if ( last ) { + TIXMLASSERT( last->_next == 0 ); + last->_next = attrib; + } + else { + TIXMLASSERT( _rootAttribute == 0 ); + _rootAttribute = attrib; + } + attrib->SetName( name ); + } + return attrib; +} + + +void XMLElement::DeleteAttribute( const char* name ) +{ + XMLAttribute* prev = 0; + for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) { + if ( XMLUtil::StringEqual( name, a->Name() ) ) { + if ( prev ) { + prev->_next = a->_next; + } + else { + _rootAttribute = a->_next; + } + DeleteAttribute( a ); + break; + } + prev = a; + } +} + + +char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr ) +{ + XMLAttribute* prevAttribute = 0; + + // Read the attributes. + while( p ) { + p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); + if ( !(*p) ) { + _document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, "XMLElement name=%s", Name() ); + return 0; + } + + // attribute. + if (XMLUtil::IsNameStartChar( (unsigned char) *p ) ) { + XMLAttribute* attrib = CreateAttribute(); + TIXMLASSERT( attrib ); + attrib->_parseLineNum = _document->_parseCurLineNum; + + const int attrLineNum = attrib->_parseLineNum; + + p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNumPtr ); + if ( !p || Attribute( attrib->Name() ) ) { + DeleteAttribute( attrib ); + _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, attrLineNum, "XMLElement name=%s", Name() ); + return 0; + } + // There is a minor bug here: if the attribute in the source xml + // document is duplicated, it will not be detected and the + // attribute will be doubly added. However, tracking the 'prevAttribute' + // avoids re-scanning the attribute list. Preferring performance for + // now, may reconsider in the future. + if ( prevAttribute ) { + TIXMLASSERT( prevAttribute->_next == 0 ); + prevAttribute->_next = attrib; + } + else { + TIXMLASSERT( _rootAttribute == 0 ); + _rootAttribute = attrib; + } + prevAttribute = attrib; + } + // end of the tag + else if ( *p == '>' ) { + ++p; + break; + } + // end of the tag + else if ( *p == '/' && *(p+1) == '>' ) { + _closingType = CLOSED; + return p+2; // done; sealed element. + } + else { + _document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, 0 ); + return 0; + } + } + return p; +} + +void XMLElement::DeleteAttribute( XMLAttribute* attribute ) +{ + if ( attribute == 0 ) { + return; + } + MemPool* pool = attribute->_memPool; + attribute->~XMLAttribute(); + pool->Free( attribute ); +} + +XMLAttribute* XMLElement::CreateAttribute() +{ + TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() ); + XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); + TIXMLASSERT( attrib ); + attrib->_memPool = &_document->_attributePool; + attrib->_memPool->SetTracked(); + return attrib; +} + + +XMLElement* XMLElement::InsertNewChildElement(const char* name) +{ + XMLElement* node = _document->NewElement(name); + return InsertEndChild(node) ? node : 0; +} + +XMLComment* XMLElement::InsertNewComment(const char* comment) +{ + XMLComment* node = _document->NewComment(comment); + return InsertEndChild(node) ? node : 0; +} + +XMLText* XMLElement::InsertNewText(const char* text) +{ + XMLText* node = _document->NewText(text); + return InsertEndChild(node) ? node : 0; +} + +XMLDeclaration* XMLElement::InsertNewDeclaration(const char* text) +{ + XMLDeclaration* node = _document->NewDeclaration(text); + return InsertEndChild(node) ? node : 0; +} + +XMLUnknown* XMLElement::InsertNewUnknown(const char* text) +{ + XMLUnknown* node = _document->NewUnknown(text); + return InsertEndChild(node) ? node : 0; +} + + + +// +// +// foobar +// +char* XMLElement::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) +{ + // Read the element name. + p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); + + // The closing element is the form. It is + // parsed just like a regular element then deleted from + // the DOM. + if ( *p == '/' ) { + _closingType = CLOSING; + ++p; + } + + p = _value.ParseName( p ); + if ( _value.Empty() ) { + return 0; + } + + p = ParseAttributes( p, curLineNumPtr ); + if ( !p || !*p || _closingType != OPEN ) { + return p; + } + + p = XMLNode::ParseDeep( p, parentEndTag, curLineNumPtr ); + return p; +} + + + +XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern? + for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) { + element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern? + } + return element; +} + + +bool XMLElement::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLElement* other = compare->ToElement(); + if ( other && XMLUtil::StringEqual( other->Name(), Name() )) { + + const XMLAttribute* a=FirstAttribute(); + const XMLAttribute* b=other->FirstAttribute(); + + while ( a && b ) { + if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) { + return false; + } + a = a->Next(); + b = b->Next(); + } + if ( a || b ) { + // different count + return false; + } + return true; + } + return false; +} + + +bool XMLElement::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + if ( visitor->VisitEnter( *this, _rootAttribute ) ) { + for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) { + if ( !node->Accept( visitor ) ) { + break; + } + } + } + return visitor->VisitExit( *this ); +} + + +// --------- XMLDocument ----------- // + +// Warning: List must match 'enum XMLError' +const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = { + "XML_SUCCESS", + "XML_NO_ATTRIBUTE", + "XML_WRONG_ATTRIBUTE_TYPE", + "XML_ERROR_FILE_NOT_FOUND", + "XML_ERROR_FILE_COULD_NOT_BE_OPENED", + "XML_ERROR_FILE_READ_ERROR", + "XML_ERROR_PARSING_ELEMENT", + "XML_ERROR_PARSING_ATTRIBUTE", + "XML_ERROR_PARSING_TEXT", + "XML_ERROR_PARSING_CDATA", + "XML_ERROR_PARSING_COMMENT", + "XML_ERROR_PARSING_DECLARATION", + "XML_ERROR_PARSING_UNKNOWN", + "XML_ERROR_EMPTY_DOCUMENT", + "XML_ERROR_MISMATCHED_ELEMENT", + "XML_ERROR_PARSING", + "XML_CAN_NOT_CONVERT_TEXT", + "XML_NO_TEXT_NODE", + "XML_ELEMENT_DEPTH_EXCEEDED" +}; + + +XMLDocument::XMLDocument( bool processEntities, Whitespace whitespaceMode ) : + XMLNode( 0 ), + _writeBOM( false ), + _processEntities( processEntities ), + _errorID(XML_SUCCESS), + _whitespaceMode( whitespaceMode ), + _errorStr(), + _errorLineNum( 0 ), + _charBuffer( 0 ), + _parseCurLineNum( 0 ), + _parsingDepth(0), + _unlinked(), + _elementPool(), + _attributePool(), + _textPool(), + _commentPool() +{ + // avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+) + _document = this; +} + + +XMLDocument::~XMLDocument() +{ + Clear(); +} + + +void XMLDocument::MarkInUse(const XMLNode* const node) +{ + TIXMLASSERT(node); + TIXMLASSERT(node->_parent == 0); + + for (int i = 0; i < _unlinked.Size(); ++i) { + if (node == _unlinked[i]) { + _unlinked.SwapRemove(i); + break; + } + } +} + +void XMLDocument::Clear() +{ + DeleteChildren(); + while( _unlinked.Size()) { + DeleteNode(_unlinked[0]); // Will remove from _unlinked as part of delete. + } + +#ifdef TINYXML2_DEBUG + const bool hadError = Error(); +#endif + ClearError(); + + delete [] _charBuffer; + _charBuffer = 0; + _parsingDepth = 0; + +#if 0 + _textPool.Trace( "text" ); + _elementPool.Trace( "element" ); + _commentPool.Trace( "comment" ); + _attributePool.Trace( "attribute" ); +#endif + +#ifdef TINYXML2_DEBUG + if ( !hadError ) { + TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() ); + TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() ); + TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() ); + TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() ); + } +#endif +} + + +void XMLDocument::DeepCopy(XMLDocument* target) const +{ + TIXMLASSERT(target); + if (target == this) { + return; // technically success - a no-op. + } + + target->Clear(); + for (const XMLNode* node = this->FirstChild(); node; node = node->NextSibling()) { + target->InsertEndChild(node->DeepClone(target)); + } +} + +XMLElement* XMLDocument::NewElement( const char* name ) +{ + XMLElement* ele = CreateUnlinkedNode( _elementPool ); + ele->SetName( name ); + return ele; +} + + +XMLComment* XMLDocument::NewComment( const char* str ) +{ + XMLComment* comment = CreateUnlinkedNode( _commentPool ); + comment->SetValue( str ); + return comment; +} + + +XMLText* XMLDocument::NewText( const char* str ) +{ + XMLText* text = CreateUnlinkedNode( _textPool ); + text->SetValue( str ); + return text; +} + + +XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) +{ + XMLDeclaration* dec = CreateUnlinkedNode( _commentPool ); + dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" ); + return dec; +} + + +XMLUnknown* XMLDocument::NewUnknown( const char* str ) +{ + XMLUnknown* unk = CreateUnlinkedNode( _commentPool ); + unk->SetValue( str ); + return unk; +} + +static FILE* callfopen( const char* filepath, const char* mode ) +{ + TIXMLASSERT( filepath ); + TIXMLASSERT( mode ); +#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE) + FILE* fp = 0; + const errno_t err = fopen_s( &fp, filepath, mode ); + if ( err ) { + return 0; + } +#else + FILE* fp = fopen( filepath, mode ); +#endif + return fp; +} + +void XMLDocument::DeleteNode( XMLNode* node ) { + TIXMLASSERT( node ); + TIXMLASSERT(node->_document == this ); + if (node->_parent) { + node->_parent->DeleteChild( node ); + } + else { + // Isn't in the tree. + // Use the parent delete. + // Also, we need to mark it tracked: we 'know' + // it was never used. + node->_memPool->SetTracked(); + // Call the static XMLNode version: + XMLNode::DeleteNode(node); + } +} + + +XMLError XMLDocument::LoadFile( const char* filename ) +{ + if ( !filename ) { + TIXMLASSERT( false ); + SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=" ); + return _errorID; + } + + Clear(); + FILE* fp = callfopen( filename, "rb" ); + if ( !fp ) { + SetError( XML_ERROR_FILE_NOT_FOUND, 0, "filename=%s", filename ); + return _errorID; + } + LoadFile( fp ); + fclose( fp ); + return _errorID; +} + +XMLError XMLDocument::LoadFile( FILE* fp ) +{ + Clear(); + + TIXML_FSEEK( fp, 0, SEEK_SET ); + if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) { + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + + TIXML_FSEEK( fp, 0, SEEK_END ); + + unsigned long long filelength; + { + const long long fileLengthSigned = TIXML_FTELL( fp ); + TIXML_FSEEK( fp, 0, SEEK_SET ); + if ( fileLengthSigned == -1L ) { + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + TIXMLASSERT( fileLengthSigned >= 0 ); + filelength = static_cast(fileLengthSigned); + } + + const size_t maxSizeT = static_cast(-1); + // We'll do the comparison as an unsigned long long, because that's guaranteed to be at + // least 8 bytes, even on a 32-bit platform. + if ( filelength >= static_cast(maxSizeT) ) { + // Cannot handle files which won't fit in buffer together with null terminator + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + + if ( filelength == 0 ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return _errorID; + } + + const size_t size = static_cast(filelength); + TIXMLASSERT( _charBuffer == 0 ); + _charBuffer = new char[size+1]; + const size_t read = fread( _charBuffer, 1, size, fp ); + if ( read != size ) { + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + + _charBuffer[size] = 0; + + Parse(); + return _errorID; +} + + +XMLError XMLDocument::SaveFile( const char* filename, bool compact ) +{ + if ( !filename ) { + TIXMLASSERT( false ); + SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=" ); + return _errorID; + } + + FILE* fp = callfopen( filename, "w" ); + if ( !fp ) { + SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=%s", filename ); + return _errorID; + } + SaveFile(fp, compact); + fclose( fp ); + return _errorID; +} + + +XMLError XMLDocument::SaveFile( FILE* fp, bool compact ) +{ + // Clear any error from the last save, otherwise it will get reported + // for *this* call. + ClearError(); + XMLPrinter stream( fp, compact ); + Print( &stream ); + return _errorID; +} + + +XMLError XMLDocument::Parse( const char* xml, size_t nBytes ) +{ + Clear(); + + if ( nBytes == 0 || !xml || !*xml ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return _errorID; + } + if ( nBytes == static_cast(-1) ) { + nBytes = strlen( xml ); + } + TIXMLASSERT( _charBuffer == 0 ); + _charBuffer = new char[ nBytes+1 ]; + memcpy( _charBuffer, xml, nBytes ); + _charBuffer[nBytes] = 0; + + Parse(); + if ( Error() ) { + // clean up now essentially dangling memory. + // and the parse fail can put objects in the + // pools that are dead and inaccessible. + DeleteChildren(); + _elementPool.Clear(); + _attributePool.Clear(); + _textPool.Clear(); + _commentPool.Clear(); + } + return _errorID; +} + + +void XMLDocument::Print( XMLPrinter* streamer ) const +{ + if ( streamer ) { + Accept( streamer ); + } + else { + XMLPrinter stdoutStreamer( stdout ); + Accept( &stdoutStreamer ); + } +} + + +void XMLDocument::ClearError() { + _errorID = XML_SUCCESS; + _errorLineNum = 0; + _errorStr.Reset(); +} + + +void XMLDocument::SetError( XMLError error, int lineNum, const char* format, ... ) +{ + TIXMLASSERT(error >= 0 && error < XML_ERROR_COUNT); + _errorID = error; + _errorLineNum = lineNum; + _errorStr.Reset(); + + const size_t BUFFER_SIZE = 1000; + char* buffer = new char[BUFFER_SIZE]; + + TIXMLASSERT(sizeof(error) <= sizeof(int)); + TIXML_SNPRINTF(buffer, BUFFER_SIZE, "Error=%s ErrorID=%d (0x%x) Line number=%d", + ErrorIDToName(error), static_cast(error), static_cast(error), lineNum); + + if (format) { + size_t len = strlen(buffer); + TIXML_SNPRINTF(buffer + len, BUFFER_SIZE - len, ": "); + len = strlen(buffer); + + va_list va; + va_start(va, format); + TIXML_VSNPRINTF(buffer + len, BUFFER_SIZE - len, format, va); + va_end(va); + } + _errorStr.SetStr(buffer); + delete[] buffer; +} + + +/*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID) +{ + TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT ); + const char* errorName = _errorNames[errorID]; + TIXMLASSERT( errorName && errorName[0] ); + return errorName; +} + +const char* XMLDocument::ErrorStr() const +{ + return _errorStr.Empty() ? "" : _errorStr.GetStr(); +} + + +void XMLDocument::PrintError() const +{ + printf("%s\n", ErrorStr()); +} + +const char* XMLDocument::ErrorName() const +{ + return ErrorIDToName(_errorID); +} + +void XMLDocument::Parse() +{ + TIXMLASSERT( NoChildren() ); // Clear() must have been called previously + TIXMLASSERT( _charBuffer ); + _parseCurLineNum = 1; + _parseLineNum = 1; + char* p = _charBuffer; + p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum ); + p = const_cast( XMLUtil::ReadBOM( p, &_writeBOM ) ); + if ( !*p ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return; + } + ParseDeep(p, 0, &_parseCurLineNum ); +} + +void XMLDocument::PushDepth() +{ + _parsingDepth++; + if (_parsingDepth == TINYXML2_MAX_ELEMENT_DEPTH) { + SetError(XML_ELEMENT_DEPTH_EXCEEDED, _parseCurLineNum, "Element nesting is too deep." ); + } +} + +void XMLDocument::PopDepth() +{ + TIXMLASSERT(_parsingDepth > 0); + --_parsingDepth; +} + +XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) : + _elementJustOpened( false ), + _stack(), + _firstElement( true ), + _fp( file ), + _depth( depth ), + _textDepth( -1 ), + _processEntities( true ), + _compactMode( compact ), + _buffer() +{ + for( int i=0; i(entityValue); + TIXMLASSERT( flagIndex < ENTITY_RANGE ); + _entityFlag[flagIndex] = true; + } + _restrictedEntityFlag[static_cast('&')] = true; + _restrictedEntityFlag[static_cast('<')] = true; + _restrictedEntityFlag[static_cast('>')] = true; // not required, but consistency is nice + _buffer.Push( 0 ); +} + + +void XMLPrinter::Print( const char* format, ... ) +{ + va_list va; + va_start( va, format ); + + if ( _fp ) { + vfprintf( _fp, format, va ); + } + else { + const int len = TIXML_VSCPRINTF( format, va ); + // Close out and re-start the va-args + va_end( va ); + TIXMLASSERT( len >= 0 ); + va_start( va, format ); + TIXMLASSERT( _buffer.Size() > 0 && _buffer[_buffer.Size() - 1] == 0 ); + char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator. + TIXML_VSNPRINTF( p, len+1, format, va ); + } + va_end( va ); +} + + +void XMLPrinter::Write( const char* data, size_t size ) +{ + if ( _fp ) { + fwrite ( data , sizeof(char), size, _fp); + } + else { + char* p = _buffer.PushArr( static_cast(size) ) - 1; // back up over the null terminator. + memcpy( p, data, size ); + p[size] = 0; + } +} + + +void XMLPrinter::Putc( char ch ) +{ + if ( _fp ) { + fputc ( ch, _fp); + } + else { + char* p = _buffer.PushArr( sizeof(char) ) - 1; // back up over the null terminator. + p[0] = ch; + p[1] = 0; + } +} + + +void XMLPrinter::PrintSpace( int depth ) +{ + for( int i=0; i 0 && *q < ENTITY_RANGE ) { + // Check for entities. If one is found, flush + // the stream up until the entity, write the + // entity, and keep looking. + if ( flag[static_cast(*q)] ) { + while ( p < q ) { + const size_t delta = q - p; + const int toPrint = ( INT_MAX < delta ) ? INT_MAX : static_cast(delta); + Write( p, toPrint ); + p += toPrint; + } + bool entityPatternPrinted = false; + for( int i=0; i(delta); + Write( p, toPrint ); + } + } + else { + Write( p ); + } +} + + +void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) +{ + if ( writeBOM ) { + static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 }; + Write( reinterpret_cast< const char* >( bom ) ); + } + if ( writeDec ) { + PushDeclaration( "xml version=\"1.0\"" ); + } +} + +void XMLPrinter::PrepareForNewNode( bool compactMode ) +{ + SealElementIfJustOpened(); + + if ( compactMode ) { + return; + } + + if ( _firstElement ) { + PrintSpace (_depth); + } else if ( _textDepth < 0) { + Putc( '\n' ); + PrintSpace( _depth ); + } + + _firstElement = false; +} + +void XMLPrinter::OpenElement( const char* name, bool compactMode ) +{ + PrepareForNewNode( compactMode ); + _stack.Push( name ); + + Write ( "<" ); + Write ( name ); + + _elementJustOpened = true; + ++_depth; +} + + +void XMLPrinter::PushAttribute( const char* name, const char* value ) +{ + TIXMLASSERT( _elementJustOpened ); + Putc ( ' ' ); + Write( name ); + Write( "=\"" ); + PrintString( value, false ); + Putc ( '\"' ); +} + + +void XMLPrinter::PushAttribute( const char* name, int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute(const char* name, int64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + PushAttribute(name, buf); +} + + +void XMLPrinter::PushAttribute(const char* name, uint64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + PushAttribute(name, buf); +} + + +void XMLPrinter::PushAttribute( const char* name, bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::CloseElement( bool compactMode ) +{ + --_depth; + const char* name = _stack.Pop(); + + if ( _elementJustOpened ) { + Write( "/>" ); + } + else { + if ( _textDepth < 0 && !compactMode) { + Putc( '\n' ); + PrintSpace( _depth ); + } + Write ( "" ); + } + + if ( _textDepth == _depth ) { + _textDepth = -1; + } + if ( _depth == 0 && !compactMode) { + Putc( '\n' ); + } + _elementJustOpened = false; +} + + +void XMLPrinter::SealElementIfJustOpened() +{ + if ( !_elementJustOpened ) { + return; + } + _elementJustOpened = false; + Putc( '>' ); +} + + +void XMLPrinter::PushText( const char* text, bool cdata ) +{ + _textDepth = _depth-1; + + SealElementIfJustOpened(); + if ( cdata ) { + Write( "" ); + } + else { + PrintString( text, true ); + } +} + + +void XMLPrinter::PushText( int64_t value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( uint64_t value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(value, buf, BUF_SIZE); + PushText(buf, false); +} + + +void XMLPrinter::PushText( int value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( unsigned value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( bool value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( float value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( double value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushComment( const char* comment ) +{ + PrepareForNewNode( _compactMode ); + + Write( "" ); +} + + +void XMLPrinter::PushDeclaration( const char* value ) +{ + PrepareForNewNode( _compactMode ); + + Write( "" ); +} + + +void XMLPrinter::PushUnknown( const char* value ) +{ + PrepareForNewNode( _compactMode ); + + Write( "' ); +} + + +bool XMLPrinter::VisitEnter( const XMLDocument& doc ) +{ + _processEntities = doc.ProcessEntities(); + if ( doc.HasBOM() ) { + PushHeader( true, false ); + } + return true; +} + + +bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) +{ + const XMLElement* parentElem = 0; + if ( element.Parent() ) { + parentElem = element.Parent()->ToElement(); + } + const bool compactMode = parentElem ? CompactMode( *parentElem ) : _compactMode; + OpenElement( element.Name(), compactMode ); + while ( attribute ) { + PushAttribute( attribute->Name(), attribute->Value() ); + attribute = attribute->Next(); + } + return true; +} + + +bool XMLPrinter::VisitExit( const XMLElement& element ) +{ + CloseElement( CompactMode(element) ); + return true; +} + + +bool XMLPrinter::Visit( const XMLText& text ) +{ + PushText( text.Value(), text.CData() ); + return true; +} + + +bool XMLPrinter::Visit( const XMLComment& comment ) +{ + PushComment( comment.Value() ); + return true; +} + +bool XMLPrinter::Visit( const XMLDeclaration& declaration ) +{ + PushDeclaration( declaration.Value() ); + return true; +} + + +bool XMLPrinter::Visit( const XMLUnknown& unknown ) +{ + PushUnknown( unknown.Value() ); + return true; +} + +} // namespace tinyxml2 \ No newline at end of file diff --git a/src/lib/gdb/tinyxml2.h b/src/lib/gdb/tinyxml2.h new file mode 100644 index 0000000000..b2c9a3a0dd --- /dev/null +++ b/src/lib/gdb/tinyxml2.h @@ -0,0 +1,2384 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef TINYXML2_INCLUDED +#define TINYXML2_INCLUDED + +#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__) +# include +# include +# include +# include +# include +# if defined(__PS3__) +# include +# endif +#else +# include +# include +# include +# include +# include +#endif +#include + +/* + gcc: + g++ -Wall -DTINYXML2_DEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe + + Formatting, Artistic Style: + AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h +*/ + +#if defined( _DEBUG ) || defined (__DEBUG__) +# ifndef TINYXML2_DEBUG +# define TINYXML2_DEBUG +# endif +#endif + +#ifdef _MSC_VER +# pragma warning(push) +# pragma warning(disable: 4251) +#endif + +#ifdef _MSC_VER +# ifdef TINYXML2_EXPORT +# define TINYXML2_LIB __declspec(dllexport) +# elif defined(TINYXML2_IMPORT) +# define TINYXML2_LIB __declspec(dllimport) +# else +# define TINYXML2_LIB +# endif +#elif __GNUC__ >= 4 +# define TINYXML2_LIB __attribute__((visibility("default"))) +#else +# define TINYXML2_LIB +#endif + + +#if !defined(TIXMLASSERT) +#if defined(TINYXML2_DEBUG) +# if defined(_MSC_VER) +# // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like +# define TIXMLASSERT( x ) do { if ( !((void)0,(x))) { __debugbreak(); } } while(false) +# elif defined (ANDROID_NDK) +# include +# define TIXMLASSERT( x ) do { if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); } } while(false) +# else +# include +# define TIXMLASSERT assert +# endif +#else +# define TIXMLASSERT( x ) do {} while(false) +#endif +#endif + +/* Versioning, past 1.0.14: + http://semver.org/ +*/ +static const int TIXML2_MAJOR_VERSION = 10; +static const int TIXML2_MINOR_VERSION = 0; +static const int TIXML2_PATCH_VERSION = 0; + +#define TINYXML2_MAJOR_VERSION 10 +#define TINYXML2_MINOR_VERSION 0 +#define TINYXML2_PATCH_VERSION 0 + +// A fixed element depth limit is problematic. There needs to be a +// limit to avoid a stack overflow. However, that limit varies per +// system, and the capacity of the stack. On the other hand, it's a trivial +// attack that can result from ill, malicious, or even correctly formed XML, +// so there needs to be a limit in place. +static const int TINYXML2_MAX_ELEMENT_DEPTH = 500; + +namespace tinyxml2 +{ +class XMLDocument; +class XMLElement; +class XMLAttribute; +class XMLComment; +class XMLText; +class XMLDeclaration; +class XMLUnknown; +class XMLPrinter; + +/* + A class that wraps strings. Normally stores the start and end + pointers into the XML file itself, and will apply normalization + and entity translation if actually read. Can also store (and memory + manage) a traditional char[] + + Isn't clear why TINYXML2_LIB is needed; but seems to fix #719 +*/ +class TINYXML2_LIB StrPair +{ +public: + enum Mode { + NEEDS_ENTITY_PROCESSING = 0x01, + NEEDS_NEWLINE_NORMALIZATION = 0x02, + NEEDS_WHITESPACE_COLLAPSING = 0x04, + + TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, + TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, + ATTRIBUTE_NAME = 0, + ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, + ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, + COMMENT = NEEDS_NEWLINE_NORMALIZATION + }; + + StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {} + ~StrPair(); + + void Set( char* start, char* end, int flags ) { + TIXMLASSERT( start ); + TIXMLASSERT( end ); + Reset(); + _start = start; + _end = end; + _flags = flags | NEEDS_FLUSH; + } + + const char* GetStr(); + + bool Empty() const { + return _start == _end; + } + + void SetInternedStr( const char* str ) { + Reset(); + _start = const_cast(str); + } + + void SetStr( const char* str, int flags=0 ); + + char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr ); + char* ParseName( char* in ); + + void TransferTo( StrPair* other ); + void Reset(); + +private: + void CollapseWhitespace(); + + enum { + NEEDS_FLUSH = 0x100, + NEEDS_DELETE = 0x200 + }; + + int _flags; + char* _start; + char* _end; + + StrPair( const StrPair& other ); // not supported + void operator=( const StrPair& other ); // not supported, use TransferTo() +}; + + +/* + A dynamic array of Plain Old Data. Doesn't support constructors, etc. + Has a small initial memory pool, so that low or no usage will not + cause a call to new/delete +*/ +template +class DynArray +{ +public: + DynArray() : + _mem( _pool ), + _allocated( INITIAL_SIZE ), + _size( 0 ) + { + } + + ~DynArray() { + if ( _mem != _pool ) { + delete [] _mem; + } + } + + void Clear() { + _size = 0; + } + + void Push( T t ) { + TIXMLASSERT( _size < INT_MAX ); + EnsureCapacity( _size+1 ); + _mem[_size] = t; + ++_size; + } + + T* PushArr( int count ) { + TIXMLASSERT( count >= 0 ); + TIXMLASSERT( _size <= INT_MAX - count ); + EnsureCapacity( _size+count ); + T* ret = &_mem[_size]; + _size += count; + return ret; + } + + T Pop() { + TIXMLASSERT( _size > 0 ); + --_size; + return _mem[_size]; + } + + void PopArr( int count ) { + TIXMLASSERT( _size >= count ); + _size -= count; + } + + bool Empty() const { + return _size == 0; + } + + T& operator[](int i) { + TIXMLASSERT( i>= 0 && i < _size ); + return _mem[i]; + } + + const T& operator[](int i) const { + TIXMLASSERT( i>= 0 && i < _size ); + return _mem[i]; + } + + const T& PeekTop() const { + TIXMLASSERT( _size > 0 ); + return _mem[ _size - 1]; + } + + int Size() const { + TIXMLASSERT( _size >= 0 ); + return _size; + } + + int Capacity() const { + TIXMLASSERT( _allocated >= INITIAL_SIZE ); + return _allocated; + } + + void SwapRemove(int i) { + TIXMLASSERT(i >= 0 && i < _size); + TIXMLASSERT(_size > 0); + _mem[i] = _mem[_size - 1]; + --_size; + } + + const T* Mem() const { + TIXMLASSERT( _mem ); + return _mem; + } + + T* Mem() { + TIXMLASSERT( _mem ); + return _mem; + } + +private: + DynArray( const DynArray& ); // not supported + void operator=( const DynArray& ); // not supported + + void EnsureCapacity( int cap ) { + TIXMLASSERT( cap > 0 ); + if ( cap > _allocated ) { + TIXMLASSERT( cap <= INT_MAX / 2 ); + const int newAllocated = cap * 2; + T* newMem = new T[static_cast(newAllocated)]; + TIXMLASSERT( newAllocated >= _size ); + memcpy( newMem, _mem, sizeof(T)*static_cast(_size) ); // warning: not using constructors, only works for PODs + if ( _mem != _pool ) { + delete [] _mem; + } + _mem = newMem; + _allocated = newAllocated; + } + } + + T* _mem; + T _pool[static_cast(INITIAL_SIZE)]; + int _allocated; // objects allocated + int _size; // number objects in use +}; + + +/* + Parent virtual class of a pool for fast allocation + and deallocation of objects. +*/ +class MemPool +{ +public: + MemPool() {} + virtual ~MemPool() {} + + virtual int ItemSize() const = 0; + virtual void* Alloc() = 0; + virtual void Free( void* ) = 0; + virtual void SetTracked() = 0; +}; + + +/* + Template child class to create pools of the correct type. +*/ +template< int ITEM_SIZE > +class MemPoolT : public MemPool +{ +public: + MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {} + ~MemPoolT() { + MemPoolT< ITEM_SIZE >::Clear(); + } + + void Clear() { + // Delete the blocks. + while( !_blockPtrs.Empty()) { + Block* lastBlock = _blockPtrs.Pop(); + delete lastBlock; + } + _root = 0; + _currentAllocs = 0; + _nAllocs = 0; + _maxAllocs = 0; + _nUntracked = 0; + } + + virtual int ItemSize() const override{ + return ITEM_SIZE; + } + int CurrentAllocs() const { + return _currentAllocs; + } + + virtual void* Alloc() override{ + if ( !_root ) { + // Need a new block. + Block* block = new Block; + _blockPtrs.Push( block ); + + Item* blockItems = block->items; + for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) { + blockItems[i].next = &(blockItems[i + 1]); + } + blockItems[ITEMS_PER_BLOCK - 1].next = 0; + _root = blockItems; + } + Item* const result = _root; + TIXMLASSERT( result != 0 ); + _root = _root->next; + + ++_currentAllocs; + if ( _currentAllocs > _maxAllocs ) { + _maxAllocs = _currentAllocs; + } + ++_nAllocs; + ++_nUntracked; + return result; + } + + virtual void Free( void* mem ) override { + if ( !mem ) { + return; + } + --_currentAllocs; + Item* item = static_cast( mem ); +#ifdef TINYXML2_DEBUG + memset( item, 0xfe, sizeof( *item ) ); +#endif + item->next = _root; + _root = item; + } + void Trace( const char* name ) { + printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n", + name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs, + ITEM_SIZE, _nAllocs, _blockPtrs.Size() ); + } + + void SetTracked() override { + --_nUntracked; + } + + int Untracked() const { + return _nUntracked; + } + + // This number is perf sensitive. 4k seems like a good tradeoff on my machine. + // The test file is large, 170k. + // Release: VS2010 gcc(no opt) + // 1k: 4000 + // 2k: 4000 + // 4k: 3900 21000 + // 16k: 5200 + // 32k: 4300 + // 64k: 4000 21000 + // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK + // in private part if ITEMS_PER_BLOCK is private + enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE }; + +private: + MemPoolT( const MemPoolT& ); // not supported + void operator=( const MemPoolT& ); // not supported + + union Item { + Item* next; + char itemData[static_cast(ITEM_SIZE)]; + }; + struct Block { + Item items[ITEMS_PER_BLOCK]; + }; + DynArray< Block*, 10 > _blockPtrs; + Item* _root; + + int _currentAllocs; + int _nAllocs; + int _maxAllocs; + int _nUntracked; +}; + + + +/** + Implements the interface to the "Visitor pattern" (see the Accept() method.) + If you call the Accept() method, it requires being passed a XMLVisitor + class to handle callbacks. For nodes that contain other nodes (Document, Element) + you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs + are simply called with Visit(). + + If you return 'true' from a Visit method, recursive parsing will continue. If you return + false, no children of this node or its siblings will be visited. + + All flavors of Visit methods have a default implementation that returns 'true' (continue + visiting). You need to only override methods that are interesting to you. + + Generally Accept() is called on the XMLDocument, although all nodes support visiting. + + You should never change the document from a callback. + + @sa XMLNode::Accept() +*/ +class TINYXML2_LIB XMLVisitor +{ +public: + virtual ~XMLVisitor() {} + + /// Visit a document. + virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { + return true; + } + /// Visit a document. + virtual bool VisitExit( const XMLDocument& /*doc*/ ) { + return true; + } + + /// Visit an element. + virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { + return true; + } + /// Visit an element. + virtual bool VisitExit( const XMLElement& /*element*/ ) { + return true; + } + + /// Visit a declaration. + virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { + return true; + } + /// Visit a text node. + virtual bool Visit( const XMLText& /*text*/ ) { + return true; + } + /// Visit a comment node. + virtual bool Visit( const XMLComment& /*comment*/ ) { + return true; + } + /// Visit an unknown node. + virtual bool Visit( const XMLUnknown& /*unknown*/ ) { + return true; + } +}; + +// WARNING: must match XMLDocument::_errorNames[] +enum XMLError { + XML_SUCCESS = 0, + XML_NO_ATTRIBUTE, + XML_WRONG_ATTRIBUTE_TYPE, + XML_ERROR_FILE_NOT_FOUND, + XML_ERROR_FILE_COULD_NOT_BE_OPENED, + XML_ERROR_FILE_READ_ERROR, + XML_ERROR_PARSING_ELEMENT, + XML_ERROR_PARSING_ATTRIBUTE, + XML_ERROR_PARSING_TEXT, + XML_ERROR_PARSING_CDATA, + XML_ERROR_PARSING_COMMENT, + XML_ERROR_PARSING_DECLARATION, + XML_ERROR_PARSING_UNKNOWN, + XML_ERROR_EMPTY_DOCUMENT, + XML_ERROR_MISMATCHED_ELEMENT, + XML_ERROR_PARSING, + XML_CAN_NOT_CONVERT_TEXT, + XML_NO_TEXT_NODE, + XML_ELEMENT_DEPTH_EXCEEDED, + + XML_ERROR_COUNT +}; + + +/* + Utility functionality. +*/ +class TINYXML2_LIB XMLUtil +{ +public: + static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) { + TIXMLASSERT( p ); + + while( IsWhiteSpace(*p) ) { + if (curLineNumPtr && *p == '\n') { + ++(*curLineNumPtr); + } + ++p; + } + TIXMLASSERT( p ); + return p; + } + static char* SkipWhiteSpace( char* const p, int* curLineNumPtr ) { + return const_cast( SkipWhiteSpace( const_cast(p), curLineNumPtr ) ); + } + + // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't + // correct, but simple, and usually works. + static bool IsWhiteSpace( char p ) { + return !IsUTF8Continuation(p) && isspace( static_cast(p) ); + } + + inline static bool IsNameStartChar( unsigned char ch ) { + if ( ch >= 128 ) { + // This is a heuristic guess in attempt to not implement Unicode-aware isalpha() + return true; + } + if ( isalpha( ch ) ) { + return true; + } + return ch == ':' || ch == '_'; + } + + inline static bool IsNameChar( unsigned char ch ) { + return IsNameStartChar( ch ) + || isdigit( ch ) + || ch == '.' + || ch == '-'; + } + + inline static bool IsPrefixHex( const char* p) { + p = SkipWhiteSpace(p, 0); + return p && *p == '0' && ( *(p + 1) == 'x' || *(p + 1) == 'X'); + } + + inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) { + if ( p == q ) { + return true; + } + TIXMLASSERT( p ); + TIXMLASSERT( q ); + TIXMLASSERT( nChar >= 0 ); + return strncmp( p, q, static_cast(nChar) ) == 0; + } + + inline static bool IsUTF8Continuation( const char p ) { + return ( p & 0x80 ) != 0; + } + + static const char* ReadBOM( const char* p, bool* hasBOM ); + // p is the starting location, + // the UTF-8 value of the entity will be placed in value, and length filled in. + static const char* GetCharacterRef( const char* p, char* value, int* length ); + static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ); + + // converts primitive types to strings + static void ToStr( int v, char* buffer, int bufferSize ); + static void ToStr( unsigned v, char* buffer, int bufferSize ); + static void ToStr( bool v, char* buffer, int bufferSize ); + static void ToStr( float v, char* buffer, int bufferSize ); + static void ToStr( double v, char* buffer, int bufferSize ); + static void ToStr(int64_t v, char* buffer, int bufferSize); + static void ToStr(uint64_t v, char* buffer, int bufferSize); + + // converts strings to primitive types + static bool ToInt( const char* str, int* value ); + static bool ToUnsigned( const char* str, unsigned* value ); + static bool ToBool( const char* str, bool* value ); + static bool ToFloat( const char* str, float* value ); + static bool ToDouble( const char* str, double* value ); + static bool ToInt64(const char* str, int64_t* value); + static bool ToUnsigned64(const char* str, uint64_t* value); + // Changes what is serialized for a boolean value. + // Default to "true" and "false". Shouldn't be changed + // unless you have a special testing or compatibility need. + // Be careful: static, global, & not thread safe. + // Be sure to set static const memory as parameters. + static void SetBoolSerialization(const char* writeTrue, const char* writeFalse); + +private: + static const char* writeBoolTrue; + static const char* writeBoolFalse; +}; + + +/** XMLNode is a base class for every object that is in the + XML Document Object Model (DOM), except XMLAttributes. + Nodes have siblings, a parent, and children which can + be navigated. A node is always in a XMLDocument. + The type of a XMLNode can be queried, and it can + be cast to its more defined type. + + A XMLDocument allocates memory for all its Nodes. + When the XMLDocument gets deleted, all its Nodes + will also be deleted. + + @verbatim + A Document can contain: Element (container or leaf) + Comment (leaf) + Unknown (leaf) + Declaration( leaf ) + + An Element can contain: Element (container or leaf) + Text (leaf) + Attributes (not on tree) + Comment (leaf) + Unknown (leaf) + + @endverbatim +*/ +class TINYXML2_LIB XMLNode +{ + friend class XMLDocument; + friend class XMLElement; +public: + + /// Get the XMLDocument that owns this XMLNode. + const XMLDocument* GetDocument() const { + TIXMLASSERT( _document ); + return _document; + } + /// Get the XMLDocument that owns this XMLNode. + XMLDocument* GetDocument() { + TIXMLASSERT( _document ); + return _document; + } + + /// Safely cast to an Element, or null. + virtual XMLElement* ToElement() { + return 0; + } + /// Safely cast to Text, or null. + virtual XMLText* ToText() { + return 0; + } + /// Safely cast to a Comment, or null. + virtual XMLComment* ToComment() { + return 0; + } + /// Safely cast to a Document, or null. + virtual XMLDocument* ToDocument() { + return 0; + } + /// Safely cast to a Declaration, or null. + virtual XMLDeclaration* ToDeclaration() { + return 0; + } + /// Safely cast to an Unknown, or null. + virtual XMLUnknown* ToUnknown() { + return 0; + } + + virtual const XMLElement* ToElement() const { + return 0; + } + virtual const XMLText* ToText() const { + return 0; + } + virtual const XMLComment* ToComment() const { + return 0; + } + virtual const XMLDocument* ToDocument() const { + return 0; + } + virtual const XMLDeclaration* ToDeclaration() const { + return 0; + } + virtual const XMLUnknown* ToUnknown() const { + return 0; + } + + // ChildElementCount was originally suggested by msteiger on the sourceforge page for TinyXML and modified by KB1SPH for TinyXML-2. + + int ChildElementCount(const char *value) const; + + int ChildElementCount() const; + + /** The meaning of 'value' changes for the specific type. + @verbatim + Document: empty (NULL is returned, not an empty string) + Element: name of the element + Comment: the comment text + Unknown: the tag contents + Text: the text string + @endverbatim + */ + const char* Value() const; + + /** Set the Value of an XML node. + @sa Value() + */ + void SetValue( const char* val, bool staticMem=false ); + + /// Gets the line number the node is in, if the document was parsed from a file. + int GetLineNum() const { return _parseLineNum; } + + /// Get the parent of this node on the DOM. + const XMLNode* Parent() const { + return _parent; + } + + XMLNode* Parent() { + return _parent; + } + + /// Returns true if this node has no children. + bool NoChildren() const { + return !_firstChild; + } + + /// Get the first child node, or null if none exists. + const XMLNode* FirstChild() const { + return _firstChild; + } + + XMLNode* FirstChild() { + return _firstChild; + } + + /** Get the first child element, or optionally the first child + element with the specified name. + */ + const XMLElement* FirstChildElement( const char* name = 0 ) const; + + XMLElement* FirstChildElement( const char* name = 0 ) { + return const_cast(const_cast(this)->FirstChildElement( name )); + } + + /// Get the last child node, or null if none exists. + const XMLNode* LastChild() const { + return _lastChild; + } + + XMLNode* LastChild() { + return _lastChild; + } + + /** Get the last child element or optionally the last child + element with the specified name. + */ + const XMLElement* LastChildElement( const char* name = 0 ) const; + + XMLElement* LastChildElement( const char* name = 0 ) { + return const_cast(const_cast(this)->LastChildElement(name) ); + } + + /// Get the previous (left) sibling node of this node. + const XMLNode* PreviousSibling() const { + return _prev; + } + + XMLNode* PreviousSibling() { + return _prev; + } + + /// Get the previous (left) sibling element of this node, with an optionally supplied name. + const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ; + + XMLElement* PreviousSiblingElement( const char* name = 0 ) { + return const_cast(const_cast(this)->PreviousSiblingElement( name ) ); + } + + /// Get the next (right) sibling node of this node. + const XMLNode* NextSibling() const { + return _next; + } + + XMLNode* NextSibling() { + return _next; + } + + /// Get the next (right) sibling element of this node, with an optionally supplied name. + const XMLElement* NextSiblingElement( const char* name = 0 ) const; + + XMLElement* NextSiblingElement( const char* name = 0 ) { + return const_cast(const_cast(this)->NextSiblingElement( name ) ); + } + + /** + Add a child node as the last (right) child. + If the child node is already part of the document, + it is moved from its old location to the new location. + Returns the addThis argument or 0 if the node does not + belong to the same document. + */ + XMLNode* InsertEndChild( XMLNode* addThis ); + + XMLNode* LinkEndChild( XMLNode* addThis ) { + return InsertEndChild( addThis ); + } + /** + Add a child node as the first (left) child. + If the child node is already part of the document, + it is moved from its old location to the new location. + Returns the addThis argument or 0 if the node does not + belong to the same document. + */ + XMLNode* InsertFirstChild( XMLNode* addThis ); + /** + Add a node after the specified child node. + If the child node is already part of the document, + it is moved from its old location to the new location. + Returns the addThis argument or 0 if the afterThis node + is not a child of this node, or if the node does not + belong to the same document. + */ + XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ); + + /** + Delete all the children of this node. + */ + void DeleteChildren(); + + /** + Delete a child of this node. + */ + void DeleteChild( XMLNode* node ); + + /** + Make a copy of this node, but not its children. + You may pass in a Document pointer that will be + the owner of the new Node. If the 'document' is + null, then the node returned will be allocated + from the current Document. (this->GetDocument()) + + Note: if called on a XMLDocument, this will return null. + */ + virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0; + + /** + Make a copy of this node and all its children. + + If the 'target' is null, then the nodes will + be allocated in the current document. If 'target' + is specified, the memory will be allocated is the + specified XMLDocument. + + NOTE: This is probably not the correct tool to + copy a document, since XMLDocuments can have multiple + top level XMLNodes. You probably want to use + XMLDocument::DeepCopy() + */ + XMLNode* DeepClone( XMLDocument* target ) const; + + /** + Test if 2 nodes are the same, but don't test children. + The 2 nodes do not need to be in the same Document. + + Note: if called on a XMLDocument, this will return false. + */ + virtual bool ShallowEqual( const XMLNode* compare ) const = 0; + + /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the + XML tree will be conditionally visited and the host will be called back + via the XMLVisitor interface. + + This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse + the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this + interface versus any other.) + + The interface has been based on ideas from: + + - http://www.saxproject.org/ + - http://c2.com/cgi/wiki?HierarchicalVisitorPattern + + Which are both good references for "visiting". + + An example of using Accept(): + @verbatim + XMLPrinter printer; + tinyxmlDoc.Accept( &printer ); + const char* xmlcstr = printer.CStr(); + @endverbatim + */ + virtual bool Accept( XMLVisitor* visitor ) const = 0; + + /** + Set user data into the XMLNode. TinyXML-2 in + no way processes or interprets user data. + It is initially 0. + */ + void SetUserData(void* userData) { _userData = userData; } + + /** + Get user data set into the XMLNode. TinyXML-2 in + no way processes or interprets user data. + It is initially 0. + */ + void* GetUserData() const { return _userData; } + +protected: + explicit XMLNode( XMLDocument* ); + virtual ~XMLNode(); + + virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr); + + XMLDocument* _document; + XMLNode* _parent; + mutable StrPair _value; + int _parseLineNum; + + XMLNode* _firstChild; + XMLNode* _lastChild; + + XMLNode* _prev; + XMLNode* _next; + + void* _userData; + +private: + MemPool* _memPool; + void Unlink( XMLNode* child ); + static void DeleteNode( XMLNode* node ); + void InsertChildPreamble( XMLNode* insertThis ) const; + const XMLElement* ToElementWithName( const char* name ) const; + + XMLNode( const XMLNode& ); // not supported + XMLNode& operator=( const XMLNode& ); // not supported +}; + + +/** XML text. + + Note that a text node can have child element nodes, for example: + @verbatim + This is bold + @endverbatim + + A text node can have 2 ways to output the next. "normal" output + and CDATA. It will default to the mode it was parsed from the XML file and + you generally want to leave it alone, but you can change the output mode with + SetCData() and query it with CData(). +*/ +class TINYXML2_LIB XMLText : public XMLNode +{ + friend class XMLDocument; +public: + virtual bool Accept( XMLVisitor* visitor ) const override; + + virtual XMLText* ToText() override { + return this; + } + virtual const XMLText* ToText() const override { + return this; + } + + /// Declare whether this should be CDATA or standard text. + void SetCData( bool isCData ) { + _isCData = isCData; + } + /// Returns true if this is a CDATA text element. + bool CData() const { + return _isCData; + } + + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + explicit XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {} + virtual ~XMLText() {} + + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override; + +private: + bool _isCData; + + XMLText( const XMLText& ); // not supported + XMLText& operator=( const XMLText& ); // not supported +}; + + +/** An XML Comment. */ +class TINYXML2_LIB XMLComment : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLComment* ToComment() override { + return this; + } + virtual const XMLComment* ToComment() const override { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const override; + + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + explicit XMLComment( XMLDocument* doc ); + virtual ~XMLComment(); + + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr) override; + +private: + XMLComment( const XMLComment& ); // not supported + XMLComment& operator=( const XMLComment& ); // not supported +}; + + +/** In correct XML the declaration is the first entry in the file. + @verbatim + + @endverbatim + + TinyXML-2 will happily read or write files without a declaration, + however. + + The text of the declaration isn't interpreted. It is parsed + and written as a string. +*/ +class TINYXML2_LIB XMLDeclaration : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLDeclaration* ToDeclaration() override { + return this; + } + virtual const XMLDeclaration* ToDeclaration() const override { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const override; + + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + explicit XMLDeclaration( XMLDocument* doc ); + virtual ~XMLDeclaration(); + + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override; + +private: + XMLDeclaration( const XMLDeclaration& ); // not supported + XMLDeclaration& operator=( const XMLDeclaration& ); // not supported +}; + + +/** Any tag that TinyXML-2 doesn't recognize is saved as an + unknown. It is a tag of text, but should not be modified. + It will be written back to the XML, unchanged, when the file + is saved. + + DTD tags get thrown into XMLUnknowns. +*/ +class TINYXML2_LIB XMLUnknown : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLUnknown* ToUnknown() override { + return this; + } + virtual const XMLUnknown* ToUnknown() const override { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const override; + + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + explicit XMLUnknown( XMLDocument* doc ); + virtual ~XMLUnknown(); + + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override; + +private: + XMLUnknown( const XMLUnknown& ); // not supported + XMLUnknown& operator=( const XMLUnknown& ); // not supported +}; + + + +/** An attribute is a name-value pair. Elements have an arbitrary + number of attributes, each with a unique name. + + @note The attributes are not XMLNodes. You may only query the + Next() attribute in a list. +*/ +class TINYXML2_LIB XMLAttribute +{ + friend class XMLElement; +public: + /// The name of the attribute. + const char* Name() const; + + /// The value of the attribute. + const char* Value() const; + + /// Gets the line number the attribute is in, if the document was parsed from a file. + int GetLineNum() const { return _parseLineNum; } + + /// The next attribute in the list. + const XMLAttribute* Next() const { + return _next; + } + + /** IntValue interprets the attribute as an integer, and returns the value. + If the value isn't an integer, 0 will be returned. There is no error checking; + use QueryIntValue() if you need error checking. + */ + int IntValue() const { + int i = 0; + QueryIntValue(&i); + return i; + } + + int64_t Int64Value() const { + int64_t i = 0; + QueryInt64Value(&i); + return i; + } + + uint64_t Unsigned64Value() const { + uint64_t i = 0; + QueryUnsigned64Value(&i); + return i; + } + + /// Query as an unsigned integer. See IntValue() + unsigned UnsignedValue() const { + unsigned i=0; + QueryUnsignedValue( &i ); + return i; + } + /// Query as a boolean. See IntValue() + bool BoolValue() const { + bool b=false; + QueryBoolValue( &b ); + return b; + } + /// Query as a double. See IntValue() + double DoubleValue() const { + double d=0; + QueryDoubleValue( &d ); + return d; + } + /// Query as a float. See IntValue() + float FloatValue() const { + float f=0; + QueryFloatValue( &f ); + return f; + } + + /** QueryIntValue interprets the attribute as an integer, and returns the value + in the provided parameter. The function will return XML_SUCCESS on success, + and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful. + */ + XMLError QueryIntValue( int* value ) const; + /// See QueryIntValue + XMLError QueryUnsignedValue( unsigned int* value ) const; + /// See QueryIntValue + XMLError QueryInt64Value(int64_t* value) const; + /// See QueryIntValue + XMLError QueryUnsigned64Value(uint64_t* value) const; + /// See QueryIntValue + XMLError QueryBoolValue( bool* value ) const; + /// See QueryIntValue + XMLError QueryDoubleValue( double* value ) const; + /// See QueryIntValue + XMLError QueryFloatValue( float* value ) const; + + /// Set the attribute to a string value. + void SetAttribute( const char* value ); + /// Set the attribute to value. + void SetAttribute( int value ); + /// Set the attribute to value. + void SetAttribute( unsigned value ); + /// Set the attribute to value. + void SetAttribute(int64_t value); + /// Set the attribute to value. + void SetAttribute(uint64_t value); + /// Set the attribute to value. + void SetAttribute( bool value ); + /// Set the attribute to value. + void SetAttribute( double value ); + /// Set the attribute to value. + void SetAttribute( float value ); + +private: + enum { BUF_SIZE = 200 }; + + XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {} + virtual ~XMLAttribute() {} + + XMLAttribute( const XMLAttribute& ); // not supported + void operator=( const XMLAttribute& ); // not supported + void SetName( const char* name ); + + char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr ); + + mutable StrPair _name; + mutable StrPair _value; + int _parseLineNum; + XMLAttribute* _next; + MemPool* _memPool; +}; + + +/** The element is a container class. It has a value, the element name, + and can contain other elements, text, comments, and unknowns. + Elements also contain an arbitrary number of attributes. +*/ +class TINYXML2_LIB XMLElement : public XMLNode +{ + friend class XMLDocument; +public: + /// Get the name of an element (which is the Value() of the node.) + const char* Name() const { + return Value(); + } + /// Set the name of the element. + void SetName( const char* str, bool staticMem=false ) { + SetValue( str, staticMem ); + } + + virtual XMLElement* ToElement() override { + return this; + } + virtual const XMLElement* ToElement() const override { + return this; + } + virtual bool Accept( XMLVisitor* visitor ) const override; + + /** Given an attribute name, Attribute() returns the value + for the attribute of that name, or null if none + exists. For example: + + @verbatim + const char* value = ele->Attribute( "foo" ); + @endverbatim + + The 'value' parameter is normally null. However, if specified, + the attribute will only be returned if the 'name' and 'value' + match. This allow you to write code: + + @verbatim + if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar(); + @endverbatim + + rather than: + @verbatim + if ( ele->Attribute( "foo" ) ) { + if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar(); + } + @endverbatim + */ + const char* Attribute( const char* name, const char* value=0 ) const; + + /** Given an attribute name, IntAttribute() returns the value + of the attribute interpreted as an integer. The default + value will be returned if the attribute isn't present, + or if there is an error. (For a method with error + checking, see QueryIntAttribute()). + */ + int IntAttribute(const char* name, int defaultValue = 0) const; + /// See IntAttribute() + unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const; + /// See IntAttribute() + int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const; + /// See IntAttribute() + uint64_t Unsigned64Attribute(const char* name, uint64_t defaultValue = 0) const; + /// See IntAttribute() + bool BoolAttribute(const char* name, bool defaultValue = false) const; + /// See IntAttribute() + double DoubleAttribute(const char* name, double defaultValue = 0) const; + /// See IntAttribute() + float FloatAttribute(const char* name, float defaultValue = 0) const; + + /** Given an attribute name, QueryIntAttribute() returns + XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion + can't be performed, or XML_NO_ATTRIBUTE if the attribute + doesn't exist. If successful, the result of the conversion + will be written to 'value'. If not successful, nothing will + be written to 'value'. This allows you to provide default + value: + + @verbatim + int value = 10; + QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10 + @endverbatim + */ + XMLError QueryIntAttribute( const char* name, int* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryIntValue( value ); + } + + /// See QueryIntAttribute() + XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryUnsignedValue( value ); + } + + /// See QueryIntAttribute() + XMLError QueryInt64Attribute(const char* name, int64_t* value) const { + const XMLAttribute* a = FindAttribute(name); + if (!a) { + return XML_NO_ATTRIBUTE; + } + return a->QueryInt64Value(value); + } + + /// See QueryIntAttribute() + XMLError QueryUnsigned64Attribute(const char* name, uint64_t* value) const { + const XMLAttribute* a = FindAttribute(name); + if(!a) { + return XML_NO_ATTRIBUTE; + } + return a->QueryUnsigned64Value(value); + } + + /// See QueryIntAttribute() + XMLError QueryBoolAttribute( const char* name, bool* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryBoolValue( value ); + } + /// See QueryIntAttribute() + XMLError QueryDoubleAttribute( const char* name, double* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryDoubleValue( value ); + } + /// See QueryIntAttribute() + XMLError QueryFloatAttribute( const char* name, float* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryFloatValue( value ); + } + + /// See QueryIntAttribute() + XMLError QueryStringAttribute(const char* name, const char** value) const { + const XMLAttribute* a = FindAttribute(name); + if (!a) { + return XML_NO_ATTRIBUTE; + } + *value = a->Value(); + return XML_SUCCESS; + } + + + + /** Given an attribute name, QueryAttribute() returns + XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion + can't be performed, or XML_NO_ATTRIBUTE if the attribute + doesn't exist. It is overloaded for the primitive types, + and is a generally more convenient replacement of + QueryIntAttribute() and related functions. + + If successful, the result of the conversion + will be written to 'value'. If not successful, nothing will + be written to 'value'. This allows you to provide default + value: + + @verbatim + int value = 10; + QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10 + @endverbatim + */ + XMLError QueryAttribute( const char* name, int* value ) const { + return QueryIntAttribute( name, value ); + } + + XMLError QueryAttribute( const char* name, unsigned int* value ) const { + return QueryUnsignedAttribute( name, value ); + } + + XMLError QueryAttribute(const char* name, int64_t* value) const { + return QueryInt64Attribute(name, value); + } + + XMLError QueryAttribute(const char* name, uint64_t* value) const { + return QueryUnsigned64Attribute(name, value); + } + + XMLError QueryAttribute( const char* name, bool* value ) const { + return QueryBoolAttribute( name, value ); + } + + XMLError QueryAttribute( const char* name, double* value ) const { + return QueryDoubleAttribute( name, value ); + } + + XMLError QueryAttribute( const char* name, float* value ) const { + return QueryFloatAttribute( name, value ); + } + + XMLError QueryAttribute(const char* name, const char** value) const { + return QueryStringAttribute(name, value); + } + + /// Sets the named attribute to value. + void SetAttribute( const char* name, const char* value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, int value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, unsigned value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + + /// Sets the named attribute to value. + void SetAttribute(const char* name, int64_t value) { + XMLAttribute* a = FindOrCreateAttribute(name); + a->SetAttribute(value); + } + + /// Sets the named attribute to value. + void SetAttribute(const char* name, uint64_t value) { + XMLAttribute* a = FindOrCreateAttribute(name); + a->SetAttribute(value); + } + + /// Sets the named attribute to value. + void SetAttribute( const char* name, bool value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, double value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, float value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + + /** + Delete an attribute. + */ + void DeleteAttribute( const char* name ); + + /// Return the first attribute in the list. + const XMLAttribute* FirstAttribute() const { + return _rootAttribute; + } + /// Query a specific attribute in the list. + const XMLAttribute* FindAttribute( const char* name ) const; + + /** Convenience function for easy access to the text inside an element. Although easy + and concise, GetText() is limited compared to getting the XMLText child + and accessing it directly. + + If the first child of 'this' is a XMLText, the GetText() + returns the character string of the Text node, else null is returned. + + This is a convenient method for getting the text of simple contained text: + @verbatim + This is text + const char* str = fooElement->GetText(); + @endverbatim + + 'str' will be a pointer to "This is text". + + Note that this function can be misleading. If the element foo was created from + this XML: + @verbatim + This is text + @endverbatim + + then the value of str would be null. The first child node isn't a text node, it is + another element. From this XML: + @verbatim + This is text + @endverbatim + GetText() will return "This is ". + */ + const char* GetText() const; + + /** Convenience function for easy access to the text inside an element. Although easy + and concise, SetText() is limited compared to creating an XMLText child + and mutating it directly. + + If the first child of 'this' is a XMLText, SetText() sets its value to + the given string, otherwise it will create a first child that is an XMLText. + + This is a convenient method for setting the text of simple contained text: + @verbatim + This is text + fooElement->SetText( "Hullaballoo!" ); + Hullaballoo! + @endverbatim + + Note that this function can be misleading. If the element foo was created from + this XML: + @verbatim + This is text + @endverbatim + + then it will not change "This is text", but rather prefix it with a text element: + @verbatim + Hullaballoo!This is text + @endverbatim + + For this XML: + @verbatim + + @endverbatim + SetText() will generate + @verbatim + Hullaballoo! + @endverbatim + */ + void SetText( const char* inText ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( int value ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( unsigned value ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText(int64_t value); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText(uint64_t value); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( bool value ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( double value ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( float value ); + + /** + Convenience method to query the value of a child text node. This is probably best + shown by example. Given you have a document is this form: + @verbatim + + 1 + 1.4 + + @endverbatim + + The QueryIntText() and similar functions provide a safe and easier way to get to the + "value" of x and y. + + @verbatim + int x = 0; + float y = 0; // types of x and y are contrived for example + const XMLElement* xElement = pointElement->FirstChildElement( "x" ); + const XMLElement* yElement = pointElement->FirstChildElement( "y" ); + xElement->QueryIntText( &x ); + yElement->QueryFloatText( &y ); + @endverbatim + + @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted + to the requested type, and XML_NO_TEXT_NODE if there is no child text to query. + + */ + XMLError QueryIntText( int* ival ) const; + /// See QueryIntText() + XMLError QueryUnsignedText( unsigned* uval ) const; + /// See QueryIntText() + XMLError QueryInt64Text(int64_t* uval) const; + /// See QueryIntText() + XMLError QueryUnsigned64Text(uint64_t* uval) const; + /// See QueryIntText() + XMLError QueryBoolText( bool* bval ) const; + /// See QueryIntText() + XMLError QueryDoubleText( double* dval ) const; + /// See QueryIntText() + XMLError QueryFloatText( float* fval ) const; + + int IntText(int defaultValue = 0) const; + + /// See QueryIntText() + unsigned UnsignedText(unsigned defaultValue = 0) const; + /// See QueryIntText() + int64_t Int64Text(int64_t defaultValue = 0) const; + /// See QueryIntText() + uint64_t Unsigned64Text(uint64_t defaultValue = 0) const; + /// See QueryIntText() + bool BoolText(bool defaultValue = false) const; + /// See QueryIntText() + double DoubleText(double defaultValue = 0) const; + /// See QueryIntText() + float FloatText(float defaultValue = 0) const; + + /** + Convenience method to create a new XMLElement and add it as last (right) + child of this node. Returns the created and inserted element. + */ + XMLElement* InsertNewChildElement(const char* name); + /// See InsertNewChildElement() + XMLComment* InsertNewComment(const char* comment); + /// See InsertNewChildElement() + XMLText* InsertNewText(const char* text); + /// See InsertNewChildElement() + XMLDeclaration* InsertNewDeclaration(const char* text); + /// See InsertNewChildElement() + XMLUnknown* InsertNewUnknown(const char* text); + + + // internal: + enum ElementClosingType { + OPEN, // + CLOSED, // + CLOSING // + }; + ElementClosingType ClosingType() const { + return _closingType; + } + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override; + +private: + XMLElement( XMLDocument* doc ); + virtual ~XMLElement(); + XMLElement( const XMLElement& ); // not supported + void operator=( const XMLElement& ); // not supported + + XMLAttribute* FindOrCreateAttribute( const char* name ); + char* ParseAttributes( char* p, int* curLineNumPtr ); + static void DeleteAttribute( XMLAttribute* attribute ); + XMLAttribute* CreateAttribute(); + + enum { BUF_SIZE = 200 }; + ElementClosingType _closingType; + // The attribute list is ordered; there is no 'lastAttribute' + // because the list needs to be scanned for dupes before adding + // a new attribute. + XMLAttribute* _rootAttribute; +}; + + +enum Whitespace { + PRESERVE_WHITESPACE, + COLLAPSE_WHITESPACE, + PEDANTIC_WHITESPACE +}; + + +/** A Document binds together all the functionality. + It can be saved, loaded, and printed to the screen. + All Nodes are connected and allocated to a Document. + If the Document is deleted, all its Nodes are also deleted. +*/ +class TINYXML2_LIB XMLDocument : public XMLNode +{ + friend class XMLElement; + // Gives access to SetError and Push/PopDepth, but over-access for everything else. + // Wishing C++ had "internal" scope. + friend class XMLNode; + friend class XMLText; + friend class XMLComment; + friend class XMLDeclaration; + friend class XMLUnknown; +public: + /// constructor + XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE ); + ~XMLDocument(); + + virtual XMLDocument* ToDocument() override { + TIXMLASSERT( this == _document ); + return this; + } + virtual const XMLDocument* ToDocument() const override { + TIXMLASSERT( this == _document ); + return this; + } + + /** + Parse an XML file from a character string. + Returns XML_SUCCESS (0) on success, or + an errorID. + + You may optionally pass in the 'nBytes', which is + the number of bytes which will be parsed. If not + specified, TinyXML-2 will assume 'xml' points to a + null terminated string. + */ + XMLError Parse( const char* xml, size_t nBytes=static_cast(-1) ); + + /** + Load an XML file from disk. + Returns XML_SUCCESS (0) on success, or + an errorID. + */ + XMLError LoadFile( const char* filename ); + + /** + Load an XML file from disk. You are responsible + for providing and closing the FILE*. + + NOTE: The file should be opened as binary ("rb") + not text in order for TinyXML-2 to correctly + do newline normalization. + + Returns XML_SUCCESS (0) on success, or + an errorID. + */ + XMLError LoadFile( FILE* ); + + /** + Save the XML file to disk. + Returns XML_SUCCESS (0) on success, or + an errorID. + */ + XMLError SaveFile( const char* filename, bool compact = false ); + + /** + Save the XML file to disk. You are responsible + for providing and closing the FILE*. + + Returns XML_SUCCESS (0) on success, or + an errorID. + */ + XMLError SaveFile( FILE* fp, bool compact = false ); + + bool ProcessEntities() const { + return _processEntities; + } + Whitespace WhitespaceMode() const { + return _whitespaceMode; + } + + /** + Returns true if this document has a leading Byte Order Mark of UTF8. + */ + bool HasBOM() const { + return _writeBOM; + } + /** Sets whether to write the BOM when writing the file. + */ + void SetBOM( bool useBOM ) { + _writeBOM = useBOM; + } + + /** Return the root element of DOM. Equivalent to FirstChildElement(). + To get the first node, use FirstChild(). + */ + XMLElement* RootElement() { + return FirstChildElement(); + } + const XMLElement* RootElement() const { + return FirstChildElement(); + } + + /** Print the Document. If the Printer is not provided, it will + print to stdout. If you provide Printer, this can print to a file: + @verbatim + XMLPrinter printer( fp ); + doc.Print( &printer ); + @endverbatim + + Or you can use a printer to print to memory: + @verbatim + XMLPrinter printer; + doc.Print( &printer ); + // printer.CStr() has a const char* to the XML + @endverbatim + */ + void Print( XMLPrinter* streamer=0 ) const; + virtual bool Accept( XMLVisitor* visitor ) const override; + + /** + Create a new Element associated with + this Document. The memory for the Element + is managed by the Document. + */ + XMLElement* NewElement( const char* name ); + /** + Create a new Comment associated with + this Document. The memory for the Comment + is managed by the Document. + */ + XMLComment* NewComment( const char* comment ); + /** + Create a new Text associated with + this Document. The memory for the Text + is managed by the Document. + */ + XMLText* NewText( const char* text ); + /** + Create a new Declaration associated with + this Document. The memory for the object + is managed by the Document. + + If the 'text' param is null, the standard + declaration is used.: + @verbatim + + @endverbatim + */ + XMLDeclaration* NewDeclaration( const char* text=0 ); + /** + Create a new Unknown associated with + this Document. The memory for the object + is managed by the Document. + */ + XMLUnknown* NewUnknown( const char* text ); + + /** + Delete a node associated with this document. + It will be unlinked from the DOM. + */ + void DeleteNode( XMLNode* node ); + + /// Clears the error flags. + void ClearError(); + + /// Return true if there was an error parsing the document. + bool Error() const { + return _errorID != XML_SUCCESS; + } + /// Return the errorID. + XMLError ErrorID() const { + return _errorID; + } + const char* ErrorName() const; + static const char* ErrorIDToName(XMLError errorID); + + /** Returns a "long form" error description. A hopefully helpful + diagnostic with location, line number, and/or additional info. + */ + const char* ErrorStr() const; + + /// A (trivial) utility function that prints the ErrorStr() to stdout. + void PrintError() const; + + /// Return the line where the error occurred, or zero if unknown. + int ErrorLineNum() const + { + return _errorLineNum; + } + + /// Clear the document, resetting it to the initial state. + void Clear(); + + /** + Copies this document to a target document. + The target will be completely cleared before the copy. + If you want to copy a sub-tree, see XMLNode::DeepClone(). + + NOTE: that the 'target' must be non-null. + */ + void DeepCopy(XMLDocument* target) const; + + // internal + char* Identify( char* p, XMLNode** node, bool first ); + + // internal + void MarkInUse(const XMLNode* const); + + virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const override{ + return 0; + } + virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const override{ + return false; + } + +private: + XMLDocument( const XMLDocument& ); // not supported + void operator=( const XMLDocument& ); // not supported + + bool _writeBOM; + bool _processEntities; + XMLError _errorID; + Whitespace _whitespaceMode; + mutable StrPair _errorStr; + int _errorLineNum; + char* _charBuffer; + int _parseCurLineNum; + int _parsingDepth; + // Memory tracking does add some overhead. + // However, the code assumes that you don't + // have a bunch of unlinked nodes around. + // Therefore it takes less memory to track + // in the document vs. a linked list in the XMLNode, + // and the performance is the same. + DynArray _unlinked; + + MemPoolT< sizeof(XMLElement) > _elementPool; + MemPoolT< sizeof(XMLAttribute) > _attributePool; + MemPoolT< sizeof(XMLText) > _textPool; + MemPoolT< sizeof(XMLComment) > _commentPool; + + static const char* _errorNames[XML_ERROR_COUNT]; + + void Parse(); + + void SetError( XMLError error, int lineNum, const char* format, ... ); + + // Something of an obvious security hole, once it was discovered. + // Either an ill-formed XML or an excessively deep one can overflow + // the stack. Track stack depth, and error out if needed. + class DepthTracker { + public: + explicit DepthTracker(XMLDocument * document) { + this->_document = document; + document->PushDepth(); + } + ~DepthTracker() { + _document->PopDepth(); + } + private: + XMLDocument * _document; + }; + void PushDepth(); + void PopDepth(); + + template + NodeType* CreateUnlinkedNode( MemPoolT& pool ); +}; + +template +inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT& pool ) +{ + TIXMLASSERT( sizeof( NodeType ) == PoolElementSize ); + TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() ); + NodeType* returnNode = new (pool.Alloc()) NodeType( this ); + TIXMLASSERT( returnNode ); + returnNode->_memPool = &pool; + + _unlinked.Push(returnNode); + return returnNode; +} + +/** + A XMLHandle is a class that wraps a node pointer with null checks; this is + an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2 + DOM structure. It is a separate utility class. + + Take an example: + @verbatim + + + + + + + @endverbatim + + Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very + easy to write a *lot* of code that looks like: + + @verbatim + XMLElement* root = document.FirstChildElement( "Document" ); + if ( root ) + { + XMLElement* element = root->FirstChildElement( "Element" ); + if ( element ) + { + XMLElement* child = element->FirstChildElement( "Child" ); + if ( child ) + { + XMLElement* child2 = child->NextSiblingElement( "Child" ); + if ( child2 ) + { + // Finally do something useful. + @endverbatim + + And that doesn't even cover "else" cases. XMLHandle addresses the verbosity + of such code. A XMLHandle checks for null pointers so it is perfectly safe + and correct to use: + + @verbatim + XMLHandle docHandle( &document ); + XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement(); + if ( child2 ) + { + // do something useful + @endverbatim + + Which is MUCH more concise and useful. + + It is also safe to copy handles - internally they are nothing more than node pointers. + @verbatim + XMLHandle handleCopy = handle; + @endverbatim + + See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects. +*/ +class TINYXML2_LIB XMLHandle +{ +public: + /// Create a handle from any node (at any depth of the tree.) This can be a null pointer. + explicit XMLHandle( XMLNode* node ) : _node( node ) { + } + /// Create a handle from a node. + explicit XMLHandle( XMLNode& node ) : _node( &node ) { + } + /// Copy constructor + XMLHandle( const XMLHandle& ref ) : _node( ref._node ) { + } + /// Assignment + XMLHandle& operator=( const XMLHandle& ref ) { + _node = ref._node; + return *this; + } + + /// Get the first child of this handle. + XMLHandle FirstChild() { + return XMLHandle( _node ? _node->FirstChild() : 0 ); + } + /// Get the first child element of this handle. + XMLHandle FirstChildElement( const char* name = 0 ) { + return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 ); + } + /// Get the last child of this handle. + XMLHandle LastChild() { + return XMLHandle( _node ? _node->LastChild() : 0 ); + } + /// Get the last child element of this handle. + XMLHandle LastChildElement( const char* name = 0 ) { + return XMLHandle( _node ? _node->LastChildElement( name ) : 0 ); + } + /// Get the previous sibling of this handle. + XMLHandle PreviousSibling() { + return XMLHandle( _node ? _node->PreviousSibling() : 0 ); + } + /// Get the previous sibling element of this handle. + XMLHandle PreviousSiblingElement( const char* name = 0 ) { + return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 ); + } + /// Get the next sibling of this handle. + XMLHandle NextSibling() { + return XMLHandle( _node ? _node->NextSibling() : 0 ); + } + /// Get the next sibling element of this handle. + XMLHandle NextSiblingElement( const char* name = 0 ) { + return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 ); + } + + /// Safe cast to XMLNode. This can return null. + XMLNode* ToNode() { + return _node; + } + /// Safe cast to XMLElement. This can return null. + XMLElement* ToElement() { + return ( _node ? _node->ToElement() : 0 ); + } + /// Safe cast to XMLText. This can return null. + XMLText* ToText() { + return ( _node ? _node->ToText() : 0 ); + } + /// Safe cast to XMLUnknown. This can return null. + XMLUnknown* ToUnknown() { + return ( _node ? _node->ToUnknown() : 0 ); + } + /// Safe cast to XMLDeclaration. This can return null. + XMLDeclaration* ToDeclaration() { + return ( _node ? _node->ToDeclaration() : 0 ); + } + +private: + XMLNode* _node; +}; + + +/** + A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the + same in all regards, except for the 'const' qualifiers. See XMLHandle for API. +*/ +class TINYXML2_LIB XMLConstHandle +{ +public: + explicit XMLConstHandle( const XMLNode* node ) : _node( node ) { + } + explicit XMLConstHandle( const XMLNode& node ) : _node( &node ) { + } + XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) { + } + + XMLConstHandle& operator=( const XMLConstHandle& ref ) { + _node = ref._node; + return *this; + } + + const XMLConstHandle FirstChild() const { + return XMLConstHandle( _node ? _node->FirstChild() : 0 ); + } + const XMLConstHandle FirstChildElement( const char* name = 0 ) const { + return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 ); + } + const XMLConstHandle LastChild() const { + return XMLConstHandle( _node ? _node->LastChild() : 0 ); + } + const XMLConstHandle LastChildElement( const char* name = 0 ) const { + return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 ); + } + const XMLConstHandle PreviousSibling() const { + return XMLConstHandle( _node ? _node->PreviousSibling() : 0 ); + } + const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const { + return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 ); + } + const XMLConstHandle NextSibling() const { + return XMLConstHandle( _node ? _node->NextSibling() : 0 ); + } + const XMLConstHandle NextSiblingElement( const char* name = 0 ) const { + return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 ); + } + + + const XMLNode* ToNode() const { + return _node; + } + const XMLElement* ToElement() const { + return ( _node ? _node->ToElement() : 0 ); + } + const XMLText* ToText() const { + return ( _node ? _node->ToText() : 0 ); + } + const XMLUnknown* ToUnknown() const { + return ( _node ? _node->ToUnknown() : 0 ); + } + const XMLDeclaration* ToDeclaration() const { + return ( _node ? _node->ToDeclaration() : 0 ); + } + +private: + const XMLNode* _node; +}; + + +/** + Printing functionality. The XMLPrinter gives you more + options than the XMLDocument::Print() method. + + It can: + -# Print to memory. + -# Print to a file you provide. + -# Print XML without a XMLDocument. + + Print to Memory + + @verbatim + XMLPrinter printer; + doc.Print( &printer ); + SomeFunction( printer.CStr() ); + @endverbatim + + Print to a File + + You provide the file pointer. + @verbatim + XMLPrinter printer( fp ); + doc.Print( &printer ); + @endverbatim + + Print without a XMLDocument + + When loading, an XML parser is very useful. However, sometimes + when saving, it just gets in the way. The code is often set up + for streaming, and constructing the DOM is just overhead. + + The Printer supports the streaming case. The following code + prints out a trivially simple XML file without ever creating + an XML document. + + @verbatim + XMLPrinter printer( fp ); + printer.OpenElement( "foo" ); + printer.PushAttribute( "foo", "bar" ); + printer.CloseElement(); + @endverbatim +*/ +class TINYXML2_LIB XMLPrinter : public XMLVisitor +{ +public: + /** Construct the printer. If the FILE* is specified, + this will print to the FILE. Else it will print + to memory, and the result is available in CStr(). + If 'compact' is set to true, then output is created + with only required whitespace and newlines. + */ + XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 ); + virtual ~XMLPrinter() {} + + /** If streaming, write the BOM and declaration. */ + void PushHeader( bool writeBOM, bool writeDeclaration ); + /** If streaming, start writing an element. + The element must be closed with CloseElement() + */ + void OpenElement( const char* name, bool compactMode=false ); + /// If streaming, add an attribute to an open element. + void PushAttribute( const char* name, const char* value ); + void PushAttribute( const char* name, int value ); + void PushAttribute( const char* name, unsigned value ); + void PushAttribute( const char* name, int64_t value ); + void PushAttribute( const char* name, uint64_t value ); + void PushAttribute( const char* name, bool value ); + void PushAttribute( const char* name, double value ); + /// If streaming, close the Element. + virtual void CloseElement( bool compactMode=false ); + + /// Add a text node. + void PushText( const char* text, bool cdata=false ); + /// Add a text node from an integer. + void PushText( int value ); + /// Add a text node from an unsigned. + void PushText( unsigned value ); + /// Add a text node from a signed 64bit integer. + void PushText( int64_t value ); + /// Add a text node from an unsigned 64bit integer. + void PushText( uint64_t value ); + /// Add a text node from a bool. + void PushText( bool value ); + /// Add a text node from a float. + void PushText( float value ); + /// Add a text node from a double. + void PushText( double value ); + + /// Add a comment + void PushComment( const char* comment ); + + void PushDeclaration( const char* value ); + void PushUnknown( const char* value ); + + virtual bool VisitEnter( const XMLDocument& /*doc*/ ) override; + virtual bool VisitExit( const XMLDocument& /*doc*/ ) override { + return true; + } + + virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) override; + virtual bool VisitExit( const XMLElement& element ) override; + + virtual bool Visit( const XMLText& text ) override; + virtual bool Visit( const XMLComment& comment ) override; + virtual bool Visit( const XMLDeclaration& declaration ) override; + virtual bool Visit( const XMLUnknown& unknown ) override; + + /** + If in print to memory mode, return a pointer to + the XML file in memory. + */ + const char* CStr() const { + return _buffer.Mem(); + } + /** + If in print to memory mode, return the size + of the XML file in memory. (Note the size returned + includes the terminating null.) + */ + int CStrSize() const { + return _buffer.Size(); + } + /** + If in print to memory mode, reset the buffer to the + beginning. + */ + void ClearBuffer( bool resetToFirstElement = true ) { + _buffer.Clear(); + _buffer.Push(0); + _firstElement = resetToFirstElement; + } + +protected: + virtual bool CompactMode( const XMLElement& ) { return _compactMode; } + + /** Prints out the space before an element. You may override to change + the space and tabs used. A PrintSpace() override should call Print(). + */ + virtual void PrintSpace( int depth ); + virtual void Print( const char* format, ... ); + virtual void Write( const char* data, size_t size ); + virtual void Putc( char ch ); + + inline void Write(const char* data) { Write(data, strlen(data)); } + + void SealElementIfJustOpened(); + bool _elementJustOpened; + DynArray< const char*, 10 > _stack; + +private: + /** + Prepares to write a new node. This includes sealing an element that was + just opened, and writing any whitespace necessary if not in compact mode. + */ + void PrepareForNewNode( bool compactMode ); + void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities. + + bool _firstElement; + FILE* _fp; + int _depth; + int _textDepth; + bool _processEntities; + bool _compactMode; + + enum { + ENTITY_RANGE = 64, + BUF_SIZE = 200 + }; + bool _entityFlag[ENTITY_RANGE]; + bool _restrictedEntityFlag[ENTITY_RANGE]; + + DynArray< char, 20 > _buffer; + + // Prohibit cloning, intentionally not implemented + XMLPrinter( const XMLPrinter& ); + XMLPrinter& operator=( const XMLPrinter& ); +}; + + +} // tinyxml2 + +#if defined(_MSC_VER) +# pragma warning(pop) +#endif + +#endif // TINYXML2_INCLUDED \ No newline at end of file diff --git a/src/lib/memory/FixedLatencyMemoryInterface.cc b/src/lib/memory/FixedLatencyMemoryInterface.cc index ee33ce7357..a0fb05a403 100644 --- a/src/lib/memory/FixedLatencyMemoryInterface.cc +++ b/src/lib/memory/FixedLatencyMemoryInterface.cc @@ -80,5 +80,15 @@ bool FixedLatencyMemoryInterface::hasPendingRequests() const { return !pendingRequests_.empty(); } +void FixedLatencyMemoryInterface::rawRead(void* dest, uint64_t src, + uint16_t size) const { + memcpy(dest, memory_ + src, size); +} + +void FixedLatencyMemoryInterface::rawWrite(uint64_t dest, void* src, + uint16_t size) { + memcpy(memory_ + dest, src, size); +} + } // namespace memory } // namespace simeng diff --git a/src/lib/memory/FlatMemoryInterface.cc b/src/lib/memory/FlatMemoryInterface.cc index 4d46db8781..4e0d4daa24 100644 --- a/src/lib/memory/FlatMemoryInterface.cc +++ b/src/lib/memory/FlatMemoryInterface.cc @@ -47,6 +47,15 @@ void FlatMemoryInterface::clearCompletedReads() { completedReads_.clear(); } bool FlatMemoryInterface::hasPendingRequests() const { return false; } +void FlatMemoryInterface::rawRead(void* dest, uint64_t src, + uint16_t size) const { + memcpy(dest, memory_ + src, size); +} + +void FlatMemoryInterface::rawWrite(uint64_t dest, void* src, uint16_t size) { + memcpy(memory_ + dest, src, size); +} + void FlatMemoryInterface::tick() {} } // namespace memory diff --git a/src/lib/models/emulation/Core.cc b/src/lib/models/emulation/Core.cc index bf0129b5ee..faad8e7e4a 100644 --- a/src/lib/models/emulation/Core.cc +++ b/src/lib/models/emulation/Core.cc @@ -44,12 +44,20 @@ void Core::tick() { return; } - ticks_++; - isa_.updateSystemTimerRegisters(®isterFileSet_, ticks_); + if (macroOp_.empty()) { + ticks_++; + isa_.updateSystemTimerRegisters(®isterFileSet_, ticks_); + } else { + macroOp_.clear(); + } // Fetch & Decode assert(macroOp_.empty() && "Cannot begin emulation tick with un-executed micro-ops."); + + // Fetch memory for next cycle + instructionMemory_.requestRead({pc_, FETCH_SIZE}); + // We only fetch one instruction at a time, so only ever one result in // complete reads const auto& instructionBytes = instructionMemory_.getCompletedReads()[0].data; @@ -59,6 +67,8 @@ void Core::tick() { // Clear the fetched data instructionMemory_.clearCompletedReads(); + const auto prevPC = pc_; + pc_ += bytesRead; // Loop over all micro-ops and execute one by one @@ -66,7 +76,11 @@ void Core::tick() { auto& uop = macroOp_.front(); if (uop->exceptionEncountered()) { - handleException(uop); + if (handleException(uop)) { + // syscall catch + pc_ = prevPC; + return; + } // If fatal, return if (hasHalted_) return; } @@ -83,9 +97,40 @@ void Core::tick() { // Execute & Write-back if (uop->isLoad()) { auto addresses = uop->generateAddresses(); + + if (rp_) { + for (const auto& rp : *rp_) { + for (const auto& address : addresses) { + if (rp.overlaps(address)) { + br_ = {BreakReason::Read, address.address, + uop->getInstructionAddress()}; + pc_ = prevPC; + return; + } + } + } + } + + if (ap_) { + for (const auto& ap : *ap_) { + for (const auto& address : addresses) { + if (ap.overlaps(address)) { + br_ = {BreakReason::Access, address.address, + uop->getInstructionAddress()}; + pc_ = prevPC; + return; + } + } + } + } + previousAddresses_.clear(); if (uop->exceptionEncountered()) { - handleException(uop); + if (handleException(uop)) { + // syscall catch + pc_ = prevPC; + return; + } // If fatal, return if (hasHalted_) return; } @@ -111,9 +156,40 @@ void Core::tick() { } } else if (uop->isStoreAddress()) { auto addresses = uop->generateAddresses(); + + if (wp_) { + for (const auto& wp : *wp_) { + for (const auto& address : addresses) { + if (wp.overlaps(address)) { + br_ = {BreakReason::Write, address.address, + uop->getInstructionAddress()}; + pc_ = prevPC; + return; + } + } + } + } + + if (ap_) { + for (const auto& ap : *ap_) { + for (const auto& address : addresses) { + if (ap.overlaps(address)) { + br_ = {BreakReason::Access, address.address, + uop->getInstructionAddress()}; + pc_ = prevPC; + return; + } + } + } + } + previousAddresses_.clear(); if (uop->exceptionEncountered()) { - handleException(uop); + if (handleException(uop)) { + // syscall catch + pc_ = prevPC; + return; + } // If fatal, return if (hasHalted_) return; } @@ -127,12 +203,28 @@ void Core::tick() { continue; } } - execute(uop); + if (execute(uop)) { + // syscall catch + pc_ = prevPC; + return; + } macroOp_.erase(macroOp_.begin()); } + + if ((step_from_ != nullptr) && (*step_from_)) { + if (pc_ != **step_from_) { + br_ = {BreakReason::Break, 0, pc_}; + } + } else if (bp_ != nullptr) { + for (const auto bp : *bp_) { + if (bp == pc_) { + br_ = {BreakReason::Break, 0, pc_}; + break; + } + } + } + instructionsExecuted_++; - // Fetch memory for next cycle - instructionMemory_.requestRead({pc_, FETCH_SIZE}); } bool Core::hasHalted() const { return hasHalted_; } @@ -152,12 +244,11 @@ std::map Core::getStats() const { {"branch.executed", std::to_string(branchesExecuted_)}}; } -void Core::execute(std::shared_ptr& uop) { +bool Core::execute(std::shared_ptr& uop) { uop->execute(); if (uop->exceptionEncountered()) { - handleException(uop); - return; + return handleException(uop); } if (uop->isStoreData()) { @@ -177,11 +268,51 @@ void Core::execute(std::shared_ptr& uop) { auto reg = destinations[i]; registerFileSet_.set(reg, results[i]); } + + return false; } -void Core::handleException(const std::shared_ptr& instruction) { +bool Core::handleException(const std::shared_ptr& instruction) { + if (instruction->isSyscall()) { + const auto& isa = getISA(); + + const auto syscallID = getArchitecturalRegisterFileSet() + .get(isa.getSyscallIDReg()) + .get(); + + // hack to retrieve the exit code + if ((syscallID == 93) || (syscallID == 94)) { + exit_code_ = getArchitecturalRegisterFileSet() + .get(isa.getExitCodeReg()) + .get(); + } + + if ((syscalls_ != nullptr) && (*syscalls_) && (!current_syscall_)) { + if (std::any_of( + (*syscalls_)->cbegin(), (*syscalls_)->cend(), + [&](const auto syscall) { return syscall == syscallID; })) { + br_ = simeng::BreakReason{simeng::BreakReason::SyscallEntry, syscallID, + instruction->getNextInstructionAddress()}; + brn_ = + simeng::BreakReason{simeng::BreakReason::SyscallReturn, syscallID, + instruction->getNextInstructionAddress()}; + current_syscall_ = syscallID; + return true; + } + } + } + exceptionHandler_ = isa_.handleException(instruction, *this, dataMemory_); processExceptionHandler(); + + current_syscall_ = std::nullopt; + + if (brn_) { + br_ = brn_; + brn_ = std::nullopt; + } + + return false; } void Core::processExceptionHandler() { @@ -207,6 +338,34 @@ void Core::processExceptionHandler() { exceptionHandler_ = nullptr; } +const uint64_t Core::getProgramCounter() const { return pc_; } + +void Core::setProgramCounter(uint64_t pc) { + pc_ = pc; + // may need to be removed when rebased on dev + instructionMemory_.requestRead({pc_, FETCH_SIZE}); +} + +void Core::prepareBreakpoints( + const std::optional* step_from, const std::vector* bp, + const std::vector* wp, + const std::vector* rp, + const std::vector* ap, + const std::optional>* syscalls) { + br_ = std::nullopt; + + step_from_ = step_from; + bp_ = bp; + wp_ = wp; + rp_ = rp; + ap_ = ap; + syscalls_ = syscalls; +} + +const std::optional Core::getBreakReason() const { + return br_; +} + } // namespace emulation } // namespace models } // namespace simeng diff --git a/src/lib/models/inorder/Core.cc b/src/lib/models/inorder/Core.cc index b196d2cf8c..fc15095951 100644 --- a/src/lib/models/inorder/Core.cc +++ b/src/lib/models/inorder/Core.cc @@ -315,6 +315,14 @@ void Core::readRegisters() { } } +const uint64_t Core::getProgramCounter() const { return fetchUnit_.getPC(); } + +void Core::setProgramCounter(uint64_t pc) { fetchUnit_.updatePC(pc); } + +const std::optional Core::getBreakReason() const { + return std::nullopt; +} + } // namespace inorder } // namespace models } // namespace simeng diff --git a/src/lib/models/outoforder/Core.cc b/src/lib/models/outoforder/Core.cc index 4f7cf0f42d..967a700feb 100644 --- a/src/lib/models/outoforder/Core.cc +++ b/src/lib/models/outoforder/Core.cc @@ -298,6 +298,37 @@ void Core::processExceptionHandler() { return; } + if (exceptionGeneratingInstruction_->isSyscall()) { + const auto& isa = getISA(); + + const auto syscallID = getArchitecturalRegisterFileSet() + .get(isa.getSyscallIDReg()) + .get(); + + // hack to retrieve the exit code + if ((syscallID == 93) || (syscallID == 94)) { + exit_code_ = getArchitecturalRegisterFileSet() + .get(isa.getExitCodeReg()) + .get(); + } + + if ((syscalls_ != nullptr) && (*syscalls_) && (!current_syscall_)) { + if (std::any_of( + (*syscalls_)->cbegin(), (*syscalls_)->cend(), + [&](const auto syscall) { return syscall == syscallID; })) { + reorderBuffer_.setBreakReasons( + simeng::BreakReason{ + simeng::BreakReason::SyscallEntry, syscallID, + exceptionGeneratingInstruction_->getNextInstructionAddress()}, + simeng::BreakReason{ + simeng::BreakReason::SyscallReturn, syscallID, + exceptionGeneratingInstruction_->getNextInstructionAddress()}); + current_syscall_ = syscallID; + return; + } + } + } + bool success = exceptionHandler_->tick(); if (!success) { // Exception handler requires further ticks to complete @@ -315,6 +346,8 @@ void Core::processExceptionHandler() { applyStateChange(result.stateChange); } + current_syscall_ = std::nullopt; + exceptionHandler_ = nullptr; } @@ -378,6 +411,30 @@ void Core::flushIfNeeded() { } } +const uint64_t Core::getProgramCounter() const { + // only useful to get the initial entrypoint + return fetchUnit_.getPC(); +} + +void Core::setProgramCounter(uint64_t pc) { + fetchUnit_.updatePC(pc); + reorderBuffer_.clobberAfter(1, pc); +} + +void Core::prepareBreakpoints( + const std::optional* step_from, const std::vector* bp, + const std::vector* wp, + const std::vector* rp, + const std::vector* ap, + const std::optional>* syscalls) { + reorderBuffer_.prepareBreakpoints(step_from, bp, wp, rp, ap); + syscalls_ = syscalls; +} + +const std::optional Core::getBreakReason() const { + return reorderBuffer_.getBreakReason(); +} + } // namespace outoforder } // namespace models } // namespace simeng diff --git a/src/lib/pipeline/FetchUnit.cc b/src/lib/pipeline/FetchUnit.cc index 553c8198f4..ec58d61aba 100644 --- a/src/lib/pipeline/FetchUnit.cc +++ b/src/lib/pipeline/FetchUnit.cc @@ -271,5 +271,7 @@ void FetchUnit::flushLoopBuffer() { loopBoundaryAddress_ = 0; } +const uint64_t FetchUnit::getPC() const { return pc_; } + } // namespace pipeline } // namespace simeng diff --git a/src/lib/pipeline/MappedRegisterFileSet.cc b/src/lib/pipeline/MappedRegisterFileSet.cc index db75641beb..a909ef4194 100644 --- a/src/lib/pipeline/MappedRegisterFileSet.cc +++ b/src/lib/pipeline/MappedRegisterFileSet.cc @@ -8,11 +8,11 @@ MappedRegisterFileSet::MappedRegisterFileSet( : ArchitecturalRegisterFileSet(physicalRegisterFileSet), rat_(rat) {} const RegisterValue& MappedRegisterFileSet::get(Register reg) const { - return ArchitecturalRegisterFileSet::get(rat_.getMapping(reg)); + return ArchitecturalRegisterFileSet::get(rat_.getCommittedMapping(reg)); } void MappedRegisterFileSet::set(Register reg, const RegisterValue& value) { - return ArchitecturalRegisterFileSet::set(rat_.getMapping(reg), value); + return ArchitecturalRegisterFileSet::set(rat_.getCommittedMapping(reg), value); } } // namespace pipeline diff --git a/src/lib/pipeline/RegisterAliasTable.cc b/src/lib/pipeline/RegisterAliasTable.cc index 2a67585831..611a510460 100644 --- a/src/lib/pipeline/RegisterAliasTable.cc +++ b/src/lib/pipeline/RegisterAliasTable.cc @@ -9,6 +9,7 @@ RegisterAliasTable::RegisterAliasTable( std::vector architecturalStructure, std::vector physicalRegisterCounts) : mappingTable_(architecturalStructure.size()), + commitTable_(architecturalStructure.size()), historyTable_(architecturalStructure.size()), destinationTable_(architecturalStructure.size()), freeQueues_(architecturalStructure.size()) { @@ -24,10 +25,12 @@ RegisterAliasTable::RegisterAliasTable( // Set up the initial mapping table state for this register type mappingTable_[type].resize(archCount); + commitTable_[type].resize(archCount); for (size_t tag = 0; tag < archCount; tag++) { // Pre-assign a physical register to each architectural register mappingTable_[type][tag] = tag; + commitTable_[type][tag] = tag; } // Add remaining physical registers to free queue @@ -53,6 +56,18 @@ Register RegisterAliasTable::getMapping(Register architectural) const { return {architectural.type, tag, true}; } +Register RegisterAliasTable::getCommittedMapping(Register architectural) const { + // Asserts to ensure mapping isn't attempted for an out-of-bound index (i.e. + // mapping of WZR / XZR) + assert(architectural.type < mappingTable_.size() && + "Invalid register type. Cannot find RAT mapping."); + assert(architectural.type >= 0 && + "Invalid register type. Cannot find RAT mapping."); + + const auto tag = commitTable_[architectural.type][architectural.tag]; + return {architectural.type, tag, true}; +} + bool RegisterAliasTable::canAllocate(uint8_t type, unsigned int quantity) const { return (freeQueues_[type].size() >= quantity); @@ -92,6 +107,9 @@ void RegisterAliasTable::commit(Register physical) { // free it auto oldTag = historyTable_[physical.type][physical.tag]; freeQueues_[physical.type].push(oldTag); + + const auto archTag = destinationTable_[physical.type][physical.tag]; + commitTable_[physical.type][archTag] = physical.tag; } void RegisterAliasTable::rewind(Register physical) { diff --git a/src/lib/pipeline/ReorderBuffer.cc b/src/lib/pipeline/ReorderBuffer.cc index 32889bf93e..fa43c44443 100644 --- a/src/lib/pipeline/ReorderBuffer.cc +++ b/src/lib/pipeline/ReorderBuffer.cc @@ -79,12 +79,39 @@ unsigned int ReorderBuffer::commit(uint64_t maxCommitSize) { std::min(static_cast(maxCommitSize), buffer_.size()); unsigned int n; - for (n = 0; n < maxCommits; n++) { + for (n = 0; (n < maxCommits) && (!br_); n++) { auto& uop = buffer_[0]; if (!uop->canCommit()) { break; } + // TODO: is it possible for putting this ↓ before uop->canCommit() to cause + // problems? + + const auto pc = uop->getInstructionAddress(); + pc_ = pc; + + if (brn_) { + br_ = brn_; + brn_ = std::nullopt; + break; + } + + if ((step_from_ != nullptr) && (*step_from_)) { + if (pc != **step_from_) { + br_ = simeng::BreakReason{simeng::BreakReason::Break, 0, pc}; + // breakpoints hit before the instruction is executed + break; + } + } else if (bp_ != nullptr) { + if (std::any_of(bp_->cbegin(), bp_->cend(), + [&](const auto bp) { return pc == bp; })) { + br_ = simeng::BreakReason{simeng::BreakReason::Break, 0, pc}; + // breakpoints hit before the instruction is executed + break; + } + } + if (uop->isLastMicroOp()) instructionsCommitted_++; if (uop->exceptionEncountered()) { @@ -100,17 +127,64 @@ unsigned int ReorderBuffer::commit(uint64_t maxCommitSize) { // If it's a memory op, commit the entry at the head of the respective queue if (uop->isLoad()) { + const auto addresses = uop->getGeneratedAddresses(); + + if (rp_) { + for (const auto& rp : *rp_) { + for (const auto& address : addresses) { + if (rp.overlaps(address)) { + br_ = {BreakReason::Read, address.address, + uop->getInstructionAddress()}; + } + } + } + } + + if (ap_) { + for (const auto& ap : *ap_) { + for (const auto& address : addresses) { + if (ap.overlaps(address)) { + br_ = {BreakReason::Access, address.address, + uop->getInstructionAddress()}; + } + } + } + } + lsq_.commitLoad(uop); } if (uop->isStoreAddress()) { + auto addresses = uop->generateAddresses(); + + if (wp_) { + for (const auto& wp : *wp_) { + for (const auto& address : addresses) { + if (wp.overlaps(address)) { + br_ = {BreakReason::Write, address.address, + uop->getInstructionAddress()}; + } + } + } + } + + if (ap_) { + for (const auto& ap : *ap_) { + for (const auto& address : addresses) { + if (ap.overlaps(address)) { + br_ = {BreakReason::Access, address.address, + uop->getInstructionAddress()}; + } + } + } + } + bool violationFound = lsq_.commitStore(uop); if (violationFound) { loadViolations_++; // Memory order violation found; aborting commits and flushing auto load = lsq_.getViolatingLoad(); - shouldFlush_ = true; - flushAfter_ = load->getInstructionId() - 1; - pc_ = load->getInstructionAddress(); + clobberAfter(load->getInstructionId() - 1, + load->getInstructionAddress()); buffer_.pop_front(); return n + 1; @@ -206,5 +280,38 @@ uint64_t ReorderBuffer::getViolatingLoadsCount() const { return loadViolations_; } +void ReorderBuffer::clobberAfter(uint64_t id, uint64_t pc) { + shouldFlush_ = true; + flushAfter_ = id; + pc_ = pc; +} + +void ReorderBuffer::prepareBreakpoints( + const std::optional* step_from, const std::vector* bp, + const std::vector* wp, + const std::vector* rp, + const std::vector* ap) { + br_ = std::nullopt; + + step_from_ = step_from; + bp_ = bp; + wp_ = wp; + rp_ = rp; + ap_ = ap; +} + +const std::optional ReorderBuffer::getBreakReason() const { + return br_; +} + +void ReorderBuffer::setBreakReasons( + std::optional reason, + std::optional next_reason) { + br_ = reason; + brn_ = next_reason; +} + +const uint64_t ReorderBuffer::getPC() const { return pc_; } + } // namespace pipeline } // namespace simeng diff --git a/src/tools/simeng/CMakeLists.txt b/src/tools/simeng/CMakeLists.txt index c8de0d34a9..98558f66e9 100644 --- a/src/tools/simeng/CMakeLists.txt +++ b/src/tools/simeng/CMakeLists.txt @@ -6,6 +6,10 @@ if( YAML_OUTPUT ) target_compile_definitions(simeng PRIVATE -DYAML_OUTPUT) endif() +if (SIMENG_ENABLE_GDB) + target_compile_definitions(simeng PUBLIC -DGDB_ENABLED) +endif() + target_include_directories(simeng PUBLIC ${PROJECT_SOURCE_DIR}/src/lib) target_link_libraries(simeng libsimeng) diff --git a/src/tools/simeng/main.cc b/src/tools/simeng/main.cc index 68ec40e86d..9a23bf807e 100644 --- a/src/tools/simeng/main.cc +++ b/src/tools/simeng/main.cc @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -7,6 +9,7 @@ #include "simeng/Core.hh" #include "simeng/CoreInstance.hh" #include "simeng/config/SimInfo.hh" +#include "simeng/gdb/GDBStub.hh" #include "simeng/memory/MemoryInterface.hh" #include "simeng/version.hh" @@ -41,8 +44,105 @@ int main(int argc, char** argv) { std::cout << "[SimEng] \tCompile options: " SIMENG_COMPILE_OPTIONS << std::endl; std::cout << "[SimEng] \tTest suite: " SIMENG_ENABLE_TESTS << std::endl; + std::cout << "[SimEng] \tGDB stub enabled: " SIMENG_ENABLE_GDB << std::endl; std::cout << std::endl; +#if GDB_ENABLED + + // Parse options first using getopt_long + auto gdb_verbose = false; + uint16_t gdb_port = 2424; + auto use_gdb = false; + + const std::string prog_name = argv[0]; + + while (true) { + static const struct option long_options[] = { + {"gdb-verbose", no_argument, nullptr, 'v'}, + {"gdb-port", required_argument, nullptr, 'p'}, + {"use-gdb", no_argument, nullptr, 'g'}, + {"help", no_argument, nullptr, 'h'}, + {0, 0, 0, 0}}; + + int option_index = 0; + + const int c = + getopt_long(argc, argv, "gvp:h?", long_options, &option_index); + + if (c == -1) { + break; + } + + switch (c) { + case '0': { + } + + case 'g': { + use_gdb = true; + break; + } + + case 'v': { + use_gdb = true; + gdb_verbose = true; + break; + } + + case 'p': { + use_gdb = true; + + int port; + try { + port = std::stoi(optarg); + } catch (const std::exception& e) { + std::cerr << "[SimEng] Invalid port number: " << optarg << std::endl; + exit(EXIT_FAILURE); + } + + if ((port < 0) || (port > UINT16_MAX)) { + std::cerr << "[SimEng] Port out of range: " << port << std::endl; + exit(EXIT_FAILURE); + } + + gdb_port = static_cast(port); + break; + } + + case 'h': + case '?': { + std::cout << prog_name << " usage:\n" + << std::endl + << "\t--use-gdb, -g : enable GDB " + "stub" + << std::endl + << "\t--gdb-verbose, -v : print verbose " + "communication info (implies --use-gdb)" + << std::endl + << "\t--gdb-port, -p port: listen on " + "port (default: 2424, implies --use-gdb)" + << std::endl + << "\t--help, -h -? : " + "show this help" + << std::endl; + + exit(EXIT_FAILURE); + } + + default: { + std::cerr << "[SimEng] Ignoring unrecognised option '" << c << "'" + << std::endl; + break; + } + } + } + + // continue argument parsing as usual + // argv[0] will be incorrect, but that's not used here + argc -= optind - 1; + argv += optind - 1; + +#endif + // Create the instance of the core to be simulated std::unique_ptr coreInstance; std::string executablePath = ""; @@ -119,7 +219,18 @@ int main(int argc, char** argv) { std::cout << "[SimEng] Starting...\n" << std::endl; uint64_t iterations = 0; auto startTime = std::chrono::high_resolution_clock::now(); - iterations = simulate(*core, *dataMemory, *instructionMemory); + +#if GDB_ENABLED + + if (use_gdb) { + auto GDBStub = simeng::GDBStub(*coreInstance, gdb_verbose, gdb_port); + iterations = GDBStub.run(); + } else { +#endif + iterations = simulate(*core, *dataMemory, *instructionMemory); +#if GDB_ENABLED + } +#endif // Get timing information auto endTime = std::chrono::high_resolution_clock::now(); @@ -137,9 +248,17 @@ int main(int argc, char** argv) { std::cout << "[SimEng] " << key << ": " << value << std::endl; } std::cout << std::endl; - std::cout << "[SimEng] Finished " << iterations << " ticks in " << duration - << "ms (" << std::round(khz) << " kHz, " << std::setprecision(2) - << mips << " MIPS)" << std::endl; + +#if GDB_ENABLED + // Timing stats are useless when using GDB + if (!use_gdb) { +#endif + std::cout << "[SimEng] Finished " << iterations << " ticks in " << duration + << "ms (" << std::round(khz) << " kHz, " << std::setprecision(2) + << mips << " MIPS)" << std::endl; +#if GDB_ENABLED + } +#endif // Print build metadata and core statistics in YAML format // to facilitate parsing. Print "YAML-SEQ" to indicate beginning diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ee2d48491a..e5a74de115 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(unit) add_subdirectory(regression) add_subdirectory(integration) +add_subdirectory(gdb) diff --git a/test/gdb/CMakeLists.txt b/test/gdb/CMakeLists.txt new file mode 100644 index 0000000000..c68ac935dc --- /dev/null +++ b/test/gdb/CMakeLists.txt @@ -0,0 +1,17 @@ +set(TEST_SOURCES + GDBTest.cc +) + +add_executable(gdbtests ${TEST_SOURCES}) + +target_include_directories(gdbtests PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) +target_include_directories(gdbtests PUBLIC ${PROJECT_SOURCE_DIR}/src/lib) +target_link_libraries(gdbtests libsimeng) +target_link_libraries(gdbtests gmock_main) +target_compile_options(gdbtests PRIVATE ${SIMENG_COMPILE_OPTIONS}) + +add_test(NAME gdb_tests COMMAND gdbtests) + +if (SIMENG_ENABLE_GDB) + target_compile_definitions(gdbtests PUBLIC -DGDB_ENABLED) +endif() diff --git a/test/gdb/GDBStub.cc b/test/gdb/GDBStub.cc new file mode 100644 index 0000000000..dd214f0a70 --- /dev/null +++ b/test/gdb/GDBStub.cc @@ -0,0 +1,2125 @@ +#include "simeng/gdb/GDBStub.hh" + +#include +#include +#include +#include + +#include +#include + +#include "simeng/arch/Architecture.hh" +#include "tinyxml2.h" + +// read buffer size +#define BUF_SIZE (1000) + +// number of requests to queue +#define NUM_REQUESTS (1) + +// colour codes for pretty printing +#define RESET "\033[0m" +#define CYAN "\033[36m" +#define GREEN "\033[32m" +#define RED "\033[31m" + +struct SupportedFeature { + std::string name; + std::optional value; + + std::string format() const; +}; + +std::string SupportedFeature::format() const { + if (value) { + return name + '=' + *value; + } else { + return name + '+'; + } +} + +template +std::string int_to_hex(T i) { + char* ptr; + const auto bytesWritten = + asprintf(&ptr, "%0*x", static_cast(sizeof(T) * 2), i); + const auto str = std::string{ptr, static_cast(bytesWritten)}; + free(ptr); + return str; +} + +template +std::string int_to_hex_ne(T i) { + uint8_t bytes[sizeof(T)]; + memcpy(bytes, &i, sizeof(T)); + std::string rv; + for (const auto byte : bytes) { + rv += int_to_hex(byte); + } + return rv; +} + +template +std::optional hex_to_int_ne(const std::string& str) { + if (str.size() != sizeof(T) * 2) { + return std::nullopt; + } + + uint8_t bytes[sizeof(T)]; + try { + for (size_t i = 0; i < sizeof(T); i++) { + bytes[i] = std::stoi(str.substr(i * 2, 2), nullptr, 16); + } + } catch (const std::exception& e) { + return std::nullopt; + } + + T rv; + + memcpy(&rv, bytes, sizeof(T)); + + return rv; +} + +template +std::optional> hex_to_vect_ne(const std::string& str) { + if (str.size() != N * 2) { + return std::nullopt; + } + + char bytes[N]; + try { + for (size_t i = 0; i < N; i++) { + bytes[i] = std::stoi(str.substr(i * 2, 2), nullptr, 16); + } + } catch (const std::exception& e) { + return std::nullopt; + } + + std::array rv; + std::copy(std::begin(bytes), std::end(bytes), rv.begin()); + + return rv; +} + +const SupportedFeature supported_features[] = { + { + "QStartNoAckMode", + std::nullopt, + }, + {"PacketSize", int_to_hex(BUF_SIZE - 10)}, + {"hwbreak", std::nullopt}, + {"swbreak", std::nullopt}, + {"QCatchSyscalls", std::nullopt}, + {"qXfer:features:read", std::nullopt}}; + +template +std::string formatSignal( + uint8_t signal, + const std::vector, std::string>>& + values) { + if (values.empty()) { + return 'S' + int_to_hex(signal); + } else { + std::string rv; + + rv += 'T'; + rv += int_to_hex(signal); + + for (const auto& param : values) { + const auto& type = std::get<0>(param); + const auto& value = std::get<1>(param); + + switch (type.index()) { + case 0: { + // string + rv += std::get<0>(type); + break; + } + case 1: { + // T + rv += int_to_hex(std::get<1>(type)); + break; + } + } + + rv += ':'; + + rv += value; + + rv += ';'; + } + + return rv; + } +} + +std::string formatError(const std::string& textual_error) { + return "E." + textual_error; +} + +std::string formatError(uint8_t error_num) { + return "E" + int_to_hex(error_num); +} + +std::string formatExit(uint8_t status) { return "W" + int_to_hex(status); } + +std::vector splitBy(const std::string& param_string, + const char by) { + std::vector rv; + std::string cur; + + for (const auto& c : param_string) { + if (c == by) { + rv.push_back(cur); + cur = ""; + } else { + cur += c; + } + } + + rv.push_back(cur); + + return rv; +} + +/*std::tuple, uint8_t> +getCurrentInstruction( const simeng::CoreInstance& coreInstance) { const auto +core = coreInstance.getCore(); const auto instructionMemory = +coreInstance.getInstructionMemory(); const auto pc = core->getProgramCounter(); + const auto& isa = core->getISA(); + + const auto ptr = instructionMemory->getMemoryPointer(); + const auto size = isa.getMaxInstructionSize(); + + uint8_t buffer[size]; + memcpy(buffer, ptr + pc, size); + + simeng::MacroOp macroOp; + auto bytesRead = isa.predecode(buffer, size, pc, macroOp); + + // TODO: is this always valid? + return std::make_tuple(macroOp[0], bytesRead); +}*/ + +enum RegSize { + Byte, + Short, + Word, + ByteWord, + DoubleWord, + Double, + Vector, + Predicate, + PC, + VG, + SVG, + ZA, +}; + +unsigned int getBitsize(RegSize size) { + switch (size) { + case Byte: + return 8; + case Short: + return 16; + case Word: + return 32; + case ByteWord: + return 32; + case DoubleWord: + return 32; + case Double: + return 64; + case Vector: + return 2048; + case Predicate: + return 256; + case PC: + return 64; + case VG: + return 64; + case SVG: + return 64; + case ZA: + return 0; + default: + return 0; + } +} + +using RegList = std::vector>; + +struct TargetSpec { + std::string spec; + RegList regs; +}; + +RegList::value_type makeReg(uint8_t type, uint16_t tag, RegSize size) { + const simeng::Register reg = {type, tag}; + return std::make_tuple(reg, size); +} + +void addReg( + tinyxml2::XMLPrinter& printer, RegList& regs, uint8_t type, uint16_t tag, + const std::string& name, RegSize size, + const std::optional& data_type = std::nullopt, + const std::optional& override_bitsize = std::nullopt) { + printer.OpenElement("reg", true); + + printer.PushAttribute("name", name.c_str()); + printer.PushAttribute( + "bitsize", (override_bitsize) ? *override_bitsize : getBitsize(size)); + if (data_type) { + printer.PushAttribute("type", data_type->c_str()); + } + + printer.CloseElement(true); + + regs.push_back(makeReg(type, tag, size)); +} + +struct FlagsField { + std::string name; + uint8_t start; + uint8_t end; +}; +void addFlags(tinyxml2::XMLPrinter& printer, const std::string& name, + uint8_t size, const std::vector& fields) { + printer.OpenElement("flags"); + + printer.PushAttribute("id", name.c_str()); + printer.PushAttribute("size", size); + + for (const auto& [name, start, end] : fields) { + printer.OpenElement("field", true); + + printer.PushAttribute("name", name.c_str()); + printer.PushAttribute("start", start); + printer.PushAttribute("end", end); + + printer.CloseElement(true); + } + + printer.CloseElement(); +} + +struct UnionField { + std::string name; + std::string type; +}; +void addUnion(tinyxml2::XMLPrinter& printer, const std::string& name, + const std::vector& fields) { + printer.OpenElement("union"); + + printer.PushAttribute("id", name.c_str()); + + for (const auto& [name, type] : fields) { + printer.OpenElement("field", true); + + printer.PushAttribute("name", name.c_str()); + printer.PushAttribute("type", type.c_str()); + + printer.CloseElement(true); + } + + printer.CloseElement(); +} + +void addVector(tinyxml2::XMLPrinter& printer, const std::string& name, + const std::string& type, uint16_t count) { + printer.OpenElement("vector", true); + + printer.PushAttribute("id", name.c_str()); + printer.PushAttribute("type", type.c_str()); + printer.PushAttribute("count", count); + + printer.CloseElement(true); +} + +void deriveCore(tinyxml2::XMLPrinter& printer, RegList& regs) { + printer.OpenElement("feature"); + + printer.PushAttribute("name", "org.gnu.gdb.aarch64.core"); + + for (auto i = 0; i < 32; i++) { + const auto name = "x" + std::to_string(i); + + addReg(printer, regs, 0, i, (i == 31) ? "sp" : ("x" + std::to_string(i)), + Double, (i == 31) ? std::optional("data_ptr") : std::nullopt); + } + + addReg(printer, regs, 0, 0, "pc", PC, "code_ptr"); + + addFlags( + printer, "cpsr_flags", 4, + { + {"SP", 0, 0}, {"EL", 2, 3}, {"nRW", 4, 4}, {"F", 6, 6}, + {"I", 7, 7}, {"A", 8, 8}, {"D", 9, 9}, {"BTYPE", 10, 11}, + {"SSBS", 12, 12}, {"IL", 20, 20}, {"SS", 21, 21}, {"PAN", 22, 22}, + {"UAO", 23, 23}, {"DIT", 24, 24}, {"TCO", 25, 25}, {"V", 28, 28}, + {"C", 29, 29}, {"Z", 30, 30}, {"N", 31, 31}, + }); + + addReg(printer, regs, 3, 0, "cpsr", ByteWord, "cpsr_flags"); + + printer.CloseElement(); +} + +void deriveSVE(tinyxml2::XMLPrinter& printer, RegList& regs) { + printer.OpenElement("feature"); + + printer.PushAttribute("name", "org.gnu.gdb.aarch64.sve"); + + addVector(printer, "svevqu", "uint128", 16); + addVector(printer, "svevqs", "int128", 16); + addVector(printer, "svevdf", "ieee_double", 32); + addVector(printer, "svevdu", "uint64", 32); + addVector(printer, "svevds", "int64", 32); + addVector(printer, "svevsf", "ieee_single", 64); + addVector(printer, "svevsu", "uint32", 64); + addVector(printer, "svevss", "int32", 64); + addVector(printer, "svevhf", "ieee_half", 128); + addVector(printer, "svevhu", "uint16", 128); + addVector(printer, "svevhs", "int16", 128); + addVector(printer, "svevbu", "uint8", 256); + addVector(printer, "svevbs", "int8", 256); + addVector(printer, "svep", "uint8", 32); + + addUnion(printer, "svevnq", + { + {"u", "svevqu"}, + {"s", "svevqs"}, + }); + addUnion(printer, "svevnd", + { + {"f", "svevdf"}, + {"u", "svevdu"}, + {"s", "svevds"}, + }); + addUnion(printer, "svevns", + { + {"f", "svevsf"}, + {"u", "svevsu"}, + {"s", "svevss"}, + }); + addUnion(printer, "svevnh", + { + {"f", "svevhf"}, + {"u", "svevhu"}, + {"s", "svevhs"}, + }); + addUnion(printer, "svevnb", + { + {"u", "svevbu"}, + {"s", "svevbs"}, + }); + addUnion(printer, "svev", + { + {"q", "svevnq"}, + {"d", "svevnd"}, + {"s", "svevns"}, + {"h", "svevnh"}, + {"b", "svevnb"}, + }); + + addFlags(printer, "fpsr_flags", 4, + { + {"IOC", 0, 0}, + {"DZC", 1, 1}, + {"OFC", 2, 2}, + {"UFC", 3, 3}, + {"IXC", 4, 4}, + {"IDC", 7, 7}, + {"QC", 27, 27}, + {"V", 28, 28}, + {"C", 29, 29}, + {"Z", 30, 30}, + {"N", 31, 31}, + }); + addFlags(printer, "fpcr_flags", 4, + { + {"FIZ", 0, 0}, + {"AH", 1, 1}, + {"NEP", 2, 2}, + {"IOE", 8, 8}, + {"DZE", 9, 9}, + {"OFE", 10, 10}, + {"UFE", 11, 11}, + {"IXE", 12, 12}, + {"EBF", 13, 13}, + {"IDE", 15, 15}, + {"Len", 16, 18}, + {"FZ16", 19, 19}, + {"Stride", 20, 21}, + {"RMode", 22, 23}, + {"FZ", 24, 24}, + {"DN", 25, 25}, + {"AHP", 26, 26}, + }); + + for (auto i = 0; i < 32; i++) { + addReg(printer, regs, 1, i, "z" + std::to_string(i), Vector, "svev"); + } + + addReg(printer, regs, 4, 2, "fpsr", DoubleWord, "fpsr_flags"); + addReg(printer, regs, 4, 1, "fpcr", DoubleWord, "fpcr_flags"); + + for (auto i = 0; i < 16; i++) { + addReg(printer, regs, 2, i, "p" + std::to_string(i), Predicate, "svep"); + } + + addReg(printer, regs, 2, 16, "ffr", Predicate, "svep"); + addReg(printer, regs, 0, 0, "vg", VG, "int"); + + printer.CloseElement(); +} + +void deriveSME(tinyxml2::XMLPrinter& printer, RegList& regs, + unsigned int rows) { + printer.OpenElement("feature"); + + printer.PushAttribute("name", "org.gnu.gdb.aarch64.sme"); + + addReg(printer, regs, 0, 0, "svg", SVG, "int"); + + addFlags(printer, "svcr_flags", 8, + { + {"SM", 0, 0}, + {"ZA", 1, 1}, + }); + + addReg(printer, regs, 4, 7, "svcr", Double, "svcr_flags"); + + addVector(printer, "sme_bv", "uint8", 256); + addVector(printer, "sme_bvv", "sme_bv", rows); + + addReg(printer, regs, 0, 0, "za", ZA, "sme_bvv", rows * 2048); + + printer.CloseElement(); +} + +TargetSpec deriveSpec() { + TargetSpec rv; + + tinyxml2::XMLPrinter printer; + + printer.OpenElement("target"); + printer.PushAttribute("version", "1.0"); + + printer.OpenElement("architecture"); + printer.PushText("aarch64"); + printer.CloseElement(); + + deriveCore(printer, rv.regs); + + deriveSVE(printer, rv.regs); + + deriveSME(printer, rv.regs, 256); + + printer.CloseElement(); + + rv.spec = std::string(printer.CStr(), printer.CStrSize() - 1); + + return rv; +} + +TargetSpec target_spec; + +void checkSpec(const simeng::CoreInstance& coreInstance) { + if (target_spec.spec.empty()) { + const auto core = coreInstance.getCore(); + const auto& isa = core->getISA(); + + //const auto [_vl, svl] = isa.getVectorSize(); + + target_spec = deriveSpec(); + + /*std::ofstream out("spec.xml"); + out << target_spec.spec; + out.close();*/ + } +} + +std::string readRegister(const RegList::value_type& which, + const simeng::ArchitecturalRegisterFileSet& registers, + uint64_t pc, uint64_t vl, uint64_t svl) { + const auto& [reg, size] = which; + + std::string rv; + + switch (size) { + case Byte: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case Short: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case Word: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case ByteWord: { + rv += int_to_hex_ne(registers.get(reg).zeroExtend(1, 4).get()); + break; + } + + case DoubleWord: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case Double: { + rv += int_to_hex_ne(registers.get(reg).get()); + break; + } + + case Vector: { + const auto vect = registers.get(reg).getAsVector(); + for (auto j = 0; j < 256; j++) { + rv += int_to_hex(vect[j]); + } + break; + } + + case Predicate: { + const auto vect = registers.get(reg).getAsVector(); + for (auto j = 0; j < 32; j++) { + rv += int_to_hex(vect[j]); + } + break; + } + + case PC: { + rv += int_to_hex_ne(pc); + break; + } + + case VG: { + rv += int_to_hex_ne(vl / 64); + break; + } + + case SVG: { + rv += int_to_hex_ne(svl / 64); + break; + } + + case ZA: { + for (uint16_t i = 0; i < 256; i++) { + if (i < (svl / 8)) { + const auto vect = registers.get({5, i}).getAsVector(); + for (auto j = 0; j < 256; j++) { + rv += int_to_hex(vect[j]); + } + } else { + for (auto j = 0; j < 256; j++) { + rv += "00"; + } + } + } + break; + } + } + + return rv; +} + +std::optional parseRegister( + const RegList::value_type& which, const std::string& str) { + const auto& [reg, size] = which; + + switch (size) { + case Byte: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(*value); + } else { + return std::nullopt; + } + } + + case Short: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(*value); + } else { + return std::nullopt; + } + } + + case Word: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(*value); + } else { + return std::nullopt; + } + } + + case ByteWord: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(static_cast(*value)); + } else { + return std::nullopt; + } + } + + case DoubleWord: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(static_cast(*value)); + } else { + return std::nullopt; + } + } + + case Double: + case PC: + case VG: + case SVG: { + const auto value = hex_to_int_ne(str); + if (value) { + return simeng::RegisterValue(*value); + } else { + return std::nullopt; + } + } + + case Vector: { + const auto value = hex_to_vect_ne<256>(str); + if (value) { + const auto arr = *value; + return simeng::RegisterValue(arr.data(), arr.size()); + } else { + return std::nullopt; + } + } + + case Predicate: { + const auto value = hex_to_vect_ne<32>(str); + if (value) { + const auto arr = *value; + return simeng::RegisterValue(arr.data(), arr.size()); + } else { + return std::nullopt; + } + } + + case ZA: { + return std::nullopt; + } + } + + return std::nullopt; +} + +namespace simeng { +GDBStub::GDBStub(simeng::CoreInstance& coreInstance, bool verbose, + uint16_t port) + : coreInstance_(coreInstance), + verbose_(verbose), + port_(port), + pc_(coreInstance.getCore()->getProgramCounter()) {} + +uint64_t GDBStub::run() { + iterations = 0; + + connection = openSocket(port_); + std::cout << "[SimEng:GDBStub] Connection to GDB client established, " + "debugging in progress\n" + << std::endl; + + char buffer[BUF_SIZE]; + + auto running = true; + + while (running) { + auto packet = ParseResult{ExpectStart, "", 0, 0, 2}; + + while (!packet.done()) { + const ssize_t bytesRead = read(connection, buffer, sizeof(buffer)); + + if (bytesRead == 0) { + std::cout << "[SimEng:GDBStub] Client disconnected (read EOF from " + "connection)." + << std::endl; + break; + } else if (bytesRead < 0) { + std::cerr + << RED + << "[SimEng:GDBStub] An error occurred while reading from the " + "connection. errno: " + << errno << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + + // safety: we've already checked whether bytesRead is less than 0, so we + // know it's positive and less than SIZE_T_MAX + auto bufferString = std::string{buffer, static_cast(bytesRead)}; + + if (verbose_) { + std::cout << CYAN << "[SimEng:GDBStub] <- Raw packet: '" << bufferString + << "' (" << bufferString.size() << ")" << RESET << std::endl; + } + + if (bufferString[0] == '-') { + sendResponse(lastResponse); + continue; + } + + // etx + if (bufferString[0] == '\3') { + sendResponse(encodePacket(formatSignal(SIGTRAP, {}))); + continue; + } + + if (ack_mode != Disabled) { + // '+' is an acknowledgement of successful receipt of message + // '-' is a request for retransmission + + while (!bufferString.empty()) { + if (bufferString[0] == '+') { + if (verbose_) { + std::cout + << CYAN + << "[SimEng:GDBStub] <- Received message acknowledgement" + << RESET << std::endl; + } + bufferString = bufferString.substr(1); + continue; + } + break; + } + + sendResponse("+"); + + if (ack_mode == Transition) { + ack_mode = Disabled; + } + } + + // if the packet was just an acknowledgement and nothing else, + // bufferString is now empty + if (bufferString.empty()) { + continue; + } + + const auto next_packet = decodePacket(bufferString, packet); + if (next_packet) { + packet = *next_packet; + } else { + sendResponse("-"); + } + } + + if (!packet.valid()) { + if (verbose_) { + std::cerr + << RED << std::hex + << "[SimEng:GDBStub] Packet checksum does not match expected; " + "received " + << int_to_hex(packet.receivedChecksum) << ", calculated " + << int_to_hex(packet.calculatedChecksum) << std::dec << RESET + << std::endl; + } + sendResponse("-"); + continue; + } + + const auto command = packet.packet; + + if (verbose_) { + std::cout << GREEN << "[SimEng:GDBStub] <- " << command << RESET + << std::endl; + } + + if (command.size() < 1) { + sendResponse("-"); + continue; + } + + std::string rawResponse; + + // safety: we've already checked whether the size was less than 1, so + // there must be at least one character in the string + const auto commandType = command[0]; + const auto commandParams = command.substr(1); + + if (verbose_) { + std::cout << "[SimEng:GDBStub] <- Command " << commandType << ", params " + << commandParams << std::endl; + } + + switch (command[0]) { + case '?': { + rawResponse = handleHaltReason(); + break; + } + + case 'c': { + rawResponse = handleContinue(commandParams); + break; + } + + case 'g': { + rawResponse = handleReadRegisters(); + break; + } + + case 'G': { + rawResponse = handleWriteRegisters(commandParams); + break; + } + + case 'k': { + if (verbose_) { + std::cout << CYAN + << "[SimEng:GDBStub] Received kill request from " + "client, exiting" + << RESET << std::endl; + } + running = false; + continue; + } + + case 'm': { + rawResponse = handleReadMemory(commandParams); + break; + } + + case 'M': { + rawResponse = handleWriteMemory(commandParams); + break; + } + + case 'p': { + rawResponse = handleReadRegister(commandParams); + break; + } + + case 'P': { + rawResponse = handleWriteRegister(commandParams); + break; + } + + case 'q': { + rawResponse = handleQuery(commandParams); + break; + } + + case 'Q': { + rawResponse = handleSet(commandParams); + break; + } + + case 's': { + rawResponse = handleStep(commandParams); + break; + } + + case 'z': { + rawResponse = handleRemoveBreakpoint(commandParams); + break; + } + + case 'Z': { + rawResponse = handleAddBreakpoint(commandParams); + break; + } + + default: { + // unsupported + rawResponse = ""; + break; + } + } + + sendResponse(encodePacket(rawResponse)); + } + + return iterations; +} + +std::string GDBStub::runUntilStop(const std::optional& step_from) { + auto core = coreInstance_.getCore(); + const auto dataMemory = coreInstance_.getDataMemory(); + + std::vector bp; + std::vector wp, rp, ap; + for (const auto [type, addr, kind] : breakpoints) { + switch (type) { + case HardwareBP: + bp.push_back(addr); + break; + + case WriteWP: + wp.push_back(simeng::memory::MemoryAccessTarget{ + addr, static_cast(kind)}); + break; + + case ReadWP: + rp.push_back(simeng::memory::MemoryAccessTarget{ + addr, static_cast(kind)}); + break; + + case AccessWP: + ap.push_back(simeng::memory::MemoryAccessTarget{ + addr, static_cast(kind)}); + break; + + default: + // do nothing + break; + } + } + + // TODO: currently, must manually ensure these are reset to nullptr after exit + core->prepareBreakpoints(&step_from, &bp, &wp, &rp, &ap, &syscalls_); + + while (!core->hasHalted() || dataMemory->hasPendingRequests()) { + iterations++; + + // TODO: exception to clear the pipeline on breakpoint in OoO? + + core->tick(); + dataMemory->tick(); + + /* + const auto pc = core->getProgramCounter(); + + // only check breakpoints if we're not single-stepping + if (step_from) { + if (pc != *step_from) { + core->prepareBreakpoints(); + return formatSignal(SIGTRAP, {std::make_tuple("hwbreak", "")}); + } + } else { + for (const auto [type, addr, kind] : breakpoints) { + if (type == HardwareBP) { + if (addr == pc) { + core->prepareBreakpoints(); + return formatSignal(SIGTRAP, {std::make_tuple("hwbreak", "")}); + } + } + } + } + */ + + const auto reason = core->getBreakReason(); + + if (reason) { + core->prepareBreakpoints(); + + pc_ = reason->pc; + + switch (reason->reason) { + case BreakReason::Break: + return formatSignal(SIGTRAP, {std::make_tuple("hwbreak", "")}); + + case BreakReason::Write: + return formatSignal( + SIGTRAP, {std::make_tuple("watch", int_to_hex(reason->info))}); + + case BreakReason::Read: + return formatSignal( + SIGTRAP, {std::make_tuple("rwatch", int_to_hex(reason->info))}); + + case BreakReason::Access: + return formatSignal( + SIGTRAP, {std::make_tuple("awatch", int_to_hex(reason->info))}); + + case BreakReason::SyscallEntry: + return formatSignal( + SIGTRAP, + {std::make_tuple("syscall_entry", int_to_hex(reason->info))}); + + case BreakReason::SyscallReturn: + return formatSignal( + SIGTRAP, + {std::make_tuple("syscall_return", int_to_hex(reason->info))}); + } + } + } + + core->prepareBreakpoints(); + + return formatExit(core->getExitCode()); +} + +std::string GDBStub::handleHaltReason() { + // for now, assume breakpoint + return formatSignal(SIGTRAP, {std::make_tuple("hwbreak", "")}); +} + +std::string GDBStub::handleContinue(const std::string& addr) { + return runUntilStop(); +} + +std::string GDBStub::handleReadRegister(const std::string& reg) { + const auto core = coreInstance_.getCore(); + const auto& registers = core->getArchitecturalRegisterFileSet(); + const auto& isa = core->getISA(); + + // const auto pc = core->getProgramCounter(); + const auto [vl, svl] = isa.getVectorSize(); + + int reg_num; + try { + reg_num = std::stoi(reg); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register number: " << reg + << RESET << std::endl; + } + return formatError("invalid single register number"); + } + + if ((reg_num < 0) || ((unsigned int)reg_num >= target_spec.regs.size())) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Reg num of range: " << reg_num + << RESET << std::endl; + } + return formatError("single register number out of range"); + } + + return readRegister(target_spec.regs[reg_num], registers, pc_, vl, svl); +} + +std::string GDBStub::handleReadRegisters() { + const auto core = coreInstance_.getCore(); + const auto& registers = core->getArchitecturalRegisterFileSet(); + const auto& isa = core->getISA(); + + // const auto pc = core->getProgramCounter(); + const auto [vl, svl] = isa.getVectorSize(); + + checkSpec(coreInstance_); + + std::string rv; + + for (const auto& reg : target_spec.regs) { + rv += readRegister(reg, registers, pc_, vl, svl); + } + + return rv; +} + +std::string GDBStub::handleWriteRegister( + const std::string& raw_register_value) { + auto core = coreInstance_.getCore(); + auto& registers = core->getArchitecturalRegisterFileSet(); + const auto& isa = core->getISA(); + const auto [vl, svl] = isa.getVectorSize(); + + const auto register_value = splitBy(raw_register_value, '='); + + if (register_value.size() != 2) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a register write" + << RESET << std::endl; + } + return formatError("invalid number of parameters for register write"); + } + + int reg_num; + try { + reg_num = std::stoi(register_value[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register number: " + << register_value[0] << RESET << std::endl; + } + return formatError("invalid single register number"); + } + + if ((reg_num < 0) || (reg_num >= target_spec.regs.size())) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Reg num of range: " << reg_num + << RESET << std::endl; + } + return formatError("single register number out of range"); + } + + const auto reg_size = target_spec.regs[reg_num]; + + const auto [reg, size] = reg_size; + + switch (size) { + case PC: { + const auto value = hex_to_int_ne(register_value[1]); + if (!value) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register data" << RESET + << std::endl; + } + return formatError("single register data invalid (pc)"); + } + core->setProgramCounter(*value); + break; + } + + case VG: { + // cannot write VG + break; + } + + case SVG: { + // cannot write SVG + break; + } + + case ZA: { + std::vector regs; + std::vector vals; + + const auto value = hex_to_vect_ne<256 * 256>(register_value[1]); + if (!value) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register data" << RESET + << std::endl; + } + return formatError("single register data invalid (za)"); + } + + for (uint16_t i = 0; i < 256; i++) { + if (i < (svl / 8)) { + regs.push_back(simeng::Register{5, i}); + + vals.push_back(simeng::RegisterValue(value->data() + (i * 256), 256)); + } + } + + core->applyStateChange(simeng::arch::ProcessStateChange{ + simeng::arch::ChangeType::REPLACEMENT, regs, vals, {}, {}}); + break; + } + + default: { + const auto value = parseRegister(reg_size, register_value[1]); + if (!value) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register data" << RESET + << std::endl; + } + return formatError("single register data invalid"); + } + + core->applyStateChange(simeng::arch::ProcessStateChange{ + simeng::arch::ChangeType::REPLACEMENT, {reg}, {*value}, {}, {}}); + break; + } + } + + return "OK"; +} + +std::string GDBStub::handleWriteRegisters(const std::string& register_values) { + auto core = coreInstance_.getCore(); + auto& registers = core->getArchitecturalRegisterFileSet(); + const auto& isa = core->getISA(); + + const auto& reg_layout = simeng::config::SimInfo::getArchRegStruct(); + + const auto error = [&] { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid register set write" << RESET + << std::endl; + } + return formatError("invalid register set write"); + }; + + const auto [vl, svl] = isa.getVectorSize(); + + checkSpec(coreInstance_); + + size_t string_offset = 0; + std::vector regs; + std::vector vals; + + for (const auto& reg_size : target_spec.regs) { + const auto [reg, size] = reg_size; + + switch (size) { + case PC: { + const auto value = hex_to_int_ne( + register_values.substr(string_offset, 2 * sizeof(uint64_t))); + string_offset += 2 * sizeof(uint64_t); + if (!value) { + return error(); + } + core->setProgramCounter(*value); + break; + } + + case VG: { + // cannot write VG + string_offset += 2 * sizeof(uint64_t); + break; + } + + case SVG: { + // cannot write SVG + string_offset += 2 * sizeof(uint64_t); + break; + } + + case ZA: { + const auto value = hex_to_vect_ne<256 * 256>( + register_values.substr(string_offset, 2 * 256 * 256)); + string_offset += 2 * 256 * 256; + if (!value) { + return error(); + } + + for (uint16_t i = 0; i < 256; i++) { + if (i < (svl / 8)) { + regs.push_back(simeng::Register{5, i}); + + vals.push_back( + simeng::RegisterValue(value->data() + (i * 256), 256)); + } + } + break; + } + + default: { + const auto value = parseRegister( + reg_size, + register_values.substr(string_offset, (getBitsize(size) / 8) * 2)); + string_offset += (getBitsize(size) / 8) * 2; + if (!value) { + return error(); + } + + regs.push_back(reg); + vals.push_back(*value); + break; + } + } + } + + core->applyStateChange(simeng::arch::ProcessStateChange{ + simeng::arch::ChangeType::REPLACEMENT, regs, vals, {}, {}}); + + return "OK"; +} + +std::string GDBStub::handleReadMemory(const std::string& raw_params) { + const auto params = splitBy(raw_params, ','); + + if (params.size() != 2) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a memory read" + << RESET << std::endl; + } + return formatError(0); + } + + unsigned long long startAddress; + unsigned long long numberOfBytes; + + try { + startAddress = std::stoull(params[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory read address invalid" + << RESET << std::endl; + } + return formatError(1); + }; + + try { + numberOfBytes = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory read length invalid" << RESET + << std::endl; + } + return formatError(2); + }; + + const auto buffer = new uint8_t[numberOfBytes]; + + if (verbose_) { + std::cout << "[SimEng:GDBStub] Reading " << numberOfBytes + << " bytes from memory address " << int_to_hex(startAddress) + << std::endl; + } + + coreInstance_.getDataMemory()->rawRead(buffer, startAddress, numberOfBytes); + + std::string rv; + for (auto i = 0; i < numberOfBytes; i++) { + rv += int_to_hex(buffer[i]); + } + + delete[] buffer; + + return rv; +} +std::string GDBStub::handleWriteMemory(const std::string& raw_params) { + auto core = coreInstance_.getCore(); + const auto dataMemory = coreInstance_.getDataMemory(); + + const auto data = splitBy(raw_params, ':'); + + if (data.size() != 2) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a memory write" + << RESET << std::endl; + } + return formatError(0); + } + + const auto params = splitBy(data[0], ','); + + if (params.size() != 2) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a memory write" + << RESET << std::endl; + } + return formatError(1); + } + + unsigned long long startAddress; + unsigned long long numberOfBytes; + + try { + startAddress = std::stoull(params[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory write address invalid" + << RESET << std::endl; + } + return formatError(2); + }; + + try { + numberOfBytes = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory write length invalid" + << RESET << std::endl; + } + return formatError(3); + }; + + if (data[1].size() != numberOfBytes * 2) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory write data length invalid" + << RESET << std::endl; + } + return formatError(4); + } + + const auto buffer = new uint8_t[numberOfBytes]; + + try { + for (size_t i = 0; i < numberOfBytes; i++) { + buffer[i] = std::stoi(data[1].substr(i * 2, 2), nullptr, 16); + } + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Memory write data invalid" << RESET + << std::endl; + } + return formatError(5); + } + + if (verbose_) { + std::cout << "[SimEng:GDBStub] Writing " << numberOfBytes + << " bytes to memory address " << int_to_hex(startAddress) + << std::endl; + } + + std::vector targets; + std::vector values; + + for (auto i = 0; i < numberOfBytes; i += UINT16_MAX) { + const auto remaining = numberOfBytes - i; + const auto len = static_cast( + (remaining <= UINT16_MAX) ? remaining : UINT16_MAX); + dataMemory->rawWrite(startAddress + i, buffer + i, len); + } + + delete[] buffer; + + return "OK"; +} + +std::string GDBStub::handleQuery(const std::string& query) { + // parse out the query + // any number of any character except :, optionally followed by : and then any + // number of any character + const std::regex query_regex("^([^:]+)(?::(.*))?$"); + std::smatch query_match; + + if (regex_match(query, query_match, query_regex)) { + // safety: we know that the match succeeded and the first capture group is + // non-optional + const auto query_type = query_match[1].str(); + const auto query_params = (query_match.size() == 3) + ? std::optional{query_match[2].str()} + : std::nullopt; + + if (query_type == "Supported") { + if (query_params) { + const auto params = splitBy(*query_params, ';'); + + for (const auto& param : params) { + // here's where we would handle the features GDB supports, if we cared + // at all + if (verbose_) { + std::cout << param << std::endl; + } + } + + std::string features; + for (const auto& feature : supported_features) { + features += feature.format(); + features += ';'; + } + + if (features.back() == ';') { + features.pop_back(); + } + + return features; + } else { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] 'Supported' query requires parameters" + << RESET << std::endl; + } + return ""; + } + } else if (query_type == "Xfer") { + if (query_params) { + const auto params = splitBy(*query_params, ':'); + + const auto transfer_type = params[0]; + + if (transfer_type == "features") { + return queryFeatures(params); + } + + return "l"; + } else { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] 'Supported' query requires parameters" + << RESET << std::endl; + } + return ""; + } + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Unsupported query type '" + << query_type << "'" << RESET << std::endl; + } + return ""; + } + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid query '" << query << "'" + << RESET << std::endl; + } + return ""; + } +} + +std::string GDBStub::handleSet(const std::string& set) { + // parse out the set + // any number of any character except :, optionally followed by : and then any + // number of any character + const std::regex set_regex("^([^:]+)(?::(.*))?$"); + std::smatch set_match; + + if (regex_match(set, set_match, set_regex)) { + // safety: we know that the match succeeded and the first capture group is + // non-optional + const auto set_type = set_match[1].str(); + const auto set_params = (set_match.size() == 3) + ? std::optional{set_match[2].str()} + : std::nullopt; + + if (set_type == "StartNoAckMode") { + ack_mode = Transition; + return "OK"; + } else if (set_type == "CatchSyscalls") { + if (set_params) { + const auto params = splitBy(*set_params, ';'); + + if (params.size() == 1) { + if (params[0] == "0") { + syscalls_ = std::nullopt; + } else if (params[0] == "1") { + syscalls_ = {}; + } else { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid enable option in set " + "'CatchSyscalls'" + << RESET << std::endl; + } + return ""; + } + } else { + std::vector calls; + + for (auto it = params.cbegin() + 1; it != params.cend(); it++) { + uint64_t num; + + try { + num = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid syscall number in set " + "'CatchSyscalls'" + << RESET << std::endl; + } + return ""; + }; + + calls.push_back(num); + } + + syscalls_ = calls; + } + + return "OK"; + } else { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] 'CatchSyscalls' set requires parameters" + << RESET << std::endl; + } + return ""; + } + return ""; + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Unsupported set type '" + << set_type << "'" << RESET << std::endl; + } + return ""; + } + } else { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid set '" << set << "'" + << RESET << std::endl; + } + return ""; + } +} + +std::string GDBStub::handleStep(const std::string& addr) { + // auto [op, size] = getCurrentInstruction(coreInstance_); + + const auto core = coreInstance_.getCore(); + // const auto pc = core->getProgramCounter(); + + return runUntilStop(pc_); +} + +std::string GDBStub::handleRemoveBreakpoint(const std::string& raw_params) { + const auto params = splitBy(raw_params, ','); + + if (params.size() != 3) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid number of parameters to a " + "breakpoint remove" + << RESET << std::endl; + } + return formatError("invalid number of parameters"); + } + + unsigned int type; + unsigned long long address; + unsigned int kind; + + try { + type = std::stoi(params[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint type invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint type"); + }; + + try { + address = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint address invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint address"); + }; + + try { + kind = std::stoi(params[2], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint kind invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint kind"); + }; + + if (type < SWStepBP) { + if (type == SoftwareBP) { + // TODO: convince GDB we really don't support software breakpoints + type = HardwareBP; + } + + bool found = false; + for (auto it = breakpoints.begin(); it < breakpoints.end(); it++) { + // if (*it == Breakpoint{static_cast(type), address, + // kind}) { + if ((it->addr == address) && (it->kind == kind) && (it->type == type)) { + breakpoints.erase(it); + + found = true; + + // avoid deleting twice + break; + } + } + + if (found) { + return "OK"; + } else { + return ""; + } + } else { + return ""; + } +} + +std::string GDBStub::handleAddBreakpoint(const std::string& raw_params) { + const auto params = splitBy(raw_params, ','); + + if (params.size() != 3) { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Invalid number of parameters to a breakpoint set" + << RESET << std::endl; + } + return formatError("invalid number of parameters"); + } + + unsigned int type; + unsigned long long address; + unsigned int kind; + + try { + type = std::stoi(params[0], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint type invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint type"); + }; + + try { + address = std::stoull(params[1], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint address invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint address"); + }; + + try { + kind = std::stoi(params[2], nullptr, 16); + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Breakpoint kind invalid" << RESET + << std::endl; + } + return formatError("invalid breakpoint kind"); + }; + + if (type < SWStepBP) { + if (type == SoftwareBP) { + // TODO: convince GDB we really don't support software breakpoints + type = HardwareBP; + } + + breakpoints.push_back( + Breakpoint{static_cast(type), address, kind}); + + return "OK"; + } else { + return ""; + } +} + +std::string GDBStub::queryFeatures(const std::vector& params) { + if (params.size() != 4) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Received transfer query with incorrect " + "number of parameters" + << RESET << std::endl; + } + return formatError( + "invalid number of parameters to features transfer query"); + } + + const auto& type = params[1]; + const auto& annex = params[2]; + const auto& where = splitBy(params[3], ','); + if (where.size() != 2) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Received transfer query with incorrect " + "offset/length info" + << RESET << std::endl; + } + return formatError( + "invalid offset/length info in features transfer request"); + } + int offset; + int length; + try { + offset = std::stoi(where[0], nullptr, 16); + length = std::stoi(where[1], nullptr, 16); + + // TODO: awful + if ((offset < 0) || (length < 0)) { + throw std::exception(); + } + } catch (const std::exception& e) { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid offset or length parameters to " + "transfer query" + << RESET << std::endl; + } + return formatError( + "invalid offset or length parameters in transfer request"); + } + + checkSpec(coreInstance_); + + const auto max_len = target_spec.spec.size() - offset; + if (length > max_len) { + length = max_len; + } + + if (type != "read") { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] Received unsupported non-read transfer query" + << RESET << std::endl; + } + return formatError("invalid non-read features transfer query"); + } + + if (annex == "target.xml") { + if (length > 1) { + return "m" + target_spec.spec.substr(offset, length); + } else { + return "l"; + } + } else { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Received unsupported read transfer query " + "of file '" + << annex << "'" << RESET << std::endl; + } + return formatError("invalid file for features transfer query"); + } +} + +std::optional GDBStub::decodePacket( + const std::string& encodedPacket, ParseResult result) { + for (const auto& c : encodedPacket) { + switch (result.state) { + case ExpectStart: { + switch (c) { + case '$': { + result.state = Packet; + break; + } + + case '-': + case '+': { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Unexpected acknowledgement in " + "packet (should have been handled already)" + << RESET << std::endl; + } + break; + } + + default: { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Unexpected character '" << c + << "' in packet" << RESET << std::endl; + } + break; + } + } + + break; + } + case Packet: { + switch (c) { + case '}': { + result.state = Escape; + break; + } + + case '#': { + result.state = Checksum; + + // do not add the hash to the checksum + continue; + } + + case '$': { + if (verbose_) { + std::cerr << RED + << "[SimEng:GDBStub] Invalid character '$' in packet" + << RESET << std::endl; + } + + // return error + return std::nullopt; + } + + default: { + result.packet += c; + break; + } + } + + result.calculatedChecksum += c; + + break; + } + case Escape: { + result.packet += c ^ 0x20; + result.calculatedChecksum += c; + + result.state = Packet; + + break; + } + case Checksum: { + result.receivedChecksum <<= 4; + switch (c) { + case '0' ... '9': { + result.receivedChecksum |= c - '0'; + break; + } + case 'A' ... 'F': { + result.receivedChecksum |= c - 'A' + 10; + break; + } + case 'a' ... 'f': { + result.receivedChecksum |= c - 'a' + 10; + break; + } + default: { + if (verbose_) { + std::cerr << RED << "[SimEng:GDBStub] Invalid character '" << c + << "' in checksum" << RESET << std::endl; + } + + // return error + return std::nullopt; + } + } + + result.checksumRemaining--; + if (result.checksumRemaining <= 0) { + result.state = Done; + } + + break; + } + case Done: { + if (verbose_) { + std::cerr + << RED + << "[SimEng:GDBStub] More data follows after packet, ignoring" + << RESET << std::endl; + } + result.state = ExtraData; + + break; + } + + case ExtraData: { + // do nothing + break; + } + } + } + + return result; +} + +std::string GDBStub::encodePacket(const std::string& response) { + uint8_t checksum = 0; + std::string rv; + + auto calcChar = [&](const auto c) { + checksum += c; + rv += c; + }; + + rv += '$'; + + auto rle_char = '*'; + + for (size_t i = 0; i < response.length(); i++) { + const auto c = response[i]; + + switch (c) { + case '#': + case '$': + case '}': + case '*': { + calcChar('}'); + calcChar(c ^ 0x20); + rle_char = '*'; + break; + } + + default: { + if (((i + 2) < response.length()) && + std::all_of(response.cbegin() + i, response.cbegin() + i + 3, + [&](const auto c) { return c == rle_char; })) { + // worth doing RLE + + uint8_t n = 3; + while (((i + n) < response.length()) && + (response[i + n] == rle_char) && (n < 97)) { + n++; + } + + if ((n == 6) || (n == 7)) { + // cannot do RLE + n = 5; + } + + calcChar('*'); + calcChar(n + 29); + rle_char = '*'; + + i += n - 1; + } else { + calcChar(c); + // rle_char = c; + } + } + } + } + + rv += '#'; + + rv += int_to_hex(checksum); + + return rv; +} + +void GDBStub::sendResponse(const std::string& response) { + if (verbose_) { + std::cout << GREEN << "[SimEng:GDBStub] -> " << response << RESET + << std::endl; + } + + const auto bytesSent = send(connection, response.data(), response.size(), 0); + if (bytesSent < 0) { + std::cerr << RED << "[SimEng:GDBStub] Error retransmitting packet. errno: " + << errno << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + lastResponse = response; +} + +// this is a static function +int GDBStub::openSocket(const uint16_t port) { + // Create an INET stream socket, picking the protocol automatically + const auto sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + std::cerr << RED + << "[SimEng:GDBStub] Failed to create socket. errno: " << errno + << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + + sockaddr_in sockaddr; + sockaddr.sin_family = AF_INET; + sockaddr.sin_addr.s_addr = INADDR_ANY; + sockaddr.sin_port = + htons(port); // convert the native integer to network byte order + + const auto addrlen = sizeof(sockaddr); + + if (bind(sockfd, (struct sockaddr*)&sockaddr, addrlen) < 0) { + std::cerr << RED << "[SimEng:GDBStub] Failed to bind to port " << port + << ". errno: " << errno << " (" << strerror(errno) << ")" << RESET + << std::endl; + exit(EXIT_FAILURE); + } else { + std::cout << "[SimEng:GDBStub] Started listening on port " << port + << std::endl; + } + + // Start listening + if (listen(sockfd, NUM_REQUESTS) < 0) { + std::cerr << RED + << "[SimEng:GDBStub] Failed to listen on socket. errno: " + << errno << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + + // safety assumption: addrlen fits into a socklen_t + // this should always hold, since sizeof(sockaddr) is pretty small (~16) in + // all sane cases + auto realAddrLen = (socklen_t)addrlen; + + // Grab a connection from the queue + const auto connection = + accept(sockfd, (struct sockaddr*)&sockaddr, &realAddrLen); + if (connection < 0) { + std::cerr << RED << "[SimEng:GDBStub] Failed to grab connection. errno: " + << errno << " (" << strerror(errno) << ")" << RESET << std::endl; + exit(EXIT_FAILURE); + } + + return connection; +} + +} // namespace simeng \ No newline at end of file diff --git a/test/gdb/GDBTest.cc b/test/gdb/GDBTest.cc new file mode 100644 index 0000000000..449296cea0 --- /dev/null +++ b/test/gdb/GDBTest.cc @@ -0,0 +1,89 @@ + +#include + +#include "gtest/gtest.h" +#include "simeng/config/SimInfo.hh" +#include "simeng/gdb/GDBStub.hh" +#include "simeng/version.hh" + +namespace { + +void setup_config() { + simeng::config::SimInfo::generateDefault(simeng::config::ISA::AArch64, true); + + simeng::config::SimInfo::addToConfig(R"YAML( +{ + Core: + { + Clock-Frequency-GHz: 2.5, + }, + Register-Set: + { + GeneralPurpose-Count: 154, + FloatingPoint/SVE-Count: 90, + Predicate-Count: 17, + Conditional-Count: 128, + Matrix-Count: 2, + }, + L1-Data-Memory: + { + Interface-Type: Flat, + }, + L1-Instruction-Memory: + { + Interface-Type: Flat, + }, + Ports: + { + '0': { Portname: 0, Instruction-Group-Support: [INT, FP, SVE, PREDICATE, LOAD, STORE, BRANCH, SME] }, + }, +} +)YAML"); + + simeng::config::SimInfo::addToConfig("{Core: {Simulation-Mode: outoforder}}"); + + simeng::config::SimInfo::reBuild(); +} + +TEST(GDBTest, Default) { + setup_config(); + + auto coreInstance = std::make_unique( + std::string(SIMENG_SOURCE_DIR "/SimEngDefaultProgram"), + std::vector{}); + + auto GDBStub = simeng::GDBStub(*coreInstance, false, 24689); + + // i am not entirely sure whether this is actually legal to do in googletest + auto pid = fork(); + + ASSERT_NE(pid, -1); + + switch (pid) { + case 0: { // child process + const char* gdb = std::getenv("GDB"); + if (gdb == nullptr) { + gdb = "gdb-multiarch"; + } + + execlp(gdb, "SimEngDefaultProgram", "-ex", "set width 0", "-ex", + "set height 0", "-ex", "set verbose off", "-ex", + "target remote localhost:24689", "-ex", "c", "-ex", "quit 0", + nullptr); + FAIL(); // execlp should never return + break; + } + + default: { // original process + GDBStub.run(); + + kill(pid, SIGTERM); + + // check conditions here + + break; + } + } +} + +} // namespace \ No newline at end of file diff --git a/test/unit/GenericPredictorTest.cc b/test/unit/GenericPredictorTest.cc index c546157021..a3f671f8e5 100644 --- a/test/unit/GenericPredictorTest.cc +++ b/test/unit/GenericPredictorTest.cc @@ -8,6 +8,7 @@ class GenericPredictorTest : public testing::Test { public: GenericPredictorTest() : uop(new MockInstruction), uopPtr(uop) { uop->setInstructionAddress(0); + uop->setNextInstructionAddress(4); } protected: diff --git a/test/unit/MockArchitecture.hh b/test/unit/MockArchitecture.hh index 5bdeeb9f18..e569d5cca4 100644 --- a/test/unit/MockArchitecture.hh +++ b/test/unit/MockArchitecture.hh @@ -23,6 +23,9 @@ class MockArchitecture : public arch::Architecture { MOCK_CONST_METHOD0(getMinInstructionSize, uint8_t()); MOCK_CONST_METHOD2(updateSystemTimerRegisters, void(RegisterFileSet* regFile, const uint64_t iterations)); + MOCK_CONST_METHOD0(getVectorSize, const std::tuple()); + MOCK_CONST_METHOD0(getSyscallIDReg, const Register()); + MOCK_CONST_METHOD0(getExitCodeReg, const Register()); }; } // namespace simeng diff --git a/test/unit/MockCore.hh b/test/unit/MockCore.hh index c76f8ea808..e59208a66a 100644 --- a/test/unit/MockCore.hh +++ b/test/unit/MockCore.hh @@ -18,6 +18,10 @@ class MockCore : public Core { MOCK_CONST_METHOD0(getInstructionsRetiredCount, uint64_t()); MOCK_CONST_METHOD0(getSystemTimer, uint64_t()); MOCK_CONST_METHOD0(getStats, std::map()); + MOCK_CONST_METHOD0(getProgramCounter, const uint64_t()); + MOCK_METHOD1(setProgramCounter, void(uint64_t pc)); + MOCK_CONST_METHOD0(getBreakReason, + const std::optional()); }; } // namespace simeng diff --git a/test/unit/MockInstruction.hh b/test/unit/MockInstruction.hh index 56510f4948..fc7c758578 100644 --- a/test/unit/MockInstruction.hh +++ b/test/unit/MockInstruction.hh @@ -42,6 +42,8 @@ class MockInstruction : public Instruction { MOCK_METHOD1(setExecutionInfo, void(const ExecutionInfo& info)); + MOCK_CONST_METHOD0(isSyscall, bool()); + void setBranchResults(bool wasTaken, uint64_t targetAddress) { branchTaken_ = wasTaken; branchAddress_ = targetAddress; diff --git a/test/unit/MockMemoryInterface.hh b/test/unit/MockMemoryInterface.hh index c0d7285338..86949864bc 100644 --- a/test/unit/MockMemoryInterface.hh +++ b/test/unit/MockMemoryInterface.hh @@ -20,6 +20,10 @@ class MockMemoryInterface : public memory::MemoryInterface { MOCK_CONST_METHOD0(hasPendingRequests, bool()); + MOCK_CONST_METHOD3(rawRead, void(void* dest, uint64_t src, uint16_t len)); + + MOCK_METHOD3(rawWrite, void(uint64_t dest, void* src, uint16_t len)); + MOCK_METHOD0(tick, void()); }; diff --git a/test/unit/PerceptronPredictorTest.cc b/test/unit/PerceptronPredictorTest.cc index b276b3795f..de7654e70c 100644 --- a/test/unit/PerceptronPredictorTest.cc +++ b/test/unit/PerceptronPredictorTest.cc @@ -8,6 +8,7 @@ class PerceptronPredictorTest : public testing::Test { public: PerceptronPredictorTest() : uop(new MockInstruction), uopPtr(uop) { uop->setInstructionAddress(0); + uop->setNextInstructionAddress(4); } protected: diff --git a/test/unit/aarch64/ArchitectureTest.cc b/test/unit/aarch64/ArchitectureTest.cc index 7ad5fcacf4..1afbc20bd2 100644 --- a/test/unit/aarch64/ArchitectureTest.cc +++ b/test/unit/aarch64/ArchitectureTest.cc @@ -96,6 +96,7 @@ TEST_F(AArch64ArchitectureTest, predecode) { Instruction* aarch64Insn = reinterpret_cast(output[0].get()); EXPECT_EQ(result, 1); EXPECT_EQ(aarch64Insn->getInstructionAddress(), 0x7); + EXPECT_EQ(aarch64Insn->getNextInstructionAddress(), 0x7 + result); EXPECT_EQ(aarch64Insn->exceptionEncountered(), true); EXPECT_EQ(aarch64Insn->getException(), InstructionException::MisalignedPC); @@ -106,6 +107,7 @@ TEST_F(AArch64ArchitectureTest, predecode) { aarch64Insn = reinterpret_cast(output[0].get()); EXPECT_EQ(result, 4); EXPECT_EQ(aarch64Insn->getInstructionAddress(), 0x8); + EXPECT_EQ(aarch64Insn->getNextInstructionAddress(), 0x8 + result); EXPECT_EQ(aarch64Insn->exceptionEncountered(), true); EXPECT_EQ(aarch64Insn->getException(), InstructionException::EncodingUnallocated); @@ -116,6 +118,7 @@ TEST_F(AArch64ArchitectureTest, predecode) { output); EXPECT_EQ(result, 4); EXPECT_EQ(output[0]->getInstructionAddress(), 0x4); + EXPECT_EQ(output[0]->getNextInstructionAddress(), 0x4 + result); EXPECT_EQ(output[0]->exceptionEncountered(), false); } @@ -137,6 +140,7 @@ TEST_F(AArch64ArchitectureTest, handleException) { Instruction* aarch64Insn = reinterpret_cast(insn[0].get()); EXPECT_EQ(bytes, 4); EXPECT_EQ(aarch64Insn->getInstructionAddress(), 0x4); + EXPECT_EQ(aarch64Insn->getNextInstructionAddress(), 0x4 + bytes); EXPECT_EQ(aarch64Insn->exceptionEncountered(), true); EXPECT_EQ(aarch64Insn->getException(), InstructionException::EncodingUnallocated); @@ -222,6 +226,7 @@ TEST_F(AArch64ArchitectureTest, getExecutionInfo) { Instruction* aarch64Insn = reinterpret_cast(insn[0].get()); EXPECT_EQ(bytes, 4); EXPECT_EQ(aarch64Insn->getInstructionAddress(), 0x4); + EXPECT_EQ(aarch64Insn->getNextInstructionAddress(), 0x4 + bytes); EXPECT_EQ(aarch64Insn->exceptionEncountered(), false); ExecutionInfo info = arch->getExecutionInfo(*aarch64Insn); diff --git a/test/unit/aarch64/ExceptionHandlerTest.cc b/test/unit/aarch64/ExceptionHandlerTest.cc index b64e1bfbee..af7fdbfdf3 100644 --- a/test/unit/aarch64/ExceptionHandlerTest.cc +++ b/test/unit/aarch64/ExceptionHandlerTest.cc @@ -68,6 +68,7 @@ TEST_F(AArch64ExceptionHandlerTest, testSyscall) { std::shared_ptr insn = std::make_shared( arch, static_cast(uops[0].get())->getMetadata(), exception); insn->setInstructionAddress(insnAddr); + insn->setNextInstructionAddress(insnAddr + 4); // Setup register file for `uname` syscall (chosen as minimal functionality) archRegFileSet.set(R0, RegisterValue(1234, 8)); @@ -85,7 +86,7 @@ TEST_F(AArch64ExceptionHandlerTest, testSyscall) { EXPECT_TRUE(retVal); EXPECT_FALSE(result.fatal); - EXPECT_EQ(result.instructionAddress, insnAddr + 4); + EXPECT_EQ(result.instructionAddress, insn->getNextInstructionAddress()); EXPECT_EQ(result.stateChange.type, ChangeType::REPLACEMENT); std::vector modRegs = {R0}; EXPECT_EQ(result.stateChange.modifiedRegisters, modRegs); diff --git a/test/unit/aarch64/InstructionTest.cc b/test/unit/aarch64/InstructionTest.cc index 92b8e9393a..d86d582639 100644 --- a/test/unit/aarch64/InstructionTest.cc +++ b/test/unit/aarch64/InstructionTest.cc @@ -102,6 +102,7 @@ TEST_F(AArch64InstructionTest, validInsn) { const std::vector ports = {1, 2, 3}; insn.setExecutionInfo({3, 4, ports}); insn.setInstructionAddress(0x48); + insn.setNextInstructionAddress(0x4C); insn.setInstructionId(11); insn.setSequenceId(12); @@ -121,6 +122,7 @@ TEST_F(AArch64InstructionTest, validInsn) { EXPECT_EQ(insn.getGeneratedAddresses().size(), 0); EXPECT_EQ(insn.getGroup(), InstructionGroups::SVE_DIV_OR_SQRT); EXPECT_EQ(insn.getInstructionAddress(), 0x48); + EXPECT_EQ(insn.getNextInstructionAddress(), 0x4C); EXPECT_EQ(insn.getInstructionId(), 11); EXPECT_EQ(insn.getKnownOffset(), 0); EXPECT_EQ(insn.getLatency(), 3); @@ -165,6 +167,7 @@ TEST_F(AArch64InstructionTest, invalidInsn_1) { const std::vector ports = {}; insn.setExecutionInfo({1, 1, ports}); insn.setInstructionAddress(0x44); + insn.setNextInstructionAddress(0x48); insn.setInstructionId(13); insn.setSequenceId(14); @@ -185,6 +188,7 @@ TEST_F(AArch64InstructionTest, invalidInsn_1) { // Default Group EXPECT_EQ(insn.getGroup(), InstructionGroups::INT_SIMPLE_ARTH_NOSHIFT); EXPECT_EQ(insn.getInstructionAddress(), 0x44); + EXPECT_EQ(insn.getNextInstructionAddress(), 0x48); EXPECT_EQ(insn.getInstructionId(), 13); EXPECT_EQ(insn.getKnownOffset(), 0); EXPECT_EQ(insn.getLatency(), 1); @@ -231,6 +235,7 @@ TEST_F(AArch64InstructionTest, invalidInsn_2) { const std::vector ports = {}; insn.setExecutionInfo({1, 1, ports}); insn.setInstructionAddress(0x43); + insn.setNextInstructionAddress(0x44); insn.setInstructionId(15); insn.setSequenceId(16); @@ -251,6 +256,7 @@ TEST_F(AArch64InstructionTest, invalidInsn_2) { // Default Group EXPECT_EQ(insn.getGroup(), InstructionGroups::INT_SIMPLE_ARTH_NOSHIFT); EXPECT_EQ(insn.getInstructionAddress(), 0x43); + EXPECT_EQ(insn.getNextInstructionAddress(), 0x44); EXPECT_EQ(insn.getInstructionId(), 15); EXPECT_EQ(insn.getKnownOffset(), 0); EXPECT_EQ(insn.getLatency(), 1); @@ -466,6 +472,7 @@ TEST_F(AArch64InstructionTest, earlyBranchMisprediction) { // Insn is `fdivr z1.s, p0/m, z1.s, z0.s` Instruction insn = Instruction(arch, *fdivMetadata.get(), MicroOpInfo()); insn.setInstructionAddress(64); + insn.setNextInstructionAddress(68); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; @@ -475,7 +482,7 @@ TEST_F(AArch64InstructionTest, earlyBranchMisprediction) { EXPECT_EQ(insn.getBranchAddress(), 0); EXPECT_EQ(insn.getBranchType(), BranchType::Unknown); EXPECT_FALSE(insn.isBranch()); - std::tuple tup = {false, insn.getInstructionAddress() + 4}; + std::tuple tup = {false, insn.getNextInstructionAddress()}; EXPECT_EQ(insn.checkEarlyBranchMisprediction(), tup); // Set prediction and ensure expected state changes / outcomes are seen @@ -489,7 +496,7 @@ TEST_F(AArch64InstructionTest, earlyBranchMisprediction) { // Check logic of `checkEarlyBranchMisprediction` which is different for // non-branch instructions EXPECT_FALSE(insn.isBranch()); - tup = {true, insn.getInstructionAddress() + 4}; + tup = {true, insn.getNextInstructionAddress()}; EXPECT_EQ(insn.checkEarlyBranchMisprediction(), tup); } @@ -498,6 +505,7 @@ TEST_F(AArch64InstructionTest, correctPred_taken) { // insn is `cbz x2, #0x28` Instruction insn = Instruction(arch, *cbzMetadata.get(), MicroOpInfo()); insn.setInstructionAddress(80); + insn.setNextInstructionAddress(84); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; @@ -527,6 +535,7 @@ TEST_F(AArch64InstructionTest, correctPred_notTaken) { // insn is `cbz x2, #0x28` Instruction insn = Instruction(arch, *cbzMetadata.get(), MicroOpInfo()); insn.setInstructionAddress(80); + insn.setNextInstructionAddress(84); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; @@ -556,6 +565,7 @@ TEST_F(AArch64InstructionTest, incorrectPred_target) { // insn is `cbz x2, #0x28` Instruction insn = Instruction(arch, *cbzMetadata.get(), MicroOpInfo()); insn.setInstructionAddress(100); + insn.setNextInstructionAddress(104); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; @@ -585,6 +595,7 @@ TEST_F(AArch64InstructionTest, incorrectPred_taken) { // insn is `cbz x2, #0x28` Instruction insn = Instruction(arch, *cbzMetadata.get(), MicroOpInfo()); insn.setInstructionAddress(100); + insn.setNextInstructionAddress(104); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; diff --git a/test/unit/riscv/ArchitectureTest.cc b/test/unit/riscv/ArchitectureTest.cc index 49e64e42b0..210d5801c1 100644 --- a/test/unit/riscv/ArchitectureTest.cc +++ b/test/unit/riscv/ArchitectureTest.cc @@ -77,6 +77,7 @@ TEST_F(RiscVArchitectureTest, predecode) { validInstrBytes.size(), 0x7, output); EXPECT_EQ(result, 1); EXPECT_EQ(output[0]->getInstructionAddress(), 0x7); + EXPECT_EQ(output[0]->getNextInstructionAddress(), 0x7 + result); EXPECT_EQ(output[0]->exceptionEncountered(), true); // Test that an invalid instruction returns instruction with an exception @@ -85,6 +86,7 @@ TEST_F(RiscVArchitectureTest, predecode) { 0x8, output); EXPECT_EQ(result, 4); EXPECT_EQ(output[0]->getInstructionAddress(), 0x8); + EXPECT_EQ(output[0]->getNextInstructionAddress(), 0x8 + result); EXPECT_EQ(output[0]->exceptionEncountered(), true); // Test that an instruction can be properly decoded @@ -93,6 +95,7 @@ TEST_F(RiscVArchitectureTest, predecode) { output); EXPECT_EQ(result, 4); EXPECT_EQ(output[0]->getInstructionAddress(), 0x4); + EXPECT_EQ(output[0]->getNextInstructionAddress(), 0x4 + result); EXPECT_EQ(output[0]->exceptionEncountered(), false); } @@ -113,6 +116,7 @@ TEST_F(RiscVArchitectureTest, handleException) { invalidInstrBytes.size(), 0x4, insn); EXPECT_EQ(bytes, 4); EXPECT_EQ(insn[0]->getInstructionAddress(), 0x4); + EXPECT_EQ(insn[0]->getNextInstructionAddress(), 0x4 + bytes); EXPECT_EQ(insn[0]->exceptionEncountered(), true); // Get Core diff --git a/test/unit/riscv/ExceptionHandlerTest.cc b/test/unit/riscv/ExceptionHandlerTest.cc index 3e9ac92be7..74df93ffff 100644 --- a/test/unit/riscv/ExceptionHandlerTest.cc +++ b/test/unit/riscv/ExceptionHandlerTest.cc @@ -64,6 +64,7 @@ TEST_F(RiscVExceptionHandlerTest, testSyscall) { std::shared_ptr insn = std::make_shared( arch, static_cast(uops[0].get())->getMetadata(), exception); insn->setInstructionAddress(insnAddr); + insn->setNextInstructionAddress(insnAddr + 4); // Setup register file for `uname` syscall (chosen as minimal functionality) archRegFileSet.set(R0, RegisterValue(1234, 8)); @@ -81,7 +82,7 @@ TEST_F(RiscVExceptionHandlerTest, testSyscall) { EXPECT_TRUE(retVal); EXPECT_FALSE(result.fatal); - EXPECT_EQ(result.instructionAddress, insnAddr + 4); + EXPECT_EQ(result.instructionAddress, insn->getNextInstructionAddress()); EXPECT_EQ(result.stateChange.type, ChangeType::REPLACEMENT); std::vector modRegs = {R0}; EXPECT_EQ(result.stateChange.modifiedRegisters, modRegs); diff --git a/test/unit/riscv/InstructionTest.cc b/test/unit/riscv/InstructionTest.cc index 6103cd4f5c..9b65f29cb6 100644 --- a/test/unit/riscv/InstructionTest.cc +++ b/test/unit/riscv/InstructionTest.cc @@ -100,6 +100,7 @@ TEST_F(RiscVInstructionTest, validInsn) { const std::vector ports = {1, 2, 3}; insn.setExecutionInfo({3, 4, ports}); insn.setInstructionAddress(0x48); + insn.setNextInstructionAddress(0x4C); insn.setInstructionId(11); insn.setSequenceId(12); @@ -119,6 +120,7 @@ TEST_F(RiscVInstructionTest, validInsn) { EXPECT_EQ(insn.getGeneratedAddresses().size(), 0); EXPECT_EQ(insn.getGroup(), InstructionGroups::INT_DIV_OR_SQRT); EXPECT_EQ(insn.getInstructionAddress(), 0x48); + EXPECT_EQ(insn.getNextInstructionAddress(), 0x4C); EXPECT_EQ(insn.getInstructionId(), 11); EXPECT_EQ(insn.getKnownOffset(), 0); EXPECT_EQ(insn.getLatency(), 3); @@ -161,6 +163,7 @@ TEST_F(RiscVInstructionTest, invalidInsn_1) { const std::vector ports = {}; insn.setExecutionInfo({1, 1, ports}); insn.setInstructionAddress(0x44); + insn.setNextInstructionAddress(0x48); insn.setInstructionId(13); insn.setSequenceId(14); @@ -181,6 +184,7 @@ TEST_F(RiscVInstructionTest, invalidInsn_1) { // Default Group EXPECT_EQ(insn.getGroup(), InstructionGroups::INT_SIMPLE_ARTH); EXPECT_EQ(insn.getInstructionAddress(), 0x44); + EXPECT_EQ(insn.getNextInstructionAddress(), 0x48); EXPECT_EQ(insn.getInstructionId(), 13); EXPECT_EQ(insn.getKnownOffset(), 0); EXPECT_EQ(insn.getLatency(), 1); @@ -225,6 +229,7 @@ TEST_F(RiscVInstructionTest, invalidInsn_2) { const std::vector ports = {}; insn.setExecutionInfo({1, 1, ports}); insn.setInstructionAddress(0x43); + insn.setNextInstructionAddress(0x44); insn.setInstructionId(15); insn.setSequenceId(16); @@ -245,6 +250,7 @@ TEST_F(RiscVInstructionTest, invalidInsn_2) { // Default Group EXPECT_EQ(insn.getGroup(), InstructionGroups::INT_SIMPLE_ARTH); EXPECT_EQ(insn.getInstructionAddress(), 0x43); + EXPECT_EQ(insn.getNextInstructionAddress(), 0x44); EXPECT_EQ(insn.getInstructionId(), 15); EXPECT_EQ(insn.getKnownOffset(), 0); EXPECT_EQ(insn.getLatency(), 1); @@ -440,6 +446,7 @@ TEST_F(RiscVInstructionTest, earlyBranchMisprediction) { // Insn is `div a3, a3, a0` Instruction insn = Instruction(arch, *divMetadata.get()); insn.setInstructionAddress(64); + insn.setNextInstructionAddress(68); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; @@ -449,7 +456,7 @@ TEST_F(RiscVInstructionTest, earlyBranchMisprediction) { EXPECT_EQ(insn.getBranchAddress(), 0); EXPECT_EQ(insn.getBranchType(), BranchType::Unknown); EXPECT_FALSE(insn.isBranch()); - std::tuple tup = {false, insn.getInstructionAddress() + 4}; + std::tuple tup = {false, insn.getNextInstructionAddress()}; EXPECT_EQ(insn.checkEarlyBranchMisprediction(), tup); // Set prediction and ensure expected state changes / outcomes are seen @@ -463,7 +470,7 @@ TEST_F(RiscVInstructionTest, earlyBranchMisprediction) { // Check logic of `checkEarlyBranchMisprediction` which is different for // non-branch instructions EXPECT_FALSE(insn.isBranch()); - tup = {true, insn.getInstructionAddress() + 4}; + tup = {true, insn.getNextInstructionAddress()}; EXPECT_EQ(insn.checkEarlyBranchMisprediction(), tup); } @@ -472,6 +479,7 @@ TEST_F(RiscVInstructionTest, correctPred_taken) { // insn is `bgeu a5, a4, -86` Instruction insn = Instruction(arch, *bgeuMetadata.get()); insn.setInstructionAddress(400); + insn.setNextInstructionAddress(404); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; @@ -502,6 +510,7 @@ TEST_F(RiscVInstructionTest, correctPred_notTaken) { // insn is `bgeu a5, a4, -86` Instruction insn = Instruction(arch, *bgeuMetadata.get()); insn.setInstructionAddress(400); + insn.setNextInstructionAddress(404); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; @@ -533,6 +542,7 @@ TEST_F(RiscVInstructionTest, incorrectPred_target) { // insn is `bgeu a5, a4, -86` Instruction insn = Instruction(arch, *bgeuMetadata.get()); insn.setInstructionAddress(400); + insn.setNextInstructionAddress(404); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0}; @@ -564,6 +574,7 @@ TEST_F(RiscVInstructionTest, incorrectPred_taken) { // insn is `bgeu a5, a4, -86` Instruction insn = Instruction(arch, *bgeuMetadata.get()); insn.setInstructionAddress(400); + insn.setNextInstructionAddress(404); // Check initial state of an instruction's branch related options BranchPrediction pred = {false, 0};