initial commit

This commit is contained in:
2025-11-19 09:28:12 -06:00
commit 398f96cdfe
2 changed files with 252 additions and 0 deletions

111
app.py Normal file
View File

@@ -0,0 +1,111 @@
#!/usr/bin/env python3
from flask import Flask, request, jsonify, send_from_directory
import subprocess
import re
import os
app = Flask(__name__)
TYPE_TO_GDB_FMT = {
"int8": "db",
"uint8": "dub",
"int16": "dh",
"uint16": "duh",
"int32": "dw",
"uint32": "duw",
"float": "f",
"double": "g",
}
def run_gdb_once(gdb_path, target, addr, length, data_type):
if data_type not in TYPE_TO_GDB_FMT:
raise ValueError(f"Unsupported type '{data_type}'")
fmt = TYPE_TO_GDB_FMT[data_type]
# Build gdb command:
# gdb -q --nx -ex "target remote ..." -ex "x/<len><fmt> <addr>" -ex "quit"
examine_cmd = f"x/{length}{fmt} {addr}"
result = subprocess.run(
[
gdb_path,
"--quiet",
"--nx",
"-ex", f"target remote {target}",
"-ex", examine_cmd,
"-ex", "quit",
],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise RuntimeError(
f"GDB exited with code {result.returncode}:\n{result.stderr or result.stdout}"
)
return result.stdout.splitlines()
def parse_values(lines, data_type):
values = []
line_re = re.compile(r"^0x[0-9a-fA-F]+:\s*(.*)$")
for line in lines:
line = line.strip()
m = line_re.match(line)
if not m:
continue
rest = m.group(1)
tokens = rest.split()
for t in tokens:
try:
if data_type in ("float", "double"):
v = float(t)
else:
v = int(t, 10)
values.append(v)
except ValueError:
pass
return values
@app.route("/")
def index():
return send_from_directory(os.path.dirname(__file__), "index.html")
@app.route("/api/read_memory", methods=["POST"])
def read_memory():
data = request.get_json(force=True)
gdb_path = data.get("gdb_path", "arm-none-eabi-gdb")
target = data.get("target", "localhost:3333")
addr = data.get("addr")
length = data.get("length")
data_type = data.get("type")
if addr is None or length is None or data_type is None:
return jsonify({"error": "addr, length, and type are required"}), 400
try:
length = int(length)
except ValueError:
return jsonify({"error": "length must be an integer"}), 400
try:
lines = run_gdb_once(gdb_path, target, addr, length, data_type)
values = parse_values(lines, data_type)
if len(values) > length:
values = values[:length]
return jsonify({
"addr": addr,
"length": length,
"type": data_type,
"values": values,
})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
# For dev only — behind firewall, not on the open internet.
app.run(host="127.0.0.1", port=5000, debug=True)

141
index.html Normal file
View File

@@ -0,0 +1,141 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MCU Memory Plotter</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: sans-serif;
margin: 20px;
}
form {
margin-bottom: 20px;
}
label {
display: inline-block;
width: 80px;
}
input, select {
margin-bottom: 8px;
}
#error {
color: red;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>MCU Memory Plotter</h1>
<form id="memForm">
<div>
<label for="addr">Address</label>
<input type="text" id="addr" value="0x20000000" required>
</div>
<div>
<label for="length">Length</label>
<input type="number" id="length" value="128" required>
</div>
<div>
<label for="type">Type</label>
<select id="type">
<option value="int8">int8</option>
<option value="uint8">uint8</option>
<option value="int16">int16</option>
<option value="uint16">uint16</option>
<option value="int32">int32</option>
<option value="uint32">uint32</option>
<option value="float" selected>float</option>
<option value="double">double</option>
</select>
</div>
<div>
<label for="target">Target</label>
<input type="text" id="target" value="localhost:3333">
</div>
<div>
<label for="gdb">GDB</label>
<input type="text" id="gdb" value="arm-none-eabi-gdb">
</div>
<button type="submit">Read & Plot</button>
</form>
<div id="error"></div>
<canvas id="chart" width="800" height="400"></canvas>
<script>
const ctx = document.getElementById('chart').getContext('2d');
let chart = null;
document.getElementById('memForm').addEventListener('submit', async (e) => {
e.preventDefault();
document.getElementById('error').textContent = "";
const addr = document.getElementById('addr').value;
const length = parseInt(document.getElementById('length').value, 10);
const type = document.getElementById('type').value;
const target = document.getElementById('target').value;
const gdb = document.getElementById('gdb').value;
try {
const res = await fetch('/api/read_memory', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
addr: addr,
length: length,
type: type,
target: target,
gdb_path: gdb
})
});
const data = await res.json();
if (!res.ok) {
throw new Error(data.error || "Unknown error");
}
const values = data.values || [];
const labels = values.map((_, i) => i);
if (chart) {
chart.destroy();
}
chart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: `Memory at ${data.addr} (${data.type})`,
data: values,
}]
},
options: {
responsive: true,
scales: {
x: {
title: {
display: true,
text: 'Index'
}
},
y: {
title: {
display: true,
text: 'Value'
}
}
}
}
});
} catch (err) {
document.getElementById('error').textContent = err.message;
}
});
</script>
</body>
</html>