-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmongo_hook.go
838 lines (666 loc) · 21.6 KB
/
mongo_hook.go
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
package gorr
import (
"bytes"
"context"
"encoding/gob"
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"unsafe"
"github.com/brahma-adshonor/gohook"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
// need a way to tag object pointer
// 1. cookie to identify whether a pointer is created by hook
// --> false positive is possible by definition.
// 2. put pointer into a set
// --> garbage collection leads to stale pointer in the set(worse, if pointer is reallocated for the same type of object)
// mongo.Connect
// the reason to hook mongo.Connect is that:
// 1. we need to obtain the client object pointer
// 2. client object to build key to record all mongo call from collection
// hook for replay mode only
// mongo.Cursor: all public functions
// mongo.SingleResult: all public functions
// struct to hold client info
type clientHolder struct {
cookie1 uint64
client mongo.Client
opts *options.ClientOptions
cookie2 uint64
}
type CursorData struct {
Id int64
Err string
Data []bson.Raw
}
type cursorHolder struct {
cookie1 uint64
cur int64
cd CursorData
cursor mongo.Cursor
registry *bsoncodec.Registry
cookie2 uint64
}
type SingleResultData struct {
Err string
Data bson.Raw
}
type singleResultHolder struct {
cookie1 uint64
sd SingleResultData
ret mongo.SingleResult
registry *bsoncodec.Registry
cookie2 uint64
}
const (
cookieValue1 uint64 = 0x1badf00d2badf00d
cookieValue2 uint64 = 0x3badf00d4badf00d
)
func clientHolderFinalizer(c *clientHolder) {
c.cookie1 = 23
c.cookie2 = 23
}
func cursorHolderFinalizer(c *cursorHolder) {
c.cookie1 = 23
c.cookie2 = 23
}
func singleResultHolderFinalizer(c *singleResultHolder) {
c.cookie1 = 23
c.cookie2 = 23
}
func getClientHolder(c *mongo.Client) *clientHolder {
if c == nil {
return nil
}
var tmp clientHolder
h := (*clientHolder)(unsafe.Pointer(uintptr(unsafe.Pointer(c)) - unsafe.Offsetof(tmp.client)))
if h.cookie1 != cookieValue1 || h.cookie2 != cookieValue2 {
return nil
}
return h
}
func getCursorHolder(c *mongo.Cursor) *cursorHolder {
var tmp cursorHolder
if c == nil {
return nil
}
h := (*cursorHolder)(unsafe.Pointer(uintptr(unsafe.Pointer(c)) - unsafe.Offsetof(tmp.cursor)))
if h.cookie1 != cookieValue1 || h.cookie2 != cookieValue2 {
return nil
}
return h
}
func getSingleResultHolder(c *mongo.SingleResult) *singleResultHolder {
if c == nil {
return nil
}
var tmp singleResultHolder
h := (*singleResultHolder)(unsafe.Pointer(uintptr(unsafe.Pointer(c)) - unsafe.Offsetof(tmp.ret)))
if h.cookie1 != cookieValue1 || h.cookie2 != cookieValue2 {
return nil
}
return h
}
func getRegistryByClient(c *mongo.Client) *bsoncodec.Registry {
h := getClientHolder(c)
if h == nil {
return bson.DefaultRegistry
}
return h.opts.Registry
}
func buildKeyByClient(c *mongo.Client, op string, key string) string {
h := getClientHolder(c)
cred := h.opts.Auth
authMech := "notspecified"
authSource := "notspecified"
authName := "notspecified"
if cred != nil {
authName = cred.Username
authSource = cred.AuthSource
authMech = cred.AuthMechanism
}
dbinfo := fmt.Sprintf("mongo_hook_key@%s@%s_%s_%s@%s@%s",
strings.Join(h.opts.Hosts, "||"), authMech, authSource, authName, op, key)
return dbinfo
}
func extractCursorData(c *mongo.Cursor) CursorData {
cd := CursorData{
Id: c.ID(),
Err: c.Err().Error(),
}
for c.Next(context.Background()) {
cd.Data = append(cd.Data, c.Current)
}
return cd
}
func buildCursorHolder(data CursorData, cl *mongo.Client) *cursorHolder {
h := cursorHolder{}
h.cd = data
h.cookie1 = cookieValue1
h.cookie2 = cookieValue2
h.registry = getRegistryByClient(cl)
runtime.SetFinalizer(&h, cursorHolderFinalizer)
return &h
}
func extractSingleResult(r *mongo.SingleResult) SingleResultData {
err := r.Err()
sd := SingleResultData{}
if err != nil {
sd.Err = err.Error()
}
sd.Data, err = r.DecodeBytes()
if err != nil {
sd.Err = err.Error()
}
return sd
}
func buildSingleResultHolder(d SingleResultData, cl *mongo.Client) *singleResultHolder {
h := singleResultHolder{}
h.sd = d
h.cookie1 = cookieValue1
h.cookie2 = cookieValue2
h.registry = getRegistryByClient(cl)
runtime.SetFinalizer(&h, singleResultHolderFinalizer)
return &h
}
func marshalValue(v interface{}) ([]byte, error) {
var buff bytes.Buffer
enc := gob.NewEncoder(&buff)
err := enc.Encode(v)
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
func unmarshalValue(d []byte, v interface{}) error {
buff := bytes.NewBuffer(d)
dec := gob.NewDecoder(buff)
err := dec.Decode(v)
return err
}
type cursorRealFunc func() (*mongo.Cursor, error)
type singleResultRealFunc func() *mongo.SingleResult
func getSingleResultData(fname, key string, c *mongo.Client, realFunc singleResultRealFunc) *mongo.SingleResult {
if GlobalMgr.ShouldRecord() {
sr := realFunc()
h := buildSingleResultHolder(extractSingleResult(sr), c)
dd, err := marshalValue(h.sd)
if err != nil {
GlobalMgr.notifier(fmt.Sprintf("mongo.%s record SingleResult", fname), "marshal failed", []byte(err.Error()))
return nil
}
GlobalMgr.StoreValue(key, dd)
GlobalMgr.notifier(fmt.Sprintf("mongo.%s record SingleResult", fname), fmt.Sprintf("%s (done)", key), dd)
return &h.ret
}
dd, err := GlobalMgr.GetValue(key)
if err != nil {
GlobalMgr.notifier(fmt.Sprintf("mongo.%s replay SingleResult", fname), "get value from db failed", []byte(err.Error()))
return nil
}
var v SingleResultData
err = unmarshalValue(dd, &v)
if err != nil {
GlobalMgr.notifier(fmt.Sprintf("mongo.%s replay SingleResult", fname), "unmarshal failed", []byte(err.Error()))
return nil
}
h := buildSingleResultHolder(v, c)
GlobalMgr.notifier(fmt.Sprintf("mongo.%s replay SingleResult", fname), fmt.Sprintf("%s(done)", key), dd)
return &h.ret
}
func getCursorData(fname, key string, c *mongo.Client, realFunc cursorRealFunc) (*mongo.Cursor, error) {
if GlobalMgr.ShouldRecord() {
cs, err := realFunc()
if err != nil {
GlobalMgr.notifier(fmt.Sprintf("mongo.%s record Cursor", fname), fmt.Sprintf("%s (call origin fail)", key), []byte(""))
return cs, err
}
h := buildCursorHolder(extractCursorData(cs), c)
dd, err := marshalValue(h.cd)
if err != nil {
GlobalMgr.notifier(fmt.Sprintf("mongo.%s record Cursor", fname), fmt.Sprintf("%s (marshal ret fail)", key), []byte(""))
return nil, fmt.Errorf("mongo.%s record Cursor, marshal failed, err:%s", fname, err)
}
GlobalMgr.StoreValue(key, dd)
GlobalMgr.notifier(fmt.Sprintf("mongo.%s record Cursor", fname), fmt.Sprintf("%s (done)", key), dd)
return &h.cursor, err
}
dd, err := GlobalMgr.GetValue(key)
if err != nil {
GlobalMgr.notifier(fmt.Sprintf("mongo.%s replay Cursor", fname), fmt.Sprintf("%s (get from db fail)", key), []byte(""))
return nil, fmt.Errorf("mongo.%s replay Cursor, failed to get value from db, err:%s", fname, err)
}
var v CursorData
err = unmarshalValue(dd, &v)
if err != nil {
GlobalMgr.notifier(fmt.Sprintf("mongo.%s replay Cursor", fname), fmt.Sprintf("%s (unmarshal fail)", key), []byte(""))
return nil, fmt.Errorf("mongo.%s replay Cursor, unmarshal failed, err:%s", fname, err)
}
h := buildCursorHolder(v, c)
GlobalMgr.notifier(fmt.Sprintf("mongo.%s replay Cursor", fname), fmt.Sprintf("%s(done)", key), dd)
return &h.cursor, nil
}
type updateResultData struct {
MatchCount int64
ModifiedCount int64
UpsertedCount int64
UpsertedID bsoncore.Value
}
func marshalUpdateResult(ret *mongo.UpdateResult, fname string) ([]byte, error) {
val := updateResultData{
MatchCount: ret.MatchedCount,
ModifiedCount: ret.ModifiedCount,
UpsertedCount: ret.UpsertedCount,
}
t, d, err2 := bson.MarshalValue(ret.UpsertedID)
if err2 != nil {
return nil, fmt.Errorf("marshal %s result id failed, err:%s", fname, err2)
}
val.UpsertedID.Type = t
val.UpsertedID.Data = d
d, err := marshalValue(&val)
if err != nil {
return nil, fmt.Errorf("marshal %s return data failed, err:%s", fname, err)
}
return d, err
}
func unmarshalUpdateResult(d []byte, fname string) (*mongo.UpdateResult, error) {
var val updateResultData
err := unmarshalValue(d, &val)
if err != nil {
return nil, fmt.Errorf("%s unmarshal val from db failed, err:%s", fname, err)
}
ret := mongo.UpdateResult{
MatchedCount: val.MatchCount,
ModifiedCount: val.ModifiedCount,
UpsertedCount: val.UpsertedCount,
}
raw := bson.RawValue{Type: val.UpsertedID.Type, Value: val.UpsertedID.Data}
err = raw.Unmarshal(&ret.UpsertedID)
if err != nil {
return nil, fmt.Errorf("%s unmarshal upserted id failed, err:%s", fname, err)
}
return &ret, nil
}
// cursor hook
func mgCursorIDHook(c *mongo.Cursor) int64 {
h := getCursorHolder(c)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgCursorIDTramp(c)
}
}
return h.cd.Id
}
func mgCursorIDTramp(c *mongo.Cursor) int64 {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v", c)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if c != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return 0
}
func mgCursorNextHook(c *mongo.Cursor, ctx context.Context) bool {
h := getCursorHolder(c)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgCursorNextTramp(c, ctx)
}
}
if h.cur == int64(len(h.cd.Data)) {
return false
}
h.cursor.Current = h.cd.Data[h.cur]
h.cur++
return true
}
func mgCursorNextTramp(c *mongo.Cursor, ctx context.Context) bool {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v, %+v", c, ctx)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if c != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return true
}
func mgCursorTryNextHook(c *mongo.Cursor, ctx context.Context) bool {
h := getCursorHolder(c)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgCursorTryNextTramp(c, ctx)
}
}
return mgCursorNextHook(c, ctx)
}
func mgCursorTryNextTramp(c *mongo.Cursor, ctx context.Context) bool {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v, %+v", c, ctx)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if c != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return true
}
func mgCursorDecodeHook(c *mongo.Cursor, val interface{}) error {
h := getCursorHolder(c)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgCursorDecodeTramp(c, val)
}
}
return bson.UnmarshalWithRegistry(h.registry, h.cursor.Current, val)
}
func mgCursorDecodeTramp(c *mongo.Cursor, val interface{}) error {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v, %+v", c, val)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if c != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil
}
func mgCursorErrHook(c *mongo.Cursor) error {
h := getCursorHolder(c)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgCursorErrTramp(c)
}
}
if len(h.cd.Err) == 0 {
return nil
}
return fmt.Errorf(h.cd.Err)
}
func mgCursorErrTramp(c *mongo.Cursor) error {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v", c)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if c != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil
}
func mgCursorCloseHook(c *mongo.Cursor, ctx context.Context) error {
h := getCursorHolder(c)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgCursorCloseTramp(c, ctx)
}
}
return nil
}
func mgCursorCloseTramp(c *mongo.Cursor, ctx context.Context) error {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v", c)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if c != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil
}
func mgCursorAllHook(c *mongo.Cursor, ctx context.Context, results interface{}) error {
h := getCursorHolder(c)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgCursorAllTramp(c, ctx, results)
}
}
resultsVal := reflect.ValueOf(results)
if resultsVal.Kind() != reflect.Ptr {
return errors.New("results argument must be a pointer to a slice")
}
var index int
var err error
sliceVal := resultsVal.Elem()
elementType := sliceVal.Type().Elem()
docs := make([]bsoncore.Document, 0, len(h.cd.Data))
for i := int(h.cur); i < len(h.cd.Data); i++ {
docs = append(docs, bsoncore.Document(h.cd.Data[i]))
}
h.cur = int64(len(h.cd.Data))
sliceVal, index, err = addFromBatch(h.registry, sliceVal, elementType, docs, index)
if err != nil {
return err
}
resultsVal.Elem().Set(sliceVal.Slice(0, index))
return nil
}
func addFromBatch(registry *bsoncodec.Registry, sliceVal reflect.Value, elemType reflect.Type, docs []bsoncore.Document,
index int) (reflect.Value, int, error) {
for _, doc := range docs {
if sliceVal.Len() == index {
// slice is full
newElem := reflect.New(elemType)
sliceVal = reflect.Append(sliceVal, newElem.Elem())
sliceVal = sliceVal.Slice(0, sliceVal.Cap())
}
currElem := sliceVal.Index(index).Addr().Interface()
if err := bson.UnmarshalWithRegistry(registry, doc, currElem); err != nil {
return sliceVal, index, err
}
index++
}
return sliceVal, index, nil
}
func mgCursorAllTramp(c *mongo.Cursor, ctx context.Context, results interface{}) error {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v,%+v,%+v", c, ctx, results)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if c != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil
}
// SingleResult hook
func mgSingleResultDecodeHook(sr *mongo.SingleResult, v interface{}) error {
h := getSingleResultHolder(sr)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgSingleResultDecodeTramp(sr, v)
}
}
if len(h.sd.Err) != 0 {
return fmt.Errorf(h.sd.Err)
}
bson.UnmarshalWithRegistry(h.registry, h.sd.Data, v)
return nil
}
func mgSingleResultDecodeTramp(sr *mongo.SingleResult, v interface{}) error {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v,%+v", sr, v)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if sr != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil
}
func mgSingleResultDecodeBytesHook(sr *mongo.SingleResult) (bson.Raw, error) {
h := getSingleResultHolder(sr)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgSingleResultDecodeBytesTramp(sr)
}
}
if len(h.sd.Err) != 0 {
return nil, fmt.Errorf(h.sd.Err)
}
return h.sd.Data, nil
}
func mgSingleResultDecodeBytesTramp(sr *mongo.SingleResult) (bson.Raw, error) {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v", sr)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if sr != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil, nil
}
func mgSingleResultErrHook(sr *mongo.SingleResult) error {
h := getSingleResultHolder(sr)
if GlobalMgr.ShouldRecord() {
if h == nil {
return mgSingleResultErrTramp(sr)
}
}
if len(h.sd.Err) == 0 {
return nil
}
return fmt.Errorf(h.sd.Err)
}
func mgSingleResultErrTramp(sr *mongo.SingleResult) error {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v", sr)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if sr != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil
}
// global mongo.Connect() hook
func mgConnectHook(ctx context.Context, opts ...*options.ClientOptions) (*mongo.Client, error) {
var err error
var client *mongo.Client
clientOpt := options.MergeClientOptions(opts...)
client, err = mgConnectTramp(ctx, clientOpt)
if clientOpt.Registry == nil {
clientOpt.Registry = bson.DefaultRegistry
}
if err != nil {
GlobalMgr.notifier("call to mongo.NewClient failed", fmt.Sprintf("err:%s", err), []byte(""))
return nil, err
}
ch := &clientHolder{
client: *client,
opts: clientOpt,
cookie1: cookieValue1,
cookie2: cookieValue2}
runtime.SetFinalizer(ch, clientHolderFinalizer)
return &ch.client, err
}
func mgConnectTramp(ctx context.Context, opts ...*options.ClientOptions) (*mongo.Client, error) {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%+v, %+v", ctx, opts)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if opts != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return &mongo.Client{}, nil
}
// dynamic hook api
type hooklist struct {
obj interface{}
method string
replace interface{}
trampoline interface{}
mode int // 1 for record 2 for replay 3 for both
}
var (
hl = []hooklist{
hooklist{&mongo.Cursor{}, "All", mgCursorAllHook, mgCursorAllTramp, 3},
hooklist{&mongo.Cursor{}, "Close", mgCursorCloseHook, mgCursorCloseTramp, 3},
hooklist{&mongo.Cursor{}, "Err", mgCursorErrHook, mgCursorErrTramp, 3},
hooklist{&mongo.Cursor{}, "ID", mgCursorIDHook, mgCursorIDTramp, 3},
hooklist{&mongo.Cursor{}, "Next", mgCursorNextHook, mgCursorNextTramp, 3},
hooklist{&mongo.Cursor{}, "TryNext", mgCursorTryNextHook, mgCursorTryNextTramp, 3},
hooklist{&mongo.Cursor{}, "Decode", mgCursorDecodeHook, mgCursorDecodeTramp, 3},
hooklist{&mongo.SingleResult{}, "DecodeBytes", mgSingleResultDecodeBytesHook, mgSingleResultDecodeBytesTramp, 3},
hooklist{&mongo.SingleResult{}, "Decode", mgSingleResultDecodeHook, mgSingleResultDecodeTramp, 3},
hooklist{&mongo.SingleResult{}, "Err", mgSingleResultErrHook, mgSingleResultErrTramp, 3},
hooklist{&mongo.Client{}, "Connect", mgClientConnectHook, nil, 2},
hooklist{&mongo.Client{}, "Disconnect", mgClientDisconnectHook, nil, 2},
hooklist{&mongo.Client{}, "ListDatabases", mgClientListDatabasesHook, mgClientListDatabasesTramp, 3},
hooklist{&mongo.Database{}, "Aggregate", mgDatabaseAggregateHook, mgDatabaseAggregateTramp, 3},
hooklist{&mongo.Database{}, "Drop", mgDatabaseDropHook, nil, 2},
hooklist{&mongo.Database{}, "ListCollections", mgDatabaseListCollectionsHook, mgDatabaseListCollectionsTramp, 3},
hooklist{&mongo.Database{}, "RunCommand", mgDatabaseRunCommandHook, mgDatabaseRunCommandTramp, 3},
hooklist{&mongo.Database{}, "RunCommandCursor", mgDatabaseRunCommandCursorHook, mgDatabaseRunCommandCursorTramp, 3},
hooklist{&mongo.Collection{}, "Aggregate", mgCollectionAggregateHook, mgCollectionAggregateTramp, 3},
hooklist{&mongo.Collection{}, "CountDocuments", mgCollectionCountDocumentsHook, mgCollectionCountDocumentsTramp, 3},
hooklist{&mongo.Collection{}, "DeleteMany", mgCollectionDeleteManyHook, mgCollectionDeleteManyTramp, 3},
hooklist{&mongo.Collection{}, "DeleteOne", mgCollectionDeleteOneHook, mgCollectionDeleteOneTramp, 3},
hooklist{&mongo.Collection{}, "Distinct", mgCollectionDistinctHook, mgCollectionDistinctTramp, 3},
hooklist{&mongo.Collection{}, "Drop", mgCollectionDropHook, nil, 2},
hooklist{&mongo.Collection{}, "EstimatedDocumentCount", mgCollectionEstimatedDocumentCountHook, mgCollectionEstimatedDocumentCountTramp, 3},
hooklist{&mongo.Collection{}, "Find", mgCollectionFindHook, mgCollectionFindTramp, 3},
hooklist{&mongo.Collection{}, "FindOneAndDelete", mgCollectionFindOneAndDeleteHook, mgCollectionFindOneAndDeleteTramp, 3},
hooklist{&mongo.Collection{}, "FindOneAndReplace", mgCollectionFindOneAndReplaceHook, mgCollectionFindOneAndReplaceTramp, 3},
hooklist{&mongo.Collection{}, "FindOneAndUpdate", mgCollectionFindOneAndUpdateHook, mgCollectionFindOneAndUpdateTramp, 3},
hooklist{&mongo.Collection{}, "InsertMany", mgCollectionInsertManyHook, nil, 2},
hooklist{&mongo.Collection{}, "InsertOne", mgCollectionInsertOneHook, nil, 2},
hooklist{&mongo.Collection{}, "ReplaceOne", mgCollectionReplaceOneHook, mgCollectionReplaceOneTramp, 3},
hooklist{&mongo.Collection{}, "UpdateMany", mgCollectionUpdateManyHook, mgCollectionUpdateManyTramp, 3},
}
)
func DisableMongoHook() error {
gohook.UnHook(mongo.Connect)
for i := range hl {
gohook.UnHookMethod(hl[i].obj, hl[i].method)
}
return nil
}
func EnableMongoHook() error {
var err error
defer func() {
if err != nil {
DisableMongoHook()
}
}()
mode := 2
if GlobalMgr.ShouldRecord() {
mode = 1
}
err = gohook.Hook(mongo.Connect, mgConnectHook, mgConnectTramp)
if err != nil {
return err
}
for i := range hl {
if (mode & hl[i].mode) > 0 {
err = gohook.HookMethod(hl[i].obj, hl[i].method, hl[i].replace, hl[i].trampoline)
if err != nil {
return fmt.Errorf("HookMethod failed, fn:%+v,err:%s", hl[i], err)
}
}
}
return nil
}