如何使用 gdb 从核心文件中获取 lua 堆栈跟踪

发布于 2024-12-21 17:30:14 字数 361 浏览 2 评论 0原文

我有一个 C++ 应用程序(适用于 OS X),它调用 lua 作为脚本语言。 我正在运行大量这些应用程序(100 个)并且它们可以运行 很长一段时间(几天或几周)。

有时会崩溃。当它崩溃时,它给我留下了一个可爱的核心文件。

我可以在 gdb 中打开这个核心文件并找到应用程序崩溃的位置。 我可以遍历调用堆栈并找到 lua_State 变量的实例。 我的问题是我想看看 lua 调用堆栈是什么样子的 这次...

请记住,由于这是一个核心,所以我无权调用 C 函数,这排除了调试 lua 脚本的几种常用方法。

我想避免通过调试挂钩添加手动跟踪,因为我担心额外的性能损失和增加的复杂性。

如何遍历lua内部结构来获取调用堆栈信息?

I have a C++ application (for OS X) that calls lua as a scripting language.
I'm running a large number of these applications (100s) and they can run
for a very long time (days or weeks).

Sometimes one crashes. And when it crashes it leaves me a lovely core file.

I can open this core file in gdb and find where the application crashes.
I can walk the call stack and find an instance of a lua_State variable.
My problem is that I'd like to see what the lua call stack looks like at
this time...

Keep in mind that since this is a core I don't have access to calling C functions, which rules out several of the usual ways of debugging lua scripts.

Id like to avoid adding manual traces through debug hooks as I'm worried about the additional performance penalties, and added complexity.

How can I traverse the lua internal structures to get at call stack information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若能看破又如何 2024-12-28 17:30:14

我创建了一个 GDB 脚本来执行 mac 链接到的网页中的操作。它并不漂亮,可能应该正确地包装到函数等中,但这里是为了好奇。

注意:网页上关于 lua 函数的文件名似乎是错误的。如果字符串来自 luaL_dofile(),则文件名以 @ 符号开头。如果它们是从 lua_dostring() 调用的。在这种情况下,$filename 变量被设置为传递给 lua_dostring() 的整个字符串 - 用户可能只对其中的一两行上下文感兴趣那个文件。我不知道如何解决这个问题。

set $p = L->base_ci
while ($p <= L->ci )
  if ( $p->func->value.gc->cl.c.isC == 1 )
    printf "0x%x   C FUNCTION", $p
    output $p->func->value.gc->cl.c.f
    printf "\n"
  else
    if ($p->func.tt==6)
      set $proto = $p->func->value.gc->cl.l.p
      set $filename = (char*)(&($proto->source->tsv) + 1)
      set $lineno = $proto->lineinfo[ $p->savedpc - $proto->code -1 ]
      printf "0x%x LUA FUNCTION : %d %s\n", $p, $lineno, $filename
    else
      printf "0x%x LUA BASE\n", $p
    end
  end
  set $p = $p+1
end

这输出类似:

0x1002b0 LUA BASE
0x1002c8 LUA FUNCTION : 4 @a.lua
0x1002e0 LUA FUNCTION : 3 @b.lua
0x100310   C FUNCTION(lua_CFunction) 0x1fda <crash_function(lua_State*)>

当我调试此代码的崩溃时:

// This is a file designed to crash horribly when run.
// It should generate a core, and it should crash inside some lua functions

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#include <iostream>
#include <signal.h>

int crash_function(lua_State * L)
{
  raise( SIGABRT ); //This should dump core!
  return 0;
}



int main()
{
  lua_State * L = luaL_newstate();
  lua_pushcfunction(L, crash_function);
  lua_setfield(L, LUA_GLOBALSINDEX, "C");

  luaopen_base(L);
  if( 1 == luaL_dofile(L, "a.lua" ))
  {
    std::cout<<"ERROR: "<<lua_tostring(L,-1)<<std::endl;
    return 1;
  }
  if( 1 == luaL_dofile(L, "b.lua" ))
  {
    std::cout<<"ERROR: "<<lua_tostring(L,-1)<<std::endl;
    return 1;
  }

  lua_getfield(L, LUA_GLOBALSINDEX, "A");
  lua_pcall(L, 0, 0, NULL);
}

使用 a.lua

-- a.lua
-- just calls B, which calls C which should crash
function A()
  B()
end

和 b.lua

-- b.lua
function B()
  C()
end

I've created a GDB script to do the stuff in the web page linked to by macs. Its not beautiful, and should probably be properly wrapped into a function etc, but here it is for the curious.

NOTE: It seems that the web page is wrong about the filename for lua functions. In the case where the string comes from luaL_dofile() the filename starts with a @ symbol. If they're called from lua_dostring(). In that case the $filename variable is set to the whole of the string passed to lua_dostring() - and the user is probably only interested in one or two lines of context from that file. I wasn't sure how to fix that up.

set $p = L->base_ci
while ($p <= L->ci )
  if ( $p->func->value.gc->cl.c.isC == 1 )
    printf "0x%x   C FUNCTION", $p
    output $p->func->value.gc->cl.c.f
    printf "\n"
  else
    if ($p->func.tt==6)
      set $proto = $p->func->value.gc->cl.l.p
      set $filename = (char*)(&($proto->source->tsv) + 1)
      set $lineno = $proto->lineinfo[ $p->savedpc - $proto->code -1 ]
      printf "0x%x LUA FUNCTION : %d %s\n", $p, $lineno, $filename
    else
      printf "0x%x LUA BASE\n", $p
    end
  end
  set $p = $p+1
end

This outputs something like:

0x1002b0 LUA BASE
0x1002c8 LUA FUNCTION : 4 @a.lua
0x1002e0 LUA FUNCTION : 3 @b.lua
0x100310   C FUNCTION(lua_CFunction) 0x1fda <crash_function(lua_State*)>

When I debug the crash from this code:

// This is a file designed to crash horribly when run.
// It should generate a core, and it should crash inside some lua functions

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#include <iostream>
#include <signal.h>

int crash_function(lua_State * L)
{
  raise( SIGABRT ); //This should dump core!
  return 0;
}



int main()
{
  lua_State * L = luaL_newstate();
  lua_pushcfunction(L, crash_function);
  lua_setfield(L, LUA_GLOBALSINDEX, "C");

  luaopen_base(L);
  if( 1 == luaL_dofile(L, "a.lua" ))
  {
    std::cout<<"ERROR: "<<lua_tostring(L,-1)<<std::endl;
    return 1;
  }
  if( 1 == luaL_dofile(L, "b.lua" ))
  {
    std::cout<<"ERROR: "<<lua_tostring(L,-1)<<std::endl;
    return 1;
  }

  lua_getfield(L, LUA_GLOBALSINDEX, "A");
  lua_pcall(L, 0, 0, NULL);
}

With a.lua

-- a.lua
-- just calls B, which calls C which should crash
function A()
  B()
end

and b.lua

-- b.lua
function B()
  C()
end
寻找一个思念的角度 2024-12-28 17:30:14

这是 Michael Anderson 的 GDB 脚本的一个小变体:我必须使用它,因为我在他的脚本中遇到了 Cannot access memory at address 0x656d 错误,原因是 L->base_ci 在我的核心转储中无效。它从顶部框架 (L->ci) 开始,并以相反的方向向下,避免无效的 L->base_ci 指针。

set $p = L->ci
while ($p > L->base_ci )
  if ( $p->func->value.gc->cl.c.isC == 1 )
    printf "0x%x   C FUNCTION ", $p
    output $p->func->value.gc->cl.c.f
    printf "\n"
  else
    if ($p->func.tt==6)
      set $proto = $p->func->value.gc->cl.l.p
      set $filename = (char*)(&($proto->source->tsv) + 1)
      set $lineno = $proto->lineinfo[ $p->savedpc - $proto->code -1 ]
      printf "0x%x LUA FUNCTION : %d %s\n", $p, $lineno, $filename
    else
      printf "0x%x LUA BASE\n", $p
    end
  end
  set $p = $p - 1
end

This is a small variation to Michael Anderson's GDB script: I had to use this because I was getting Cannot access memory at address 0x656d errors with his script, due to L->base_ci being invalid in my core dump. This starts from the top frame (L->ci) and goes down, in the opposite direction, avoiding the invalid L->base_ci pointer.

set $p = L->ci
while ($p > L->base_ci )
  if ( $p->func->value.gc->cl.c.isC == 1 )
    printf "0x%x   C FUNCTION ", $p
    output $p->func->value.gc->cl.c.f
    printf "\n"
  else
    if ($p->func.tt==6)
      set $proto = $p->func->value.gc->cl.l.p
      set $filename = (char*)(&($proto->source->tsv) + 1)
      set $lineno = $proto->lineinfo[ $p->savedpc - $proto->code -1 ]
      printf "0x%x LUA FUNCTION : %d %s\n", $p, $lineno, $filename
    else
      printf "0x%x LUA BASE\n", $p
    end
  end
  set $p = $p - 1
end
锦欢 2024-12-28 17:30:14

根据上面的评论,我推荐以下文章: 带有 C++ 调试器的 Lua 调用堆栈。它很好地概述了调试 Lua / C++ 组合,特别是“检查 Lua 数据结构”部分在调试核心转储时很有帮助。

Based on the comments above, I'd recommend the following article: Lua callstack with C++ debugger. It's giving a good overview about debugging the Lua / C++ combination, especially the section "Inspect Lua data structures" is helpful, when it comes to debugging of core dumps.

疾风者 2024-12-28 17:30:14

你可以查看我的 Lua GDB 助手。它是宏的集合,可让您检查堆栈和值,甚至打印回溯。本质上是 macs 引用的文章包含的内容,在一个简单易用的包中。

它提供了以下宏:

  • luastack [L] - 列出当前 Lua C 堆栈上的值。

  • <代码>luaprint <值> [verbose] - 漂亮地打印作为参数传递的 TValue。需要一个指向 TValue 的指针。当 verbose 为 1 时,扩展表、元表和用户数据环境。

  • <代码>luaprinttable < table > - 漂亮地打印 Lua 表。需要一个指向 Table 的指针。

  • <代码>luavalue <指数> [L] - 转储索引处的单个值。

  • luatraceback [L] - 调用debug.traceback()。不确定它是否适用于核心文件...

You could check out my Lua GDB helpers. It is a collection of macros that let you inspect the stack and values, and even print backtrace. Essentially what the article referenced by macs contains, in a simple-to-use package.

It provides these macros:

  • luastack [L] - lists the values on the current Lua C stack.

  • luaprint < value > [verbose] - Pretty-prints a TValue passed as argument. Expects a pointer to a TValue. When verbose is 1, expands tables, metatables and userdata environments.

  • luaprinttable < table > - Pretty-prints a Lua Table. Expects a pointer to Table.

  • luavalue < index > [L] - Dumps a single value at an index.

  • luatraceback [L] - Calls debug.traceback(). Not sure if it will work on core file though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文