PostgreSQL Source Code git master
Loading...
Searching...
No Matches
injection_points.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "injection_points.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  InjectionPointSharedState
 

Macros

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

Typedefs

typedef struct InjectionPointSharedState InjectionPointSharedState
 

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 41 of file injection_points.c.

◆ INJ_NAME_MAXLEN

#define INJ_NAME_MAXLEN   64

Definition at line 42 of file injection_points.c.

◆ NUM_INJECTION_POINTS_LIST

#define NUM_INJECTION_POINTS_LIST   3

Typedef Documentation

◆ InjectionPointSharedState

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 559 of file injection_points.c.

560{
562 return;
563
565}
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:873

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 194 of file injection_points.c.

195{
196 const InjectionPointCondition *condition = private_data;
197 char *argstr = arg;
198
199 if (!injection_point_allowed(condition))
200 return;
201
202 if (argstr)
203 elog(ERROR, "error triggered for injection point %s (%s)",
204 name, argstr);
205 else
206 elog(ERROR, "error triggered for injection point %s", name);
207}
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 133 of file injection_points.c.

134{
135 bool found;
136
137 if (inj_state != NULL)
138 return;
139
140 inj_state = GetNamedDSMSegment("injection_points",
143 &found, NULL);
144}
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 210 of file injection_points.c.

211{
212 const InjectionPointCondition *condition = private_data;
213 char *argstr = arg;
214
215 if (!injection_point_allowed(condition))
216 return;
217
218 if (argstr)
219 elog(NOTICE, "notice triggered for injection point %s (%s)",
220 name, argstr);
221 else
222 elog(NOTICE, "notice triggered for injection point %s", name);
223}
#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 153 of file injection_points.c.

154{
155 bool result = true;
156
157 switch (condition->type)
158 {
160 if (MyProcPid != condition->pid)
161 result = false;
162 break;
164 break;
165 }
166
167 return result;
168}
uint32 result
int MyProcPid
Definition globals.c:49
@ INJ_CONDITION_PID
@ INJ_CONDITION_ALWAYS
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 100 of file injection_points.c.

101{
103
104 SpinLockInit(&state->lock);
105 memset(state->wait_counts, 0, sizeof(state->wait_counts));
106 memset(state->name, 0, sizeof(state->name));
107 ConditionVariableInit(&state->wait_point);
108}
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 294 of file injection_points.c.

295{
298 char *function;
299 InjectionPointCondition condition = {0};
300
301 if (strcmp(action, "error") == 0)
302 function = "injection_error";
303 else if (strcmp(action, "notice") == 0)
304 function = "injection_notice";
305 else if (strcmp(action, "wait") == 0)
306 function = "injection_wait";
307 else
308 elog(ERROR, "incorrect action \"%s\" for injection point creation", action);
309
311 {
312 condition.type = INJ_CONDITION_PID;
313 condition.pid = MyProcPid;
314 }
315
316 InjectionPointAttach(name, "injection_points", function, &condition,
318
320 {
322
323 /* Local injection point, so track it for automated cleanup */
327 }
328
330}
#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:1910
MemoryContext TopMemoryContext
Definition mcxt.c:167
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
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 338 of file injection_points.c.

339{
340 char *name;
341 char *lib_name;
342 char *function;
343 bytea *private_data = NULL;
344 int private_data_size = 0;
345
346 if (PG_ARGISNULL(0))
347 elog(ERROR, "injection point name cannot be NULL");
348 if (PG_ARGISNULL(1))
349 elog(ERROR, "injection point library cannot be NULL");
350 if (PG_ARGISNULL(2))
351 elog(ERROR, "injection point function cannot be NULL");
352
356
357 if (!PG_ARGISNULL(3))
358 {
359 private_data = PG_GETARG_BYTEA_PP(3);
360 private_data_size = VARSIZE_ANY_EXHDR(private_data);
361 }
362
363 if (private_data != NULL)
366 else
368 0);
370}
#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 416 of file injection_points.c.

417{
418 char *name;
419 char *arg = NULL;
420
421 if (PG_ARGISNULL(0))
424
425 if (!PG_ARGISNULL(1))
427
429
431}
#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 175 of file injection_points.c.

176{
177 ListCell *lc;
178
179 /* Leave if nothing is tracked locally */
181 return;
182
183 /* Detach all the local points */
184 foreach(lc, inj_list_local)
185 {
186 char *name = strVal(lfirst(lc));
187
189 }
190}
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 500 of file injection_points.c.

501{
503
505 elog(ERROR, "could not detach injection point \"%s\"", name);
506
507 /* Remove point from local list, if required */
508 if (inj_list_local != NIL)
509 {
511
515 }
516
518}
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 525 of file injection_points.c.

526{
527#define NUM_INJECTION_POINTS_LIST 3
528 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
530 ListCell *lc;
531
532 /* Build a tuplestore to return our results in */
533 InitMaterializedSRF(fcinfo, 0);
534
536
537 foreach(lc, inj_points)
538 {
540 bool nulls[NUM_INJECTION_POINTS_LIST];
542
543 memset(values, 0, sizeof(values));
544 memset(nulls, 0, sizeof(nulls));
545
549
550 /* shove row into tuplestore */
551 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
552 }
553
554 return (Datum) 0;
555#undef NUM_INJECTION_POINTS_LIST
556}
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 377 of file injection_points.c.

378{
380
381 if (inj_state == NULL)
383
385
387}
#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 394 of file injection_points.c.

395{
396 char *name;
397 char *arg = NULL;
398
399 if (PG_ARGISNULL(0))
402
403 if (!PG_ARGISNULL(1))
405
407
409}
#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 478 of file injection_points.c.

479{
480 /* Enable flag to add a runtime condition based on this process ID */
482
483 if (inj_state == NULL)
485
486 /*
487 * Register a before_shmem_exit callback to remove any injection points
488 * linked to this process.
489 */
491
493}
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 438 of file injection_points.c.

439{
441 int index = -1;
442
443 if (inj_state == NULL)
445
446 /* First bump the wait counter for the injection point to wake up */
448 for (int i = 0; i < INJ_MAX_WAIT; i++)
449 {
450 if (strcmp(name, inj_state->name[i]) == 0)
451 {
452 index = i;
453 break;
454 }
455 }
456 if (index < 0)
457 {
459 elog(ERROR, "could not find injection point %s to wake up", name);
460 }
463
464 /* And broadcast the change to the waiters */
467}
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:97

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 120 of file injection_points.c.

121{
122 /*
123 * First time through, so initialize. This is shared with the dynamic
124 * initialization using a DSM.
125 */
127}

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

◆ injection_shmem_request()

static void injection_shmem_request ( void arg)
static

Definition at line 111 of file injection_points.c.

112{
113 ShmemRequestStruct(.name = "injection_points",
114 .size = sizeof(InjectionPointSharedState),
115 .ptr = (void **) &inj_state,
116 );
117}
#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 227 of file injection_points.c.

228{
230 int index = -1;
232 const InjectionPointCondition *condition = private_data;
233
234 if (inj_state == NULL)
236
237 if (!injection_point_allowed(condition))
238 return;
239
240 /*
241 * Use the injection point name for this custom wait event. Note that
242 * this custom wait event name is not released, but we don't care much for
243 * testing as this should be short-lived.
244 */
246
247 /*
248 * Find a free slot to wait for, and register this injection point's name.
249 */
251 for (int i = 0; i < INJ_MAX_WAIT; i++)
252 {
253 if (inj_state->name[i][0] == '\0')
254 {
255 index = i;
258 break;
259 }
260 }
262
263 if (index < 0)
264 elog(ERROR, "could not find free slot for wait of injection point %s ",
265 name);
266
267 /* And sleep.. */
269 for (;;)
270 {
272
276
278 break;
280 }
282
283 /* Remove this injection point from the waiters. */
285 inj_state->name[index][0] = '\0';
287}
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 90 of file injection_points.c.

90 {
91 .request_fn = injection_shmem_request,
92 .init_fn = injection_shmem_init,
93};

Referenced by _PG_init().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 38 of file injection_points.c.