Breakpoints.java

1
package com.rox.debug.breakpoints;
2
3
import com.rox.debug.BeamPosition;
4
5
import java.util.Collections;
6
import java.util.Map;
7
import java.util.Optional;
8
import java.util.SortedMap;
9
import java.util.SortedSet;
10
import java.util.TreeMap;
11
import java.util.TreeSet;
12
import java.util.function.Supplier;
13
14
/**
15
 * Breakpoints (stop when the PC reaches an address) and watchpoints (stop after an instruction reads or writes
16
 * one). Mirrored addresses count as the same address: CPU RAM ({@code $0000-$1FFF}, every {@code $0800}) and the
17
 * PPU registers ({@code $2000-$3FFF}, every 8 bytes).
18
 * <ul>
19
 *     <li><b>Edited</b> from any thread: each edit swaps in a new immutable set, so the clock thread never sees one
20
 *     half-made.</li>
21
 *     <li><b>Checked</b> on the ticking thread only: {@link #recordAccess} as the CPU touches memory,
22
 *     {@link #check} after every tick. With nothing set, each costs one volatile read.</li>
23
 * </ul>
24
 */
25
public final class Breakpoints {
26
    private static final int ADDRESS_MASK = 0xFFFF;
27
    private static final int CPU_RAM_END = 0x1FFF;
28
    private static final int CPU_RAM_MIRROR_MASK = 0x07FF;
29
    private static final int PPU_REGISTERS_START = 0x2000;
30
    private static final int PPU_REGISTERS_END = 0x3FFF;
31
    private static final int PPU_REGISTER_MIRROR_MASK = 0x0007;
32
33
    /** Fast lookups (indexed by address) alongside the sorted views handed out. */
34
    private record State(SortedSet<Integer> breakpoints, SortedMap<Integer, WatchKind> watches,
35
                         boolean[] breakpointAt, WatchKind[] watchAt){
36
        static final State EMPTY = new State(new TreeSet<>(), new TreeMap<>(), null, null);
37
38
        static State of(final SortedSet<Integer> breakpoints, final SortedMap<Integer, WatchKind> watches){
39
            boolean[] breakpointAt = null;
40 1 1. of : negated conditional → KILLED
            if (!breakpoints.isEmpty()){
41
                breakpointAt = new boolean[ADDRESS_MASK + 1];
42
                for (final int address : breakpoints){
43
                    breakpointAt[address] = true;
44
                }
45
            }
46
            WatchKind[] watchAt = null;
47 1 1. of : negated conditional → KILLED
            if (!watches.isEmpty()){
48
                watchAt = new WatchKind[ADDRESS_MASK + 1];
49
                for (final Map.Entry<Integer, WatchKind> watch : watches.entrySet()){
50
                    watchAt[watch.getKey()] = watch.getValue();
51
                }
52
            }
53 1 1. of : replaced return value with null for com/rox/debug/breakpoints/Breakpoints$State::of → KILLED
            return new State(Collections.unmodifiableSortedSet(breakpoints), Collections.unmodifiableSortedMap(watches),
54
                    breakpointAt, watchAt);
55
        }
56
    }
57
58
    private volatile State state = State.EMPTY;
59
    /** Where the PPU is drawing - asked only when something hits. */
60 1 1. lambda$new$0 : replaced return value with null for com/rox/debug/breakpoints/Breakpoints::lambda$new$0 → KILLED
    private volatile Supplier<BeamPosition> beam = () -> BeamPosition.UNKNOWN;
61
    /** Where the instruction now running started - what a watchpoint hit reports. Ticking thread only. */
62
    private int instructionStart;
63
    /** A watchpoint hit, held until its instruction finishes. Ticking thread only. */
64
    private BreakHit pendingWatchHit;
65
66
    /** @throws IllegalArgumentException for an address outside {@code $0000-$FFFF} */
67
    public synchronized void addBreakpoint(final int address){
68
        final SortedSet<Integer> breakpoints = new TreeSet<>(state.breakpoints());
69
        breakpoints.add(canonical(address));
70
        state = State.of(breakpoints, new TreeMap<>(state.watches()));
71
    }
72
73
    public synchronized void removeBreakpoint(final int address){
74
        final SortedSet<Integer> breakpoints = new TreeSet<>(state.breakpoints());
75
        breakpoints.remove(canonical(address));
76
        state = State.of(breakpoints, new TreeMap<>(state.watches()));
77
    }
78
79
    /** Adds a watchpoint, or changes the kind of the one already at {@code address}. */
80
    public synchronized void addWatch(final int address, final WatchKind kind){
81
        final SortedMap<Integer, WatchKind> watches = new TreeMap<>(state.watches());
82
        watches.put(canonical(address), kind);
83
        state = State.of(new TreeSet<>(state.breakpoints()), watches);
84
    }
85
86
    public synchronized void removeWatch(final int address){
87
        final SortedMap<Integer, WatchKind> watches = new TreeMap<>(state.watches());
88
        watches.remove(canonical(address));
89
        state = State.of(new TreeSet<>(state.breakpoints()), watches);
90
    }
91
92
    /** Where hits record the PPU as drawing - e.g. the NES's own PPU. Unset, hits carry {@link BeamPosition#UNKNOWN}. */
93
    public void beam(final Supplier<BeamPosition> beam){
94
        this.beam = beam;
95
    }
96
97
    /** Breakpoint addresses, ascending, each by its lowest mirror. */
98
    public SortedSet<Integer> breakpoints(){
99 1 1. breakpoints : replaced return value with null for com/rox/debug/breakpoints/Breakpoints::breakpoints → KILLED
        return state.breakpoints();
100
    }
101
102
    /** Watched addresses, ascending, each by its lowest mirror. */
103
    public SortedMap<Integer, WatchKind> watches(){
104 1 1. watches : replaced return value with null for com/rox/debug/breakpoints/Breakpoints::watches → KILLED
        return state.watches();
105
    }
106
107
    /** The CPU has read or written {@code value} at {@code address} - ticking thread only. */
108
    public void recordAccess(final int address, final boolean write, final int value){
109
        final WatchKind[] watchAt = state.watchAt();
110 2 1. recordAccess : negated conditional → KILLED
2. recordAccess : negated conditional → KILLED
        if (watchAt == null || pendingWatchHit != null){
111
            return;
112
        }
113
        final WatchKind kind = watchAt[canonical(address)];
114 2 1. recordAccess : negated conditional → KILLED
2. recordAccess : negated conditional → KILLED
        if (kind != null && kind.catches(write)){
115 1 1. recordAccess : negated conditional → KILLED
            pendingWatchHit = new BreakHit(write ? BreakHit.Cause.WRITE : BreakHit.Cause.READ, canonical(address), value,
116
                    instructionStart, beam.get());
117
        }
118
    }
119
120
    /**
121
     * After every tick, on the ticking thread: between instructions, a watchpoint hit by the instruction just
122
     * finished, or else a breakpoint on the one about to run.
123
     *
124
     * @param pc the CPU's program counter - the next instruction's address, when {@code atInstructionBoundary}
125
     */
126
    public Optional<BreakHit> check(final boolean atInstructionBoundary, final int pc){
127 1 1. check : negated conditional → KILLED
        if (!atInstructionBoundary){
128
            return Optional.empty();
129
        }
130
        final State current = state;
131 2 1. check : negated conditional → KILLED
2. check : negated conditional → KILLED
        if (current.breakpointAt() == null && current.watchAt() == null){
132
            pendingWatchHit = null; //its watch was removed before its instruction finished
133
            return Optional.empty();
134
        }
135
        instructionStart = pc;
136 1 1. check : negated conditional → KILLED
        if (pendingWatchHit != null){
137
            final BreakHit hit = pendingWatchHit;
138
            pendingWatchHit = null;
139 1 1. check : replaced return value with Optional.empty for com/rox/debug/breakpoints/Breakpoints::check → KILLED
            return Optional.of(hit);
140
        }
141 2 1. check : negated conditional → KILLED
2. check : negated conditional → KILLED
        if (current.breakpointAt() != null && current.breakpointAt()[pc]){
142 1 1. check : replaced return value with Optional.empty for com/rox/debug/breakpoints/Breakpoints::check → KILLED
            return Optional.of(new BreakHit(BreakHit.Cause.BREAKPOINT, pc, 0, pc, beam.get()));
143
        }
144
        return Optional.empty();
145
    }
146
147
    /** {@code address} by its lowest mirror. */
148
    static int canonical(final int address){
149 4 1. canonical : changed conditional boundary → KILLED
2. canonical : negated conditional → KILLED
3. canonical : changed conditional boundary → KILLED
4. canonical : negated conditional → KILLED
        if (address < 0 || address > ADDRESS_MASK){
150
            throw new IllegalArgumentException(String.format("Not a CPU address: $%X", address));
151
        }
152 2 1. canonical : negated conditional → KILLED
2. canonical : changed conditional boundary → KILLED
        if (address <= CPU_RAM_END){
153 2 1. canonical : replaced int return with 0 for com/rox/debug/breakpoints/Breakpoints::canonical → KILLED
2. canonical : Replaced bitwise AND with OR → KILLED
            return address & CPU_RAM_MIRROR_MASK;
154
        }
155 2 1. canonical : negated conditional → KILLED
2. canonical : changed conditional boundary → KILLED
        if (address <= PPU_REGISTERS_END){
156 3 1. canonical : Replaced bitwise OR with AND → KILLED
2. canonical : replaced int return with 0 for com/rox/debug/breakpoints/Breakpoints::canonical → KILLED
3. canonical : Replaced bitwise AND with OR → KILLED
            return PPU_REGISTERS_START | (address & PPU_REGISTER_MIRROR_MASK);
157
        }
158 1 1. canonical : replaced int return with 0 for com/rox/debug/breakpoints/Breakpoints::canonical → KILLED
        return address;
159
    }
160
}

Mutations

40

1.1
Location : of
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aBreakpointHitsWhenThePcReachesItBetweenInstructions()]
negated conditional → KILLED

47

1.1
Location : of
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:aWatchCatchesOnlyItsKindOfAccess(com.rox.debug.breakpoints.WatchKind, boolean, boolean)]/[test-template-invocation:#4]
negated conditional → KILLED

53

1.1
Location : of
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:addingAWatchAgainChangesItsKind()]
replaced return value with null for com/rox/debug/breakpoints/Breakpoints$State::of → KILLED

60

1.1
Location : lambda$new$0
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aBreakpointHitsWhenThePcReachesItBetweenInstructions()]
replaced return value with null for com/rox/debug/breakpoints/Breakpoints::lambda$new$0 → KILLED

99

1.1
Location : breakpoints
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aRemovedBreakpointNoLongerHits()]
replaced return value with null for com/rox/debug/breakpoints/Breakpoints::breakpoints → KILLED

104

1.1
Location : watches
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:addingAWatchAgainChangesItsKind()]
replaced return value with null for com/rox/debug/breakpoints/Breakpoints::watches → KILLED

110

1.1
Location : recordAccess
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:nothingSetNeverHits()]
negated conditional → KILLED

2.2
Location : recordAccess
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:aWatchCatchesOnlyItsKindOfAccess(com.rox.debug.breakpoints.WatchKind, boolean, boolean)]/[test-template-invocation:#4]
negated conditional → KILLED

114

1.1
Location : recordAccess
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:aWatchCatchesOnlyItsKindOfAccess(com.rox.debug.breakpoints.WatchKind, boolean, boolean)]/[test-template-invocation:#2]
negated conditional → KILLED

2.2
Location : recordAccess
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:anAccessElsewhereIsNotCaught()]
negated conditional → KILLED

115

1.1
Location : recordAccess
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:theFirstWatchHitInAnInstructionIsTheOneReported()]
negated conditional → KILLED

127

1.1
Location : check
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aBreakpointDoesNotHitMidInstructionOrElsewhere()]
negated conditional → KILLED

131

1.1
Location : check
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:aWatchCatchesOnlyItsKindOfAccess(com.rox.debug.breakpoints.WatchKind, boolean, boolean)]/[test-template-invocation:#4]
negated conditional → KILLED

2.2
Location : check
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aBreakpointHitsWhenThePcReachesItBetweenInstructions()]
negated conditional → KILLED

136

1.1
Location : check
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aBreakpointDoesNotHitMidInstructionOrElsewhere()]
negated conditional → KILLED

139

1.1
Location : check
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aWatchOnAPpuRegisterCatchesItsMirrors()]
replaced return value with Optional.empty for com/rox/debug/breakpoints/Breakpoints::check → KILLED

141

1.1
Location : check
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:anAccessElsewhereIsNotCaught()]
negated conditional → KILLED

2.2
Location : check
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aBreakpointDoesNotHitMidInstructionOrElsewhere()]
negated conditional → KILLED

142

1.1
Location : check
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[method:aBreakpointHitsWhenThePcReachesItBetweenInstructions()]
replaced return value with Optional.empty for com/rox/debug/breakpoints/Breakpoints::check → KILLED

149

1.1
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#1]
changed conditional boundary → KILLED

2.2
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#11]
negated conditional → KILLED

3.3
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#11]
changed conditional boundary → KILLED

4.4
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#11]
negated conditional → KILLED

152

1.1
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#11]
negated conditional → KILLED

2.2
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#4]
changed conditional boundary → KILLED

153

1.1
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#2]
replaced int return with 0 for com/rox/debug/breakpoints/Breakpoints::canonical → KILLED

2.2
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#3]
Replaced bitwise AND with OR → KILLED

155

1.1
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#11]
negated conditional → KILLED

2.2
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#7]
changed conditional boundary → KILLED

156

1.1
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#5]
Replaced bitwise OR with AND → KILLED

2.2
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#5]
replaced int return with 0 for com/rox/debug/breakpoints/Breakpoints::canonical → KILLED

3.3
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#5]
Replaced bitwise AND with OR → KILLED

158

1.1
Location : canonical
Killed by : com.rox.debug.breakpoints.BreakpointsTest.[engine:junit-jupiter]/[class:com.rox.debug.breakpoints.BreakpointsTest]/[test-template:canonicalIsTheLowestMirror(java.lang.String, java.lang.String)]/[test-template-invocation:#11]
replaced int return with 0 for com/rox/debug/breakpoints/Breakpoints::canonical → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1