__static_initialization_and_destruction_0(int, int) () 分段错误

huangapple go评论83阅读模式
英文:

__static_initialization_and_destruction_0(int, int) () segmentation fault

问题

我知道类似的问题已经被提出,但根据我看到的内容,我无法确定为什么会出现这个错误。

这是回溯信息:
__static_initialization_and_destruction_0(int, int) () 分段错误

SymbolStack的定义(在Symbols.h中):

#ifndef HW3_SYMBOLS_H
#define HW3_SYMBOLS_H

#include <string>
#include <vector>
#include <iostream>
#include "shared_def.h"

using std::vector; using std::string;
extern int yylineno;

...

class SymbolStack {
public:
    vector<int> pos_stack = vector<int>();
    vector<SymbolTable*> scope_stack = vector<SymbolTable*>();
public:
    SymbolStack();
    ErrorType is_func_valid(const string& func_name, const string& retype, vector<string>* params_types, bool is_override);
    void push(bool is_while, Types retType = T_NONE);
    void pop();
    SymbolTable* get_curr_scope();
    void symbol_document(const string& symbol_name,const string& symbol_type, bool is_func = false,
                         bool is_over = false, vector<string>* func_params_types = nullptr);
    void func_param_document(const string& symbol_name, const string& symbol_type, int pos);
    bool symbol_exists(const string& symbol_name);
    SymbolEntry* get_symbol(const string& symbol_name);
    SymbolEntry* get_func_symbol(const string& func_name, const string& retype, vector<string>* params_types, int* count);
    bool check_program();
    ~SymbolStack();
};

...

#endif //HW3_SYMBOLS_H

Types是在shared_def.h中定义的枚举。
Symbols.cpp中的方法:

SymbolStack::SymbolStack(){
    pos_stack.push_back(0); // 初始化为偏移量为零
    this->push(false, T_VOID);
    vector<string> print_param = vector<string>();
    vector<string> printi_param = vector<string>();
    print_param.push_back("string");
    printi_param.push_back("int");
    this->symbol_document("print", "void", FUNC, &print_param);
    this->symbol_document("printi", "void", FUNC, &printi_param);
}

void SymbolStack::push(bool is_while, Types retType){
    retType = retType == T_NONE ? scope_stack.back()->ret_type : retType;
    SymbolTable* first_scope = new SymbolTable(pos_stack.back(), is_while, retType);
    pos_stack.push_back(pos_stack.back());
    scope_stack.push_back(first_scope);
}

SymbolStack的构造函数在parser.ypp中被调用:

%{
    #include <stdio.h>
    #include <iostream>
    #include "types.h"
    #include "hw3_output.hpp"
    extern int yylineno;
    extern int yylex();
    int yyerror(const char* message);
    extern SymbolStack tables;
%}

定义...

%%

规则...

%%

tables = SymbolStack();
int main(){
    int res = yyparse();
    bool is_valid_program = tables.check_program();
    if(!is_valid_program){
        output::errorMainMissing();
        exit(0);
    }
    tables.pop();
    return res;
}

int yyerror(const char * message){
    output::errorSyn(yylineno);
    exit(0);
}

我无法使用gdb进行调试,因为它甚至在main函数被调用之前就发生了。如果我从构造函数中删除这行代码 "this->push(false, T_VOID);",错误仍然会发生。

任何帮助将不胜感激。

编辑:我使用的任何源文件中都没有定义为静态的内容。

英文:

I know similar questions have been asked, but I couldn't determine based on what I've seen why I'm getting this error.

This is the backtrace:
__static_initialization_and_destruction_0(int, int) () 分段错误

The definition of SymbolStack (in Symbols.h):

#ifndef HW3_SYMBOLS_H
#define HW3_SYMBOLS_H

#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &quot;shared_def.h&quot;

using std::vector; using std::string;
extern int yylineno;

...

class SymbolStack {
public:
    vector&lt;int&gt; pos_stack = vector&lt;int&gt;();
    vector&lt;SymbolTable*&gt; scope_stack = vector&lt;SymbolTable*&gt;();
public:
    SymbolStack();
    ErrorType is_func_valid(const string&amp; func_name, const string&amp; retype, vector&lt;string&gt;* params_types, bool is_override);
    void push(bool is_while, Types retType = T_NONE);
    void pop();
    SymbolTable* get_curr_scope();
    void symbol_document(const string&amp; symbol_name,const string&amp; symbol_type, bool is_func = false,
                         bool is_over = false, vector&lt;string&gt;* func_params_types = nullptr); // add new symbol to the stack
    void func_param_document(const string&amp; symbol_name, const string&amp; symbol_type, int pos); // add new function to the stack
    bool symbol_exists(const string&amp; symbol_name); // does a symbol exists?
    SymbolEntry* get_symbol(const string&amp; symbol_name);
    SymbolEntry* get_func_symbol(const string&amp; func_name, const string&amp; retype, vector&lt;string&gt;* params_types, int* count); // if retype == &quot;&quot; error if count &gt;= 2
   // bool function_exists(const string&amp; name, vector&lt;string&gt;* params_types = nullptr);
    bool check_program();
    ~SymbolStack();
};

...

#endif //HW3_SYMBOLS_H

Types is an enum defined in shared_def.h.
The methods in Symbols.cpp:

SymbolStack::SymbolStack(){
    pos_stack.push_back(0); // init to offset = zero
    this-&gt;push(false, T_VOID);
    vector&lt;string&gt; print_param = vector&lt;string&gt;();
    vector&lt;string&gt; printi_param = vector&lt;string&gt;();
    print_param.push_back(&quot;string&quot;);
    printi_param.push_back(&quot;int&quot;);
    this-&gt;symbol_document(&quot;print&quot;, &quot;void&quot;, FUNC, &amp;print_param);
    this-&gt;symbol_document(&quot;printi&quot;, &quot;void&quot;, FUNC, &amp;printi_param);
}
void SymbolStack::push(bool is_while, Types retType){
    retType = retType == T_NONE? scope_stack.back()-&gt;ret_type : retType;
    SymbolTable* first_scope = new SymbolTable(pos_stack.back(), is_while, retType);
    pos_stack.push_back(pos_stack.back());
    scope_stack.push_back(first_scope);
}

The constructor of SymbolStack is called in parser.ypp:

%{
    #include &lt;stdio.h&gt;
    #include &lt;iostream&gt;
    #include &quot;types.h&quot;
    #include &quot;hw3_output.hpp&quot;
    extern int yylineno;
    extern int yylex();
    int yyerror(const char* message);
    extern SymbolStack tables;
%}

definitions...
%%
Rule...
%%

tables = SymbolStack();
int main(){
        int res = yyparse();
        bool is_valid_program = tables.check_program();
        if(!is_valid_program){
            output::errorMainMissing();
            exit(0);
        }
        tables.pop();
        return res;
}
int yyerror(const char * message){
	output::errorSyn(yylineno);
	exit(0);
}

I didn't manage to debug it using gdb because it happens even before main is called. It also happens if I remove this line "this->push(false, T_VOID);" from the constructor.

Any help would be appreciated.

EDIT: There is nothing in any source file I use that is defined as static.

答案1

得分: 1

  1. GDB 能够完全调试在 main 函数调用之前发生的崩溃。
  2. 您展示了 GDB 输出(但您的代码没有使用 -g 构建以进行调试)。

您有一个全局变量 tables。很可能崩溃发生在该变量初始化期间。


没有 http://stackoverflow.com/help/mcve,我们无法提供太多帮助。

但是您应该能够通过使用 -g 重新构建并在 SymbolStack::SymbolStack 上设置断点来自行调试此问题。

我唯一能想到的是,如果 T_VOID == T_NONE,那么这行代码:

retType = retType == T_NONE? scope_stack.back()-&gt;ret_type : retType;

将使用不存在的 scope_stack.back()scope_stack 仍然为空),这应该会导致崩溃。

附言:要理解崩溃,通常有助于检查崩溃指令((gdb) x/i $pc)和寄存器值((gdb) info regs)。

英文:

> I didn't manage to debug it using gdb because it happens even before main is called.

  1. GDB is perfectly capable of debugging crashes which happen before main.
  2. You show GDB output (but your code is built without debugging (-g)).

> There is nothing in any source file I use that is defined as static.

You have a global tables variable. It is very likely that the crash is happening during that variable's initialization.


Without http://stackoverflow.com/help/mcve there isn't much help we can offer.

But you should be able to debug this yourself by re-building with -g and setting a breakpoint on SymbolStack::SymbolStack.

The only thing I can think of is that if T_VOID == T_NONE, then this line:

    retType = retType == T_NONE? scope_stack.back()-&gt;ret_type : retType;

will use non-existent scope_stack.back() (scope_stack is still empty), and that should crash.

P.S. To understand the crash, it is often helpful to examine the crashing instruction ((gdb) x/i $pc) and register values ((gdb) info regs).

huangapple
  • 本文由 发表于 2023年5月28日 21:50:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定