Valgrind: Intentionally invoke segfault

This is a crazy hack, but I'm trying to consciously call segfault at a specific place in the execution, so valgrind will give me a stack trace.

If there is a better way to do this, please tell me, but I would still be interested to know how to deliberaly call segfault, and why my attempt did not work.

This is my unsuccessful attempt:

long* ptr = (long *)0xF0000000;
ptr = 10;

I thought valgrind should at least pick this up as an invalid entry, even if it is not a segmentation violation. Walgrind says nothing about this.

Any ideas why?

EDIT

The answer is accepted, but I still have a few votes for any suggestions for a more reasonable way to get the stack trace ...

+2
source share
6

*, *ptr = 10? , , .

, , , seg-fault, . .

, segfault, :

inline void seg_fault(void)
{
    volatile int *p = reinterpret_cast<volatile int*>(0);
    *p = 0x1337D00D;
}
+5

abort(). segfault, .

+7

, gdb breakbpoint, backtrace?

(gdb) b somewhere
(gdb) r
(gdb) bt
+5

, abort(), kill(getpid(), SIGSEGV), . , gdb , valgrind.

valgrind valgrind dump , . , valgrind.

#include <valgrind/valgrind.h>
...
VALGRIND_PRINTF_BACKTRACE("Encountered the foobar problem, x=%d, y=%d\n", x, y);
+3

x86? , CPU, " ". CC int 3. :

inline void debugger_halt(void)
{
#ifdef MSVC
   __asm int 3;
#elif defined(GCC)
   asm("int 3");
#else
#pragma error Well, you'll have to figure out how to do inline assembly 
              in your compiler
#endif
}

MSVC __debugbreak(), MSIL "break" .

+2

sig SEGV (11) ?

+2

Source: https://habr.com/ru/post/1720542/


All Articles