-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_cov.go
147 lines (124 loc) · 3.5 KB
/
line_cov.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
type LineCovGuider struct {
ObjectPath string
GcovProgramPath string
Lines map[string]bool
*TLCStateGuider
}
var _ Guider = &LineCovGuider{}
func NewLineCovGuider(objectPath, tlcAddr, recordPath string) *LineCovGuider {
return &LineCovGuider{
ObjectPath: objectPath,
GcovProgramPath: "/usr/bin/gcov",
Lines: make(map[string]bool),
TLCStateGuider: NewTLCStateGuider(tlcAddr, recordPath),
}
}
func (l *LineCovGuider) Check(iter string, trace *Trace, events *EventTrace, record bool) (bool, int) {
l.TLCStateGuider.Check(iter, trace, events, record)
// Get new lines
lines, err := getLines(l.ObjectPath, l.GcovProgramPath)
if err != nil {
return false, 0
}
oldLines := coveredLines(l.Lines)
l.Lines = mergeLines(l.Lines, lines)
newLines := coveredLines(l.Lines)
return newLines > oldLines, newLines - oldLines
}
func (l *LineCovGuider) Reset() {
l.TLCStateGuider.Reset()
l.Lines = make(map[string]bool)
}
type gcovOutput struct {
CurrentWorkingDirectory string `json:"current_working_directory"`
DataFile string `json:"data_file"`
FormatVersion string `json:"format_version"`
GCCVersion string `json:"gcc_version"`
Files []*gcovFileOutput `json:"files"`
}
type gcovFileOutput struct {
File string `json:"file"`
Functions []interface{} `json:"functions"`
Lines []*gcovLineOutput `json:"lines"`
}
type gcovLineOutput struct {
BlockIDs []interface{} `json:"block_ids"`
Branches []interface{} `json:"branches"`
Calls []interface{} `json:"calls"`
Count int `json:"count"`
LineNumber int `json:"line_number"`
UnexecutedBlock bool `json:"unexecuted_block"`
FunctionName string `json:"function_name"`
}
func (g *gcovOutput) GetLines() map[string]bool {
lines := make(map[string]bool)
for _, f := range g.Files {
for _, l := range f.Lines {
key := f.File + "_" + strconv.Itoa(l.LineNumber)
lines[key] = !l.UnexecutedBlock
}
}
return lines
}
func getLines(objectFilePath string, gcovProgramPath string) (map[string]bool, error) {
lines := make(map[string]bool)
if info, err := os.Stat(objectFilePath); err != nil || !info.IsDir() {
return lines, fmt.Errorf("invalid object directory path: %s", err)
}
files, err := os.ReadDir(objectFilePath)
if err != nil {
return lines, err
}
for _, file := range files {
if strings.Contains(file.Name(), "gcda") {
args := make([]string, 0)
args = append(args, "--json-format", "--stdout", file.Name())
out := new(bytes.Buffer)
cmd := exec.Command(gcovProgramPath, args...)
cmd.Dir = objectFilePath
cmd.Stdout = out
if err := cmd.Run(); err != nil {
return lines, fmt.Errorf("error running gcov program: %s", err)
}
data := &gcovOutput{}
if err := json.Unmarshal(out.Bytes(), data); err != nil {
return lines, fmt.Errorf("error unmarshalling gcov output: %s", err)
}
lines = mergeLines(lines, data.GetLines())
}
}
return lines, nil
}
func mergeLines(one map[string]bool, two map[string]bool) map[string]bool {
out := make(map[string]bool)
for k, v := range one {
out[k] = v
}
for k, v := range two {
val, ok := one[k]
if !ok {
out[k] = v
}
out[k] = val || v
}
return out
}
func coveredLines(l map[string]bool) int {
c := 0
for _, v := range l {
if v {
c += 1
}
}
return c
}