[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Signal handler for SIGSEGV, might be a bug in mprotect?

Thank you for your replying.

Regarding gdb, the problem was that I couldn't load symbol-file while
debugging. Assume that my file was test.c, executable file was test. I tried
like this:

> target remote axis:port
Remote debugging using asis:port
> symbol-file test
Can not load init.c

The problem was gdb tried to load init.c instead of test.c. I'm using
gdb-cris v5, release 4.

For the SIGSEGV question, I think I didn't explain clearly. I have a program
like this:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <asm/sigcontext.h>
#include <sys/mman.h>

int *x;
int ps;

void segv_handler(int sig, siginfo_t *sip, void *xxx) {
  char *vadr;
  int writing;

  printf("Addr: 0x%x, 0x%x\n", (int)sip, (int)xxx);

  vadr = (caddr_t)sip->si_addr;
  writing = (sip->si_errno & 2);

  fprintf(stderr, "SEGV addr: %x, %s\n", vadr, writing? "writing" :

  mprotect(vadr, ps, PROT_READ | PROT_WRITE);


int main(int argc, char* argv[]) {
  int s;
  struct sigaction sa;

  ps = getpagesize();
  printf("Page size: %d. PID = %d\n", ps, (int)getpid()); fflush(stdout);

  sa.sa_handler = &segv_handler;

  sigaddset(&sa.sa_mask, SIGIO);
  sigaddset(&sa.sa_mask, SIGALRM);

  sa.sa_flags = SA_SIGINFO;

  if (sigaction(SIGSEGV, &sa, NULL)) {
    printf("Error on assigning signal.\n");

  x = (int *)malloc(2*ps);
  printf("1 %x\n", (int)x); fflush(0);

  x = ((unsigned int)x / ps +1) * (ps);

  s = mprotect(x, ps, PROT_READ | PROT_WRITE);
  printf("2 %x, error=%d\n", (int)x, s); fflush(0);

  x[0] = 111;
  s = mprotect(x, ps, PROT_NONE);
  printf("3 %x, x=%d, error = %d. SIGSEGV should rise after this.\n",
(int)x, *x, s); fflush(0);
  x[0] = 222;

  printf("Done. x[0] = %d\n", *x);
  return 0;

When running on host, it worked fine. The handler was called and print
expected information. But when running on devb, although mprotect was
called, but SIGSEGV didn't rise. I think there might be a bug in mprotect.
Is there any one encounter the same problem?

Best regards

Nguyen Thai

> -----Original Message-----
> From: orjan.friberg@xxxxxxx.com">mailto:orjan.friberg@xxxxxxx.com]
> Sent: 02 August 2001 17:31
> To: Nguyen Thai
> Cc: dev-etrax@xxxxxxx.com
> Subject: Re: Signal handler for SIGSEGV
> Nguyen Thai wrote:
> >
> > Hi all,
> > I just want to ask some small questions :):
> >
> > Does anyone work with replacing SIGSEGV handler on developer
> board lx? Could
> > you give me an example?
> >
> To register another signal handler you simply call
>    sighandler_t signal(int signum, sighandler_t action);
> where signum is SIGSEGV in your case, and action is your new signal
> handler, or one of SIG_IGN (to ignore the signal) and SIG_DFL (for
> default behaviour).  The signal handler is declared as:
>    typedef void (*sighandler_t)(int);
> which means it should take an integer argument and return void.  There
> are some restrictions on what you can do inside a signal handler; refer
> to a UNIX programming book for that.  There are a couple of examples in
> busybox/syslogd.c.
> > Regarding with gdbserver: can we open source file while debuging with
> > gdbserver, if yes, how?
> I don't think I understand your problem.  Do you want to be able to see
> the source code of the debugged program in gdb?  In that case you need
> the -g flag when compiling to get debug info, and the -s flag should be
> omitted when linking (more info at
> http://developer.axis.com/software/tools/gdb-cris/ under So You Want To
> Debug).
> /Orjan
> --
> Orjan Friberg              E-mail: orjan.friberg@xxxxxxx.com
> Axis Communications AB     Phone:  +46 46 272 17 68