简体   繁体   中英

How do I write a bash script which runs a program until it senses signal SIGSEGV, Segmentation fault?

Learning C-pointers and I am convinced that a program which ran successfully yesterday involving pointers to a struct core dumped on me today without me even touching it, at least that is what I believe. Is it reasonable? I do not allocate memory dynamically. Changed it today but hower but how do I write a bash script which restarts the program until it returns with a segmentation fault. Maybe also count how many times it can execute the program until the event occurs? How do I sense the SIGSEGV signal?

You can detect this by checking its exit status. Here's man bash :

   The return value of a simple command is its exit status,  or  128+n  if
   the command is terminated by signal n.

Since kill -l shows SIGSEGV as signal 11, you can therefore run the program until it exits with 139:

until ./yourprogram; [ $? -eq 139 ]; do printf '.'; done

If this is yourprogram.c :

#include <unistd.h>

int main()
{
  if(getpid()%100 == 0 ) {
    char* ptr = 1;
    *ptr = 1;
  }
  return 0;
}

You get a result like this (one dot per non-segfault invocation):

............................Segmentation fault

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM