C++ this=0x0 在各种帧中进行回溯

C++ backtrace with this=0x0 in various frames

本文关键字:回溯 this 0x0 C++      更新时间:2023-10-16

我在 mips 多核系统中有一个程序,我从核心得到一个回溯,真的很难弄清楚(至少对我来说),我想也许其他内核之一写入 mem,但并非所有堆栈都已损坏,这让我更加困惑。

在帧 #2 中,这是 NULL,在帧 #0 中,这也是 NULL(核心转储的原因)。

这是(部分)回溯:

#0 E::m (this=0x0, string=0x562f148 ", size=202) at E.cc:315#1 在 P::e 中0x00000000105c773c (this=0x361ecd00, string=0x562f148 ", size=202, offset=28) 在 P.cc:137#2 0x00000000105c8c5c in M::e (this=0x0, id=7 '\a', r=2, string=0x562f148 ", size=202, oneClass=0x562f148 ", secondClass=0x14eff439 ",偏移量=28)在 M.cc:75                                        #3 0x0000000010596354 in m::find (this=0x4431fd70, string=0x562f148 ", size=202, oneClass=0x14eff438 ", secondClass=0x14eff439 ",                   up=false) at A.cc:458                   #4 在 A::trigger (this=0x4431fd70, triggerType=ONE, string=0x562f148 ", size=0, up=true) 中的 0x0000000010597364 at A.cc:2084#5 0x000000001059bcf0在 A::findOne (this=0x4431fd70, index=2, budget=0x562f148 ", size=202, up=true) at A.cc:1155#6 0x000000001059c934 in A::shouldpathNow (this=0x4431fd70, index=2, budget=0x562f148 ", size=202, up=false, startAt=0x0, short=)   在 A.cc:783   #7 0x00000000105a385c in A::shouldpath (this=0x4431fd70, index=2, rbudget=, rsize=, up=false,                   direct=) at A.cc:1104

关于 m::find 函数

   442 m_t m::find(无符号字符常量*字符串,无符号整数大小,    443 hClass_t *hClass, h_t *fHClass,    444 bool isUp) {     445      446                                                                               447 const Iterator &it=arr_[getIndex()]->getSearchIterator((char const*)value, len);    448                                                               449 无符号 int const 偏移量 = 值 - engine_->getData();     450 451 int ret=未知;               452 米 *人;                       453 for(const void* match=it.next();    454 ret == 未知 && 匹配 != 空;                                                    455 match = it.next()){    456 p = (M*)匹配;      457 if(p->needMore()){               458 ret = p->e(id_, getIndex(), value, len, hClass, fHClass, offset);

this=0x0实际上很容易发生。 例如:

E *instance = NULL;
instance->method();

this将在 method 内为 NULL。

无需假设内存已损坏或堆栈已被覆盖。 事实上,如果堆栈的其余内容似乎有意义(并且您似乎认为它们有意义),那么堆栈可能没问题。

不必查找内存损坏,而是检查逻辑以查看是否有未初始化的 (NULL) 指针或引用。

无法看到所有代码,很难想象发生了什么。您能否也添加 M::e() 和 P::e() 的代码,或者至少添加重要部分的代码。

可能解决所有问题的事情是添加一个 NULL 检查,如下 m::find():

456     p = (M*)match;   
        if(!p) { return; /* or do whatever */ }
457     if(p->needMore()){            
458       ret = p->e(id_, getIndex(), value, len, hClass, fHClass, offset);

如果p是 NULL,我本来以为它会在调用 p->needMore() 时崩溃,但根据该方法的作用,它可能不会崩溃。