0

General question that I would like answered

I have some x86 assembly code that I'm trying to debug. I'd like to get a core dump so I can inspect what is going on. Is there an x86 instruction (or set of instructions) that will generate a core dump at a given point in a program? Is there a way to assemble the assembly to make it core dump if there is an error?

Specific issue (explained here for context)

I am writing a compiler for a small lambda calculus following An Incremental Approach to Compiler Construction. I'm working now on implementing closures, and I need to issue an indirect jump. I'm trying to compile this code:

(labels ((f (code (n) () (+ n 1)))) (app (closure f) 3))

My compiler generates the following:

     .text
     .p2align 4,,15
     .globl _scheme_entry
 _scheme_entry:
     movq %rdi, %r15
     jmp _definition_end38349
 _func_f38350:
     movq $4, %rax
     movq %rax, -16(%rsp)
     movq -8(%rsp), %rax
     addq -16(%rsp), %rax
     ret
 _definition_end38349:
     movq $12, %rax
     movq %rax, -24(%rsp)
     movq %rdi, -8(%rsp)
     leaq _func_f38350(%rip), %rax
     movq %rax, 0(%r15)
     movq %r15, %rax
     orq $6, %rax
     addq $8, %r15
     xorq $6, %rax
     movq %rax, %rdi
     addq $8, %rsp
     callq *%rdi
     subq $8, %rsp
     movq -8(%rsp), %rdi
     ret

I have an accompanying driver file written in C that handles the formatting and display of the result of the compiled code. For reference, here it is:

#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>

 #define fixnum_mask  3
 #define fixnum_tag   0
 #define fixnum_shift 2

 #define data_mask    7
 #define cons_tag     1
 #define vector_tag   2
 #define string_tag   3
 #define symb_tag     5
 #define closure_tag  6

 #define empty_list   47

 #define char_tag     15
 #define char_mask    255
 #define char_shift   8

 #define bool_tag     31
 #define bool_mask    127
 #define bool_shift   7

 #define heap_size    8192

 size_t scheme_entry(size_t *heap);
 void format_val(size_t val);

 int main(int argc, char** argv) {
   size_t *heap = malloc(heap_size);
   size_t val = scheme_entry(heap);

   format_val(val);
   return 0;
 }

 void format_val(size_t val) {
   if ((val & bool_mask) == bool_tag) {
     printf((val >> bool_shift) ? "#t" : "#f");
   }
   else if ((val & fixnum_mask) == fixnum_tag) {
     printf("%zu", val >> fixnum_shift);
   }
   else if ((val & data_mask) == closure_tag) {
     printf("#<closure %zx>", val);
   }
   else if ((val & fixnum_mask) == cons_tag) {
     val--;
     size_t car = *((size_t*)val);
     size_t cdr = *((size_t*)val + 1);
     printf("("); format_val(car); printf(" . "); format_val(cdr); printf(")");
   }
   else if (val == empty_list) {
     printf("()");
   }
   /* else if ((val & char_mask) == char_tag) { */
   /*   printf("%c", val >> char_shift); */
   /* } */
   else {
     printf("#<unknown value: %zx>", val);
   }
 }

It compiles without complaint on macOS when I run gcc assembly-file.s driver.c. When I run the resulting a.out file, I get the following error:

[2]    84530 bus error  ./a.out

Is there a way I can get a core dump so I can inspect the values of the registers?

Bonus: if you can see what's wrong with my assembly, I wouldn't mind an answer to that either. ;-) I've tried using the GDB with my code, but it freezes every time I try it out on the a.out file.

I'm running this on macOS; gcc --version gives:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Much appreciated.

Ashton Wiersdorf
  • 1,865
  • 12
  • 33
  • 2
    Why not use `lldb` to single step and see what's going on? – Gene Jun 10 '20 at 04:11
  • 3
    `SIGBUS` (Bus error) generates a core dump by default. If you're not getting one, then you should look into why (maybe they're disabled via `ulimit`) rather than looking for another instruction to try to make one, which will almost certainly fail for the same reason. – Joseph Sible-Reinstate Monica Jun 10 '20 at 05:27
  • You might find the `ud2` instruction useful. – Brett Hale Jun 10 '20 at 08:38
  • Run your code under a debugger to catch faults and examine everything in situ. That's one thing debuggers are really good at. You can even use an `int3` instruction in your asm source code to set a software break point. (That's the same instruction that debuggers use to overwrite the first byte of an instruction when you set a breakpoint within a debugger.) – Peter Cordes Jun 10 '20 at 11:16
  • +1 for `lldb` @Gene. I've tried using GDB, but it kept crashing or hanging when I tried running my assembly. LLDB works like a charm. Thanks all for the very useful comments. I still haven't managed to create a core dump (see comment on @yugr's answer); @JosephSible what do I need to do with `ulimit` to make sure I get a core dump? I noticed `ulimit -c` returned 0; I set it to 8192 just to see what would happen, but I still didn't get a core dump. What am I missing? – Ashton Wiersdorf Jun 11 '20 at 05:39

1 Answers1

1

You can ask GCC:

$ cat tmp.c
void foo() { __builtin_trap(); }
$ gcc -O2 tmp.c
$ ./a.out 
Illegal instruction (core dumped)
$ gcc -O2 tmp.c -S -o-
        ...
        ud2
yugr
  • 19,769
  • 3
  • 51
  • 96
  • The invocation I used on my mac was `gcc -fomit-frame-pointer -S tmp.c` and then looked in `tmp.s`. This is a good technique though! Thank you! Strangely enough, I don't see a core dump file. Is there something I need to make sure is set with `ulimit` or the like? – Ashton Wiersdorf Jun 11 '20 at 05:37
  • 1
    @AshtonWiersdorf Yes, most likely coredumps are disabled on your system/ Enabling them depends on your system (on Linux you need `ulimit -c unlimited`, for Mac see [here](https://stackoverflow.com/questions/9412156/how-to-generate-core-dumps-in-mac-os-x)). – yugr Jun 11 '20 at 08:15