Skip to content

Commit

Permalink
tracer: Add address controls to the memory view
Browse files Browse the repository at this point in the history
  • Loading branch information
oleavr committed Oct 6, 2024
1 parent 768a919 commit 3fa5210
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 29 deletions.
5 changes: 4 additions & 1 deletion apps/tracer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ export default function App() {
);

const memoryView = (
<MemoryView address={memoryLocation} />
<MemoryView
address={memoryLocation}
onSelectAddress={showMemoryLocation}
/>
);

return (
Expand Down
22 changes: 17 additions & 5 deletions apps/tracer/src/MemoryView.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.memory-view {
padding: 5px;
overflow: auto;
font-family: monospace;
font-size: 10px;
Expand All @@ -8,12 +7,25 @@
user-select: text;
}

.memory-view-toolbar {
display: flex;
background-color: white;
padding: 5px 7px;
border: 1px solid;
}

.memory-view-data {
padding: 5px 7px;
}

.memory-view .bp5-control-group .bp5-input {
font-size: 12px;
}

.memory-view .bp5-segmented-control {
position: absolute;
bottom: 5px;
right: 5px;
margin-left: 20px;
}

.memory-view .bp5-segmented-control .bp5-button {
font-size: 10px;
font-size: 12px;
}
79 changes: 56 additions & 23 deletions apps/tracer/src/MemoryView.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import "./MemoryView.css";
import { SegmentedControl, Spinner } from "@blueprintjs/core";
import { Button, ControlGroup, InputGroup, SegmentedControl, Spinner } from "@blueprintjs/core";
import { useR2 } from "@frida/react-use-r2";
import { useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";

export interface MemoryViewProps {
address?: bigint;
onSelectAddress: SelectAddressHandler;
}

export default function MemoryView({ address }: MemoryViewProps) {
export type SelectAddressHandler = (address: bigint) => void;

export default function MemoryView({ address, onSelectAddress }: MemoryViewProps) {
const addressInputRef = useRef<HTMLInputElement>(null);
const [format, setFormat] = useState("x");
const [data, setData] = useState("");
const [isLoading, setIsLoading] = useState(false);
Expand Down Expand Up @@ -38,6 +42,19 @@ export default function MemoryView({ address }: MemoryViewProps) {
};
}, [format, address, executeR2Command]);

useEffect(() => {
if (address === undefined) {
return;
}

addressInputRef.current!.value = `0x${address.toString(16)}`;
}, [address]);

const adjustAddress = useCallback((delta: number) => {
const newAddress = BigInt(addressInputRef.current!.value) + BigInt(delta);
onSelectAddress(newAddress);
}, [onSelectAddress]);

if (isLoading) {
return (
<Spinner className="memory-view" />
Expand All @@ -46,26 +63,42 @@ export default function MemoryView({ address }: MemoryViewProps) {

return (
<div className="memory-view">
<SegmentedControl
small={true}
options={[
{
label: "Raw",
value: "x",
},
{
label: "64",
value: "pxq"
},
{
label: "32",
value: "pxw"
},
]}
defaultValue="x"
onValueChange={setFormat}
/>
<div dangerouslySetInnerHTML={{ __html: data }} />
<div className="memory-view-toolbar">
<ControlGroup>
<Button icon="arrow-left" onClick={() => adjustAddress(-16)}></Button>
<InputGroup
inputRef={addressInputRef}
onKeyDown={e => {
if (e.key === "Enter") {
e.preventDefault();
onSelectAddress(BigInt(addressInputRef.current!.value));
}
}}
placeholder="Memory address…"
/>
<Button icon="arrow-right" onClick={() => adjustAddress(16)}></Button>
</ControlGroup>
<SegmentedControl
small={true}
options={[
{
label: "Raw",
value: "x",
},
{
label: "64",
value: "pxq"
},
{
label: "32",
value: "pxw"
},
]}
defaultValue="x"
onValueChange={setFormat}
/>
</div>
<div className="memory-view-data" dangerouslySetInnerHTML={{ __html: data }} />
</div>
);
}

0 comments on commit 3fa5210

Please sign in to comment.