public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mark Farnell <mark.farnell@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: getting the program counter from sparc64 for gcc/glibc
Date: Thu, 09 Apr 2009 22:44:00 -0000	[thread overview]
Message-ID: <aaf549e0904091544m27b46fc7rcdd243c63d488506@mail.gmail.com> (raw)

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

                 reply	other threads:[~2009-04-09 22:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aaf549e0904091544m27b46fc7rcdd243c63d488506@mail.gmail.com \
    --to=mark.farnell@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).