簡體   English   中英

是否有創建核心轉儲的 x86 指令?

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

我想回答的一般問題

我有一些要調試的 x86 匯編代碼。 我想要一個核心轉儲,這樣我就可以檢查發生了什么。 是否有 x86 指令(或指令集)會在程序中的給定點生成核心轉儲? 如果有錯誤,有沒有辦法組裝程序集使其核心轉儲?

具體問題(此處解釋為上下文)

我正在按照An Incremental Approach to Compiler Construction為小型 lambda 演算編寫編譯器。 我現在正在努力實現閉包,我需要發出間接跳轉。 我正在嘗試編譯這段代碼:

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

我的編譯器生成以下內容:

     .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

我有一個用 C 編寫的隨附驅動程序文件,用於處理編譯代碼結果的格式和顯示。 作為參考,這里是:

#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);
   }
 }

當我運行gcc assembly-file.s driver.c時,它會在 macOS 上毫無怨言地編譯。 當我運行生成的a.out文件時,出現以下錯誤:

[2]    84530 bus error  ./a.out

有沒有辦法獲取核心轉儲以便檢查寄存器的值?

獎勵:如果你能看到我的程序集有什么問題,我也不介意回答這個問題。 ;-) 我試過將 GDB 與我的代碼一起使用,但每次我在a.out文件上嘗試它時它都會凍結。

我在 macOS 上運行它; 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

非常感激。

您可以咨詢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