PostgreSQL Source Code  git master
assert.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * assert.c
4  * Assert support code.
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/error/assert.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <unistd.h>
18 #ifdef HAVE_EXECINFO_H
19 #include <execinfo.h>
20 #endif
21 
22 /*
23  * ExceptionalCondition - Handles the failure of an Assert()
24  *
25  * We intentionally do not go through elog() here, on the grounds of
26  * wanting to minimize the amount of infrastructure that has to be
27  * working to report an assertion failure.
28  */
29 void
30 ExceptionalCondition(const char *conditionName,
31  const char *errorType,
32  const char *fileName,
33  int lineNumber)
34 {
35  /* Report the failure on stderr (or local equivalent) */
36  if (!PointerIsValid(conditionName)
37  || !PointerIsValid(fileName)
38  || !PointerIsValid(errorType))
39  write_stderr("TRAP: ExceptionalCondition: bad arguments in PID %d\n",
40  (int) getpid());
41  else
42  write_stderr("TRAP: %s(\"%s\", File: \"%s\", Line: %d, PID: %d)\n",
43  errorType, conditionName,
44  fileName, lineNumber, (int) getpid());
45 
46  /* Usually this shouldn't be needed, but make sure the msg went out */
47  fflush(stderr);
48 
49  /* If we have support for it, dump a simple backtrace */
50 #ifdef HAVE_BACKTRACE_SYMBOLS
51  {
52  void *buf[100];
53  int nframes;
54 
55  nframes = backtrace(buf, lengthof(buf));
56  backtrace_symbols_fd(buf, nframes, fileno(stderr));
57  }
58 #endif
59 
60  /*
61  * If configured to do so, sleep indefinitely to allow user to attach a
62  * debugger. It would be nice to use pg_usleep() here, but that can sleep
63  * at most 2G usec or ~33 minutes, which seems too short.
64  */
65 #ifdef SLEEP_ON_ASSERT
66  sleep(1000000);
67 #endif
68 
69  abort();
70 }
#define write_stderr(str)
Definition: parallel.c:186
#define lengthof(array)
Definition: c.h:734
void ExceptionalCondition(const char *conditionName, const char *errorType, const char *fileName, int lineNumber)
Definition: assert.c:30
static char * buf
Definition: pg_test_fsync.c:68
#define PointerIsValid(pointer)
Definition: c.h:698