-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgcov.c
964 lines (767 loc) · 23.5 KB
/
pgcov.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
#include "postgres.h"
#include "plpgsql.h"
#include "funcapi.h"
#include "fmgr.h"
#include "access/hash.h"
#include "catalog/pg_type.h"
#include "catalog/pg_proc.h"
#include "utils/syscache.h"
#include "utils/builtins.h"
#include "executor/executor.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "storage/ipc.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "miscadmin.h"
#if PG_VERSION_NUM >= 90300
#include "access/htup_details.h"
#endif
#include "pgcov.h"
PG_MODULE_MAGIC;
/* ************* *
* shared memory *
* ************* */
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
typedef struct {
LWLockId lock;
char nest_entrance[MAX_ENTRANCE_SIZE];
} pgcovSharedState;
static pgcovSharedState *pgcov = NULL;
/* ******* *
* testing *
* ******* */
static bool pgcov_test_function_line_info = false;
/* ****************** *
* backend-local data *
* ****************** */
static List *pgcov_call_stack = NIL;
/*
* All memory in the call stack should be allocated in this memory context. We
* reset the context every time the stack is completely unwound in order to
* avoid leaking any memory.
*/
static MemoryContext pgcov_call_stack_mctx = NULL;
/* *********************************** *
* backend-local data for the listener *
* *********************************** */
typedef struct {
Oid dbid;
char *fnsignature;
} pgcovFcHashKey;
typedef struct {
pgcovFcHashKey key;
Oid fnoid;
int32 ncalls;
char *prosrc;
List *lines; /* list of pgcovFunctionLine */
} pgcovFunctionCoverage;
static uint32 pgcov_function_coverage_hashfn(const void *key, Size keysize);
static int pgcov_function_coverage_matchfn(const void *key1, const void *key2, Size keysize);
/*
* All data gathered by the listener should be kept in this memory context.
* It is created when we start listening and destroyed once we're ready to
* abandon the data, either by listening again or by an explicit call to reset.
*/
MemoryContext pgcov_listener_mcxt = NULL;
static HTAB *pgcov_function_coverage = NULL;
static void pgcov_init_listener(void);
/* ************************* *
* PL/PgSQL plugin callbacks *
* ************************* */
static void pgcov_plpgsql_func_beg(PLpgSQL_execstate *estate,
PLpgSQL_function *func);
static void pgcov_plpgsql_stmt_beg(PLpgSQL_execstate *estate,
PLpgSQL_stmt *stmt);
static PLpgSQL_plugin pgcov_plpgsql_plugin_struct = {
NULL, /* func_setup */
pgcov_plpgsql_func_beg,
NULL, /* func_end */
pgcov_plpgsql_stmt_beg,
NULL, /* stmt_end */
};
/* fmgr hooks */
static bool pgcov_needs_fmgr_hook(Oid fnoid);
static void pgcov_fmgr_hook(FmgrHookEventType event, FmgrInfo *flinfo, Datum *args);
static needs_fmgr_hook_type prev_needs_fmgr_hook = NULL;
static fmgr_hook_type prev_fmgr_hook = NULL;
/* shared memory manipulation routines */
#define pgcov_require_shmem() \
do { \
if (!pgcov) \
ereport(ERROR, \
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), \
errmsg("shared memory not initialized"), \
errhint("pgcov must be in shared_preload_libraries"))); \
} while (0)
static void pgcov_shmem_startup(void);
static void pgcov_shmem_set_listener(const pgcovNest *nest);
static void pgcov_shmem_clear_listener(const pgcovNest *nest);
void _PG_init(void);
void _PG_fini(void);
/* local functions for fetching information about functions */
static bool pgcov_get_function_line_info_enter_stmt(PLpgSQL_function *func,
PLpgSQL_stmt *stmt,
void *aux);
static void pgcov_get_function_line_info(pgcovStackFrame *fn,
PLpgSQL_function *func,
const char *prosrc);
static char *get_function_signature(HeapTuple proctup, Form_pg_proc procform,
const char *proname);
static void pgcov_record_stmt_enter(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
/*
* Module load callback
*/
void
_PG_init(void)
{
PLpgSQL_plugin **plpgsql_plugin;
/* must be loaded via shared_preload_libraries */
if (!process_shared_preload_libraries_in_progress)
return;
pgcov_call_stack_mctx =
AllocSetContextCreate(TopMemoryContext,
"pgcov call stack memory context",
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_SMALL_MAXSIZE);
plpgsql_plugin = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
*plpgsql_plugin = &pgcov_plpgsql_plugin_struct;
prev_needs_fmgr_hook = needs_fmgr_hook;
needs_fmgr_hook = pgcov_needs_fmgr_hook;
prev_fmgr_hook = fmgr_hook;
fmgr_hook = pgcov_fmgr_hook;
/* shared memory init */
RequestAddinShmemSpace(1024);
#if PG_VERSION_NUM >= 90600
RequestNamedLWLockTranche("pgcov", 1);
#else
RequestAddinLWLocks(1);
#endif
prev_shmem_startup_hook = shmem_startup_hook;
shmem_startup_hook = pgcov_shmem_startup;
}
/*
* Module unload callback
*/
void
_PG_fini(void)
{
PLpgSQL_plugin **plpgsql_plugin;
plpgsql_plugin = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
*plpgsql_plugin = NULL;
needs_fmgr_hook = prev_needs_fmgr_hook;
fmgr_hook = prev_fmgr_hook;
}
static void
pgcov_shmem_startup(void)
{
bool found;
if (prev_shmem_startup_hook)
prev_shmem_startup_hook();
/*
* Create or attach to the shared memory state, including hash table
*/
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
pgcov = ShmemInitStruct("pgcov",
sizeof(pgcovSharedState),
&found);
if (!found)
{
/* First time through ... */
#if PG_VERSION_NUM >= 90600
pgcov->lock = &(GetNamedLWLockTranche("pgcov"))->lock;
#else
pgcov->lock = LWLockAssign();
#endif
pgcov->nest_entrance[0] = '\0';
}
LWLockRelease(AddinShmemInitLock);
}
static void
pgcov_shmem_set_listener(const pgcovNest *nest)
{
volatile pgcovSharedState *shmem;
pgcov_require_shmem();
LWLockAcquire(pgcov->lock, LW_EXCLUSIVE);
shmem = pgcov;
if (shmem->nest_entrance[0] != '\0')
elog(ERROR, "an active listener already exists");
memcpy(pgcov->nest_entrance, nest->entrance, MAX_ENTRANCE_SIZE);
LWLockRelease(pgcov->lock);
}
static void
pgcov_shmem_clear_listener(const pgcovNest *nest)
{
pgcov_require_shmem();
LWLockAcquire(pgcov->lock, LW_EXCLUSIVE);
if (memcmp(pgcov->nest_entrance, nest->entrance, MAX_ENTRANCE_SIZE) != 0)
elog(ERROR, "unexpected entrance %s, was expecting %s", pgcov->nest_entrance, nest->entrance);
pgcov->nest_entrance[0] = '\0';
LWLockRelease(pgcov->lock);
}
/*
* Init all the data structures required to track stuff. XXX
*/
static void
pgcov_init_listener(void)
{
HASHCTL ctl;
int flags;
pgcov_listener_mcxt =
AllocSetContextCreate(TopMemoryContext,
"pgcov listener aggregated data memory context",
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_SMALL_MAXSIZE);
memset(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(pgcovFcHashKey);
ctl.entrysize = sizeof(pgcovFunctionCoverage);
ctl.hash = pgcov_function_coverage_hashfn;
ctl.match = pgcov_function_coverage_matchfn;
/* use our memory context for the hash table */
ctl.hcxt = pgcov_listener_mcxt;
flags = HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT;
pgcov_function_coverage =
hash_create("pgcov_function_coverage_hash_table", 64, &ctl, flags);
}
static uint32
pgcov_function_coverage_hashfn(const void *ptr, Size keysize)
{
pgcovFcHashKey *key = (pgcovFcHashKey *) ptr;
Assert(keysize == sizeof(pgcovFcHashKey));
return DatumGetUInt32(hash_any((void *) key->fnsignature, strlen(key->fnsignature)));
}
static int
pgcov_function_coverage_matchfn(const void *ptr1, const void *ptr2, Size keysize)
{
pgcovFcHashKey *key1 = (pgcovFcHashKey *) ptr1;
pgcovFcHashKey *key2 = (pgcovFcHashKey *) ptr2;
Assert(keysize == sizeof(pgcovFcHashKey));
if (key1->dbid < key2->dbid)
return -1;
else if (key1->dbid > key2->dbid)
return 1;
else
return strcmp(key1->fnsignature, key2->fnsignature);
}
Datum pgcov_called_functions(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgcov_called_functions);
Datum
pgcov_called_functions(PG_FUNCTION_ARGS)
{
MemoryContext oldcxt;
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
Tuplestorestate *tupstore;
TupleDesc tupdesc;
if (!pgcov_function_coverage)
elog(ERROR, "record some data first (see pgcov_listen())");
/* check to see if caller supports us returning a tuplestore */
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set")));
if (!(rsinfo->allowedModes & SFRM_Materialize))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("materialize mode required, but it is not " \
"allowed in this context")));
/* switch to long-lived memory context */
oldcxt = MemoryContextSwitchTo(rsinfo->econtext->ecxt_per_query_memory);
/* get the requested return tuple description */
tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
if (tupdesc->natts != 4)
elog(ERROR, "unexpected natts %d", tupdesc->natts);
tupstore =
tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
false, work_mem);
/* walk over the hash table */
{
HASH_SEQ_STATUS hseq;
pgcovFunctionCoverage *fn;
Datum values[4];
bool isnull[4] = { false, false, false, true };
hash_seq_init(&hseq, pgcov_function_coverage);
while ((fn = (pgcovFunctionCoverage *) hash_seq_search(&hseq)) != NULL)
{
values[0] = CStringGetTextDatum(fn->key.fnsignature);
values[1] = ObjectIdGetDatum(fn->fnoid);
values[2] = Int32GetDatum(fn->ncalls);
tuplestore_putvalues(tupstore, tupdesc, values, isnull);
}
}
MemoryContextSwitchTo(oldcxt);
/* let the caller know we're sending back a tuplestore */
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
return (Datum) 0;
}
Datum pgcov_fn_line_coverage(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgcov_fn_line_coverage);
Datum
pgcov_fn_line_coverage(PG_FUNCTION_ARGS)
{
MemoryContext oldcxt;
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
Tuplestorestate *tupstore;
TupleDesc tupdesc;
char *fnsignature;
if (!pgcov_function_coverage)
elog(ERROR, "record some data first (see pgcov_listen())");
/* check to see if caller supports us returning a tuplestore */
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set")));
if (!(rsinfo->allowedModes & SFRM_Materialize))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("materialize mode required, but it is not " \
"allowed in this context")));
/* get a cstring of the argument in the short-lived context */
fnsignature = TextDatumGetCString(PG_GETARG_DATUM(0));
/* .. and then switch to long-lived memory context */
oldcxt = MemoryContextSwitchTo(rsinfo->econtext->ecxt_per_query_memory);
/* get the requested return tuple description */
tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
if (tupdesc->natts != 3)
elog(ERROR, "unexpected natts %d", tupdesc->natts);
tupstore =
tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
false, work_mem);
{
pgcovFunctionCoverage *fn;
bool found;
Datum values[3];
bool isnull[3] = { false, false, true };
const pgcovFcHashKey key = { 0, fnsignature };
ListCell *lc;
fn = hash_search(pgcov_function_coverage, (void *) &key, HASH_FIND, &found);
if (!found)
elog(ERROR, "could not find function %s", fnsignature);
foreach(lc, fn->lines)
{
pgcovFunctionLine *line = (pgcovFunctionLine *) lfirst(lc);
values[0] = Int32GetDatum(line->lineno);
values[1] = Int32GetDatum(line->num_executed);
tuplestore_putvalues(tupstore, tupdesc, values, isnull);
}
}
MemoryContextSwitchTo(oldcxt);
/* let the caller know we're sending back a tuplestore */
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
return (Datum) 0;
}
Datum pgcov_fn_line_coverage_src(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgcov_fn_line_coverage_src);
Datum
pgcov_fn_line_coverage_src(PG_FUNCTION_ARGS)
{
pgcovFunctionCoverage *fn;
bool found;
pgcovFcHashKey key = { 0, NULL };
if (!pgcov_function_coverage)
elog(ERROR, "record some data first (see pgcov_listen())");
key.fnsignature = TextDatumGetCString(PG_GETARG_DATUM(0));
fn = hash_search(pgcov_function_coverage, (void *) &key, HASH_FIND, &found);
if (!found)
elog(ERROR, "could not find function %s", key.fnsignature);
if (!fn->prosrc)
PG_RETURN_NULL();
PG_RETURN_DATUM(CStringGetTextDatum(fn->prosrc));
}
/*
* Takes a newly received function coverage report and incorporates its data
* into the data we've gathered so far.
*/
void
pgcov_function_coverage_sfunc(Oid fnoid, char *fnsignature, int32 ncalls,
char *prosrc, List *lines)
{
const pgcovFcHashKey key = { 0, fnsignature };
bool found;
pgcovFunctionCoverage *fn;
MemoryContext oldcxt;
ListCell *lc;
fn = hash_search(pgcov_function_coverage, (const void *) &key, HASH_ENTER, &found);
if (!found)
{
/* we need to copy the signature out of the parse context */
fn->key.fnsignature = MemoryContextStrdup(pgcov_listener_mcxt, key.fnsignature);
fn->prosrc = NULL;
fn->lines = NIL;
goto replace;
}
/*
* See if the function's source code is still the same. If it's not,
* forget everything we thought we knew about the function and replace it.
* This allows us to still somewhat function if e.g. a test suite replaces
* a function with a mocked version. N.B. we specifically do *not* look at
* the oid of the function. This way we still keep track of a function
* even if it gets replaced via DROP/CREATE function instead of CREATE OR
* REPLACE.
*/
if ((prosrc == NULL && fn->prosrc == NULL) ||
(prosrc != NULL && fn->prosrc != NULL &&
strcmp(fn->prosrc, prosrc) == 0))
{
ListCell *lc1, *lc2;
fn->ncalls += ncalls;
if (list_length(fn->lines) != list_length(lines))
elog(ERROR, "line list_length oops");
forboth(lc1, fn->lines, lc2, lines)
{
pgcovFunctionLine *old = lfirst(lc1);
pgcovFunctionLine *new = lfirst(lc2);
if (old->lineno != new->lineno)
elog(ERROR, "lineno oops");
old->num_executed += new->num_executed;
}
return;
}
replace:
/*
* This is either a function we haven't seen before, or the function with
* this signature got replaced with a new definition. Copy all items out
* of the parse context into ours. Also remember to free the previous ones
* if we're replacing or repeatedly redefining functions would result in
* memory leaks.
*/
oldcxt = MemoryContextSwitchTo(pgcov_listener_mcxt);
fn->fnoid = fnoid;
fn->ncalls = ncalls;
if (fn->prosrc)
pfree(fn->prosrc);
if (prosrc)
fn->prosrc = pstrdup(prosrc);
else
fn->prosrc = NULL;
if (fn->lines)
{
list_free(fn->lines);
fn->lines = NIL;
}
foreach(lc, lines)
{
pgcovFunctionLine *line = (pgcovFunctionLine *) palloc(sizeof(pgcovFunctionLine));
memcpy(line, lfirst(lc), sizeof(pgcovFunctionLine));
fn->lines = lappend(fn->lines, line);
}
MemoryContextSwitchTo(oldcxt);
}
/*
* Returns true if an active listener exists, false otherwise. If the return
* value is true, "entrance" is populated with information about the entrance.
*/
bool
pgcov_get_active_listener(char entrance[MAX_ENTRANCE_SIZE])
{
bool active;
volatile pgcovSharedState *shmem;
if (!pgcov)
return false;
LWLockAcquire(pgcov->lock, LW_SHARED);
shmem = pgcov;
active = (shmem->nest_entrance[0] != '\0');
if (active && entrance != NULL)
memcpy(entrance, pgcov->nest_entrance, MAX_ENTRANCE_SIZE);
LWLockRelease(pgcov->lock);
return active;
}
Datum pgcov_reset(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgcov_reset);
Datum
pgcov_reset(PG_FUNCTION_ARGS)
{
if (pgcov_listener_mcxt == NULL)
{
/* nothing to do */
PG_RETURN_VOID();
}
hash_destroy(pgcov_function_coverage); /* XXX any reason to do this? (any reason not to?) */
pgcov_function_coverage = NULL;
MemoryContextDelete(pgcov_listener_mcxt);
pgcov_listener_mcxt = NULL;
PG_RETURN_VOID();
}
Datum pgcov_listen(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgcov_listen);
Datum
pgcov_listen(PG_FUNCTION_ARGS)
{
pgcovNest nest;
/*
* See if there's already an active listener. There's a race between us
* checking for one and registering ourselves into the shared memory (since
* we don't hold the lock while creating the socket), but that's all right;
* pgcov_shmem_set_listener will raise an exception in that case.
*/
if (pgcov_get_active_listener(NULL))
elog(ERROR, "an active listener already exists");
/* make sure we don't have any previous crap left */
//DirectFunctionCall0(pgcov_reset);
pgcov_reset(NULL); // XXX WTF
pgcov_init_listener();
pgcov_start_listener(&nest);
PG_TRY();
{
pgcov_shmem_set_listener(&nest);
/* now gather information until we're cancelled */
pgcov_gather_information(&nest);
}
PG_CATCH();
{
pgcov_shmem_clear_listener(&nest);
pgcov_stop_listener(&nest);
PG_RE_THROW();
}
PG_END_TRY();
pgcov_shmem_clear_listener(&nest);
pgcov_stop_listener(&nest);
PG_RETURN_NULL();
}
/*
* Enables the tester for pgcov_get_function_line_info().
*/
Datum pgcov_enable_test_function_line_info(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgcov_enable_test_function_line_info);
Datum
pgcov_enable_test_function_line_info(PG_FUNCTION_ARGS)
{
pgcov_test_function_line_info = true;
return (Datum) 0;
}
/*
* Fetches the function's signature. None of the types in pg_catalog are
* schema-qualified, other types always are.
* TODO actually implement that :-D
*/
static char *
get_function_signature(HeapTuple proctup, Form_pg_proc procform, const char *proname)
{
StringInfoData str;
int nargs;
int i;
int input_argno;
Oid *argtypes;
char **argnames;
char *argmodes;
initStringInfo(&str);
appendStringInfo(&str, "%s(", proname);
nargs = get_func_arg_info(proctup, &argtypes, &argnames, &argmodes);
input_argno = 0;
for (i = 0; i < nargs; ++i)
{
Oid argtype = argtypes[i];
if (argmodes &&
argmodes[i] != PROARGMODE_IN &&
argmodes[i] != PROARGMODE_INOUT)
continue;
if (input_argno++ > 0)
appendStringInfoString(&str, ", ");
appendStringInfoString(&str, format_type_be(argtype));
}
appendStringInfoChar(&str, ')');
return str.data;
}
static bool
pgcov_get_function_line_info_enter_stmt(PLpgSQL_function *func,
PLpgSQL_stmt *stmt,
void *aux)
{
Bitmapset **bms = (Bitmapset **) aux;
if (stmt->lineno > 0)
*bms = bms_add_member(*bms, stmt->lineno);
return false;
}
/*
* Finds all lines in a function which contain (the first line of) a PL/PgSQL
* statement.
*
* The caller must make sure we're in pgcov_call_stack_mctx.
*/
static void
pgcov_get_function_line_info(pgcovStackFrame *fn,
PLpgSQL_function *func,
const char *prosrc)
{
fniter_context ctx;
Bitmapset *linebms = NULL;
Assert(fn->lines == NIL);
memset(&ctx, 0, sizeof(fniter_context));
ctx.enter_stmt = pgcov_get_function_line_info_enter_stmt;
ctx.auxiliary = (void *) &linebms;
fniter_function_iterate(func, &ctx);
if (linebms)
{
int lineno;
while ((lineno = bms_first_member(linebms)) >= 0)
{
pgcovFunctionLine *line =
(pgcovFunctionLine *) palloc(sizeof(pgcovFunctionLine));
line->lineno = (int32) lineno;
line->num_executed = 0;
fn->lines = lappend(fn->lines, line);
}
pfree(linebms);
}
if (pgcov_test_function_line_info)
{
ListCell *lc;
pgcovFunctionLine *line;
elog(INFO, "%d lines:", list_length(fn->lines));
foreach(lc, fn->lines)
{
line = (pgcovFunctionLine *) lfirst(lc);
elog(INFO, " %d", line->lineno);
}
}
}
static void
pgcov_plpgsql_func_beg(PLpgSQL_execstate *estate,
PLpgSQL_function *func)
{
MemoryContext oldctx;
HeapTuple proctup;
pgcovStackFrame *fn;
Datum prosrc;
bool isnull;
if (estate->func->fn_oid == InvalidOid)
return;
Assert(pgcov_call_stack != NIL);
fn = (pgcovStackFrame *) linitial(pgcov_call_stack);
if (fn->fnoid != func->fn_oid)
elog(ERROR, "PL/PgSQL function oid %u does not match stack frame %u",
func->fn_oid, fn->fnoid);
oldctx = MemoryContextSwitchTo(fn->mcxt);
/* look up prosrc */
proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn->fnoid));
if (!HeapTupleIsValid(proctup))
elog(ERROR, "cache lookup failed for function %u", fn->fnoid);
prosrc = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosrc, &isnull);
if (isnull)
elog(ERROR, "unexpected null prosrc for function %u", fn->fnoid);
fn->prosrc = TextDatumGetCString(prosrc);
ReleaseSysCache(proctup);
/* .. and information about the statements in this function */
pgcov_get_function_line_info(fn, func, fn->prosrc);
pgcov_record_stmt_enter(estate, (PLpgSQL_stmt *) func->action);
MemoryContextSwitchTo(oldctx);
}
static void
pgcov_plpgsql_stmt_beg(PLpgSQL_execstate *estate,
PLpgSQL_stmt *stmt)
{
if (estate->func->fn_oid == InvalidOid)
return;
Assert(pgcov_call_stack != NIL);
/* skip dummy returns; see pl_comp.c */
if (stmt->cmd_type == PLPGSQL_STMT_RETURN &&
stmt->lineno == 0)
return;
pgcov_record_stmt_enter(estate, stmt);
}
static void
pgcov_record_stmt_enter(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
{
pgcovStackFrame *fn;
ListCell *lc;
if (estate->func->fn_oid == InvalidOid)
return;
Assert(pgcov_call_stack != NIL);
fn = (pgcovStackFrame *) linitial(pgcov_call_stack);
Assert(fn->fnoid == estate->func->fn_oid);
foreach(lc, fn->lines)
{
pgcovFunctionLine *line = (pgcovFunctionLine *) lfirst(lc);
if (line->lineno == (int32) stmt->lineno)
{
line->num_executed++;
return;
}
}
/* XXX this probably shouldn't happen? */
elog(WARNING, "could not find lineno %d of function %u, stmt %d",
stmt->lineno, fn->fnoid, stmt->cmd_type);
}
static bool
pgcov_needs_fmgr_hook(Oid fnoid)
{
/* always need */
return true;
}
static void
pgcov_enter_func_guts(Oid fnoid)
{
MemoryContext oldctx;
pgcovStackFrame *newfn;
oldctx = MemoryContextSwitchTo(pgcov_call_stack_mctx);
newfn = (pgcovStackFrame *) palloc(sizeof(pgcovStackFrame));
newfn->mcxt = AllocSetContextCreate(TopMemoryContext,
"pgcov stack frame memory context",
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_SMALL_MAXSIZE);
(void) MemoryContextSwitchTo(newfn->mcxt);
newfn->fnoid = fnoid;
newfn->lines = NIL;
{
HeapTuple proctup;
Form_pg_proc procform;
const char *proname;
proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fnoid));
if (!HeapTupleIsValid(proctup))
elog(ERROR, "cache lookup failed for function %d", fnoid);
procform = (Form_pg_proc) GETSTRUCT(proctup);
proname = NameStr(procform->proname);
newfn->fnsignature = get_function_signature(proctup, procform, proname);
ReleaseSysCache(proctup);
}
/*
* If it's a PL/PgSQL function, pgcov_plpgsql_func_beg will fetch the
* actual prosrc for the function.
*/
newfn->prosrc = NULL;
pgcov_call_stack = lcons(newfn, pgcov_call_stack);
MemoryContextSwitchTo(oldctx);
}
static void
pgcov_exit_func_guts(Oid fnoid)
{
pgcovStackFrame *fn;
Assert(pgcov_call_stack != NIL);
fn = (pgcovStackFrame *) linitial(pgcov_call_stack);
if (fn->fnoid != fnoid)
elog(FATAL, "XXX %d != %d", fn->fnoid, fnoid);
pgcov_emit_function_coverage_report(fn);
MemoryContextDelete(fn->mcxt);
pfree(fn);
pgcov_call_stack = list_delete_first(pgcov_call_stack);
if (pgcov_call_stack == NIL)
{
/* last frame, clean up */
MemoryContextReset(pgcov_call_stack_mctx);
}
}
static void
pgcov_fmgr_hook(FmgrHookEventType event, FmgrInfo *flinfo, Datum *args)
{
switch (event)
{
case FHET_START:
pgcov_enter_func_guts(flinfo->fn_oid);
break;
case FHET_END:
case FHET_ABORT:
// TODO
pgcov_exit_func_guts(flinfo->fn_oid);
break;
default:
elog(ERROR, "unknown FmgrHookEventType %d", event);
}
if (prev_fmgr_hook)
(*prev_fmgr_hook)(event, flinfo, args);
}