public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* getting the program counter from sparc64 for gcc/glibc
@ 2009-04-09 22:44 Mark Farnell
  0 siblings, 0 replies; only message in thread
From: Mark Farnell @ 2009-04-09 22:44 UTC (permalink / raw)
  To: gcc-help

I am running a program which needs to determine whether a memory
access attempt is read or write.  This is intended to be achieved by a
SIGSEGV handler which gets the program counter by getting:

pc = ucontext_t.mcontext_t.mc_gregs[MC_PC]

Then I tried to get the instruction by dereferencing the pc and
bit-test the instruction to get the memory.

Here is the source code for the program:

#include <errno.h>
#if 0
#include <siginfo.h>
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include <unistd.h>

#define is_load_or_store(inst) \
        (((inst) >> 30) == 3)

#define is_store(inst) \
        (is_load_or_store(inst) && \
         ((0xC0F0 >> (((inst) >> 19) & 0xF)) & 1) != 0)

static void my_segv_action(int signo, siginfo_t *info, void *dummy) {
  ucontext_t *uc = dummy;

#if 0
  greg_t pc = uc->uc_mcontext.gregs[REG_PC];
#endif
  mc_greg_t pc = uc->uc_mcontext.mc_gregs[MC_PC];


  unsigned long inst = *(unsigned long const *)pc;

  if (is_load_or_store(inst)) {
    if (is_store(inst)) {
      write(2, "It is a store.\n", 15);
    } else {
      write(2, "It is a load.\n", 14);
    }
  } else {
    write(2, "It is neither.\n", 15);
  }
  fprintf(stderr, "Address = %p, instruction = %08x\n", pc, inst);
#if 0
  psiginfo(info, "my_segv_action");
#endif
  abort();
}


int main(void) {
  struct sigaction New_Action;
  struct sigaction Old_Action;
  char *p = 0;

  New_Action.sa_handler = 0;
  New_Action.sa_sigaction = my_segv_action;
  (void)sigemptyset(&New_Action.sa_mask);
  New_Action.sa_flags = SA_SIGINFO | SA_RESETHAND;

#if 1
  if (sigaction(SIGSEGV, &New_Action, &Old_Action) != 0) {
    perror("sigaction");
    return EXIT_FAILURE;
  }
#endif
  (*p) = 77;          /* write attempt */
  return EXIT_SUCCESS;
}


However in the SIGSEGV handler, gdb tells me that
uc->uc_mcontext.mc_gregs[MC_PC] is 0   (MC_PC is 1)

How can I get the program counter which points to the instruction that
causes the write-segmentation fault under GNU/Linux SPARC64?

Thanks!

Mark

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-04-09 22:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-09 22:44 getting the program counter from sparc64 for gcc/glibc Mark Farnell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).