-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclua_helper.go
executable file
·1504 lines (1219 loc) · 36.2 KB
/
clua_helper.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
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
package main
import (
"bytes"
"encoding/base64"
"encoding/gob"
"encoding/json"
"errors"
"flag"
"github.com/esrrhs/gohome/common"
"github.com/esrrhs/gohome/fastwalk"
"github.com/esrrhs/gohome/loggo"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
var ty = flag.String("type", "client", "client / server / gen")
var root = flag.String("path", "./", "source code path")
var skiproot = flag.String("skippath", "tables", "skip path")
var binname = flag.String("bin", "main", "binary name")
var hookso = flag.String("hookso", "./hookso", "hookso path")
var libclua = flag.String("libclua", "./libclua.so", "libclua.so path")
var clua = flag.String("clua", "./clua", "clua path")
var covpath = flag.String("covpath", "./cov", "saved coverage path")
var covinter = flag.Int("covinter", 5, "saved coverage inter")
var server = flag.String("server", "http://127.0.0.1:8877", "send to server host")
var port = flag.Int("port", 8877, "server listen port")
var getluastate = flag.String("getluastate", "test.so lua_settop 1", "get lua state command")
var tmppath = flag.String("tmppath", "./tmp", "tmp path")
var lcov = flag.String("lcov", "./lcov", "lcov bin path")
var paralel = flag.Int("paralel", 8, "max paralel")
var clientroot = flag.String("clientpath", "./", "client source code path")
var genhtml = flag.String("genhtml", "./genhtml", "genhtml bin path")
var htmloutputpath = flag.String("htmlout", "./htmlout", "html output path")
var deletecov = flag.Bool("deletecovpath", true, "delete coverage path data")
var resultdata = flag.String("resultdata", "", "save result data file path")
var lastresultdata = flag.String("lastresultdata", "", "merge last save result data file path")
var checkinter = flag.Int("checkinter", 60, "client check inter in second")
var sendinter = flag.Int("sendinter", 3600, "client send inter in second")
var statichtml = flag.String("statichtml", "static", "static html prefix")
var deletetmp = flag.Bool("deletetmp", true, "delete tmp path data")
var skipdiff = flag.Int("skipdiff", 50, "skip diff percent")
func main() {
defer common.CrashLog()
flag.Parse()
loggo.Ini(loggo.Config{
Level: loggo.LEVEL_INFO,
Prefix: "cluahelper",
MaxDay: 3,
NoLogFile: false,
NoPrint: false,
NoLogColor: false,
})
var err error
if *ty == "client" {
err = ini_client()
} else if *ty == "server" {
err = ini_server()
} else if *ty == "gen" {
err = ini_gen()
}
if err != nil {
os.Exit(-1)
}
}
/////////////////////////////////////////////////////////////////////////////////
type SouceData struct {
Content string
Md5sum string
Id string
}
type PushData struct {
Covdata [][]byte
Source map[string]SouceData
}
/////////////////////////////////////////////////////////////////////////////////
func load_pids() ([]int, error) {
var pids []int
cmd := exec.Command("bash", "-c", "ps -ef | grep \""+*binname+" \" | grep -v grep | grep -v clua_helper | awk '{print $2}' ")
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s", err)
return pids, err
}
//loggo.Info("pids = %s", string(out))
pidstrs := strings.Split(string(out), "\n")
for _, pidstr := range pidstrs {
pidstr = strings.TrimSpace(pidstr)
pid, err := strconv.Atoi(pidstr)
if err != nil {
continue
}
pids = append(pids, pid)
}
return pids, nil
}
func get_lstate(pid int) (string, error) {
// ./hookso arg $PID test.so lua_settop 1
cmd := exec.Command("bash", "-c", *hookso+" arg "+strconv.Itoa(pid)+" "+*getluastate)
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s %s", err, string(out))
return "", err
}
lstatestr := string(out)
lstatestr = strings.TrimSpace(lstatestr)
loggo.Info("pid %d L = %s", pid, lstatestr)
// ./hookso dlopen $PID ./libclua.so
cmd = exec.Command("bash", "-c", *hookso+" dlopen "+strconv.Itoa(pid)+" "+*libclua)
out, err = cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s %s", err, string(out))
return "", err
}
return lstatestr, nil
}
func stop_inject(pid int) error {
loggo.Info("start stop_inject %d", pid)
lstatestr, err := get_lstate(pid)
if err != nil {
loggo.Error("get_lstate failed with %s", err)
return err
}
// ./hookso call $PID libclua.so stop_cov i=$L
cmd := exec.Command("bash", "-c", *hookso+" call "+strconv.Itoa(pid)+" "+*libclua+" stop_cov i="+lstatestr)
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s %s", err, string(out))
return err
}
loggo.Info("end stop_inject %d", pid)
return nil
}
func get_pid_cov_file(pid int) (string, error) {
thecovpath, err := filepath.Abs(*covpath)
if err != nil {
loggo.Error("filepath Abs failed with %s", err)
return "", err
}
err = os.MkdirAll(thecovpath, 0755)
if err != nil {
loggo.Error("os MkdirAll failed with %s", err)
return "", err
}
dstfile := filepath.Join(thecovpath, strconv.Itoa(pid)+".cov")
return dstfile, nil
}
func start_inject(pid int) error {
loggo.Info("start start_inject %d", pid)
dstfile, err := get_pid_cov_file(pid)
if err != nil {
loggo.Error("get_pid_cov_file failed with %s", err)
return err
}
lstatestr, err := get_lstate(pid)
if err != nil {
loggo.Error("get_lstate failed with %s", err)
return err
}
// ./hookso call $PID libclua.so start_cov i=$L s="dst.cov" i=5
cmd := exec.Command("bash", "-c", *hookso+" call "+strconv.Itoa(pid)+" "+*libclua+" start_cov i="+lstatestr+
" s=\""+dstfile+"\" i="+strconv.Itoa(*covinter))
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s %s", err, string(out))
return err
}
loggo.Info("end start_inject %d", pid)
return nil
}
func save_source(gen_id bool) (map[string]SouceData, error) {
loggo.Info("start save_source %s", *root)
skippath := filepath.Join(*root, *skiproot)
loggo.Info("save_source skip %s", skippath)
bytes := 0
sourcemap := make(map[string]SouceData)
var mu sync.Mutex
index := 0
fun := func(path string, typ os.FileMode) error {
if typ&os.ModeSymlink == os.ModeSymlink {
return fastwalk.TraverseLink
}
if typ.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".lua") {
return nil
}
if strings.HasPrefix(filepath.Clean(path), filepath.Clean(skippath)) {
//loggo.Info("skip path %s %s %s", path, filepath.Clean(path), filepath.Base(skippath))
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
loggo.Error("ioutil ReadFile fail %q: %v", path, err)
return err
}
md5 := common.GetMd5String(string(data))
mu.Lock()
defer mu.Unlock()
sd := SouceData{string(data), md5, ""}
if gen_id {
name := filepath.Base(path)
sd.Id = strconv.Itoa(index) + "_" + strings.TrimSuffix(name, filepath.Ext(name))
index++
}
//loggo.Info("add sourcemap %s", filepath.Clean(path))
sourcemap[filepath.Clean(path)] = sd
bytes += len(data)
return nil
}
err := fastwalk.Walk(*root, fun)
if err != nil {
loggo.Error("godirwalk Walk %s", err)
return nil, err
}
loggo.Info("end save_source %s %d %d", *root, len(sourcemap), bytes)
return sourcemap, nil
}
func reset_client() (map[string]SouceData, []int, error) {
loggo.Info("start reset_client")
pids, err := load_pids()
if err != nil {
loggo.Error("load_pids failed %s", err)
return nil, nil, err
}
for _, pid := range pids {
err := stop_inject(pid)
if err != nil {
loggo.Error("stop_inject failed %s", err)
return nil, nil, err
}
}
cursource, err := save_source(false)
if err != nil {
loggo.Error("save_source failed %s", err)
return nil, nil, err
}
for _, pid := range pids {
err := start_inject(pid)
if err != nil {
loggo.Error("start_inject failed %s", err)
return nil, nil, err
}
}
loggo.Info("end reset_client")
return cursource, pids, nil
}
func get_cov_source_file(path string) ([]string, error) {
// ./clua -path ./bin/ -i cov/4157.cov -showfunc=false -showtotal=false -showcode=false -showfile=true
cmd := exec.Command("bash", "-c", *clua+" -path "+*root+" -i "+path+" -showfunc=false -showtotal=false -showcode=false -showfile=true")
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s", err)
return nil, err
}
var ret []string
filestrs := strings.Split(string(out), "\n")
for _, filestr := range filestrs {
filestr = strings.TrimSpace(filestr)
if len(filestr) <= 0 {
continue
}
ret = append(ret, filestr)
}
return ret, nil
}
func backup_cov(pids []int) ([][]byte, map[string]int, error) {
var ret [][]byte
retsourcefile := make(map[string]int)
for _, pid := range pids {
src, err := get_pid_cov_file(pid)
if err != nil {
loggo.Error("get_pid_cov_file failed %s", err)
return nil, nil, err
}
data, err := ioutil.ReadFile(src)
if err != nil {
loggo.Error("ioutil ReadFile fail %q: %v", src, err)
return nil, nil, err
}
ret = append(ret, data)
sourcefiles, err := get_cov_source_file(src)
if err != nil {
loggo.Error("get_cov_source_file fail %q: %v", src, err)
return nil, nil, err
}
for _, sourcefile := range sourcefiles {
retsourcefile[filepath.Clean(sourcefile)]++
}
}
return ret, retsourcefile, nil
}
func make_push_data(covdata [][]byte, covsource map[string]int, source map[string]SouceData) (string, error) {
tmpsource := make(map[string]SouceData)
for k, v := range source {
_, ok := covsource[filepath.Clean(k)]
if ok {
tmpsource[k] = v
}
}
loggo.Info("make_push_data %d %d", len(covdata), len(tmpsource))
pushdata := PushData{covdata, tmpsource}
b := bytes.Buffer{}
e := gob.NewEncoder(&b)
err := e.Encode(pushdata)
if err != nil {
loggo.Error("Encode fail %v", err)
return "", err
}
data := string(b.Bytes())
data = common.GzipStringBestCompression(data)
data = base64.StdEncoding.EncodeToString([]byte(data))
return data, nil
}
func send_to_server(covdata [][]byte, covsource map[string]int, source map[string]SouceData) error {
loggo.Info("start send_to_server %d %d", len(covdata), len(source))
data, err := make_push_data(covdata, covsource, source)
if err != nil {
loggo.Error("make_push_data fail %v", err)
return err
}
md5str := common.GetMd5String(data)
loggo.Info("send_to_server data bytes %d %s", len(data), md5str)
resp, err := http.PostForm(*server+"/coverage", url.Values{"md5": {md5str}, "data": {data}})
if err != nil {
loggo.Error("send_to_server fail %s", err)
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
loggo.Error("send_to_server fail %s", err)
return err
}
loggo.Info("end send_to_server %s", string(body))
return nil
}
func clear_invalid_file(pids []int) {
filepath.Walk(*covpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
loggo.Error("prevent panic by handling failure accessing a path %q: %v", path, err)
return err
}
if info == nil || info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ".cov") {
return nil
}
find := false
for _, pid := range pids {
dst, err := get_pid_cov_file(pid)
if err != nil {
loggo.Error("get_pid_cov_file failed with %s", err)
return err
}
if filepath.Clean(path) == filepath.Clean(dst) {
find = true
}
}
if !find {
osremove(path)
}
return nil
})
}
func ini_client() error {
for {
err := run_client()
if err != nil {
time.Sleep(time.Second * 10)
}
}
}
func run_client() error {
cursource, curpids, err := reset_client()
if err != nil {
loggo.Error("ini_client failed %s", err)
return err
}
var tosend_covdata [][]byte
var tosend_covsource map[string]int
var tosend_cursource map[string]SouceData
last := time.Now()
lastsend := time.Now()
for {
if time.Now().Sub(last) < time.Second*time.Duration(*checkinter) {
time.Sleep(time.Second)
continue
}
last = time.Now()
covdata, covsource, err := backup_cov(curpids)
if err != nil {
loggo.Error("backup_cov failed %s", err)
return err
}
needreset := false
newpids, err := load_pids()
if err != nil {
loggo.Error("load_pids failed %s", err)
return err
}
for _, pid := range curpids {
if !common.HasInt(newpids, pid) {
loggo.Info("pid %d exit, need reset", pid)
needreset = true
break
}
}
newsource, err := save_source(false)
if err != nil {
loggo.Error("save_source failed %s", err)
return err
}
for path, newdata := range newsource {
data, ok := cursource[path]
if ok {
if data.Md5sum != newdata.Md5sum {
loggo.Info("file %s change, need reset", path)
needreset = true
break
}
}
}
if needreset {
cursource, curpids, err = reset_client()
if err != nil {
loggo.Error("ini_client failed %s", err)
return err
}
loggo.Info("start send per hour")
if tosend_covdata != nil && tosend_covsource != nil && tosend_cursource != nil {
send_to_server(tosend_covdata, tosend_covsource, tosend_cursource)
lastsend = time.Now()
tosend_covdata = nil
tosend_covsource = nil
tosend_cursource = nil
}
continue
}
for _, newpid := range newpids {
if !common.HasInt(curpids, newpid) {
err := start_inject(newpid)
if err != nil {
loggo.Error("start_inject failed %s", err)
return err
}
}
}
loggo.Info("everything ok")
tosend_covdata = covdata
tosend_covsource = covsource
tosend_cursource = cursource
if time.Now().Sub(lastsend) >= time.Second*time.Duration(*sendinter) {
loggo.Info("start send per hour")
send_to_server(tosend_covdata, tosend_covsource, tosend_cursource)
lastsend = time.Now()
}
curpids = newpids
cursource = newsource
clear_invalid_file(curpids)
}
return nil
}
/////////////////////////////////////////////////////////////////////////////////
var gpath map[string]func(*http.Request, http.ResponseWriter, string, url.Values)
func ini_server() error {
http.HandleFunc("/", MyHandler)
fs := http.FileServer(http.Dir(*htmloutputpath))
http.Handle("/"+*statichtml+"/", http.StripPrefix("/"+*statichtml+"/", fs))
gpath = make(map[string]func(*http.Request, http.ResponseWriter, string, url.Values))
gpath["/coverage"] = CoverageHandler
loggo.Info("listen on " + strconv.Itoa(*port))
err := http.ListenAndServe(":"+strconv.Itoa(*port), nil)
if err != nil {
loggo.Error("ListenAndServe fail %v", err)
return err
}
loggo.Info("quit")
return nil
}
type Response struct {
Code string `json:"code"`
Data string `json:"data"`
}
func Res(w http.ResponseWriter, code string, data string) {
res := Response{code, data}
d, err := json.Marshal(res)
if err != nil {
loggo.Error("Res Marshal fail %v", err)
return
}
if runtime.GOOS == "windows" {
w.Header().Set("Access-Control-Allow-Origin", "*")
}
w.Write(d)
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
loggo.Info("handle %v %v", r.Method, r.RequestURI)
u, err := url.Parse(r.RequestURI)
if err != nil {
loggo.Error("Parse fail %v", r.RequestURI)
Res(w, "wrong request", r.RequestURI)
return
}
f, ok := gpath[u.Path]
if !ok {
loggo.Info("no path %v", u.Path)
Res(w, "wrong request", u.Path)
return
}
f(r, w, u.Path, u.Query())
}
func gen_data_filename() (string, error) {
thecovpath, err := filepath.Abs(*covpath)
if err != nil {
loggo.Error("filepath Abs failed with %s", err)
return "", err
}
err = os.MkdirAll(thecovpath, 0755)
if err != nil {
loggo.Error("os MkdirAll failed with %s", err)
return "", err
}
filename := time.Now().Format("2006-01-02_15:04:05_") + common.UniqueId() + ".data"
dstfile := filepath.Join(thecovpath, filename)
return dstfile, nil
}
func CoverageHandler(r *http.Request, w http.ResponseWriter, path string, param url.Values) {
md5str := r.FormValue("md5")
data := r.FormValue("data")
loggo.Info("CoverageHandler data %v %v", md5str, len(data))
if md5str != common.GetMd5String(string(data)) {
Res(w, "fail", "diff md5")
return
}
filename, err := gen_data_filename()
if err != nil {
Res(w, "fail", err.Error())
return
}
f, err := os.Create(filename)
if err != nil {
Res(w, "fail", err.Error())
return
}
defer f.Close()
_, err = f.WriteString(data)
if err != nil {
Res(w, "fail", err.Error())
return
}
Res(w, "ok", "")
loggo.Info("CoverageHandler %v", len(data))
}
/////////////////////////////////////////////////////////////////////////////////
func load_data_file_list() ([]string, string, error) {
var ret []string
retlastresultdataabs := ""
if len(*lastresultdata) != 0 {
lastresultdataabs, err := filepath.Abs(*lastresultdata)
if err != nil {
loggo.Error("gen_tmp_file failed with %s", err)
return nil, "", err
}
if !common.FileExists(lastresultdataabs) {
loggo.Error("last resultdata not find %s", lastresultdataabs)
return nil, "", err
}
ret = append(ret, lastresultdataabs)
retlastresultdataabs = lastresultdataabs
}
filepath.Walk(*covpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info == nil || info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ".data") {
return nil
}
ret = append(ret, filepath.Clean(path))
loggo.Info("load_data_file_list %s", filepath.Clean(path))
return nil
})
return ret, retlastresultdataabs, nil
}
func write_tmp_file(data []byte) (string, error) {
dstfile, err := gen_tmp_file("")
if err != nil {
loggo.Error("gen_tmp_file failed with %s", err)
return "", err
}
f, err := os.Create(dstfile)
if err != nil {
return "", err
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
return "", err
}
return dstfile, nil
}
func gen_tmp_file(filename string) (string, error) {
thetmppath, err := filepath.Abs(*tmppath)
if err != nil {
loggo.Error("filepath Abs failed with %s", err)
return "", err
}
err = os.MkdirAll(thetmppath, 0755)
if err != nil {
loggo.Error("os MkdirAll failed with %s", err)
return "", err
}
needcheck := false
if len(filename) <= 0 {
filename = common.UniqueId()
needcheck = true
}
filename += ".tmp"
dstfile := filepath.Join(thetmppath, filename)
if needcheck {
if common.FileExists(dstfile) {
loggo.Error("gen_tmp_file Exists %s", dstfile)
return "", errors.New("file Exists")
}
}
return dstfile, nil
}
func lcov_add(covfile string, sourcefile string, id string) error {
oldinfo, err := gen_tmp_file(id + ".info")
if err != nil {
loggo.Error("gen_tmp_file failed with %s", err)
return err
}
if !common.FileExists(oldinfo) {
// ./clua -path ./bin/ -i cov/4157.cov -fp sourcefile -lcov oldinfo.info -showfunc=false -showtotal=false -showcode=false -showfile=false
cmd := exec.Command("bash", "-c", *clua+" -path "+*root+" -i "+covfile+" -fp "+sourcefile+
" -lcov "+oldinfo+" -showfunc=false -showtotal=false -showcode=false -showfile=false")
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s %s %s", string(out), err, oldinfo)
return err
}
if !common.FileExists(oldinfo) {
loggo.Error("lcov_add no oldinfo %s", oldinfo)
return errors.New("no file")
}
if common.FileFind(oldinfo, "DA:") <= 0 {
osremove(oldinfo)
loggo.Info("lcov_add new empty %s %s", sourcefile, oldinfo)
} else {
loggo.Info("lcov_add new %s %s", sourcefile, oldinfo)
}
} else {
newinfo, err := gen_tmp_file(id + "_new.info")
if err != nil {
loggo.Error("gen_tmp_file failed with %s", err)
return err
}
// ./clua -path ./bin/ -i cov/4157.cov -fp sourcefile -lcov newinfo.info -showfunc=false -showtotal=false -showcode=false -showfile=false
cmd := exec.Command("bash", "-c", *clua+" -path "+*root+" -i "+covfile+" -fp "+sourcefile+
" -lcov "+newinfo+" -showfunc=false -showtotal=false -showcode=false -showfile=false")
_, err = cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s", err)
return err
}
loggo.Info("lcov add newinfo %s %s %s", covfile, sourcefile, newinfo)
if !common.FileExists(newinfo) {
loggo.Error("lcov_add no newinfo %s", newinfo)
return errors.New("no file")
}
if common.FileFind(newinfo, "DA:") <= 0 {
osremove(newinfo)
loggo.Info("lcov_add newinfo empty %s %s", sourcefile, oldinfo)
} else {
// lcov -a oldinfo.info -a newinfo.info -o oldinfo.info
cmd = exec.Command("bash", "-c", *lcov+" -a "+oldinfo+" -a "+newinfo+" -o "+oldinfo)
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s %s %s %s", string(out), err, oldinfo, newinfo)
return err
}
if !common.FileExists(oldinfo) {
loggo.Error("lcov_add no oldinfo %s", oldinfo)
return errors.New("no file")
}
osremove(newinfo)
loggo.Info("lcov_add ok %s %s", sourcefile, oldinfo)
}
}
return nil
}
func get_change_lines(sourcefile string, clientsoucefile string) (int, int, error) {
cmd := exec.Command("bash", "-c", "diff -y --suppress-common-lines "+sourcefile+" "+clientsoucefile+" | wc -l ")
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s", err)
return 0, 0, err
}
str := string(out)
str = strings.TrimSpace(str)
n, err := strconv.Atoi(str)
if err != nil {
loggo.Error("Atoi failed with %s", err)
return 0, 0, err
}
return n, common.FileLineCount(sourcefile), nil
}
func lcov_merge(covfile string, sourcefile string, clientsoucefile string, source map[string]SouceData, id string) error {
oldinfo, err := gen_tmp_file(id + ".info")
if err != nil {
loggo.Error("gen_tmp_file failed with %s", err)
return err
}
sourcedata, ok := source[clientsoucefile]
if !ok {
loggo.Error("source no soucefile %s", clientsoucefile)
return err
}
oldsourcefile, err := write_tmp_file([]byte(sourcedata.Content))
if err != nil {
loggo.Error("write_tmp_file failed with %s", err)
return err
}
diffn, newn, err := get_change_lines(sourcefile, oldsourcefile)
if err != nil {
loggo.Error("get_change_lines failed with %s", err)
return err
}
if newn*(*skipdiff)/100 < diffn {
loggo.Info("change too much, skip merge %s %d %d", sourcefile, newn, diffn)
osremove(oldsourcefile)
return nil
}
difffile, err := gen_tmp_file("")
if err != nil {
loggo.Error("gen_tmp_file failed with %s", err)
return err
}
// diff -u $PWD/old/prog.c $PWD/new/prog.c > diff
cmd := exec.Command("bash", "-c", "diff -u "+oldsourcefile+" "+sourcefile+" > "+difffile)
cmd.CombinedOutput()
if !common.FileExists(difffile) {
loggo.Error("lcov_merge no difffile %s", oldinfo)
return errors.New("no file")
}
loggo.Info("lcov_merge old sourcefile %s, new sourcefile %s, diff file %s", oldsourcefile, sourcefile, difffile)
oldsourceinfo, err := gen_tmp_file(id + "_old.info")
if err != nil {
loggo.Error("gen_tmp_file failed with %s", err)
return err
}
// ./clua -path ./bin/ -i cov/4157.cov -fp sourcefile -fpsource oldsourcefile -lcov oldsourceinfo.info -showfunc=false -showtotal=false -showcode=false -showfile=false
cmd = exec.Command("bash", "-c", *clua+" -path "+*root+" -i "+covfile+" -fp "+sourcefile+" -fpsource "+oldsourcefile+
" -lcov "+oldsourceinfo+" -showfunc=false -showtotal=false -showcode=false -showfile=false")
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s %s %s", string(out), err, oldsourceinfo)
return err
}
if !common.FileExists(oldsourceinfo) {
loggo.Error("lcov_merge no oldsourceinfo %s", oldsourceinfo)
return errors.New("no file")
}
loggo.Info("lcov_merge old sourcefile %s, cov file %s, old info %s", oldsourcefile, covfile, oldsourceinfo)
if common.FileFind(oldsourceinfo, "DA:") <= 0 {
loggo.Info("lcov_merge oldsourceinfo empty %s %s", oldsourceinfo, oldinfo)
osremove(oldsourcefile)
osremove(difffile)
osremove(oldsourceinfo)
return nil
}
if !common.FileExists(oldinfo) {
// lcov --diff oldsourceinfo.info difffile --convert-filenames -o oldinfo.info
cmd = exec.Command("bash", "-c", *lcov+" --diff "+oldsourceinfo+" "+difffile+" --convert-filenames -o "+oldinfo)
out, err := cmd.CombinedOutput()
if err != nil {
loggo.Error("exec Command failed with %s %s %s %s", string(out), err, oldsourceinfo, oldinfo)
return err
}
err = common.FileReplace(oldinfo, "TN:,diff", "TN:")
if err != nil {
loggo.Error("FileReplace failed with %s", err)
return err
}
err = common.FileReplace(oldinfo, "SF:"+oldsourcefile, "SF:"+sourcefile)
if err != nil {
loggo.Error("FileReplace failed with %s", err)
return err
}
if !common.FileExists(oldinfo) {
loggo.Error("lcov_merge no oldinfo %s", oldinfo)
return errors.New("no file")
}
if common.FileFind(oldinfo, "DA:") <= 0 {
osremove(oldinfo)
loggo.Info("lcov_merge new empty %s %s", sourcefile, oldinfo)
} else {
loggo.Info("lcov_merge new %s %s", sourcefile, oldinfo)
}
} else {
newinfo, err := gen_tmp_file(id + "_new.info")
if err != nil {
loggo.Error("gen_tmp_file failed with %s", err)
return err
}