I don't need no steeeenking debugger

I was still having troubles with lldb accepting breakpoints [1]; 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 [2] …

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() [3] for events, while for whatever reason I don't remember, the Mac OS-X version used select() [4] [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() [5] 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).

[1] /boston/2018/08/21.1

[2] https://www.youtube.com/watch?v=gCxrkl2igGY&t=32s

[3] http://man7.org/linux/man-pages/man7/epoll.7.html

[4] http://man7.org/linux/man-pages/man2/select.2.html

[5] http://man7.org/linux/man-pages/man2/poll.2.html

Gemini Mention this post

Contact the author