include/dbil_obs_perl.h

filter: Statementstmt 93.1% 54 / 58 Branchbran 67.5% 50 / 74 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.7% 104 / 132 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/dbil_future.h ↑ Summary xs/future.xs »
1 /* dbil_obs_perl.h - the Perl door onto the dbil_abi v2 statement observer.
2 *
3 * dbil_obs.h holds C function pointers, which is what an instrumented
4 * consumer written in XS wants and what keeps an uninstrumented statement at
5 * one branch. This registers a pair of SHIMS into that same table whose user
6 * data is a pair of Perl coderefs, so an application that is not an XS module
7 * can watch its own queries:
8 *
9 * DBIx::Loop->on_exec(\&start, \&done);
10 *
11 * There is no other way for it to. The native backend speaks the wire
12 * protocol and never builds a DBI statement handle, so $dbh->{Callbacks},
13 * DBI::Profile and DBI->trace see nothing at all on the fast path. dbil_exec
14 * is the one place all three backends pass through, and until now it could
15 * only be reached from C.
16 *
17 * The contract is deliberately identical to the C one rather than friendlier
18 * - process-global, no deregistration, no bind values - so the two doors
19 * cannot drift into different semantics. What differs is what it costs (one
20 * call_sv per statement, paid by the process that asked for it) and one rule
21 * that C could state and Perl cannot enforce:
22 *
23 * "Neither callback may croak."
24 *
25 * A C consumer honours that by construction. A Perl one dies on a typo, and a
26 * die propagating out of here would surface inside an unrelated statement's
27 * settle. So both shims run under G_EVAL and turn a death into a warning
28 * naming which half died: an observer is a bystander, and a broken bystander
29 * must not fail the statement it was only watching.
30 *
31 * RE-ENTRANCY. The first thing anyone builds on this is a query log, and the
32 * obvious place to write a query log is a database - which runs a statement,
33 * from inside the observer for a statement, for ever. The C door has the same
34 * shape but an XS consumer is writing C and can see it coming; a Perl one is
35 * writing five lines in a config file. So the shims hold a depth flag and
36 * skip the callbacks for any statement issued from inside one. An observer
37 * therefore cannot see its own queries, which is the only useful reading of
38 * what it should see anyway.
39 *
40 * Included after dbil_obs.h, which holds the table it registers into.
41 */
42  
43 #ifndef DBIL_OBS_PERL_H
44 #define DBIL_OBS_PERL_H
45  
46 #include "dbil_obs.h"
47  
48 typedef struct { SV *start; SV *done; } dbil_obs_perl;
49  
50 /* Non-zero while a Perl observer callback is on the stack. Process-wide, like
51 * the registration it guards, and not thread-local: an observer that reaches
52 * the database is a bug being contained, not a feature being scheduled. */
53 static int DBIL_OBS_PERL_IN = 0;
54  
55 /* The token a SKIPPED start hands back, so that its done can be skipped too.
56 * It has to be distinguishable from NULL, because NULL is what a start that
57 * really ran returns when the callback returned undef - and that one's done
58 * must still fire. Without the distinction a re-entrant statement gets a done
59 * with no start, which breaks the one-for-one pairing the whole token design
60 * rests on and hands the callback a token it was never given. Its address is
61 * the value; it is never dereferenced. */
62 static const char DBIL_OBS_PERL_SKIP_TOKEN = 0;
63 #define DBIL_OBS_PERL_SKIPPED ((void *)&DBIL_OBS_PERL_SKIP_TOKEN)
64  
65 /* The start shim. `sql` is the PREPARED statement and `nbind` the number of
66 * bind values; the values themselves are deliberately not here (see the note
67 * in dbil_obs.h - the placeholders sit exactly where the literal data would).
68 * Whatever the callback returns becomes the token: any Perl scalar, kept
69 * until done, which frees it. */
70 11 static void *dbil_obs_perl_start(pTHX_ int is_query, const char *sql,
71 STRLEN sql_len, int nbind, void *ud) {
72 11 dbil_obs_perl *p = (dbil_obs_perl *)ud;
73 11 SV *tok = NULL;
74 11 dSP;
75 int count;
76 11 ▶ if (!p || !p->start) return DBIL_OBS_PERL_SKIPPED;
Branch: 2 branches
BranchTrueFalseTotal
if (!p || !p->start) return DBIL_OBS_PERL_SKIPPED; 11 0 11
if (!p || !p->start) return DBIL_OBS_PERL_SKIPPED; 0 11 11
77 11 ▶ if (DBIL_OBS_PERL_IN) return DBIL_OBS_PERL_SKIPPED; /* re-entered */
Branch: 1 branch
BranchTrueFalseTotal
if (DBIL_OBS_PERL_IN) return DBIL_OBS_PERL_SKIPPED; /* re-entered */ 3 8 11
78 8 DBIL_OBS_PERL_IN++;
79 8 ENTER; SAVETMPS;
80 8 ▶ PUSHMARK(SP); EXTEND(SP, 3);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 3); 0 8 8
PUSHMARK(SP); EXTEND(SP, 3); 0 8 8
81 8 mPUSHi(is_query);
82 8 mPUSHp(sql, sql_len);
83 8 mPUSHi(nbind);
84 8 PUTBACK;
85 8 count = call_sv(p->start, G_SCALAR | G_EVAL);
86 8 SPAGAIN;
87 8 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 8 0 8
if (SvTRUE(ERRSV)) { 1 7 8
88 1 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 1 0 1
89 2 ▶ warn("DBIx::Loop: on_exec start callback died: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("DBIx::Loop: on_exec start callback died: %s", SvPV_nolen(ERRSV)); 1 0 1
90 }
91 7 ▶ else if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) { 7 0 7
92 7 SV *r = POPs;
93 7 ▶ if (SvOK(r)) tok = newSVsv(r); /* +1; the done shim frees it */
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(r)) tok = newSVsv(r); /* +1; the done shim frees it */ 7 0 7
94 }
95 8 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 8 0 8
96 8 DBIL_OBS_PERL_IN--;
97 8 return (void *)tok;
98 }
99  
100 /* The done shim. Fires for every start that ran, so it is also where the
101 * token dies - including when the caller registered no done callback.
102 *
103 * A start skipped for re-entrancy is skipped here too: there is nothing to
104 * report about a statement nobody was told had begun, and reporting it anyway
105 * would break the one-done-per-start pairing that makes a token worth
106 * carrying. */
107 11 static void dbil_obs_perl_done(pTHX_ void *token, SV *res, SV *err, void *ud) {
108 11 dbil_obs_perl *p = (dbil_obs_perl *)ud;
109 SV *tok;
110 11 ▶ if (token == DBIL_OBS_PERL_SKIPPED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (token == DBIL_OBS_PERL_SKIPPED) return; 3 8 11
111 8 tok = (SV *)token;
112 8 ▶ if (p && p->done && !DBIL_OBS_PERL_IN) {
Branch: 3 branches
BranchTrueFalseTotal
if (p && p->done && !DBIL_OBS_PERL_IN) { 8 0 8
if (p && p->done && !DBIL_OBS_PERL_IN) { 8 0 8
if (p && p->done && !DBIL_OBS_PERL_IN) { 8 0 8
113 8 dSP;
114 8 DBIL_OBS_PERL_IN++;
115 8 ENTER; SAVETMPS;
116 8 ▶ PUSHMARK(SP); EXTEND(SP, 3);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 3); 0 8 8
PUSHMARK(SP); EXTEND(SP, 3); 0 8 8
117 8 ▶ PUSHs(tok ? sv_2mortal(newSVsv(tok)) : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(tok ? sv_2mortal(newSVsv(tok)) : &PL_sv_undef); 7 1 8
118 8 ▶ PUSHs(res ? res : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(res ? res : &PL_sv_undef); 7 1 8
119 8 ▶ PUSHs(err ? err : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(err ? err : &PL_sv_undef); 1 7 8
120 8 PUTBACK;
121 8 call_sv(p->done, G_DISCARD | G_EVAL);
122 8 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 7 1 8
123 8 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 8 0 8
if (SvTRUE(ERRSV)) 1 7 8
124 2 ▶ warn("DBIx::Loop: on_exec done callback died: %s",
Branch: 1 branch
BranchTrueFalseTotal
warn("DBIx::Loop: on_exec done callback died: %s", 1 0 1
125 SvPV_nolen(ERRSV));
126 8 DBIL_OBS_PERL_IN--;
127 }
128 8 ▶ if (tok) SvREFCNT_dec(tok);
Branch: 1 branch
BranchTrueFalseTotal
if (tok) SvREFCNT_dec(tok); 7 1 8
129 }
130  
131 /* DBIx::Loop->on_exec(\&start, \&done). A non-coderef croaks here, in the
132 * caller's own frame: that is a mistake at registration time, and finding out
133 * about it during an unrelated statement would be far worse. `done` is
134 * optional. */
135 4 static int dbil_obs_add_perl(pTHX_ SV *start, SV *done) {
136 dbil_obs_perl *p;
137 4 ▶ int have_done = (done && SvOK(done));
Branch: 2 branches
BranchTrueFalseTotal
int have_done = (done && SvOK(done)); 4 0 4
int have_done = (done && SvOK(done)); 2 2 4
138 4 ▶ if (!(start && SvROK(start) && SvTYPE(SvRV(start)) == SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(start && SvROK(start) && SvTYPE(SvRV(start)) == SVt_PVCV)) 4 0 4
if (!(start && SvROK(start) && SvTYPE(SvRV(start)) == SVt_PVCV)) 3 1 4
if (!(start && SvROK(start) && SvTYPE(SvRV(start)) == SVt_PVCV)) 0 3 3
139 1 croak("DBIx::Loop->on_exec: "
140 "the start callback must be a code reference");
141 3 ▶ if (have_done && !(SvROK(done) && SvTYPE(SvRV(done)) == SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (have_done && !(SvROK(done) && SvTYPE(SvRV(done)) == SVt_PVCV)) 2 1 3
if (have_done && !(SvROK(done) && SvTYPE(SvRV(done)) == SVt_PVCV)) 1 1 2
if (have_done && !(SvROK(done) && SvTYPE(SvRV(done)) == SVt_PVCV)) 0 1 1
142 1 croak("DBIx::Loop->on_exec: "
143 "the done callback must be a code reference");
144 2 Newxz(p, 1, dbil_obs_perl);
145 2 p->start = newSVsv(start);
146 2 ▶ p->done = have_done ? newSVsv(done) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
p->done = have_done ? newSVsv(done) : NULL; 1 1 2
147 /* the done shim is registered either way: it owns freeing the token */
148 2 ▶ if (!dbil_obs_add(aTHX_ dbil_obs_perl_start, dbil_obs_perl_done, p)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dbil_obs_add(aTHX_ dbil_obs_perl_start, dbil_obs_perl_done, p)) { 0 2 2
149 0 SvREFCNT_dec(p->start);
150 0 ▶ if (p->done) SvREFCNT_dec(p->done);
Branch: 1 branch
BranchTrueFalseTotal
if (p->done) SvREFCNT_dec(p->done); 0 0 0
151 0 Safefree(p);
152 0 return 0;
153 }
154 2 return 1;
155 }
156  
157 #endif /* DBIL_OBS_PERL_H */
« include/dbil_future.h ↑ Summary xs/future.xs »