partially completed pass 2

This commit is contained in:
Stephen 2018-07-05 12:33:10 +08:00 committed by GitHub
parent 8a21787258
commit 42aff804cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,17 +1,10 @@
translating by stenphenxs
在 Linux 上如何得到一个段错误的核心转储 在 Linux 上如何得到一个段错误的核心转储
============================================================ ============================================================
This week at work I spent all week trying to debug a segfault. Id never done this before, and some of the basic things involved (get a core dump! find the line number that segfaulted!) took me a long time to figure out. So heres a blog post explaining how to do those things! 本周工作中,我花了整整一周的时间来尝试调试一个段错误。我以前从来没有这样做过,我花了很长时间才弄清楚其中涉及的一些基本事情(获得核心转储、找到导致段错误的行号)。于是便有了这篇博客来解释如何做那些事情!
本周工作中,我花了整整一周的时间来尝试调试一个段错误。我以前从来没有这样做过,我花了很长时间才弄清楚其中涉及的一些基本事情(获得核心转储,找到导致段错误的行号!)。于是便有了这篇博客来解释如何做那些事情!
At the end of this blog post, you should know how to go from “oh no my program is segfaulting and I have no idea what is happening” to “well I know what its stack / line number was when it segfaulted at at least!“. 在看完这篇博客后,你应该知道如何从“哦,我的程序出现段错误,但我不知道正在发生什么”到“我知道它出现段错误时的堆栈、行号了! “。
在这篇博客的最后,你应该知道如何从“哦,我的程序出现段错误,但我不知道正在发生什么”到“我知道它出现段错误时的堆栈/行号了! “。
### whats a segfault?
### 什么是段错误? ### 什么是段错误?
@ -19,36 +12,20 @@ A “segmentation fault” is when your program tries to access memory that it
一个“段错误”是指你的程序尝试访问不允许访问或尝试访问的内存地址的情况。这可能是由于第二个tries to存疑 一个“段错误”是指你的程序尝试访问不允许访问或尝试访问的内存地址的情况。这可能是由于第二个tries to存疑
* trying to dereference a null pointer (youre not allowed to access the memory address `0`) * 试图解引用空指针(你不被允许访问内存地址 `0`
* 试图解引用空指针(你不被允许访问内存地址 `0` * 试图解引用其他一些不在你内存(译者注:指不在合法的内存地址区间内)中的指针;
* trying to dereference some other pointer that isnt in your memory * 一个已被破坏并且指向错误的地方的 C++ 虚表指针,这导致程序尝试执行没有执行权限的内存地址;
* 试图解引用其他一些不在你内存(译者注:指合法的内存地址空间)中的指针 * 其他一些我不明白的事情比如我认为访问未对齐的内存地址也可能会导致段错误译者注在要求自然边界对齐的体系结构如MIPS、ARM中更容易因非对齐访问产生段错误
* a C++ vtable pointer that got corrupted and is pointing to the wrong place, which causes the program to try to execute some memory that isnt executable 这个“C++ 虚表指针”是我的程序发生段错误的情况。我可能会在未来的博客中解释这个,因为我最初并不知道任何关于 C++ 的知识,并且这种虚表查找导致程序段错误的情况也是我所不了解的。
* 一个被破坏并且指向错误的地方的 C++ 虚表指针,它导致程序尝试把一些没有执行权限的内存地址空间当成指令执行
* some other things that I dont understand, like I think misaligned memory accesses can also segfault
* 其他一些我不明白的事情,比如我认为访问未对齐的内存地址也可能会导致段错误
This “C++ vtable pointer” thing is what was happening to my segfaulting program. I might explain that in a future blog post because I didnt know any C++ at the beginning of this week and this vtable lookup thing was a new way for a program to segfault that I didnt know about.
这个“C++ 虚表指针”是我的程序发生段错误的情况。我可能会在未来的博客中解释,因为我最初并不知道任何关于 C++的知识,并且这种虚表查找导致程序段错误的情况也是我所不了解的。
But! This blog post isnt about C++ bugs. Lets talk about the basics, like, how do we even get a core dump?
但是!这篇博客后不是关于 C++ 问题的。让我们谈论的基本的东西,比如,我们如何得到一个核心转储? 但是!这篇博客后不是关于 C++ 问题的。让我们谈论的基本的东西,比如,我们如何得到一个核心转储?
### step 1: run valgrind
### 步骤1运行 valgrind ### 步骤1运行 valgrind
I found the easiest way to figure out why my program is segfaulting was to use valgrind: I ran
我发现找出为什么我的程序出现段错误的最简单的方式是使用valgrind我运行 我发现找出为什么我的程序出现段错误的最简单的方式是使用valgrind我运行
``` ```
@ -56,16 +33,10 @@ valgrind -v your-program
``` ```
and this gave me a stack trace of what happened. Neat! 这给了我一个故障时的堆栈调用序列。 简洁!
这给了我一个(故障时的)堆栈调用序列。 简洁!
But I wanted also wanted to do a more in-depth investigation and find out more than just what valgrind was telling me! So I wanted to get a core dump and explore it.
但我想也希望做一个更深入调查并找出些valgrind没告诉我的信息 所以我想获得一个核心转储并探索它。 但我想也希望做一个更深入调查并找出些valgrind没告诉我的信息 所以我想获得一个核心转储并探索它。
### How to get a core dump
### 如何获得一个核心转储 ### 如何获得一个核心转储
A core dump is a copy of your programs memory, and its useful when youre trying to debug what went wrong with your problematic program. A core dump is a copy of your programs memory, and its useful when youre trying to debug what went wrong with your problematic program.