简体   繁体   English

是否有创建核心转储的 x86 指令?

[英]Is there an x86 instruction to create a core dump?

General question that I would like answered我想回答的一般问题

I have some x86 assembly code that I'm trying to debug.我有一些要调试的 x86 汇编代码。 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?是否有 x86 指令(或指令集)会在程序中的给定点生成核心转储? 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 .我正在按照An Incremental Approach to Compiler Construction为小型 lambda 演算编写编译器。 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.我有一个用 C 编写的随附驱动程序文件,用于处理编译代码结果的格式和显示。 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 .当我运行gcc assembly-file.s driver.c时,它会在 macOS 上毫无怨言地编译。 When I run the resulting a.out file, I get the following error:当我运行生成的a.out文件时,出现以下错误:

[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. ;-) 我试过将 GDB 与我的代码一起使用,但每次我在a.out文件上尝试它时它都会冻结。

I'm running this on macOS;我在 macOS 上运行它; gcc --version gives: gcc --version给出:

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.非常感激。

You can ask GCC:您可以咨询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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM