Linux Stack Overflow Detection

Source: Internet
Author: User

Today, I saw Baidu know someone asked me how to detect stack overflow. I started with curiosity and checked it. I found that there is a powerful library libsigsegv on linux, it can detect Stack Overflow (maybe implemented by signal ). I tested it. That's OK. This library can be downloaded at http://www.gnu.org/software/libsigsegv.

#include <stdio.h> #include <stdlib.h>#include "sigsegv.h"static int size = 0;void oink() {     int garbage[1024]; // push this onto the stack. //#ifdef DEBUGsize += sizeof(int);    fprintf(stderr, "allocated %d kb of stack memory\n", size); //#endif /* DEBUG */     oink(); } voidstackoverflow_handler (int emergency, stackoverflow_context_t scp){  printf ("Stack overflow caught.");  exit(0);  //sigprocmask (SIG_SETMASK, &mainsigset, NULL);  //sigsegv_leave_handler (stackoverflow_handler_continuation,  //                       (void *) (long) (emergency ? -1 : pass), NULL, NULL);}#ifndef SIGSTKSZ# define SIGSTKSZ 16384#endifint main() { char mystack[SIGSTKSZ];stackoverflow_install_handler (&stackoverflow_handler,                                     mystack, sizeof (mystack));    oink(); return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.