Backtracing a thread in another process

Use the following sample code segment to repeatedly backtrace a thread in another process by using the remote process's pid and thread tid:

char out[1024];
bt_addr_t pc[16];
bt_accessor_t acc;

if (bt_init_accessor( &acc, BT_THREAD, tid.rawId()) == -1)
{
  fprintf( stderr, "%s:%i %s (%i)%s\n", __FUNCTION__, __LINE__,
           “bt_init_accessor”, errno, strerror(errno));
  return -1;
}

if (bt_load_memmap( &acc, &memmap ) ==-1)
{
   fprintf( stderr, "%s:%i %s (%i)%s\n", __FUNCTION__, __LINE__,
            “bt_load_memmap”, errno, strerror(errno));
   return -1;
}

if (bt_sprn_memmap(&memmap, out, sizeof(out)) == -1)
{
   fprintf( stderr, "%s:%i %s (%i)%s\n", __FUNCTION__, __LINE__,
            “bt_sprn_memmap”, errno, strerror(errno));
   return -1;
}

/* Make sure that the string is null-terminated. */
out[sizeof(out) - 1] = ’\0’;
puts (out);

for (int i=0; i < 10; i++)
{
   int cnt = bt_get_backtrace( &acc, pc, sizeof(pc) / sizeof(bt_addr_t));
   int w = 0;
   while (cnt > 0)
   {
       w = bt_sprnf_addrs(&memmap, pc + w, cnt, "%a\n", out, sizeof(out), 0);
       if (w == -1)
       {
          fprintf( stderr, "%s:%i %s (%i)%s\n", __FUNCTION__, __LINE__,
                   “bt_sprnf_addrs”, errno, strerror(errno));
          return -1;
       }
       cnt -= w;
       puts (out);
   }
}
      
bt_unload_memmap( &memmap );

if (bt_release_accessor( &acc ) == -1)
{
   fprintf( stderr, "%s:%i %s (%i)%s\n", __FUNCTION__, __LINE__,
            “bt_release_accessor”, errno, strerror(errno));
   return -1;
}