Wednesday, August 22, 2018
I don't need no steeeenking debugger
I was still having troubles with lldb
accepting breakpoints;
the breakpoints were just not being hit.
Thinking about the issue,
it seemed to me that the code might be requesting a too large amount of memory and an easy eay to do that is to track all allocations made by Lua.
It was easy enough to test:
static void *l_alloc(void *ud,void *ptr,size_t osize,size_t nsize) { (void)ud; fprintf(stderr,"osize=%zu nsize=%zu\n",osize,nsize); if (nsize == 0) { free(ptr); return NULL; } else return realloc(ptr,nsize); } int main(int argc,char *argv[]) { /* ... */ L = lua_newstate(l_alloc,NULL); /* ... */ return 0; }
Then it was a simple matter of running the program and check each allocation:
... osize=0 nsize=64 osize=0 nsize=40 osize=0 nsize=64 osize=0 nsize=0 osize=0 nsize=64 osize=0 nsize=16 osize=0 nsize=64 osize=0 nsize=0 osize=0 nsize=160 osize=0 nsize=148 osize=0 nsize=64 osize=0 nsize=16 osize=16 nsize=32 osize=0 nsize=16 osize=0 nsize=64 osize=0 nsize=18446744073709551600 ...
Hmmmmm …
Okay, force a core dump we can examine:
static void *l_alloc(void *ud,void *ptr,size_t osize,size_t nsize) { (void)ud; (void)osize; if (nsize > 10uL * 1024uL * 1024uL) abort(); /* dump core! */ if (nsize == 0) { free(ptr); return NULL; } else return realloc(ptr,nsize); }
And the problem is immedately resolved.
The Linux version used epoll()
for events,
while for whatever reason I don't remember,
the Mac OS-X version used select()
[Yes, I know that's a Linux reference and not a Mac OS-X reference,
but the call is the same between the two systems,
and I couldn't find a version of the Mac OS-X page online. —Sean],
and that code path was … a bit problematic.
The fix was easy—use poll()
for events.
With that change,
the code worked fine on Mac OS-X
(not that we use Mac OS-X in production,
but it makes it easy to test in development if it works on the Mac).