Skip to contentSkip to conversation

getting the program counter from sparc64 under gcc/glibc linux

DISCUSSION Mark Farnell 2 participants 5 messages Apr 11, 2009 – Apr 13, 2009

lore
Share & tools
lore
#0 lore 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

#1 lore raw

From: Mark Farnell <[email protected]>
Date: Sun, 12 Apr 2009 11:24:30 +1200

Quoted 2 lines

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

Look at the dwarf2 unwind support code in the GCC sources of the signal handler support in the MONO tree.

We provide the register values differently than other platforms, there isn't a ucontext available but there is a sigcontext_t:

From the sigcontext you can get at the program counter by accessing sc->sigc_regs.tpc on 64-bit sparc and sc->si_regs.pc on 32-bit sparc.

#2 lore raw
In reply to Mark Farnell

I am a bit confused here.

The signal handler will be:

void (*sa_sigaction)(int, siginfo_t *, void *);

in your last email, do you mean that I should cast the last argument to sigcontext_t rather than ucontext_t?

Thanks!

Mark

Quoted 16 lines · David Miller

From: Mark Farnell <[email protected]>
Date: Sun, 12 Apr 2009 11:24:30 +1200

Quoted 2 lines

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

Look at the dwarf2 unwind support code in the GCC sources of the signal handler support in the MONO tree.

We provide the register values differently than other platforms, there isn't a ucontext available but there is a sigcontext_t:

From the sigcontext you can get at the program counter by accessing sc->sigc_regs.tpc on 64-bit sparc and sc->si_regs.pc on 32-bit sparc.

#3 lore raw
In reply to Mark Farnell

From: Mark Farnell <[email protected]>
Date: Mon, 13 Apr 2009 09:32:33 +1200

Quoted 8 lines

I am a bit confused here.

The signal handler will be:

void (*sa_sigaction)(int, siginfo_t *, void *);

in your last email, do you mean that I should cast the last argument to sigcontext_t rather than ucontext_t?

That's correct.

#4 lore raw
In reply to Mark Farnell

Thanks! I have just hot it working.

Quoted 15 lines · David Miller

From: Mark Farnell <[email protected]>
Date: Mon, 13 Apr 2009 09:32:33 +1200

Quoted 8 lines

I am a bit confused here.

The signal handler will be:

void (*sa_sigaction)(int, siginfo_t *, void *);

in your last email, do you mean that I should cast the last argument to sigcontext_t rather than ucontext_t?

That's correct.