about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-10-10 19:51:20 -0700
committerKartik Agaram <vc@akkartik.com>2018-10-10 20:54:15 -0700
commit8950915a007f78c427f176609678c50ef9923e5b (patch)
treedbe10980201791f98d7ed02944c98d1defec38be
parent2a15acd51025c9a6e63865b6ba7b3fbbd4bdd802 (diff)
downloadmu-8950915a007f78c427f176609678c50ef9923e5b.tar.gz
4678
A debugging aid: 'subx --map translate' dumps a mapping from functions
to addresses to a file called "map", and 'subx --map run' loads the mapping
in "map", augmenting debug traces.

Let's see how much this helps. Debugging machine code has been pretty painful
lately.
-rw-r--r--subx/010---vm.cc1
-rw-r--r--subx/028translate.cc2
-rw-r--r--subx/035labels.cc13
-rw-r--r--subx/036global_variables.cc4
-rw-r--r--subx/039debug.cc25
5 files changed, 41 insertions, 4 deletions
diff --git a/subx/010---vm.cc b/subx/010---vm.cc
index a2fe41e8..745d64e0 100644
--- a/subx/010---vm.cc
+++ b/subx/010---vm.cc
@@ -264,6 +264,7 @@ inline bool already_allocated(uint32_t addr) {
 // skeleton of how x86 instructions are decoded
 void run_one_instruction() {
   uint8_t op=0, op2=0, op3=0;
+  // Run One Instruction
   trace(90, "run") << "inst: 0x" << HEXWORD << EIP << end();
   op = next();
   if (Dump_trace) {
diff --git a/subx/028translate.cc b/subx/028translate.cc
index 38f8a223..48c11786 100644
--- a/subx/028translate.cc
+++ b/subx/028translate.cc
@@ -20,6 +20,7 @@
 if (is_equal(argv[1], "translate")) {
   START_TRACING_UNTIL_END_OF_SCOPE;
   reset();
+  // Begin subx translate
   program p;
   string output_filename;
   for (int i = /*skip 'subx translate'*/2;  i < argc;  ++i) {
@@ -59,6 +60,7 @@ if (is_equal(argv[1], "translate")) {
     unlink(output_filename.c_str());
     return 1;
   }
+  // End subx translate
   return 0;
 }
 
diff --git a/subx/035labels.cc b/subx/035labels.cc
index e2f07d2b..30a6082e 100644
--- a/subx/035labels.cc
+++ b/subx/035labels.cc
@@ -118,8 +118,8 @@ void compute_byte_indices_for_labels(const segment& code, map<string, int32_t>&
           raise << "'" << to_string(inst) << "': label definition (':') not allowed in operand\n" << end();
         if (j > 0)
           raise << "'" << to_string(inst) << "': labels can only be the first word in a line.\n" << end();
-        if (Dump_map)
-          cerr << "0x" << HEXWORD << (code.start + current_byte) << ' ' << label << '\n';
+        if (Map_file.is_open())
+          Map_file << "0x" << HEXWORD << (code.start + current_byte) << ' ' << label << '\n';
         put(byte_index, label, current_byte);
         trace(99, "transform") << "label '" << label << "' is at address " << (current_byte+code.start) << end();
         // no modifying current_byte; label definitions won't be in the final binary
@@ -130,10 +130,19 @@ void compute_byte_indices_for_labels(const segment& code, map<string, int32_t>&
 
 :(before "End Globals")
 bool Dump_map = false;  // currently used only by 'subx translate'
+ofstream Map_file;
 :(before "End Commandline Options")
 else if (is_equal(*arg, "--map")) {
   Dump_map = true;
+  // End --map Settings
 }
+//: wait to open "map" for writing until we're sure we aren't trying to read it
+:(after "Begin subx translate")
+if (Dump_map)
+  Map_file.open("map");
+:(before "End subx translate")
+if (Dump_map)
+  Map_file.close();
 
 :(code)
 void drop_labels(segment& code) {
diff --git a/subx/036global_variables.cc b/subx/036global_variables.cc
index a5e15369..5281b545 100644
--- a/subx/036global_variables.cc
+++ b/subx/036global_variables.cc
@@ -47,8 +47,8 @@ void compute_addresses_for_global_variables(const segment& s, map<string, uint32
         if (trace_contains_errors()) return;
         if (j > 0)
           raise << "'" << to_string(inst) << "': global variable names can only be the first word in a line.\n" << end();
-        if (Dump_map)
-          cerr << "0x" << HEXWORD << current_address << ' ' << variable << '\n';
+        if (Map_file.is_open())
+          Map_file << "0x" << HEXWORD << current_address << ' ' << variable << '\n';
         put(address, variable, current_address);
         trace(99, "transform") << "global variable '" << variable << "' is at address 0x" << HEXWORD << current_address << end();
         // no modifying current_address; global variable definitions won't be in the final binary
diff --git a/subx/039debug.cc b/subx/039debug.cc
new file mode 100644
index 00000000..4d041fa4
--- /dev/null
+++ b/subx/039debug.cc
@@ -0,0 +1,25 @@
+//: Some helpers for debugging.
+
+// Load the 'map' file generated during 'subx --map translate' when running 'subx --map --dump run'.
+// (It'll only affect the trace.)
+
+:(before "End Globals")
+map</*address*/uint32_t, string> Symbol_name;  // used only by 'subx run'
+:(before "End --map Settings")
+load_map("map");
+:(code)
+void load_map(const string& map_filename) {
+  ifstream fin(map_filename.c_str());
+  fin >> std::hex;
+  while (has_data(fin)) {
+    uint32_t addr = 0;
+    fin >> addr;
+    string name;
+    fin >> name;
+    put(Symbol_name, addr, name);
+  }
+}
+
+:(after "Run One Instruction")
+if (contains_key(Symbol_name, EIP))
+  trace(90, "run") << "== label " << get(Symbol_name, EIP) << end();