PostgreSQL Source Code git master
Loading...
Searching...
No Matches
injection_points.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/pg_list.h"
#include "nodes/value.h"
#include "storage/condition_variable.h"
#include "storage/dsm_registry.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/injection_point.h"
#include "utils/memutils.h"
#include "utils/tuplestore.h"
#include "utils/wait_event.h"
Include dependency graph for injection_points.c:

Go to the source code of this file.

Data Structures

struct  InjectionPointCondition
 
struct  InjectionPointSharedState
 

Macros

#define INJ_MAX_WAIT   8
 
#define INJ_NAME_MAXLEN   64
 
#define NUM_INJECTION_POINTS_LIST   3
 

Typedefs

typedef enum InjectionPointConditionType InjectionPointConditionType
 
typedef struct InjectionPointCondition InjectionPointCondition
 
typedef struct InjectionPointSharedState InjectionPointSharedState
 

Enumerations

enum  InjectionPointConditionType { INJ_CONDITION_ALWAYS = 0 , INJ_CONDITION_PID }
 

Functions

PGDLLEXPORT void injection_error (const char *name, const void *private_data, void *arg)
 
PGDLLEXPORT void injection_notice (const char *name, const void *private_data, void *arg)
 
PGDLLEXPORT void injection_wait (const char *name, const void *private_data, void *arg)
 
static void injection_shmem_request (void *arg)
 
static void injection_shmem_init (void *arg)
 
static void injection_point_init_state (void *ptr, void *arg)
 
static void injection_init_shmem (void)
 
static bool injection_point_allowed (const InjectionPointCondition *condition)
 
static void injection_points_cleanup (int code, Datum arg)
 
 PG_FUNCTION_INFO_V1 (injection_points_attach)
 
Datum injection_points_attach (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_attach_func)
 
Datum injection_points_attach_func (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_load)
 
Datum injection_points_load (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_run)
 
Datum injection_points_run (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_cached)
 
Datum injection_points_cached (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_wakeup)
 
Datum injection_points_wakeup (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_set_local)
 
Datum injection_points_set_local (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_detach)
 
Datum injection_points_detach (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (injection_points_list)
 
Datum injection_points_list (PG_FUNCTION_ARGS)
 
void _PG_init (void)
 

Variables

 PG_MODULE_MAGIC
 
static Listinj_list_local = NIL
 
static InjectionPointSharedStateinj_state = NULL
 
static bool injection_point_local = false
 
static const ShmemCallbacks injection_shmem_callbacks
 

Macro Definition Documentation

◆ INJ_MAX_WAIT

#define INJ_MAX_WAIT   8

Definition at line 40 of file injection_points.c.

◆ INJ_NAME_MAXLEN

#define INJ_NAME_MAXLEN   64

Definition at line 41 of file injection_points.c.

◆ NUM_INJECTION_POINTS_LIST

#define NUM_INJECTION_POINTS_LIST   3

Typedef Documentation

◆ InjectionPointCondition

◆ InjectionPointConditionType

◆ InjectionPointSharedState

Enumeration Type Documentation

◆ InjectionPointConditionType

Enumerator
INJ_CONDITION_ALWAYS 
INJ_CONDITION_PID 

Definition at line 52 of file injection_points.c.

53{
54 INJ_CONDITION_ALWAYS = 0, /* always run */
55 INJ_CONDITION_PID, /* PID restriction */
InjectionPointConditionType
@ INJ_CONDITION_PID
@ INJ_CONDITION_ALWAYS

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 582 of file injection_points.c.

583{
585 return;
586
588}
static const ShmemCallbacks injection_shmem_callbacks
bool process_shared_preload_libraries_in_progress
Definition miscinit.c:1788
void RegisterShmemCallbacks(const ShmemCallbacks *callbacks)
Definition shmem.c:874

References injection_shmem_callbacks, process_shared_preload_libraries_in_progress, and RegisterShmemCallbacks().

◆ injection_error()

void injection_error ( const char name,
const void private_data,
void arg 
)
extern

Definition at line 217 of file injection_points.c.

218{
219 const InjectionPointCondition *condition = private_data;
220 char *argstr = arg;
221
222 if (!injection_point_allowed(condition))
223 return;
224
225 if (argstr)
226 elog(ERROR, "error triggered for injection point %s (%s)",
227 name, argstr);
228 else
229 elog(ERROR, "error triggered for injection point %s", name);
230}
Datum arg
Definition elog.c:1323
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
static bool injection_point_allowed(const InjectionPointCondition *condition)
static int fb(int x)
const char * name

References arg, elog, ERROR, fb(), injection_point_allowed(), and name.

◆ injection_init_shmem()

static void injection_init_shmem ( void  )
static

Definition at line 156 of file injection_points.c.

157{
158 bool found;
159
160 if (inj_state != NULL)
161 return;
162
163 inj_state = GetNamedDSMSegment("injection_points",
166 &found, NULL);
167}
void * GetNamedDSMSegment(const char *name, size_t size, void(*init_callback)(void *ptr, void *arg), bool *found, void *arg)
static void injection_point_init_state(void *ptr, void *arg)
static InjectionPointSharedState * inj_state

References fb(), GetNamedDSMSegment(), inj_state, and injection_point_init_state().

Referenced by injection_points_load(), injection_points_set_local(), injection_points_wakeup(), and injection_wait().

◆ injection_notice()

void injection_notice ( const char name,
const void private_data,
void arg 
)
extern

Definition at line 233 of file injection_points.c.

234{
235 const InjectionPointCondition *condition = private_data;
236 char *argstr = arg;
237
238 if (!injection_point_allowed(condition))
239 return;
240
241 if (argstr)
242 elog(NOTICE, "notice triggered for injection point %s (%s)",
243 name, argstr);
244 else
245 elog(NOTICE, "notice triggered for injection point %s", name);
246}
#define NOTICE
Definition elog.h:36

References arg, elog, fb(), injection_point_allowed(), name, and NOTICE.

◆ injection_point_allowed()

static bool injection_point_allowed ( const InjectionPointCondition condition)
static

Definition at line 176 of file injection_points.c.

177{
178 bool result = true;
179
180 switch (condition->type)
181 {
183 if (MyProcPid != condition->pid)
184 result = false;
185 break;
187 break;
188 }
189
190 return result;
191}
uint32 result
int MyProcPid
Definition globals.c:49
InjectionPointConditionType type

References INJ_CONDITION_ALWAYS, INJ_CONDITION_PID, MyProcPid, InjectionPointCondition::pid, result, and InjectionPointCondition::type.

Referenced by injection_error(), injection_notice(), and injection_wait().

◆ injection_point_init_state()

static void injection_point_init_state ( void ptr,
void arg 
)
static

Definition at line 123 of file injection_points.c.

124{
126
127 SpinLockInit(&state->lock);
128 memset(state->wait_counts, 0, sizeof(state->wait_counts));
129 memset(state->name, 0, sizeof(state->name));
130 ConditionVariableInit(&state->wait_point);
131}
void ConditionVariableInit(ConditionVariable *cv)
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50

References ConditionVariableInit(), fb(), and SpinLockInit().

Referenced by injection_init_shmem(), and injection_shmem_init().

◆ injection_points_attach()

Datum injection_points_attach ( PG_FUNCTION_ARGS  )

Definition at line 317 of file injection_points.c.

318{
321 char *function;
322 InjectionPointCondition condition = {0};
323
324 if (strcmp(action, "error") == 0)
325 function = "injection_error";
326 else if (strcmp(action, "notice") == 0)
327 function = "injection_notice";
328 else if (strcmp(action, "wait") == 0)
329 function = "injection_wait";
330 else
331 elog(ERROR, "incorrect action \"%s\" for injection point creation", action);
332
334 {
335 condition.type = INJ_CONDITION_PID;
336 condition.pid = MyProcPid;
337 }
338
339 InjectionPointAttach(name, "injection_points", function, &condition,
341
343 {
345
346 /* Local injection point, so track it for automated cleanup */
350 }
351
353}
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
void InjectionPointAttach(const char *name, const char *library, const char *function, const void *private_data, int private_data_size)
static bool injection_point_local
static List * inj_list_local
List * lappend(List *list, void *datum)
Definition list.c:339
char * pstrdup(const char *in)
Definition mcxt.c:1781
MemoryContext TopMemoryContext
Definition mcxt.c:166
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
on_exit_nicely_callback function
String * makeString(char *str)
Definition value.c:63
char * text_to_cstring(const text *t)
Definition varlena.c:217

References elog, ERROR, fb(), function, INJ_CONDITION_PID, inj_list_local, injection_point_local, InjectionPointAttach(), lappend(), makeString(), MemoryContextSwitchTo(), MyProcPid, name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, InjectionPointCondition::pid, pstrdup(), text_to_cstring(), TopMemoryContext, and InjectionPointCondition::type.

◆ injection_points_attach_func()

Datum injection_points_attach_func ( PG_FUNCTION_ARGS  )

Definition at line 361 of file injection_points.c.

362{
363 char *name;
364 char *lib_name;
365 char *function;
366 bytea *private_data = NULL;
367 int private_data_size = 0;
368
369 if (PG_ARGISNULL(0))
370 elog(ERROR, "injection point name cannot be NULL");
371 if (PG_ARGISNULL(1))
372 elog(ERROR, "injection point library cannot be NULL");
373 if (PG_ARGISNULL(2))
374 elog(ERROR, "injection point function cannot be NULL");
375
379
380 if (!PG_ARGISNULL(3))
381 {
382 private_data = PG_GETARG_BYTEA_PP(3);
383 private_data_size = VARSIZE_ANY_EXHDR(private_data);
384 }
385
386 if (private_data != NULL)
389 else
391 0);
393}
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
#define PG_ARGISNULL(n)
Definition fmgr.h:209
Definition c.h:776
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486

References elog, ERROR, fb(), function, InjectionPointAttach(), name, PG_ARGISNULL, PG_GETARG_BYTEA_PP, PG_GETARG_TEXT_PP, PG_RETURN_VOID, text_to_cstring(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ injection_points_cached()

Datum injection_points_cached ( PG_FUNCTION_ARGS  )

Definition at line 439 of file injection_points.c.

440{
441 char *name;
442 char *arg = NULL;
443
444 if (PG_ARGISNULL(0))
447
448 if (!PG_ARGISNULL(1))
450
452
454}
#define INJECTION_POINT_CACHED(name, arg)

References arg, fb(), INJECTION_POINT_CACHED, name, PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, and text_to_cstring().

◆ injection_points_cleanup()

static void injection_points_cleanup ( int  code,
Datum  arg 
)
static

Definition at line 198 of file injection_points.c.

199{
200 ListCell *lc;
201
202 /* Leave if nothing is tracked locally */
204 return;
205
206 /* Detach all the local points */
207 foreach(lc, inj_list_local)
208 {
209 char *name = strVal(lfirst(lc));
210
212 }
213}
bool InjectionPointDetach(const char *name)
#define lfirst(lc)
Definition pg_list.h:172
#define strVal(v)
Definition value.h:82

References fb(), inj_list_local, injection_point_local, InjectionPointDetach(), lfirst, name, and strVal.

Referenced by injection_points_set_local().

◆ injection_points_detach()

Datum injection_points_detach ( PG_FUNCTION_ARGS  )

Definition at line 523 of file injection_points.c.

524{
526
528 elog(ERROR, "could not detach injection point \"%s\"", name);
529
530 /* Remove point from local list, if required */
531 if (inj_list_local != NIL)
532 {
534
538 }
539
541}
List * list_delete(List *list, void *datum)
Definition list.c:853
#define NIL
Definition pg_list.h:68

References elog, ERROR, fb(), inj_list_local, InjectionPointDetach(), list_delete(), makeString(), MemoryContextSwitchTo(), name, NIL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, text_to_cstring(), and TopMemoryContext.

◆ injection_points_list()

Datum injection_points_list ( PG_FUNCTION_ARGS  )

Definition at line 548 of file injection_points.c.

549{
550#define NUM_INJECTION_POINTS_LIST 3
551 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
553 ListCell *lc;
554
555 /* Build a tuplestore to return our results in */
556 InitMaterializedSRF(fcinfo, 0);
557
559
560 foreach(lc, inj_points)
561 {
563 bool nulls[NUM_INJECTION_POINTS_LIST];
565
566 memset(values, 0, sizeof(values));
567 memset(nulls, 0, sizeof(nulls));
568
572
573 /* shove row into tuplestore */
574 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
575 }
576
577 return (Datum) 0;
578#undef NUM_INJECTION_POINTS_LIST
579}
static Datum values[MAXATTR]
Definition bootstrap.c:190
void InitMaterializedSRF(FunctionCallInfo fcinfo, uint32 flags)
Definition funcapi.c:76
List * InjectionPointList(void)
#define NUM_INJECTION_POINTS_LIST
uint64_t Datum
Definition postgres.h:70
#define PointerGetDatum(X)
Definition postgres.h:354
Definition pg_list.h:54
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition tuplestore.c:785
text * cstring_to_text(const char *s)
Definition varlena.c:184

References cstring_to_text(), fb(), InitMaterializedSRF(), InjectionPointList(), lfirst, NUM_INJECTION_POINTS_LIST, PointerGetDatum, tuplestore_putvalues(), and values.

◆ injection_points_load()

Datum injection_points_load ( PG_FUNCTION_ARGS  )

Definition at line 400 of file injection_points.c.

401{
403
404 if (inj_state == NULL)
406
408
410}
#define INJECTION_POINT_LOAD(name)
static void injection_init_shmem(void)

References fb(), inj_state, injection_init_shmem(), INJECTION_POINT_LOAD, name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, and text_to_cstring().

◆ injection_points_run()

Datum injection_points_run ( PG_FUNCTION_ARGS  )

Definition at line 417 of file injection_points.c.

418{
419 char *name;
420 char *arg = NULL;
421
422 if (PG_ARGISNULL(0))
425
426 if (!PG_ARGISNULL(1))
428
430
432}
#define INJECTION_POINT(name, arg)

References arg, fb(), INJECTION_POINT, name, PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_VOID, and text_to_cstring().

◆ injection_points_set_local()

Datum injection_points_set_local ( PG_FUNCTION_ARGS  )

Definition at line 501 of file injection_points.c.

502{
503 /* Enable flag to add a runtime condition based on this process ID */
505
506 if (inj_state == NULL)
508
509 /*
510 * Register a before_shmem_exit callback to remove any injection points
511 * linked to this process.
512 */
514
516}
static void injection_points_cleanup(int code, Datum arg)
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:344

References before_shmem_exit(), fb(), inj_state, injection_init_shmem(), injection_point_local, injection_points_cleanup(), and PG_RETURN_VOID.

◆ injection_points_wakeup()

Datum injection_points_wakeup ( PG_FUNCTION_ARGS  )

Definition at line 461 of file injection_points.c.

462{
464 int index = -1;
465
466 if (inj_state == NULL)
468
469 /* First bump the wait counter for the injection point to wake up */
471 for (int i = 0; i < INJ_MAX_WAIT; i++)
472 {
473 if (strcmp(name, inj_state->name[i]) == 0)
474 {
475 index = i;
476 break;
477 }
478 }
479 if (index < 0)
480 {
482 elog(ERROR, "could not find injection point %s to wake up", name);
483 }
486
487 /* And broadcast the change to the waiters */
490}
void ConditionVariableBroadcast(ConditionVariable *cv)
#define INJ_MAX_WAIT
int i
Definition isn.c:77
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
uint32 wait_counts[INJ_MAX_WAIT]
char name[INJ_MAX_WAIT][INJ_NAME_MAXLEN]
ConditionVariable wait_point
Definition type.h:96

References ConditionVariableBroadcast(), elog, ERROR, fb(), i, INJ_MAX_WAIT, inj_state, injection_init_shmem(), InjectionPointSharedState::lock, name, InjectionPointSharedState::name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, SpinLockAcquire(), SpinLockRelease(), text_to_cstring(), InjectionPointSharedState::wait_counts, and InjectionPointSharedState::wait_point.

◆ injection_shmem_init()

static void injection_shmem_init ( void arg)
static

Definition at line 143 of file injection_points.c.

144{
145 /*
146 * First time through, so initialize. This is shared with the dynamic
147 * initialization using a DSM.
148 */
150}

References fb(), inj_state, and injection_point_init_state().

◆ injection_shmem_request()

static void injection_shmem_request ( void arg)
static

Definition at line 134 of file injection_points.c.

135{
136 ShmemRequestStruct(.name = "injection_points",
137 .size = sizeof(InjectionPointSharedState),
138 .ptr = (void **) &inj_state,
139 );
140}
#define ShmemRequestStruct(...)
Definition shmem.h:176

References inj_state, name, and ShmemRequestStruct.

◆ injection_wait()

void injection_wait ( const char name,
const void private_data,
void arg 
)
extern

Definition at line 250 of file injection_points.c.

251{
253 int index = -1;
255 const InjectionPointCondition *condition = private_data;
256
257 if (inj_state == NULL)
259
260 if (!injection_point_allowed(condition))
261 return;
262
263 /*
264 * Use the injection point name for this custom wait event. Note that
265 * this custom wait event name is not released, but we don't care much for
266 * testing as this should be short-lived.
267 */
269
270 /*
271 * Find a free slot to wait for, and register this injection point's name.
272 */
274 for (int i = 0; i < INJ_MAX_WAIT; i++)
275 {
276 if (inj_state->name[i][0] == '\0')
277 {
278 index = i;
281 break;
282 }
283 }
285
286 if (index < 0)
287 elog(ERROR, "could not find free slot for wait of injection point %s ",
288 name);
289
290 /* And sleep.. */
292 for (;;)
293 {
295
299
301 break;
303 }
305
306 /* Remove this injection point from the waiters. */
308 inj_state->name[index][0] = '\0';
310}
uint32_t uint32
Definition c.h:624
bool ConditionVariableCancelSleep(void)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
#define INJ_NAME_MAXLEN
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
uint32 WaitEventInjectionPointNew(const char *wait_event_name)
Definition wait_event.c:155

References ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), elog, ERROR, fb(), i, INJ_MAX_WAIT, INJ_NAME_MAXLEN, inj_state, injection_init_shmem(), injection_point_allowed(), InjectionPointSharedState::lock, name, InjectionPointSharedState::name, SpinLockAcquire(), SpinLockRelease(), strlcpy(), InjectionPointSharedState::wait_counts, InjectionPointSharedState::wait_point, and WaitEventInjectionPointNew().

◆ PG_FUNCTION_INFO_V1() [1/9]

PG_FUNCTION_INFO_V1 ( injection_points_attach  )

◆ PG_FUNCTION_INFO_V1() [2/9]

PG_FUNCTION_INFO_V1 ( injection_points_attach_func  )

◆ PG_FUNCTION_INFO_V1() [3/9]

PG_FUNCTION_INFO_V1 ( injection_points_cached  )

◆ PG_FUNCTION_INFO_V1() [4/9]

PG_FUNCTION_INFO_V1 ( injection_points_detach  )

◆ PG_FUNCTION_INFO_V1() [5/9]

PG_FUNCTION_INFO_V1 ( injection_points_list  )

◆ PG_FUNCTION_INFO_V1() [6/9]

PG_FUNCTION_INFO_V1 ( injection_points_load  )

◆ PG_FUNCTION_INFO_V1() [7/9]

PG_FUNCTION_INFO_V1 ( injection_points_run  )

◆ PG_FUNCTION_INFO_V1() [8/9]

PG_FUNCTION_INFO_V1 ( injection_points_set_local  )

◆ PG_FUNCTION_INFO_V1() [9/9]

PG_FUNCTION_INFO_V1 ( injection_points_wakeup  )

Variable Documentation

◆ inj_list_local

List* inj_list_local = NIL
static

◆ inj_state

◆ injection_point_local

bool injection_point_local = false
static

◆ injection_shmem_callbacks

const ShmemCallbacks injection_shmem_callbacks
static
Initial value:
= {
.request_fn = injection_shmem_request,
}
static void injection_shmem_init(void *arg)
static void injection_shmem_request(void *arg)

Definition at line 113 of file injection_points.c.

113 {
114 .request_fn = injection_shmem_request,
115 .init_fn = injection_shmem_init,
116};

Referenced by _PG_init().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 37 of file injection_points.c.