lua_pcall 缺少错误消息
lua_pcall error message is missing
我正在使用 lua_pcall
调用一些函数,我想捕获错误。
在某些情况下,错误似乎丢失了。怎么会这样?
这适用于我使用错误处理程序和不使用错误处理程序的情况。在这两种情况下,栈顶都不是字符串。
C代码:
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_replace(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, my_func_index);
// now push n_in number of values on the stack
luaT_stackdump(L);
int pcall_ret = lua_pcall(L, n_in, n_out, -n_in - 2);
// lua_pcall will consume n_in+1 values from the stack.
if(pcall_ret != 0) {
const char* errmsg = lua_tostring(L, -1);
if(!errmsg) {
errmsg = "(No Lua error message.)";
printf("Unexpected Lua stack:\n");
luaT_stackdump(L);
}
printf("Lua error code %i: %s\n", pcall_ret, errmsg);
lua_pop(L, 2); // remove error and debug.traceback from the stack
return ...;
}
// now we got n_out values on the stack
调用的 Lua 函数如下所示(用于测试):
function (x, W, b, index)
print "hi from Lua func"
A = torch.rand(15, 12)
B = torch.rand(12, 23)
C = torch.dot(A, B)
end
它在调用 torch.dot
时以某种方式出错。
但我不完全知道为什么。而且我没有收到任何有意义的错误。
这就是我的问题。
输出:
1. Lua object type: function
2. Lua object type: function
3. userdata 4165a368 [torch.FloatTensor]
4. userdata 4165a390 [torch.FloatTensor]
5. userdata 4165a230 [torch.FloatTensor]
6. userdata 4165a258 [torch.CharTensor]
---------------------------------------------
hi from Lua func
Unexpected Lua stack:
1. Lua object type: function
2. userdata 40ea1230 [torch.DoubleTensor]
---------------------------------------------
Lua error code 2: (No Lua error message.)
或者也许我的代码是正确的,它真的应该 return 这里的错误字符串?所以在调用 torch.dot
时可能存在一些内存损坏,即有些东西搞砸了?
看来我需要打电话给torch.updateerrorhandlers()
。然后我得到一些有意义的输出:
hi from Lua func
Lua error code 2: inconsistent tensor size at /tmp/luarocks_torch-scm-1-1092/torch7/lib/TH/generic/THTensorMath.c:384
stack traceback:
[C]: at 0x7f63cd831360
[C]: in function 'dot'
[string "return ..."]:9: in function <[string "return ..."]:2>
但前提是我在 Lua 函数中有 torch.updateerrorhandlers()
。
我尝试使用此 C 代码但它不起作用:
lua_getglobal(L, "torch");
lua_getfield(L, -1, "updateerrorhandlers");
lua_replace(L, -2);
assert(lua_pcall(L, 0, 0, 0) == 0);
我发现如果我在实际 my_func_index
lua_pcall
之前再调用一次 torch.updateerrorhandlers()
,它就会起作用。
这是出乎意料的,但这可能是因为这可能是另一个线程
(这是我没有预料到的)。
实际上,我在 Torch 代码中找到了函数 torch.updatethreadlocals()
正是为了这个目的,我现在调用这个函数,就在我的另一个 lua_pcall
:
之前
lua_getglobal(L, "torch");
lua_getfield(L, -1, "updatethreadlocals");
lua_replace(L, -2);
assert(lua_pcall(L, 0, 0, 0) == 0);
现在有效。
我正在使用 lua_pcall
调用一些函数,我想捕获错误。
在某些情况下,错误似乎丢失了。怎么会这样?
这适用于我使用错误处理程序和不使用错误处理程序的情况。在这两种情况下,栈顶都不是字符串。
C代码:
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_replace(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, my_func_index);
// now push n_in number of values on the stack
luaT_stackdump(L);
int pcall_ret = lua_pcall(L, n_in, n_out, -n_in - 2);
// lua_pcall will consume n_in+1 values from the stack.
if(pcall_ret != 0) {
const char* errmsg = lua_tostring(L, -1);
if(!errmsg) {
errmsg = "(No Lua error message.)";
printf("Unexpected Lua stack:\n");
luaT_stackdump(L);
}
printf("Lua error code %i: %s\n", pcall_ret, errmsg);
lua_pop(L, 2); // remove error and debug.traceback from the stack
return ...;
}
// now we got n_out values on the stack
调用的 Lua 函数如下所示(用于测试):
function (x, W, b, index)
print "hi from Lua func"
A = torch.rand(15, 12)
B = torch.rand(12, 23)
C = torch.dot(A, B)
end
它在调用 torch.dot
时以某种方式出错。
但我不完全知道为什么。而且我没有收到任何有意义的错误。
这就是我的问题。
输出:
1. Lua object type: function
2. Lua object type: function
3. userdata 4165a368 [torch.FloatTensor]
4. userdata 4165a390 [torch.FloatTensor]
5. userdata 4165a230 [torch.FloatTensor]
6. userdata 4165a258 [torch.CharTensor]
---------------------------------------------
hi from Lua func
Unexpected Lua stack:
1. Lua object type: function
2. userdata 40ea1230 [torch.DoubleTensor]
---------------------------------------------
Lua error code 2: (No Lua error message.)
或者也许我的代码是正确的,它真的应该 return 这里的错误字符串?所以在调用 torch.dot
时可能存在一些内存损坏,即有些东西搞砸了?
看来我需要打电话给torch.updateerrorhandlers()
。然后我得到一些有意义的输出:
hi from Lua func
Lua error code 2: inconsistent tensor size at /tmp/luarocks_torch-scm-1-1092/torch7/lib/TH/generic/THTensorMath.c:384
stack traceback:
[C]: at 0x7f63cd831360
[C]: in function 'dot'
[string "return ..."]:9: in function <[string "return ..."]:2>
但前提是我在 Lua 函数中有 torch.updateerrorhandlers()
。
我尝试使用此 C 代码但它不起作用:
lua_getglobal(L, "torch");
lua_getfield(L, -1, "updateerrorhandlers");
lua_replace(L, -2);
assert(lua_pcall(L, 0, 0, 0) == 0);
我发现如果我在实际 my_func_index
lua_pcall
之前再调用一次 torch.updateerrorhandlers()
,它就会起作用。
这是出乎意料的,但这可能是因为这可能是另一个线程
(这是我没有预料到的)。
实际上,我在 Torch 代码中找到了函数 torch.updatethreadlocals()
正是为了这个目的,我现在调用这个函数,就在我的另一个 lua_pcall
:
lua_getglobal(L, "torch");
lua_getfield(L, -1, "updatethreadlocals");
lua_replace(L, -2);
assert(lua_pcall(L, 0, 0, 0) == 0);
现在有效。