[doc("Trace a golden run using FAIL*")]
[group("fail")]
trace module:
    {{ BOCHS_RUNNER }} \
        -V {{ FAIL_SHARE }}/vgabios.bin \
        -b {{ FAIL_SHARE }}/BIOS-bochs-latest \
        -1 \
        -f {{ FAIL_TRACE }} \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf \
        -i {{ BUILD_DIR }}-{{ module }}/system.iso \
        -- \
        -Wf,--start-symbol=start_trace \
        -Wf,--save-symbol=start_trace \
        -Wf,--end-symbol=stop_trace \
        -Wf,--state-file={{ BUILD_DIR }}-{{ module }}/state \
        -Wf,--trace-file={{ BUILD_DIR }}-{{ module }}/trace.pb \
        -Wf,--elf-file={{ BUILD_DIR }}-{{ module }}/system.elf
    @echo "Next step: \"just import {{ module }}\""

# [doc("Dump a FAIL* golden run trace")]
# [group("fail")]
# dump module:
#     {{ FAIL_DUMP }} {{ BUILD_DIR }}-{{ module }}/trace.pb

[doc("Import a FAIL* golden run trace")]
[group("fail")]
import module:
    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i MemoryImporter \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b mem
    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i RegisterImporter \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b regs --flags
    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i RegisterImporter \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b ip --no-gp --ip

    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i ElfImporter --objdump objdump \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b ip
    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i ElfImporter --objdump objdump \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b mem
    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i ElfImporter --objdump objdump \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b regs

    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i ElfImporter --objdump objdump \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b ip --sources
    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i ElfImporter --objdump objdump \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b mem --sources
    {{ FAIL_IMPORT }} --database-option-file ./db.conf -t {{ BUILD_DIR }}-{{ module }}/trace.pb \
        -i ElfImporter --objdump objdump \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf -v {{ module }} -b regs --sources

    {{ FAIL_PRUNE }} --database-option-file ./db.conf -v {{ module }} -b %% --overwrite
    @echo "Next step: \"just server {{ module }}\""

[doc("Start the FAIL* campaign server")]
[group("fail")]
server module:
    {{ FAIL_SERVER }} \
        --port {{FAIL_SERVER_PORT}} \
        --database-option-file ./db.conf \
        -v {{ module }} \
        -b % \
        --inject-single-bit \
        --inject-registers \
        &
    @echo "Next step: \"just client {{ module }}\""

[doc("Stop the FAIL* campaign server")]
[group("fail")]
stop-server:
    pkill -f {{ FAIL_SERVER }}

[doc("Start a FAIL* campaign client")]
[group("fail")]
client module:
    {{ BOCHS_RUNNER }} \
        -V {{ FAIL_SHARE }}/vgabios.bin \
        -b {{ FAIL_SHARE }}/BIOS-bochs-latest \
        -f {{ FAIL_INJECT }} \
        -e {{ BUILD_DIR }}-{{ module }}/system.elf \
        -i {{ BUILD_DIR }}-{{ module }}/system.iso \
        -j {{ num_cpus() }} \
        -- \
        -Wf,--server-port={{FAIL_SERVER_PORT}} \
        -Wf,--state-dir={{ BUILD_DIR }}-{{ module }}/state \
        -Wf,--trap \
        -Wf,--timeout=500000 \
        -Wf,--ok-marker=ok_marker \
        -Wf,--fail-marker=fail_marker \
        > /dev/null
    @echo "Next step: \"just result {{ module }}\" or \"just resultbrowser\""

[doc("Query FAIL* marker statistics from the database")]
[group("fail")]
result module:
    @echo "select variant, benchmark, resulttype, sum(t.time2 - t.time1 + 1) as faults \
        FROM variant v \
        JOIN trace t ON v.id = t.variant_id \
        JOIN fspgroup g ON g.variant_id = t.variant_id AND g.instr2 = t.instr2 AND g.data_address = t.data_address \
        JOIN result_GenericExperimentMessage r ON r.pilot_id = g.pilot_id \
        JOIN fsppilot p ON r.pilot_id = p.id \
        WHERE v.variant = \"{{ module }}\" \
        GROUP BY v.id, resulttype \
        ORDER BY variant, benchmark, resulttype;" | mariadb --defaults-file=./db.conf -t

[doc("Start the FAIL* resultbrowser")]
[group("fail")]
resultbrowser:
    {{ RESULT_BROWSER }} -c ./db.conf --host=0.0.0.0 --port={{RESULTBROWSER_PORT}}
