Compare commits
8 Commits
479319e7ca
...
main
Author | SHA1 | Date | |
---|---|---|---|
9a50607d5f | |||
bc47ff4883 | |||
3670bebbc4 | |||
309851ab06 | |||
7ae72ed5a8 | |||
8d2c76e410 | |||
8c3a166a5d | |||
eb8e5175bd |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
zig-out/
|
||||
zig-cache/
|
||||
.zig-cache/
|
||||
|
0
.gitmodules
vendored
0
.gitmodules
vendored
37
build.zig
37
build.zig
@@ -15,40 +15,21 @@ pub fn build(b: *std.Build) void {
|
||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
_ = b.addModule("gdbstub", .{
|
||||
.source_file = .{ .path = "src/lib.zig" },
|
||||
.dependencies = &.{},
|
||||
});
|
||||
_ = b.addModule("zba-gdbstub", .{ .root_source_file = b.path("src/lib.zig") });
|
||||
|
||||
// Creates a step for unit testing. This only builds the test executable
|
||||
// but does not run it.
|
||||
const lib_tests = b.addTest(.{
|
||||
.root_source_file = .{ .path = "src/lib.zig" },
|
||||
const lib_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/lib.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const run_lib_tests = b.addRunArtifact(lib_tests);
|
||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
||||
|
||||
// This creates a build step. It will be visible in the `zig build --help` menu,
|
||||
// and can be selected like this: `zig build test`
|
||||
// This will evaluate the `test` step rather than the default, which is "install".
|
||||
const test_step = b.step("test", "Run library tests");
|
||||
test_step.dependOn(&run_lib_tests.step);
|
||||
}
|
||||
|
||||
pub fn module(b: *std.Build) *std.Build.Module {
|
||||
return b.createModule(.{
|
||||
.source_file = .{ .path = path("/src/lib.zig") },
|
||||
.dependencies = &.{},
|
||||
});
|
||||
}
|
||||
|
||||
// https://github.com/MasterQ32/SDL.zig/blob/4d565b54227b862c1540719e0e21a36d649e87d5/build.zig#L114-L120
|
||||
fn path(comptime suffix: []const u8) []const u8 {
|
||||
if (suffix[0] != '/') @compileError("relToPath requires an absolute path!");
|
||||
return comptime blk: {
|
||||
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
|
||||
break :blk root_dir ++ suffix;
|
||||
};
|
||||
// Similar to creating the run step earlier, this exposes a `test` step to
|
||||
// the `zig build --help` menu, providing a way for the user to request
|
||||
// running the unit tests.
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&run_lib_unit_tests.step);
|
||||
}
|
||||
|
@@ -1,5 +1,72 @@
|
||||
.{
|
||||
// This is the default name used by packages depending on this one. For
|
||||
// example, when a user runs `zig fetch --save <url>`, this field is used
|
||||
// as the key in the `dependencies` table. Although the user can choose a
|
||||
// different name, most users will stick with this provided value.
|
||||
//
|
||||
// It is redundant to include "zig" in this name because it is already
|
||||
// within the Zig package namespace.
|
||||
.name = "zba-gdbstub",
|
||||
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.1.0",
|
||||
.dependencies = .{},
|
||||
|
||||
// This field is optional.
|
||||
// This is currently advisory only; Zig does not yet do anything
|
||||
// with this value.
|
||||
//.minimum_zig_version = "0.11.0",
|
||||
|
||||
// This field is optional.
|
||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||
// Once all dependencies are fetched, `zig build` no longer requires
|
||||
// internet connectivity.
|
||||
.dependencies = .{
|
||||
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||
//.example = .{
|
||||
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||
// // the new URL.
|
||||
// .url = "https://example.com/foo.tar.gz",
|
||||
//
|
||||
// // This is computed from the file contents of the directory of files that is
|
||||
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||
// // `paths`.
|
||||
// //
|
||||
// // This field is the source of truth; packages do not come from a `url`; they
|
||||
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||
// // obtain a package matching this `hash`.
|
||||
// //
|
||||
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
||||
// .hash = "...",
|
||||
//
|
||||
// // When this is provided, the package is found in a directory relative to the
|
||||
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||
// // computed. This field and `url` are mutually exclusive.
|
||||
// .path = "foo",
|
||||
|
||||
// // When this is set to `true`, a package is declared to be lazily
|
||||
// // fetched. This makes the dependency only get fetched if it is
|
||||
// // actually used.
|
||||
// .lazy = false,
|
||||
//},
|
||||
},
|
||||
|
||||
// Specifies the set of files and directories that are included in this package.
|
||||
// Only files and directories listed here are included in the `hash` that
|
||||
// is computed for this package. Only files listed here will remain on disk
|
||||
// when using the zig package manager. As a rule of thumb, one should list
|
||||
// files required for compilation plus any license(s).
|
||||
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
||||
// the build root itself.
|
||||
// A directory listed here means that all files within, recursively, are included.
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
// For example...
|
||||
//"LICENSE",
|
||||
//"README.md",
|
||||
},
|
||||
}
|
||||
|
@@ -5,9 +5,6 @@ const Emulator = @import("lib.zig").Emulator;
|
||||
const State = @import("State.zig");
|
||||
const Server = @import("Server.zig");
|
||||
|
||||
const target = @import("Server.zig").target;
|
||||
const memory_map = @import("Server.zig").memory_map;
|
||||
|
||||
const Self = @This();
|
||||
const log = std.log.scoped(.Packet);
|
||||
pub const max_len: usize = 0x1000;
|
||||
@@ -198,12 +195,19 @@ pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emul
|
||||
},
|
||||
'H' => return .{ .static = "" },
|
||||
'v' => {
|
||||
if (!substr(self.contents[1..], "MustReplyEmpty")) {
|
||||
log.warn("Unimplemented: {s}", .{self.contents});
|
||||
if (substr(self.contents[1..], "MustReplyEmpty")) return .{ .static = "" };
|
||||
|
||||
if (substr(self.contents[1..], "Cont")) {
|
||||
switch (self.contents[5]) {
|
||||
'?' => return .{ .static = "" }, // TODO: Implement vCont
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
log.warn("Unimplemented: {s}", .{self.contents});
|
||||
return .{ .static = "" };
|
||||
},
|
||||
'T' => return .{ .static = "OK " }, // We assume single threaded here
|
||||
'q' => {
|
||||
if (self.contents[1] == 'C' and self.contents.len == 2) return .{ .static = "QC1" };
|
||||
|
||||
@@ -211,11 +215,19 @@ pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emul
|
||||
if (substr(self.contents[1..], "sThreadInfo")) return .{ .static = "l" };
|
||||
if (substr(self.contents[1..], "Attached")) return .{ .static = "1" }; // Tell GDB we're attached to a process
|
||||
|
||||
if (substr(self.contents[1..], "Supported")) {
|
||||
const format = "PacketSize={x:};swbreak+;hwbreak+;qXfer:features:read+;qXfer:memory-map:read+";
|
||||
// TODO: Anything else?
|
||||
if (substr(self.contents[1..], "ThreadExtraInfo")) {
|
||||
const extra_info = "FIXME: what is even expected here?";
|
||||
const ret = try allocator.dupe(u8, &std.fmt.bytesToHex(extra_info, .lower));
|
||||
|
||||
const ret = try std.fmt.allocPrint(allocator, format, .{Self.max_len});
|
||||
return .{ .alloc = ret };
|
||||
}
|
||||
|
||||
if (substr(self.contents[1..], "Supported")) {
|
||||
const format = "PacketSize={x:};swbreak+;hwbreak+;qXfer:features:read+;{s}";
|
||||
const mem_map = if (state.memmap_xml == null) "" else "qXfer:memory-map:read+";
|
||||
|
||||
// TODO: Anything else?
|
||||
const ret = try std.fmt.allocPrint(allocator, format, .{ Self.max_len, mem_map });
|
||||
return .{ .alloc = ret };
|
||||
}
|
||||
|
||||
@@ -236,11 +248,11 @@ pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emul
|
||||
// + 2 to account for the "m " in the response
|
||||
// subtract offset so that the allocated buffer isn't
|
||||
// larger than it needs to be TODO: Test this?
|
||||
const len = @min(length, (target.len + 1) - offset);
|
||||
const len = @min(length, (state.target_xml.len + 1) - offset);
|
||||
const ret = try allocator.alloc(u8, len);
|
||||
|
||||
ret[0] = if (ret.len < length) 'l' else 'm';
|
||||
std.mem.copy(u8, ret[1..], target[offset..]);
|
||||
@memcpy(ret[1..], state.target_xml[offset..]);
|
||||
|
||||
return .{ .alloc = ret };
|
||||
} else {
|
||||
@@ -252,6 +264,8 @@ pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emul
|
||||
}
|
||||
|
||||
if (substr(self.contents[1..], "Xfer:memory-map:read")) {
|
||||
const mem_map = state.memmap_xml.?;
|
||||
|
||||
var tokens = std.mem.tokenize(u8, self.contents[1..], ":,");
|
||||
_ = tokens.next(); // Xfer
|
||||
_ = tokens.next(); // memory-map
|
||||
@@ -263,11 +277,11 @@ pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emul
|
||||
const length = try std.fmt.parseInt(usize, length_str, 16);
|
||||
|
||||
// see above
|
||||
const len = @min(length, (memory_map.len + 1) - offset);
|
||||
const len = @min(length, (mem_map.len + 1) - offset);
|
||||
const ret = try allocator.alloc(u8, len);
|
||||
|
||||
ret[0] = if (ret.len < length) 'l' else 'm';
|
||||
std.mem.copy(u8, ret[1..], memory_map[offset..]);
|
||||
@memcpy(ret[1..], mem_map[offset..]);
|
||||
|
||||
return .{ .alloc = ret };
|
||||
}
|
||||
|
@@ -2,73 +2,37 @@ const std = @import("std");
|
||||
const Packet = @import("Packet.zig");
|
||||
const Emulator = @import("lib.zig").Emulator;
|
||||
|
||||
const Atomic = std.atomic.Atomic;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Server = std.net.StreamServer;
|
||||
const Connection = Server.Connection;
|
||||
const Server = std.net.Server;
|
||||
|
||||
const Self = @This();
|
||||
const log = std.log.scoped(.Server);
|
||||
const port: u16 = 2424;
|
||||
|
||||
pub const target: []const u8 =
|
||||
\\<target version="1.0">
|
||||
\\ <architecture>armv4t</architecture>
|
||||
\\ <feature name="org.gnu.gdb.arm.core">
|
||||
\\ <reg name="r0" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r1" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r2" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r3" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r4" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r5" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r6" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r7" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r8" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r9" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r10" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r11" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r12" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="sp" bitsize="32" type="data_ptr"/>
|
||||
\\ <reg name="lr" bitsize="32"/>
|
||||
\\ <reg name="pc" bitsize="32" type="code_ptr"/>
|
||||
\\
|
||||
\\ <reg name="cpsr" bitsize="32" regnum="25"/>
|
||||
\\ </feature>
|
||||
\\</target>
|
||||
;
|
||||
|
||||
// Game Pak SRAM isn't included
|
||||
// TODO: Can i be more specific here?
|
||||
pub const memory_map: []const u8 =
|
||||
\\ <memory-map version="1.0">
|
||||
\\ <memory type="rom" start="0x00000000" length="0x00004000"/>
|
||||
\\ <memory type="ram" start="0x02000000" length="0x00040000"/>
|
||||
\\ <memory type="ram" start="0x03000000" length="0x00008000"/>
|
||||
\\ <memory type="ram" start="0x04000000" length="0x00000400"/>
|
||||
\\ <memory type="ram" start="0x05000000" length="0x00000400"/>
|
||||
\\ <memory type="ram" start="0x06000000" length="0x00018000"/>
|
||||
\\ <memory type="ram" start="0x07000000" length="0x00000400"/>
|
||||
\\ <memory type="rom" start="0x08000000" length="0x02000000"/>
|
||||
\\ <memory type="rom" start="0x0A000000" length="0x02000000"/>
|
||||
\\ <memory type="rom" start="0x0C000000" length="0x02000000"/>
|
||||
\\ </memory-map>
|
||||
;
|
||||
|
||||
// FIXME: Shouldn't this be a Packet Struct?
|
||||
pkt_cache: ?[]const u8 = null,
|
||||
|
||||
socket: Server,
|
||||
state: State = .{},
|
||||
state: State,
|
||||
|
||||
emu: Emulator,
|
||||
|
||||
pub const State = struct { should_quit: bool = false };
|
||||
pub const State = struct {
|
||||
should_quit: bool = false,
|
||||
target_xml: []const u8,
|
||||
memmap_xml: ?[]const u8,
|
||||
};
|
||||
|
||||
pub fn init(emulator: Emulator) !Self {
|
||||
var server = std.net.StreamServer.init(.{});
|
||||
try server.listen(std.net.Address.initIp4([_]u8{0} ** 4, port));
|
||||
const Xml = struct { target: []const u8, memory_map: ?[]const u8 };
|
||||
|
||||
return .{ .emu = emulator, .socket = server };
|
||||
pub fn init(emulator: Emulator, xml: Xml) !Self {
|
||||
const localhost = std.net.Address.initIp4(.{ 127, 0, 0, 1 }, port);
|
||||
|
||||
return .{
|
||||
.emu = emulator,
|
||||
.socket = try localhost.listen(.{}),
|
||||
.state = .{ .target_xml = xml.target, .memmap_xml = xml.memory_map },
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self, allocator: Allocator) void {
|
||||
@@ -86,27 +50,26 @@ const Action = union(enum) {
|
||||
nack,
|
||||
};
|
||||
|
||||
pub fn run(self: *Self, allocator: Allocator, quit: *Atomic(bool)) !void {
|
||||
pub fn run(self: *Self, allocator: Allocator, should_quit: *std.atomic.Value(bool)) !void {
|
||||
var buf: [Packet.max_len]u8 = undefined;
|
||||
|
||||
var client = try self.socket.accept();
|
||||
log.info("client connected from {}", .{client.address});
|
||||
|
||||
while (true) {
|
||||
if (self.state.should_quit) break;
|
||||
while (!should_quit.load(.monotonic)) {
|
||||
if (self.state.should_quit) {
|
||||
// Just in case its the gdbstub that exited first,
|
||||
// attempt to signal to the GUI that it should also exit
|
||||
should_quit.store(true, .monotonic);
|
||||
break;
|
||||
}
|
||||
|
||||
const len = try client.stream.read(&buf);
|
||||
if (len == 0) break;
|
||||
|
||||
if (quit.load(.Monotonic)) break;
|
||||
const action = try self.parse(allocator, buf[0..len]);
|
||||
|
||||
try self.send(allocator, client, action);
|
||||
}
|
||||
|
||||
// Just in case its the gdbstub that exited first,
|
||||
// attempt to signal to the GUI that it should also exit
|
||||
quit.store(true, .Monotonic);
|
||||
}
|
||||
|
||||
fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action {
|
||||
|
50
src/test.zig
50
src/test.zig
@@ -7,6 +7,49 @@ const Server = @import("Server.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const BarebonesEmulator = struct {
|
||||
|
||||
// I have this ARMv4T and GBA memory map xml lying around so we'll reuse it here
|
||||
const target: []const u8 =
|
||||
\\<target version="1.0">
|
||||
\\ <architecture>armv4t</architecture>
|
||||
\\ <feature name="org.gnu.gdb.arm.core">
|
||||
\\ <reg name="r0" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r1" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r2" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r3" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r4" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r5" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r6" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r7" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r8" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r9" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r10" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r11" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="r12" bitsize="32" type="uint32"/>
|
||||
\\ <reg name="sp" bitsize="32" type="data_ptr"/>
|
||||
\\ <reg name="lr" bitsize="32"/>
|
||||
\\ <reg name="pc" bitsize="32" type="code_ptr"/>
|
||||
\\
|
||||
\\ <reg name="cpsr" bitsize="32" regnum="25"/>
|
||||
\\ </feature>
|
||||
\\</target>
|
||||
;
|
||||
|
||||
const memory_map: []const u8 =
|
||||
\\ <memory-map version="1.0">
|
||||
\\ <memory type="rom" start="0x00000000" length="0x00004000"/>
|
||||
\\ <memory type="ram" start="0x02000000" length="0x00040000"/>
|
||||
\\ <memory type="ram" start="0x03000000" length="0x00008000"/>
|
||||
\\ <memory type="ram" start="0x04000000" length="0x00000400"/>
|
||||
\\ <memory type="ram" start="0x05000000" length="0x00000400"/>
|
||||
\\ <memory type="ram" start="0x06000000" length="0x00018000"/>
|
||||
\\ <memory type="ram" start="0x07000000" length="0x00000400"/>
|
||||
\\ <memory type="rom" start="0x08000000" length="0x02000000"/>
|
||||
\\ <memory type="rom" start="0x0A000000" length="0x02000000"/>
|
||||
\\ <memory type="rom" start="0x0C000000" length="0x02000000"/>
|
||||
\\ </memory-map>
|
||||
;
|
||||
|
||||
r: [16]u32 = [_]u32{0} ** 16,
|
||||
|
||||
pub fn interface(self: *@This(), allocator: Allocator) Emulator {
|
||||
@@ -57,13 +100,16 @@ test Server {
|
||||
}
|
||||
}.inner;
|
||||
|
||||
var server = try Server.init(iface);
|
||||
var server = try Server.init(
|
||||
iface,
|
||||
.{ .target = BarebonesEmulator.target, .memory_map = BarebonesEmulator.memory_map },
|
||||
);
|
||||
defer server.deinit(allocator);
|
||||
|
||||
const t = try std.Thread.spawn(.{}, clientFn, .{server.socket.listen_address});
|
||||
defer t.join();
|
||||
|
||||
var should_quit = std.atomic.Atomic(bool).init(false);
|
||||
var should_quit = std.atomic.Value(bool).init(false);
|
||||
|
||||
try server.run(std.testing.allocator, &should_quit);
|
||||
}
|
||||
|
Reference in New Issue
Block a user