Beispiel #1
0
/*
 * populate_g_idnew - the master kstat function.
 *
 * This fetches all the network data from kstat and populates the
 * global variables g_idnew and g_interfacemax. It uses a kstat control
 * pointer as an argument, and the global array g_network.
 *
 * This function works by climbing down the kstat chains looking
 * for modules that look like network interfaces. The first step is
 * to check the module name against the global array g_network (the code
 * for this will need maintenance as new network cards are developed);
 * then a kstat variable is checked "obytes" or "obytes64" to ensure
 * that this really is a network module. This approach is not ideal,
 * I'd rather base the test on the kstat class == "net", however this
 * data does not yet appear reliable across all interfaces.
 */
static void
populate_g_idnew(kstat_ctl_t *kc)
{
	kstat_t *ksp;		/* Kstat struct pointer */
	int ok, i;
	int num = 0;

	for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {

		/* Search all modules */
		for (ok = 0, i = 0; g_network[i] != NULL; i++) {
			if (strcmp(ksp->ks_module, g_network[i]) == 0)
				ok = 1;
		}

		/* Skip if this isn't a network module */
		if (ok == 0) continue;
		if (kstat_read(kc, ksp, NULL) == -1) continue;
		if ((kstat_data_lookup(ksp, "obytes") == NULL) &&
		    (kstat_data_lookup(ksp, "obytes64") == NULL)) continue;

		/* Check for tracked interfaces */
		if (g_someints) {
			for (ok = 0, i = 0; *g_tracked[i] != NULL; i++) {
				if (strcmp(ksp->ks_name, g_tracked[i]) == 0)
					ok = 1;
			}
			if (ok == 0) continue;
		}

		/* Save network values */
		g_idnew[num].rbytes = fetch6432(ksp, "rbytes64", "rbytes", 0);
		g_idnew[num].wbytes = fetch6432(ksp, "obytes64", "obytes", 0);
		g_idnew[num].rpackets =
		    fetch6432(ksp, "ipackets64", "ipackets", 0);
		g_idnew[num].wpackets =
		    fetch6432(ksp, "opackets64", "opackets", 0);
		g_idnew[num].sat = fetch32(ksp, "defer", 0);
		g_idnew[num].sat += fetch_nocanput(ksp, 0);
		g_idnew[num].sat += fetch32(ksp, "norcvbuf", 0);
		g_idnew[num].sat += fetch32(ksp, "noxmtbuf", 0);
		g_idnew[num].time = time(0);
    fetchstr( ksp, "zonename", g_idnew[num].zone );		
/* if the speed can't be fetched, this makes %util 0.0 */
		g_idnew[num].speed = fetch64(ksp, "ifspeed", 1LL << 48);
		(void) strcpy(g_idnew[num].name, ksp->ks_name);

		num++;
	}
	g_interfacemax = num - 1;
}
Beispiel #2
0
static int
intern_regions (unw_addr_space_t as, unw_accessors_t *a,
                unw_word_t *addr, unw_dyn_region_info_t **regionp, void *arg)
{
  uint32_t insn_count, op_count, i;
  unw_dyn_region_info_t *region;
  unw_word_t next_addr;
  int ret;

  *regionp = NULL;

  if (!*addr)
    return 0;   /* NULL region-list */

  if ((ret = fetchw (as, a, addr, &next_addr, arg)) < 0
      || (ret = fetch32 (as, a, addr, (int32_t *) &insn_count, arg)) < 0
      || (ret = fetch32 (as, a, addr, (int32_t *) &op_count, arg)) < 0)
    return ret;

  region = calloc (1, _U_dyn_region_info_size (op_count));
  if (!region)
    {
      ret = -UNW_ENOMEM;
      goto out;
    }

  region->insn_count = insn_count;
  region->op_count = op_count;
  for (i = 0; i < op_count; ++i)
    if ((ret = intern_op (as, a, addr, region->op + i, arg)) < 0)
      goto out;

  if (next_addr)
    if ((ret = intern_regions (as, a, &next_addr, &region->next, arg)) < 0)
      goto out;

  *regionp = region;
  return 0;

 out:
  if (region)
    free_regions (region);
  return ret;
}
Beispiel #3
0
static int
intern_op (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
           unw_dyn_op_t *op, void *arg)
{
  int ret;

  if ((ret = fetch8 (as, a, addr, &op->tag, arg)) < 0
      || (ret = fetch8 (as, a, addr, &op->qp, arg)) < 0
      || (ret = fetch16 (as, a, addr, &op->reg, arg)) < 0
      || (ret = fetch32 (as, a, addr, &op->when, arg)) < 0
      || (ret = fetchw  (as, a, addr, &op->val, arg)) < 0)
    return ret;
  return 0;
}
Beispiel #4
0
static int
intern_dyn_info (unw_addr_space_t as, unw_accessors_t *a,
                 unw_word_t *addr, unw_dyn_info_t *di, void *arg)
{
  unw_word_t first_region;
  int ret;

  switch (di->format)
    {
    case UNW_INFO_FORMAT_DYNAMIC:
      if ((ret = fetchw (as, a, addr, &di->u.pi.name_ptr, arg)) < 0
          || (ret = fetchw (as, a, addr, &di->u.pi.handler, arg)) < 0
          || (ret = fetch32 (as, a, addr,
                             (int32_t *) &di->u.pi.flags, arg)) < 0)
        goto out;
      *addr += 4;       /* skip over pad0 */
      if ((ret = fetchw (as, a, addr, &first_region, arg)) < 0
          || (ret = intern_regions (as, a, &first_region, &di->u.pi.regions,
                                    arg)) < 0)
        goto out;
      break;

    case UNW_INFO_FORMAT_TABLE:
      if ((ret = fetchw (as, a, addr, &di->u.ti.name_ptr, arg)) < 0
          || (ret = fetchw (as, a, addr, &di->u.ti.segbase, arg)) < 0
          || (ret = fetchw (as, a, addr, &di->u.ti.table_len, arg)) < 0
          || (ret = intern_array (as, a, addr, di->u.ti.table_len,
                                  &di->u.ti.table_data, arg)) < 0)
        goto out;
      break;

    case UNW_INFO_FORMAT_REMOTE_TABLE:
      if ((ret = fetchw (as, a, addr, &di->u.rti.name_ptr, arg)) < 0
          || (ret = fetchw (as, a, addr, &di->u.rti.segbase, arg)) < 0
          || (ret = fetchw (as, a, addr, &di->u.rti.table_len, arg)) < 0
          || (ret = fetchw (as, a, addr, &di->u.rti.table_data, arg)) < 0)
        goto out;
      break;

    default:
      ret = -UNW_ENOINFO;
      goto out;
    }
  return 0;

 out:
  free_dyn_info (di);
  return ret;
}
Beispiel #5
0
HIDDEN int
unwi_dyn_remote_find_proc_info (unw_addr_space_t as, unw_word_t ip,
                                unw_proc_info_t *pi,
                                int need_unwind_info, void *arg)
{
  unw_accessors_t *a = unw_get_accessors (as);
  unw_word_t dyn_list_addr, addr, next_addr, gen1, gen2, start_ip, end_ip;
  unw_dyn_info_t *di = NULL;
  int ret;

  if (as->dyn_info_list_addr)
    dyn_list_addr = as->dyn_info_list_addr;
  else
    {
      if ((*a->get_dyn_info_list_addr) (as, &dyn_list_addr, arg) < 0)
        return -UNW_ENOINFO;
      if (as->caching_policy != UNW_CACHE_NONE)
        as->dyn_info_list_addr = dyn_list_addr;
    }

  do
    {
      addr = dyn_list_addr;

      ret = -UNW_ENOINFO;

      if (fetchw (as, a, &addr, &gen1, arg) < 0
          || fetchw (as, a, &addr, &next_addr, arg) < 0)
        return ret;

      for (addr = next_addr; addr != 0; addr = next_addr)
        {
          if (fetchw (as, a, &addr, &next_addr, arg) < 0)
            goto recheck;       /* only fail if generation # didn't change */

          addr += WSIZE;        /* skip over prev_addr */

          if (fetchw (as, a, &addr, &start_ip, arg) < 0
              || fetchw (as, a, &addr, &end_ip, arg) < 0)
            goto recheck;       /* only fail if generation # didn't change */

          if (ip >= start_ip && ip < end_ip)
            {
              if (!di)
                di = calloc (1, sizeof (*di));

              di->start_ip = start_ip;
              di->end_ip = end_ip;

              if (fetchw (as, a, &addr, &di->gp, arg) < 0
                  || fetch32 (as, a, &addr, &di->format, arg) < 0)
                goto recheck;   /* only fail if generation # didn't change */

              addr += 4;        /* skip over padding */

              if (need_unwind_info
                  && intern_dyn_info (as, a, &addr, di, arg) < 0)
                goto recheck;   /* only fail if generation # didn't change */

              if (unwi_extract_dynamic_proc_info (as, ip, pi, di,
                                                  need_unwind_info, arg) < 0)
                {
                  free_dyn_info (di);
                  goto recheck; /* only fail if generation # didn't change */
                }
              ret = 0;  /* OK, found it */
              break;
            }
        }

      /* Re-check generation number to ensure the data we have is
         consistent.  */
    recheck:
      addr = dyn_list_addr;
      if (fetchw (as, a, &addr, &gen2, arg) < 0)
        return ret;
    }
  while (gen1 != gen2);

  if (ret < 0 && di)
    free (di);

  return ret;
}
Beispiel #6
0
int main(int argc, char **argv)
{
    int ch, fd, do_reloc = 0;
    uint32_t hunk_id, insz, outsz, i, x, first, last;
    uint32_t *hunk_offs, cur = 0;
    char *in, *out, *p, *buf, *outbuf;
    uint32_t base = 0;
    const static char *typename[] = { "Any", "Chip", "Fast", "Reserved" };
    const static char *hunkname[] = { "HUNK_CODE", "HUNK_DATA", "HUNK_BSS" };
    int seen_dat;

    const static char sopts[] = "hb:r";
    const static struct option lopts[] = {
        { "help", 0, NULL, 'h' },
        { "base", 1, NULL, 'b' },
        { "reloc", 0, NULL, 'r' },
        { 0, 0, 0, 0 }
    };

    while ((ch = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
        switch (ch) {
        case 'h':
            usage(0);
            break;
        case 'b':
            base = strtol(optarg, NULL, 16);
            break;
        case 'r':
            do_reloc = 1;
            break;
        default:
            usage(1);
            break;
        }
    }

    if (argc != (optind + 2))
        usage(1);
    in = argv[optind];
    out = argv[optind+1];

    fd = open(in, O_RDONLY);
    if (fd == -1)
        err(1, "%s", in);
    if ((insz = lseek(fd, 0, SEEK_END)) < 0)
        err(1, NULL);
    lseek(fd, 0, SEEK_SET);
    if ((buf = malloc(insz)) == NULL)
        err(1, NULL);
    if (read(fd, buf, insz) != insz)
        err(1, NULL);
    close(fd);
    p = buf;

    hunk_id = fetch32(&p);
    if (hunk_id != HUNK_HEADER)
        goto bad_hunk;
    printf("HUNK_HEADER\n");
    x = fetch32(&p);
    if (x)
        goto bad_hunk;
    x = fetch32(&p);
    first = fetch32(&p);
    last = fetch32(&p);
    if (first || (x != (last+1-first))) {
        /* For sanity's sake we expect only loadable hunks, numbered 0..N-1. */
        printf(" Table size: %u, First: %u, Last: %u\n", x, first, last);
        goto bad_hunk;
    }
    hunk_offs = malloc(x * sizeof(*hunk_offs));
    memset(hunk_offs, 0, x * sizeof(*hunk_offs));
    hunk_offs[0] = base;
    for (i = first; i <= last; i++) {
        uint8_t type;
        x = fetch32(&p);
        type = x >> 30;
        x &= (1u<<30)-1;
        if (type >= 3) {
            /* We don't support extended AllocMem flags (type=3) */
            printf("Bad hunk AllocFlag %u\n", type);
            goto bad_hunk;
        }
        printf("  Hunk %u: %u longwords (%s)\n", i, x, typename[type]);
        /* Real Amiga loader would AllocMem() here. */
        hunk_offs[i+1] = hunk_offs[i] + 4*x;
    }
    outsz = hunk_offs[i];
    outbuf = malloc(outsz);
    
    cur = seen_dat = 0;
    while ((p - buf) < insz) {
        hunk_id = fetch32(&p) & ((1u<<30)-1);
        switch (hunk_id) {
        case HUNK_CODE:
        case HUNK_DATA:
        case HUNK_BSS:
            if (seen_dat)
                cur++;
            seen_dat = 1;
            printf("\n%s [Hunk %u]\n", hunkname[hunk_id-HUNK_CODE], cur);
            x = fetch32(&p);
            printf("  %u longwords\n", x);
            if (hunk_id == HUNK_BSS) {
                memset(outbuf + hunk_offs[cur] - base, 0, 4*x);
            } else {
                memcpy(outbuf + hunk_offs[cur] - base, p, 4*x);
                p += 4*x;
            }
            break;
        case HUNK_RELOC32: {
            uint32_t nr, id;
            printf("HUNK_RELOC32\n");
            while ((nr = fetch32(&p)) != 0) {
                id = fetch32(&p);
                printf("  Hunk %u: %u offsets\n", id, nr);
                for (i = 0; i < nr; i++) {
                    x = fetch32(&p);
                    if (do_reloc) {
                        uint32_t *pp = (uint32_t *)(outbuf + hunk_offs[cur]
                                                    + x - base);
                        *pp = htobe32(be32toh(*pp) + hunk_offs[id]);
                    }
                }
            }
            break;
        }
        case HUNK_END:
            printf("HUNK_END\n");
            if (!seen_dat) {
                printf("Premature HUNK_END\n");
                goto bad_hunk;
            }
            cur++;
            seen_dat = 0;
            break;
        default:
            printf("%08x - UNKNOWN\n", hunk_id);
            goto bad_hunk;
        }
    }

    if (cur != (last+1))
        goto bad_hunk;

    fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
    if (fd == -1)
        err(1, "%s", out);
    if (write(fd, outbuf, outsz) != outsz)
        err(1, NULL);
    close(fd);

    return 0;

bad_hunk:
    printf("ERROR_BAD_HUNK\n");
    return 1;
}
Beispiel #7
0
int
unw_step (unw_cursor_t * cursor)
{
  struct cursor *c = (struct cursor *) cursor;
  stack_frame_t dummy;
  unw_word_t back_chain_offset, lr_save_offset, v_regs_ptr;
  struct dwarf_loc back_chain_loc, lr_save_loc, sp_loc, ip_loc, v_regs_loc;
  int ret, i;

  Debug (1, "(cursor=%p, ip=0x%016lx)\n", c, (unsigned long) c->dwarf.ip);

  /* Try DWARF-based unwinding... */

  ret = dwarf_step (&c->dwarf);

  if (ret < 0 && ret != -UNW_ENOINFO)
    {
      Debug (2, "returning %d\n", ret);
      return ret;
    }

  if (unlikely (ret < 0))
    {
      if (likely (unw_is_signal_frame (cursor) <= 0))
        {
          /* DWARF unwinding failed.  As of 09/26/2006, gcc in 64-bit mode
             produces the mandatory level of traceback record in the code, but
             I get the impression that this is transitory, that eventually gcc
             will not produce any traceback records at all.  So, for now, we
             won't bother to try to find and use these records.

             We can, however, attempt to unwind the frame by using the callback
             chain.  This is very crude, however, and won't be able to unwind
             any registers besides the IP, SP, and LR . */

          back_chain_offset = ((void *) &dummy.back_chain - (void *) &dummy);
          lr_save_offset = ((void *) &dummy.lr_save - (void *) &dummy);

          back_chain_loc = DWARF_LOC (c->dwarf.cfa + back_chain_offset, 0);

          if ((ret =
               dwarf_get (&c->dwarf, back_chain_loc, &c->dwarf.cfa)) < 0)
            {
              Debug (2,
                 "Unable to retrieve CFA from back chain in stack frame - %d\n",
                 ret);
              return ret;
            }
          if (c->dwarf.cfa == 0)
            /* Unless the cursor or stack is corrupt or uninitialized we've most
               likely hit the top of the stack */
            return 0;

          lr_save_loc = DWARF_LOC (c->dwarf.cfa + lr_save_offset, 0);

          if ((ret = dwarf_get (&c->dwarf, lr_save_loc, &c->dwarf.ip)) < 0)
            {
              Debug (2,
                 "Unable to retrieve IP from lr save in stack frame - %d\n",
                 ret);
              return ret;
            }

          /* Mark all registers unsaved */
          for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
            c->dwarf.loc[i] = DWARF_NULL_LOC;

          ret = 1;
        }
      else
        {
          /* Find the sigcontext record by taking the CFA and adjusting by
             the dummy signal frame size.

             Note that there isn't any way to determined if SA_SIGINFO was
             set in the sa_flags parameter to sigaction when the signal
             handler was established.  If it was not set, the ucontext
             record is not required to be on the stack, in which case the
             following code will likely cause a seg fault or other crash
             condition.  */

          unw_word_t ucontext = c->dwarf.cfa + __SIGNAL_FRAMESIZE;

          Debug (1, "signal frame, skip over trampoline\n");

          c->sigcontext_format = PPC_SCF_LINUX_RT_SIGFRAME;
          c->sigcontext_addr = ucontext;

          sp_loc = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R1, 0);
          ip_loc = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_NIP, 0);

          ret = dwarf_get (&c->dwarf, sp_loc, &c->dwarf.cfa);
          if (ret < 0)
            {
              Debug (2, "returning %d\n", ret);
              return ret;
            }
          ret = dwarf_get (&c->dwarf, ip_loc, &c->dwarf.ip);
          if (ret < 0)
            {
              Debug (2, "returning %d\n", ret);
              return ret;
            }

          /* Instead of just restoring the non-volatile registers, do all
             of the registers for now.  This will incur a performance hit,
             but it's rare enough not to cause too much of a problem, and
             might be useful in some cases.  */
          c->dwarf.loc[UNW_PPC64_R0] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R0, 0);
          c->dwarf.loc[UNW_PPC64_R1] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R1, 0);
          c->dwarf.loc[UNW_PPC64_R2] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R2, 0);
          c->dwarf.loc[UNW_PPC64_R3] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R3, 0);
          c->dwarf.loc[UNW_PPC64_R4] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R4, 0);
          c->dwarf.loc[UNW_PPC64_R5] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R5, 0);
          c->dwarf.loc[UNW_PPC64_R6] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R6, 0);
          c->dwarf.loc[UNW_PPC64_R7] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R7, 0);
          c->dwarf.loc[UNW_PPC64_R8] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R8, 0);
          c->dwarf.loc[UNW_PPC64_R9] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R9, 0);
          c->dwarf.loc[UNW_PPC64_R10] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R10, 0);
          c->dwarf.loc[UNW_PPC64_R11] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R11, 0);
          c->dwarf.loc[UNW_PPC64_R12] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R12, 0);
          c->dwarf.loc[UNW_PPC64_R13] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R13, 0);
          c->dwarf.loc[UNW_PPC64_R14] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R14, 0);
          c->dwarf.loc[UNW_PPC64_R15] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R15, 0);
          c->dwarf.loc[UNW_PPC64_R16] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R16, 0);
          c->dwarf.loc[UNW_PPC64_R17] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R17, 0);
          c->dwarf.loc[UNW_PPC64_R18] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R18, 0);
          c->dwarf.loc[UNW_PPC64_R19] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R19, 0);
          c->dwarf.loc[UNW_PPC64_R20] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R20, 0);
          c->dwarf.loc[UNW_PPC64_R21] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R21, 0);
          c->dwarf.loc[UNW_PPC64_R22] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R22, 0);
          c->dwarf.loc[UNW_PPC64_R23] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R23, 0);
          c->dwarf.loc[UNW_PPC64_R24] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R24, 0);
          c->dwarf.loc[UNW_PPC64_R25] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R25, 0);
          c->dwarf.loc[UNW_PPC64_R26] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R26, 0);
          c->dwarf.loc[UNW_PPC64_R27] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R27, 0);
          c->dwarf.loc[UNW_PPC64_R28] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R28, 0);
          c->dwarf.loc[UNW_PPC64_R29] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R29, 0);
          c->dwarf.loc[UNW_PPC64_R30] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R30, 0);
          c->dwarf.loc[UNW_PPC64_R31] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R31, 0);

          c->dwarf.loc[UNW_PPC64_LR] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_LINK, 0);
          c->dwarf.loc[UNW_PPC64_CTR] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_CTR, 0);
          /* This CR0 assignment is probably wrong.  There are 8 dwarf columns
             assigned to the CR registers, but only one CR register in the
             mcontext structure */
          c->dwarf.loc[UNW_PPC64_CR0] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_CCR, 0);
          c->dwarf.loc[UNW_PPC64_XER] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_XER, 0);
          c->dwarf.loc[UNW_PPC64_NIP] =
            DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_NIP, 0);

          /* TODO: Is there a way of obtaining the value of the
             pseudo frame pointer (which is sp + some fixed offset, I
             assume), based on the contents of the ucontext record
             structure?  For now, set this loc to null. */
          c->dwarf.loc[UNW_PPC64_FRAME_POINTER] = DWARF_NULL_LOC;

          c->dwarf.loc[UNW_PPC64_F0] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R0, 0);
          c->dwarf.loc[UNW_PPC64_F1] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R1, 0);
          c->dwarf.loc[UNW_PPC64_F2] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R2, 0);
          c->dwarf.loc[UNW_PPC64_F3] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R3, 0);
          c->dwarf.loc[UNW_PPC64_F4] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R4, 0);
          c->dwarf.loc[UNW_PPC64_F5] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R5, 0);
          c->dwarf.loc[UNW_PPC64_F6] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R6, 0);
          c->dwarf.loc[UNW_PPC64_F7] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R7, 0);
          c->dwarf.loc[UNW_PPC64_F8] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R8, 0);
          c->dwarf.loc[UNW_PPC64_F9] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R9, 0);
          c->dwarf.loc[UNW_PPC64_F10] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R10, 0);
          c->dwarf.loc[UNW_PPC64_F11] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R11, 0);
          c->dwarf.loc[UNW_PPC64_F12] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R12, 0);
          c->dwarf.loc[UNW_PPC64_F13] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R13, 0);
          c->dwarf.loc[UNW_PPC64_F14] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R14, 0);
          c->dwarf.loc[UNW_PPC64_F15] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R15, 0);
          c->dwarf.loc[UNW_PPC64_F16] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R16, 0);
          c->dwarf.loc[UNW_PPC64_F17] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R17, 0);
          c->dwarf.loc[UNW_PPC64_F18] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R18, 0);
          c->dwarf.loc[UNW_PPC64_F19] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R19, 0);
          c->dwarf.loc[UNW_PPC64_F20] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R20, 0);
          c->dwarf.loc[UNW_PPC64_F21] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R21, 0);
          c->dwarf.loc[UNW_PPC64_F22] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R22, 0);
          c->dwarf.loc[UNW_PPC64_F23] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R23, 0);
          c->dwarf.loc[UNW_PPC64_F24] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R24, 0);
          c->dwarf.loc[UNW_PPC64_F25] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R25, 0);
          c->dwarf.loc[UNW_PPC64_F26] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R26, 0);
          c->dwarf.loc[UNW_PPC64_F27] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R27, 0);
          c->dwarf.loc[UNW_PPC64_F28] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R28, 0);
          c->dwarf.loc[UNW_PPC64_F29] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R29, 0);
          c->dwarf.loc[UNW_PPC64_F30] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R30, 0);
          c->dwarf.loc[UNW_PPC64_F31] =
            DWARF_LOC (ucontext + UC_MCONTEXT_FREGS_R31, 0);
          /* Note that there is no .eh_section register column for the
             FPSCR register.  I don't know why this is.  */

          v_regs_loc = DWARF_LOC (ucontext + UC_MCONTEXT_V_REGS, 0);
          ret = dwarf_get (&c->dwarf, v_regs_loc, &v_regs_ptr);
          if (ret < 0)
            {
              Debug (2, "returning %d\n", ret);
              return ret;
            }
          if (v_regs_ptr != 0)
            {
              /* The v_regs_ptr is not null.  Set all of the AltiVec locs */

              c->dwarf.loc[UNW_PPC64_V0] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R0, 0);
              c->dwarf.loc[UNW_PPC64_V1] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R1, 0);
              c->dwarf.loc[UNW_PPC64_V2] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R2, 0);
              c->dwarf.loc[UNW_PPC64_V3] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R3, 0);
              c->dwarf.loc[UNW_PPC64_V4] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R4, 0);
              c->dwarf.loc[UNW_PPC64_V5] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R5, 0);
              c->dwarf.loc[UNW_PPC64_V6] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R6, 0);
              c->dwarf.loc[UNW_PPC64_V7] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R7, 0);
              c->dwarf.loc[UNW_PPC64_V8] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R8, 0);
              c->dwarf.loc[UNW_PPC64_V9] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R9, 0);
              c->dwarf.loc[UNW_PPC64_V10] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R10, 0);
              c->dwarf.loc[UNW_PPC64_V11] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R11, 0);
              c->dwarf.loc[UNW_PPC64_V12] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R12, 0);
              c->dwarf.loc[UNW_PPC64_V13] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R13, 0);
              c->dwarf.loc[UNW_PPC64_V14] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R14, 0);
              c->dwarf.loc[UNW_PPC64_V15] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R15, 0);
              c->dwarf.loc[UNW_PPC64_V16] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R16, 0);
              c->dwarf.loc[UNW_PPC64_V17] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R17, 0);
              c->dwarf.loc[UNW_PPC64_V18] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R18, 0);
              c->dwarf.loc[UNW_PPC64_V19] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R19, 0);
              c->dwarf.loc[UNW_PPC64_V20] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R20, 0);
              c->dwarf.loc[UNW_PPC64_V21] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R21, 0);
              c->dwarf.loc[UNW_PPC64_V22] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R22, 0);
              c->dwarf.loc[UNW_PPC64_V23] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R23, 0);
              c->dwarf.loc[UNW_PPC64_V24] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R24, 0);
              c->dwarf.loc[UNW_PPC64_V25] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R25, 0);
              c->dwarf.loc[UNW_PPC64_V26] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R26, 0);
              c->dwarf.loc[UNW_PPC64_V27] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R27, 0);
              c->dwarf.loc[UNW_PPC64_V28] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R28, 0);
              c->dwarf.loc[UNW_PPC64_V29] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R29, 0);
              c->dwarf.loc[UNW_PPC64_V30] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R30, 0);
              c->dwarf.loc[UNW_PPC64_V31] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_R31, 0);
              c->dwarf.loc[UNW_PPC64_VRSAVE] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_VRSAVE, 0);
              c->dwarf.loc[UNW_PPC64_VSCR] =
                DWARF_LOC (v_regs_ptr + UC_MCONTEXT_VREGS_VSCR, 0);
            }
          else
            {
              c->dwarf.loc[UNW_PPC64_V0] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V1] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V2] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V3] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V4] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V5] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V6] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V7] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V8] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V9] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V10] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V11] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V12] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V13] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V14] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V15] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V16] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V17] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V18] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V19] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V20] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V21] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V22] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V23] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V24] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V25] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V26] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V27] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V28] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V29] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V30] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_V31] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_VRSAVE] = DWARF_NULL_LOC;
              c->dwarf.loc[UNW_PPC64_VSCR] = DWARF_NULL_LOC;
            }
          ret = 1;
        }
    }

  if (c->dwarf.ip == 0)
    {
      /* Unless the cursor or stack is corrupt or uninitialized,
         we've most likely hit the top of the stack */
      Debug (2, "returning 0\n");
      return 0;
    }

  // on ppc64, R2 register is used as pointer to TOC
  // section which is used for symbol lookup in PIC code
  // ppc64 linker generates "ld r2, 40(r1)" (ELFv1) or
  // "ld r2, 24(r1)" (ELFv2) instruction after each
  // @plt call. We need restore R2, but only for @plt calls
  {
    unw_word_t ip = c->dwarf.ip;
    unw_addr_space_t as = c->dwarf.as;
    unw_accessors_t *a = unw_get_accessors_int (as);
    void *arg = c->dwarf.as_arg;
    uint32_t toc_save = (as->abi == UNW_PPC64_ABI_ELFv2)? 24 : 40;
    int32_t inst;

    if (fetch32 (as, a, &ip, &inst, arg) >= 0
	&& (uint32_t)inst == (0xE8410000U + toc_save))
      {
	// @plt call, restoring R2 from CFA+toc_save
	c->dwarf.loc[UNW_PPC64_R2] = DWARF_LOC(c->dwarf.cfa + toc_save, 0);
      }
  }

  Debug (2, "returning %d with last return statement\n", ret);
  return ret;
}