Skip to content

Commit f462888

Browse files
committed
Add proper options struct to simplified remote GDB
1 parent b7c4f3b commit f462888

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/tinykvm/machine.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,13 @@ struct Machine
212212
void print_registers() const { vcpu.print_registers(); }
213213
void print_pagetables() const;
214214
void print_exception_handlers() const;
215-
void print_remote_gdb_backtrace(const std::string& filename, const std::string& gdb_path = "/usr/bin/gdb");
215+
struct RemoteGDBOptions {
216+
std::string gdb_path = "/usr/bin/gdb";
217+
std::string command = "bt";
218+
bool verbose = false;
219+
bool quit = false;
220+
};
221+
void print_remote_gdb_backtrace(const std::string& filename, const RemoteGDBOptions& opts);
216222

217223
void install_memory(uint32_t idx, const VirtualMem&, bool ro);
218224
void delete_memory(uint32_t idx);

lib/tinykvm/machine_debug.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern char** environ;
99
namespace tinykvm {
1010
void Machine::print_remote_gdb_backtrace(
1111
const std::string& filename,
12-
const std::string& gdb_path)
12+
const RemoteGDBOptions& opts)
1313
{
1414
const uint16_t port = 2159;
1515

@@ -23,7 +23,7 @@ void Machine::print_remote_gdb_backtrace(
2323
throw std::runtime_error("Unable to create script for debugging");
2424
}
2525

26-
const std::string debugscript =
26+
std::string debugscript =
2727
// Delete the script file (after GDB closes it)
2828
"shell unlink " + std::string(scrname)
2929
+ "\n"
@@ -34,7 +34,9 @@ void Machine::print_remote_gdb_backtrace(
3434
"target remote localhost:"
3535
+ std::to_string(port)
3636
+ "\n"
37-
+ "up\nbt\n";
37+
+ opts.command + "\n";
38+
if (opts.quit)
39+
debugscript += "quit\n";
3840

3941
ssize_t len = write(fd, debugscript.c_str(), debugscript.size());
4042
if (len < (ssize_t)debugscript.size())
@@ -45,7 +47,7 @@ void Machine::print_remote_gdb_backtrace(
4547
close(fd);
4648

4749
const char* argv[]
48-
= {gdb_path.c_str(), "-x", scrname, nullptr};
50+
= {opts.gdb_path.c_str(), "-x", scrname, nullptr};
4951
// XXX: This is not kosher, but GDB is open-source, safe and let's not
5052
// pretend that anyone downloads gdb-multiarch from a website anyway.
5153
// There is a finite list of things we should pass to GDB to make it
@@ -58,10 +60,15 @@ void Machine::print_remote_gdb_backtrace(
5860
}
5961

6062
RSP server {filename, *this, port};
63+
if (opts.verbose) {
64+
printf("Waiting for GDB to connect on port %u...\n", port);
65+
}
6166
auto client = server.accept();
6267
if (client != nullptr)
6368
{
64-
printf("GDB connected\n");
69+
if (opts.verbose) {
70+
printf("GDB connected\n");
71+
}
6572
// client->set_verbose(true);
6673
while (client->process_one())
6774
;

0 commit comments

Comments
 (0)