-
Notifications
You must be signed in to change notification settings - Fork 3
/
ocitrace.c
1597 lines (1349 loc) · 44.3 KB
/
ocitrace.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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* {{{
This file is part of libtraceproc - a library for tracing Pro*C/OCI calls
Copyright (C) 2013 Georg Sauthoff <mail@georg.so>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}}} */
#if defined(__linux__)
#define _GNU_SOURCE
#endif
#include <dlfcn.h>
#include "ocitrace.h"
#include <stdbool.h>
#include <stdlib.h>
#include <search.h>
#include <time.h>
#include <ociap.h>
#include "ret_check.h"
#include "intercept.h"
#include "trace.h"
#include "timespec.h"
#include "stats.h"
#include "trap.h"
// XXX change STATS_BEGIN such that counts are incremented before
static bool intercept = false;
static bool gory = false;
static bool sql = false;
static bool count = false;
static bool leak_check = false;
struct Fn_Table {
sword (*OCIEnvInit)(OCIEnv **envp, ub4 mode,
size_t xtramem_sz, void **usrmempp);
sword (*OCIEnvCreate)(OCIEnv **envp, ub4 mode, void *ctxp,
void *(*malocfp)(void *ctxp, size_t size),
void *(*ralocfp)(void *ctxp, void *memptr, size_t newsize),
void (*mfreefp)(void *ctxp, void *memptr), size_t xtramem_sz,
void **usrmempp);
sword (*OCIEnvNlsCreate)(OCIEnv **envp, ub4 mode, void *ctxp,
void *(*malocfp)(void *ctxp, size_t size),
void *(*ralocfp)(void *ctxp, void *memptr, size_t newsize),
void (*mfreefp)(void *ctxp, void *memptr), size_t xtramem_sz,
void **usrmempp, ub2 charset, ub2 ncharset);
sword (*OCILogon)(OCIEnv *envhp, OCIError *errhp, OCISvcCtx **svchp,
const OraText *username, ub4 uname_len, const OraText *password,
ub4 passwd_len, const OraText *dbname, ub4 dbname_len);
sword (*OCILogon2)(OCIEnv *envhp, OCIError *errhp, OCISvcCtx **svchp,
const OraText *username, ub4 uname_len, const OraText *password,
ub4 passwd_len, const OraText *dbname, ub4 dbname_len, ub4 mode);
sword (*OCIStmtPrepare)(OCIStmt *stmtp, OCIError *errhp,
const OraText *stmt, ub4 stmt_len, ub4 language, ub4 mode);
sword (*OCIStmtPrepare2)(OCISvcCtx *svchp, OCIStmt **stmtp,
OCIError *errhp, const OraText *stmt, ub4 stmt_len,
const OraText *key, ub4 key_len, ub4 language, ub4 mode);
sword (*OCIBindByPos)(OCIStmt *stmtp, OCIBind **bindp, OCIError *errhp,
ub4 position, void *valuep, sb4 value_sz,
ub2 dty, void *indp, ub2 *alenp, ub2 *rcodep,
ub4 maxarr_len, ub4 *curelep, ub4 mode);
sword (*OCIBindByName)(OCIStmt *stmtp, OCIBind **bindp, OCIError *errhp,
const OraText *placeholder, sb4 placeh_len,
void *valuep, sb4 value_sz, ub2 dty,
void *indp, ub2 *alenp, ub2 *rcodep,
ub4 maxarr_len, ub4 *curelep, ub4 mode);
sword (*OCIDefineByPos)(OCIStmt *stmtp, OCIDefine **defnp,
OCIError *errhp,
ub4 position, void *valuep, sb4 value_sz, ub2 dty,
void *indp, ub2 *rlenp, ub2 *rcodep, ub4 mode);
sword (*OCITerminate)(ub4 mode);
sword (*OCIStmtExecute)(OCISvcCtx *svchp, OCIStmt *stmtp,
OCIError *errhp, ub4 iters, ub4 rowoff, const OCISnapshot *snap_in,
OCISnapshot *snap_out, ub4 mode);
sword (*OCIStmtFetch)(OCIStmt *stmtp, OCIError *errhp, ub4 nrows,
ub2 orientation, ub4 mode);
sword (*OCIStmtFetch2)(OCIStmt *stmtp, OCIError *errhp, ub4 nrows,
ub2 orientation, sb4 scrollOffset, ub4 mode);
sword (*OCITransCommit)(OCISvcCtx *svchp, OCIError *errhp, ub4 flags);
sword (*OCIHandleAlloc)(const void *parenth, void **hndlpp,
const ub4 type, const size_t xtramem_sz, void **usrmempp);
sword (*OCIHandleFree)(void *hndlp, const ub4 type);
sword (*OCIDescriptorAlloc)(const void *parenth, void **descpp,
const ub4 type, const size_t xtramem_sz, void **usrmempp);
sword (*OCIDescriptorFree)(void *descp, const ub4 type);
sword (*OCIArrayDescriptorAlloc)(const void *parenth, void **descpp,
const ub4 type, ub4 array_size,
const size_t xtramem_sz, void **usrmempp);
sword (*OCIArrayDescriptorFree)(void **descp, const ub4 type);
sword (*OCISessionBegin)(OCISvcCtx *svchp, OCIError *errhp,
OCISession *usrhp, ub4 credt, ub4 mode);
sword (*OCISessionEnd)(OCISvcCtx *svchp, OCIError *errhp,
OCISession *usrhp, ub4 mode);
sword (*OCIAttrGet)(const void *trgthndlp, ub4 trghndltyp,
void *attributep, ub4 *sizep, ub4 attrtype,
OCIError *errhp);
sword (*OCIAttrSet)(void *trgthndlp, ub4 trghndltyp, void *attributep,
ub4 size, ub4 attrtype, OCIError *errhp);
sword (*OCIServerAttach)(OCIServer *srvhp, OCIError *errhp,
const OraText *dblink, sb4 dblink_len, ub4 mode);
sword (*OCIServerDetach)(OCIServer *srvhp, OCIError *errhp, ub4 mode);
sword (*OCIInitialize)(ub4 mode, void *ctxp,
void *(*malocfp)(void *ctxp, size_t size),
void *(*ralocfp)(void *ctxp, void *memptr, size_t newsize),
void (*mfreefp)(void *ctxp, void *memptr) );
sword (*OCILobWrite)(OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *locp,
ub4 *amtp, ub4 offset, void *bufp, ub4 buflen,
ub1 piece, void *ctxp, OCICallbackLobWrite cbfp,
ub2 csid, ub1 csfrm);
sword (*OCIParamGet)(const void *hndlp, ub4 htype, OCIError *errhp,
void **parmdpp, ub4 pos);
sword (* OCIParamSet)(void *hdlp, ub4 htyp, OCIError *errhp, const void *dscp,
ub4 dtyp, ub4 pos);
sword (*OCITransStart)(OCISvcCtx *svchp, OCIError *errhp,
uword timeout, ub4 flags );
sword (*OCITransRollback)(OCISvcCtx *svchp, OCIError *errhp, ub4 flags);
sword (*OCISessionPoolCreate)(OCIEnv *envhp, OCIError *errhp, OCISPool *spoolhp,
OraText **poolName, ub4 *poolNameLen,
const OraText *connStr, ub4 connStrLen,
ub4 sessMin, ub4 sessMax, ub4 sessIncr,
OraText *userid, ub4 useridLen,
OraText *password, ub4 passwordLen,
ub4 mode);
sword (*OCISessionPoolDestroy)(OCISPool *spoolhp,
OCIError *errhp,
ub4 mode);
sword (*OCISessionGet)(OCIEnv *envhp, OCIError *errhp, OCISvcCtx **svchp,
OCIAuthInfo *authhp,
OraText *poolName, ub4 poolName_len,
const OraText *tagInfo, ub4 tagInfo_len,
OraText **retTagInfo, ub4 *retTagInfo_len,
boolean *found, ub4 mode);
sword (*OCISessionRelease)(OCISvcCtx *svchp, OCIError *errhp,
OraText *tag, ub4 tag_len,
ub4 mode);
};
typedef struct Fn_Table Fn_Table;
static Fn_Table fn_table = {0};
enum Func {
FN_OCIENVINIT,
FN_OCIENVCREATE,
FN_OCIENVNLSCREATE,
FN_OCILOGON,
FN_OCILOGON2,
FN_OCISTMTPREPARE,
FN_OCISTMTPREPARE2,
FN_OCIBINDBYPOS,
FN_OCIBINDBYNAME,
FN_OCIDEFINEBYPOS,
FN_OCITERMINATE,
FN_OCISTMTEXECUTE,
FN_OCISTMTFETCH,
FN_OCISTMTFETCH2,
FN_OCITRANSCOMMIT,
FN_OCIHANDLEALLOC,
FN_OCIHANDLEFREE,
FN_OCIDESCRIPTORALLOC,
FN_OCIDESCRIPTORFREE,
FN_OCIARRAYDESCRIPTORALLOC,
FN_OCIARRAYDESCRIPTORFREE,
FN_OCISESSIONBEGIN,
FN_OCISESSIONEND,
FN_OCIATTRGET,
FN_OCIATTRSET,
FN_OCISERVERATTACH,
FN_OCISERVERDETACH,
FN_OCIINITIALIZE,
FN_OCILOBWRITE,
FN_OCIPARAMGET,
FN_OCIPARAMSET,
FN_OCITRANSSTART,
FN_OCITRANSROLLBACK,
FN_OCISESSIONGET,
FN_OCISESSIONPOOLCREATE,
FN_OCISESSIONPOOLDESTROY,
FN_OCISESSIONRELEASE,
FN_SIZE
};
typedef enum Func Func;
static const char *func_str[] = {
"OCIEnvInit",
"OCIEnvCreate",
"OCIEnvNlsCreate",
"OCILogon",
"OCILogon2",
"OCIStmtPrepare",
"OCIStmtPrepare2",
"OCIBindByPos",
"OCIBindByName",
"OCIDefineByPos",
"OCITerminate",
"OCIStmtExecute",
"OCIStmtFetch",
"OCIStmtFetch2",
"OCITransCommit",
"OCIHandleAlloc",
"OCIHandleFree",
"OCIDescriptorAlloc",
"OCIDescriptorFree",
"OCIArrayDescriptorAlloc",
"OCIArrayDescriptorFree",
"OCISessionBegin",
"OCISessionEnd",
"OCIAttrGet",
"OCIAttrSet",
"OCIServerAttach",
"OCIServerDetach",
"OCIInitialize",
"OCILobWrite",
"OCIParamGet",
"OCIParamSet",
"OCITransStart",
"OCITransRollback",
"OCISessionEnd",
"OCISessionGet",
"OCISessionPoolCreate",
"OCISessionPoolDestroy",
"OCISessionRelease",
0
};
static Stats stats = {0};
#include "ocierr.c"
static void print_retcode(int rc)
{
if (!gory)
return;
for (const Oci_Err *e = oci_errs;e->str; ++e) {
if (e->val == rc) {
tprintf(" => %s (%d)\n", e->str, rc);
return;
}
}
tprintf(" => ERROR: unknown ret code (%d)\n", rc);
}
struct Flag_Str {
unsigned flag;
char *str;
};
typedef struct Flag_Str Flag_Str;
int flags2str(unsigned flags, const Flag_Str *a,
char *buffer, size_t buffer_size)
{
if (!buffer_size)
return -1;
if (flags == OCI_DEFAULT) {
strncpy(buffer, " OCI_DEFAULT", buffer_size-1);
return 0;
}
size_t written = 0;
for (const Flag_Str *x = a; x->str; ++x) {
if (flags & x->flag) {
written += strlen(x->str) + 3;
if (written > buffer_size)
return -2;
strcat(buffer, "| ");
strcat(buffer, x->str);
}
}
return 0;
}
const char *flag2str(unsigned flag, const Flag_Str *a)
{
for (const Flag_Str *x = a; x->str; ++x) {
if (flag == x->flag) {
return x->str;
}
}
return "FLAGNOTFOUND";
}
sword OCIEnvInit(OCIEnv **envp, ub4 mode, size_t xtramem_sz,
void **usrmempp)
{
char buffer[128] = {0};
static const Flag_Str mode_str[] = {
//{ OCI_DEFAULT, "OCI_DEFAULT" },
{ OCI_ENV_NO_MUTEX, "OCI_ENV_NO_MUTEX" },
{ OCI_ENV_NO_UCB, "OCI_ENV_NO_UCB" },
{ 0, 0}
};
if (gory) {
flags2str(mode, mode_str, buffer, 128);
tprintf("OCIEnvInit(%s)", buffer+2);
}
traceproc_trap("OCIEnvInit", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIEnvInit)(envp, mode, xtramem_sz, usrmempp);
STATS_END(count, FN_OCIENVINIT, 0);
traceproc_trap("OCIEnvInit", "", false, false);
print_retcode(ret);
return ret;
}
static const Flag_Str env_mode_str[] = {
//{ OCI_DEFAULT, "OCI_DEFAULT" },
{ OCI_THREADED, "OCI_THREADED" },
{ OCI_OBJECT, "OCI_OBJECT" },
{ OCI_EVENTS, "OCI_EVENTS" },
{ OCI_NO_UCB, "OCI_NO_UCB" },
{ OCI_NO_MUTEX, "OCI_NO_MUTEX" },
{ OCI_NEW_LENGTH_SEMANTICS, "OCI_NEW_LENGTH_SEMANTICS" },
{ OCI_SUPPRESS_NLS_VALIDATION, "OCI_SUPPRESS_NLS_VALIDATION" },
{ OCI_NCHAR_LITERAL_REPLACE_ON, "OCI_NCHAR_LITERAL_REPLACE_ON" },
{ OCI_NCHAR_LITERAL_REPLACE_OFF, "OCI_NCHAR_LITERAL_REPLACE_OFF" },
{ OCI_ENABLE_NLS_VALIDATION, "OCI_ENABLE_NLS_VALIDATION" },
{ 0, 0}
};
sword OCIEnvCreate(OCIEnv **envp, ub4 mode, void *ctxp,
void *(*malocfp)(void *ctxp, size_t size),
void *(*ralocfp)(void *ctxp, void *memptr, size_t newsize),
void (*mfreefp)(void *ctxp, void *memptr), size_t xtramem_sz,
void **usrmempp)
{
char buffer[128] = {0};
if (gory) {
flags2str(mode, env_mode_str, buffer, 128);
tprintf("OCIEnvCreate(%s)", buffer+2);
}
traceproc_trap("OCIEnvCreate", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIEnvCreate)(envp, mode, ctxp,
malocfp,
ralocfp,
mfreefp, xtramem_sz,
usrmempp);
STATS_END(count, FN_OCIENVCREATE, 0);
traceproc_trap("OCIEnvCreate", "", false, false);
print_retcode(ret);
return ret;
}
sword OCIEnvNlsCreate(OCIEnv **envp, ub4 mode, void *ctxp,
void *(*malocfp)(void *ctxp, size_t size),
void *(*ralocfp)(void *ctxp, void *memptr, size_t newsize),
void (*mfreefp)(void *ctxp, void *memptr), size_t xtramem_sz,
void **usrmempp, ub2 charset, ub2 ncharset)
{
char buffer[128] = {0};
if (gory) {
flags2str(mode, env_mode_str, buffer, 128);
tprintf("OCIEnvCreate(%s)", buffer+2);
}
traceproc_trap("OCIEnvNlsCreate", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIEnvNlsCreate)(envp, mode, ctxp,
malocfp,
ralocfp,
mfreefp, xtramem_sz,
usrmempp, charset, ncharset);
STATS_END(count, FN_OCIENVNLSCREATE, 0);
traceproc_trap("OCIEnvNlsCreate", "", false, false);
print_retcode(ret);
return ret;
}
sword OCILogon(OCIEnv *envhp, OCIError *errhp, OCISvcCtx **svchp,
const OraText *username, ub4 uname_len, const OraText *password,
ub4 passwd_len, const OraText *dbname, ub4 dbname_len)
{
if (gory) {
tprintf("OCILogon(%.*s, %.*s, %.*s) ", uname_len, username,
passwd_len, password, dbname_len, dbname);
}
traceproc_trap("OCILogon", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCILogon)(envhp, errhp, svchp,
username, uname_len, password,
passwd_len, dbname, dbname_len);
STATS_END(count, FN_OCILOGON, 0);
traceproc_trap("OCILogon", "", false, false);
print_retcode(ret);
return ret;
}
sword OCILogon2(OCIEnv *envhp, OCIError *errhp, OCISvcCtx **svchp,
const OraText *username, ub4 uname_len, const OraText *password,
ub4 passwd_len, const OraText *dbname, ub4 dbname_len, ub4 mode)
{
if (gory) {
tprintf("OCILogon2(%.*s, %.*s, %.*s) ", uname_len, username,
passwd_len, password, dbname_len, dbname);
}
traceproc_trap("OCILogon2", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCILogon2)(envhp, errhp, svchp,
username, uname_len, password,
passwd_len, dbname, dbname_len, mode);
STATS_END(count, FN_OCILOGON2, 0);
traceproc_trap("OCILogon2", "", false, false);
print_retcode(ret);
return ret;
}
sword OCIStmtPrepare(OCIStmt *stmtp, OCIError *errhp, const OraText *stmt,
ub4 stmt_len, ub4 language, ub4 mode)
{
char buffer[128] = {0};
if (gory) {
flags2str(mode, env_mode_str, buffer, 128);
tprintf("OCIStmtPrepare(%.*s)", stmt_len, stmt);
}
traceproc_trap("OCIStmtPrepare", (const char*)stmt, false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIStmtPrepare)(stmtp, errhp, stmt,
stmt_len, language, mode);
STATS_END(count, FN_OCISTMTPREPARE, 0);
traceproc_trap("OCIStmtPrepare", (const char*)stmt, false, false);
print_retcode(ret);
return ret;
}
sword OCIStmtPrepare2(OCISvcCtx *svchp, OCIStmt **stmtp, OCIError *errhp,
const OraText *stmt, ub4 stmt_len, const OraText *key, ub4 key_len,
ub4 language, ub4 mode)
{
char buffer[128] = {0};
if (gory) {
flags2str(mode, env_mode_str, buffer, 128);
tprintf("OCIStmtPrepare2(%.*s, %s, key=%.*s)", stmt_len, stmt,
buffer + 2,
key ? key_len : 0,
key? (const char*) key : ""
);
}
traceproc_trap("OCIStmtPrepare2", (const char*)stmt, false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIStmtPrepare2)(svchp, stmtp, errhp,
stmt, stmt_len, key, key_len,
language, mode);
STATS_END(count, FN_OCISTMTPREPARE2, 0);
traceproc_trap("OCIStmtPrepare2", (const char*)stmt, false, false);
print_retcode(ret);
return ret;
}
const Flag_Str bind_mode_str[] = {
{ OCI_DEFAULT, "OCI_DEFAULT" },
{ OCI_BIND_SOFT, "OCI_BIND_SOFT" },
{ OCI_DATA_AT_EXEC, "OCI_DATA_AT_EXEC" },
{ OCI_IOV, "OCI_IOV" },
{ 0, 0 }
};
sword OCIBindByPos(OCIStmt *stmtp, OCIBind **bindp, OCIError *errhp,
ub4 position, void *valuep, sb4 value_sz, ub2 dty, void *indp,
ub2 *alenp, ub2 *rcodep, ub4 maxarr_len, ub4 *curelep, ub4 mode)
{
if (gory)
tprintf("OCIBindByPos(pos=%2u, %s)", position,
flag2str(mode, bind_mode_str));
traceproc_trap("OCIBindByPos", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIBindByPos)(stmtp, bindp, errhp,
position, valuep, value_sz, dty, indp,
alenp, rcodep, maxarr_len, curelep, mode);
STATS_END(count, FN_OCIBINDBYPOS, 0);
traceproc_trap("OCIBindByPos", "", false, false);
print_retcode(ret);
return ret;
}
sword OCIBindByName(OCIStmt *stmtp, OCIBind **bindp, OCIError *errhp,
const OraText *placeholder, sb4 placeh_len, void *valuep,
sb4 value_sz, ub2 dty, void *indp, ub2 *alenp, ub2 *rcodep,
ub4 maxarr_len, ub4 *curelep, ub4 mode)
{
if (gory)
tprintf("OCIBindByName(name=%s, %s)", placeholder,
flag2str(mode, bind_mode_str));
traceproc_trap("OCIBindByName", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIBindByName)(stmtp, bindp, errhp,
placeholder, placeh_len, valuep,
value_sz, dty, indp, alenp, rcodep,
maxarr_len, curelep, mode);
STATS_END(count, FN_OCIBINDBYNAME, 0);
traceproc_trap("OCIBindByName", "", false, false);
print_retcode(ret);
return ret;
}
sword OCIDefineByPos(OCIStmt *stmtp, OCIDefine **defnp, OCIError *errhp,
ub4 position, void *valuep, sb4 value_sz, ub2 dty,
void *indp, ub2 *rlenp, ub2 *rcodep, ub4 mode)
{
static const Flag_Str mode_str[] = {
{ OCI_DEFAULT, "OCI_DEFAULT" },
{ OCI_DEFINE_SOFT, "OCI_DEFINE_SOFT" },
{ OCI_DYNAMIC_FETCH, "OCI_DYNAMIC_FETCH" },
{ OCI_IOV, "OCI_IOV" },
{ 0, 0 }
};
if (gory)
tprintf("OCIDefineByPos(pos=%2u, %s)", position,
flag2str(mode, mode_str)
);
traceproc_trap("OCIDefineByPos", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIDefineByPos)(stmtp, defnp, errhp,
position, valuep, value_sz, dty,
indp, rlenp, rcodep, mode);
STATS_END(count, FN_OCIDEFINEBYPOS, 0);
traceproc_trap("OCIDefineByPos", "", false, false);
print_retcode(ret);
return ret;
}
sword OCITerminate(ub4 mode)
{
if (gory)
tprintf("OCITerminate()");
traceproc_trap("OCITerminate", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCITerminate)(mode);
STATS_END(count, FN_OCITERMINATE, 0);
traceproc_trap("OCITerminate", "", false, false);
print_retcode(ret);
return ret;
}
sword OCIStmtExecute(OCISvcCtx *svchp, OCIStmt *stmtp, OCIError *errhp,
ub4 iters, ub4 rowoff, const OCISnapshot *snap_in,
OCISnapshot *snap_out, ub4 mode)
{
static const Flag_Str mode_str[] = {
{ OCI_BATCH_ERRORS, "OCI_BATCH_ERRORS" },
{ OCI_COMMIT_ON_SUCCESS, "OCI_COMMIT_ON_SUCCESS" },
{ OCI_DEFAULT, "OCI_DEFAULT" },
{ OCI_DESCRIBE_ONLY, "OCI_DESCRIBE_ONLY" },
{ OCI_EXACT_FETCH, "OCI_EXACT_FETCH" },
{ OCI_PARSE_ONLY, "OCI_PARSE_ONLY" },
{ OCI_STMT_SCROLLABLE_READONLY, "OCI_STMT_SCROLLABLE_READONLY" },
{ 0, 0 }
};
if (gory)
tprintf("OCIStmtExecute(iters=%u, rowoff=%u, %s)", iters, rowoff,
flag2str(mode, mode_str));
traceproc_trap("OCIStmtExecute", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIStmtExecute)(svchp, stmtp, errhp,
iters, rowoff, snap_in,
snap_out, mode);
STATS_END(count, FN_OCISTMTEXECUTE, 0);
traceproc_trap("OCIStmtExecute", "", false, false);
print_retcode(ret);
return ret;
}
static const Flag_Str orient_str[] = {
{ OCI_DEFAULT, "OCI_DEFAULT" },
{ OCI_FETCH_CURRENT, "OCI_FETCH_CURRENT" },
{ OCI_FETCH_NEXT, "OCI_FETCH_NEXT" },
{ OCI_FETCH_FIRST, "OCI_FETCH_FIRST" },
{ OCI_FETCH_LAST, "OCI_FETCH_LAST" },
{ OCI_FETCH_PRIOR, "OCI_FETCH_PRIOR" },
{ OCI_FETCH_ABSOLUTE, "OCI_FETCH_ABSOLUTE" },
{ OCI_FETCH_RELATIVE, "OCI_FETCH_RELATIVE" },
{ 0, 0}
};
sword OCIStmtFetch(OCIStmt *stmtp, OCIError *errhp, ub4 nrows,
ub2 orientation, ub4 mode)
{
if (gory)
tprintf("OCIStmtFetch(rows=%u, %s)", nrows,
flag2str(orientation, orient_str));
traceproc_trap("OCIStmtFetch", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIStmtFetch)(stmtp, errhp, nrows,
orientation, mode);
STATS_END(count, FN_OCISTMTFETCH, 0);
traceproc_trap("OCIStmtFetch", "", false, false);
print_retcode(ret);
return ret;
}
sword OCIStmtFetch2(OCIStmt *stmtp, OCIError *errhp, ub4 nrows,
ub2 orientation, sb4 scrollOffset, ub4 mode)
{
if (gory)
tprintf("OCIStmtFetch2(rows=%u, %s, scrolloffset=%u)",
nrows,
flag2str(orientation, orient_str),
scrollOffset);
traceproc_trap("OCIStmtFetch2", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIStmtFetch2)(stmtp, errhp, nrows,
orientation, scrollOffset, mode);
STATS_END(count, FN_OCISTMTFETCH2, 0);
traceproc_trap("OCIStmtFetch2", "", false, false);
print_retcode(ret);
return ret;
}
sword OCITransCommit(OCISvcCtx *svchp, OCIError *errhp, ub4 flags)
{
char buffer[1024] = {0};
static const Flag_Str flag_str[] = {
{ OCI_TRANS_TWOPHASE, "OCI_TRANS_TWOPHASE" },
{ OCI_TRANS_WRITEIMMED, "OCI_TRANS_WRITEIMMED" },
{ OCI_TRANS_WRITEBATCH, "OCI_TRANS_WRITEBATCH" },
{ OCI_TRANS_WRITEWAIT, "OCI_TRANS_WRITEWAIT" },
{ OCI_TRANS_WRITENOWAIT, "OCI_TRANS_WRITENOWAIT" },
{ 0, 0}
};
flags2str(flags, flag_str, buffer, 1024);
if (gory)
tprintf("OCITransCommit(%s)", buffer+2);
traceproc_trap("OCITransCommit", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCITransCommit)(svchp, errhp, flags);
STATS_END(count, FN_OCITRANSCOMMIT, 0);
traceproc_trap("OCITransCommit", "", false, false);
print_retcode(ret);
return ret;
}
static void *handle_tree = 0;
static size_t handle_count = 0;
struct Handle_Info {
void *ptr;
char name[32];
bool freed;
unsigned type;
};
typedef struct Handle_Info Handle_Info;
static int handle_tree_cmp(const void *a, const void *b)
{
const Handle_Info *x = a;
const Handle_Info *y = b;
if (x->ptr < y->ptr)
return -1;
if (x->ptr > y->ptr)
return 1;
return 0;
}
void handle_tree_check_leaky(const void *nodep, const VISIT which, const int depth)
{
if (!(which == postorder || which == leaf))
return;
const Handle_Info *info = *(const Handle_Info**) nodep;
if (!info->freed)
tprintf("ERROR: handle/desc leak - %s was not freed\n", info->name);
}
void handle_tree_check(void **tree, void **hndlpp,
const char *kind, const char *fn_name)
{
Handle_Info *e = 0;
Handle_Info q = {0};
if (intercept) {
if (leak_check) {
if (!hndlpp) {
tprintf("ERROR: NULL %s pointer", kind);
}
if (hndlpp && *hndlpp) {
q.ptr = *hndlpp;
strcpy(q.name, "XXX");
void *val = tfind(&q, tree, handle_tree_cmp);
Handle_Info *info = val ? *(Handle_Info**)val : 0;
if (info) {
if (!info->freed)
tprintf("ERROR: leak - %s %s was not freed yet\n",
kind,
info->name);
}
}
}
if (gory)
tprintf("%s(", fn_name);
}
}
void handle_tree_add(void **hndlpp, int ret, size_t *count, unsigned type,
const char *kind, void **tree)
{
if (intercept && hndlpp && *hndlpp && !ret) {
if (leak_check) {
Handle_Info q = {0};
q.ptr = *hndlpp;
strcpy(q.name, "XXX");
void *val = tfind(&q, tree, handle_tree_cmp);
Handle_Info *e = val ? (*(Handle_Info**)val) : 0;
if (e) {
if (e->freed) {
e->freed = false;
} else {
tprintf("ERROR: %s %s was not freed but still returned by OCI %p info\n", kind, e->name, e->ptr);
}
} else {
e = calloc(1, sizeof(Handle_Info));
e->ptr = *hndlpp;
tsearch(e, tree, handle_tree_cmp);
}
e->type = type;
snprintf(e->name, 32, "%s_%03zu type=%u", kind, (*count)++, type);
//*hndlpp);
tprintf("%s", e->name);
}
tprintf(" %p)", *hndlpp);
}
}
void handle_tree_check_free(void *hndlp, unsigned type, void **tree, const char *kind,
const char *fn_name)
{
if (!intercept)
return;
char name[64] = {0};
if (leak_check) {
Handle_Info q = {0};
Handle_Info *info = 0;
if (hndlp) {
q.ptr = hndlp;
void *val = tfind(&q, tree, handle_tree_cmp);
if (val)
info = *(Handle_Info**)val;
if (info) {
if (info->freed) {
tprintf("ERROR: double-free of %s %s\n", kind, info->name);
} else {
info->freed = true;
if (info->type != type) {
tprintf("%s %s was allocated with a different type (%u) != %u\n",
kind,
info->name, info->type, type);
snprintf(name, 64, "ERROR: %s type error", info->name);
} else {
snprintf(name, 64, "%s", info->name);
}
}
} else {
tprintf("WARN: Trying to free unknown %s\n", kind);
snprintf(name, 64, "WARN: %s %p is unknown", kind, hndlp);
}
}
}
if (gory)
tprintf("%s(%s %p)", fn_name, name, hndlp);
}
sword OCIHandleAlloc(const void *parenth, void **hndlpp, const ub4 type,
const size_t xtramem_sz, void **usrmempp)
{
handle_tree_check(&handle_tree, hndlpp, "handle",
"OCIHandleAlloc");
traceproc_trap("OCIHandleAlloc", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIHandleAlloc)(parenth, hndlpp, type,
xtramem_sz, usrmempp);
STATS_END(count, FN_OCIHANDLEALLOC, 0);
traceproc_trap("OCIHandleAlloc", "", false, false);
handle_tree_add(hndlpp, ret, &handle_count, type, "HANDLE", &handle_tree);
print_retcode(ret);
return ret;
}
sword OCIHandleFree(void *hndlp, const ub4 type)
{
handle_tree_check_free(hndlp, type, &handle_tree, "handle", "OCIHandleFree");
traceproc_trap("OCIHandleFree", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIHandleFree)(hndlp, type);
STATS_END(count, FN_OCIHANDLEFREE, 0);
traceproc_trap("OCIHandleFree", "", false, false);
print_retcode(ret);
return ret;
}
static void *descriptor_tree = 0;
static size_t descriptor_count = 0;
sword OCIDescriptorAlloc(const void *parenth, void **descpp,
const ub4 type, const size_t xtramem_sz, void **usrmempp)
{
handle_tree_check(&descriptor_tree, descpp, "descriptor",
"OCIDescriptorAlloc");
traceproc_trap("OCIDescriptorAlloc", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIDescriptorAlloc)(parenth, descpp, type,
xtramem_sz, usrmempp);
STATS_END(count, FN_OCIDESCRIPTORALLOC, 0);
traceproc_trap("OCIDescriptorAlloc", "", false, false);
handle_tree_add(descpp, ret, &descriptor_count, type, "DESC", &descriptor_tree);
print_retcode(ret);
return ret;
}
sword OCIDescriptorFree(void *descp, const ub4 type)
{
handle_tree_check_free(descp, type, &descriptor_tree, "descriptor", "OCIDescriptorFree");
traceproc_trap("OCIDescriptorFree", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIDescriptorFree)(descp, type);
STATS_END(count, FN_OCIDESCRIPTORFREE, 0);
traceproc_trap("OCIDescriptorFree", "", false, false);
print_retcode(ret);
return ret;
}
void *array_desc_tree = 0;
size_t array_desc_count = 0;
void res_tree_add(void **tree, size_t *count, void *ptr, unsigned type,
const char *kind, char *name, size_t name_size)
{
if (!intercept || !leak_check)
return;
if (!ptr) {
tprintf("ERROR: NULL pointer used for obtaining a %s\n", kind);
return;
}
Handle_Info q = {0};
q.ptr = ptr;
strcpy(q.name, "XXX");
void *val = tfind(&q, tree, handle_tree_cmp);
Handle_Info *e = val ? *(Handle_Info**)val : 0;
if (e) {
if (e->freed) {
e->freed = false;
} else {
tprintf("ERROR: resource leak - %s was not freed %p\n", e->name, e->ptr);
}
} else {
e = calloc(1, sizeof(Handle_Info));
e->ptr = ptr;
tsearch(e, tree, handle_tree_cmp);
}
e->type = type;
snprintf(e->name, 32, "%s_%03zu type=%u %p", kind, (*count)++, type,
ptr);
if (!name_size)
return;
strncpy(name, e->name, name_size);
}
void res_tree_del(void **tree, void *ptr, unsigned type, const char *kind,
char *name, size_t name_size)
{
if (!intercept || !leak_check)
return;
if (!ptr) {
tprintf("ERROR: NULL pointer used for freeing a %s\n", kind);
return;
}
Handle_Info q = {0};
q.ptr = ptr;
void *val = tfind(&q, tree, handle_tree_cmp);
Handle_Info *info = val ? *(Handle_Info**)val : 0;
if (info) {
if (info->freed) {
tprintf("ERROR: double-free of %s %s\n", kind, info->name);
} else {
info->freed = true;
if (info->type != type) {
tprintf("ERROR: %s %s was allocated with a different type (%u) != %u\n",
kind,
info->name, info->type, type);
} else {
snprintf(name, name_size, "%s", info->name);
}
}
} else {
tprintf("WARN: Trying to free unknown %s %p\n", kind, ptr);
}
}
sword OCIArrayDescriptorAlloc(const void *parenth, void **descpp,
const ub4 type, ub4 array_size,
const size_t xtramem_sz, void **usrmempp)
{
char name[32] = {0};
res_tree_add(&array_desc_tree, &array_desc_count, descpp, type, "arr_desc", name, 32);
if (gory)
tprintf("OCIArrayDescriptorAlloc(n=%u, %s, type=%u, %p)",
array_size, name, type, descpp);
traceproc_trap("OCIArrayDescriptorAlloc", "", false, true);
STATS_BEGIN(count);
sword ret = (*fn_table.OCIArrayDescriptorAlloc)(parenth, descpp,
type, array_size,