-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracer.go
140 lines (121 loc) · 3.28 KB
/
tracer.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
package main
/*
Copyright (c) 2023, Erik Kassubek
All rights reserved.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Author: Erik Kassubek <erik-kassubek@t-online.de>
Package: dedego-analyzer (Deadlock Detector in Go)
Project: Dynamic Analysis to detect potential deadlocks in
*/
/*
trace.go
Drop in replacements of common channel functionality to create a trace of the
Program
*/
import (
"fmt"
"os"
"runtime"
"sync"
"time"
"github.com/petermattis/goid"
)
var routineIndexLock sync.Mutex
var routineIndex = make(map[int64]uint32)
var numberRoutines uint32
var numberOfChan uint32
var numberOfMutex uint32
var chanSize = make(map[uint32]int)
var chanSizeLock sync.Mutex
var traces = make([]([]TraceElement), 0) // lists of traces
var tracesLock sync.RWMutex
var counter uint32 // PC
/*
Function to initialize the tracer.
@return: nil
*/
func Init(maxTime int) {
numberRoutines = 0
numberOfChan = 0
numberOfMutex = 0
counter = 0
tracesLock.Lock()
traces = append(traces, []TraceElement{})
tracesLock.Unlock()
routineIndexLock.Lock()
routineIndex[goid.Get()] = 0
routineIndexLock.Unlock()
go func() {
if maxTime == 0 {
return // no timeout
}
t := time.NewTimer(time.Duration(maxTime) * time.Second)
<-t.C
fmt.Printf("##@@##\nThe programm was terminated by the tracer, because the program "+
"runtime exceeded the maximal runtime of %ds. This may be caused "+
"by a to short max waiting time (can be changed in the instrumenter) "+
"or because the program can not terminate, e.g. because it is in a "+
"deadlock.##@@##", maxTime)
RunAnalyzer()
os.Exit(42)
}()
}
/*
Function to print the collected trace.
*/
func PrintTrace() {
fmt.Println("#####TRACE#####")
fmt.Print("[")
i := 0
for k, v := range chanSize {
i++
fmt.Printf("%d: %d", k, v)
if i != len(chanSize) {
fmt.Print(", ")
}
}
fmt.Println("]")
fmt.Println("[")
for _, trace := range traces {
fmt.Print("[")
for i, element := range trace {
element.PrintElement()
if i != len(trace)-1 {
fmt.Print(", ")
}
}
fmt.Println("]")
}
fmt.Println("]")
fmt.Println("#####TRACE#####")
}
/*
Function to get the index of the routine, from wich the function is called
*/
func getIndex() uint32 {
id := goid.Get()
routineIndexLock.Lock()
res := routineIndex[id]
routineIndexLock.Unlock()
return res
}
/*
Function to get the position of the original caller in the code
@param skip int: no. off calls to skip, 0 is the caller of getPosition
@return string: position
*/
func getPosition(skip int) string {
_, file, line, _ := runtime.Caller(skip + 1)
return file + fmt.Sprintf(":%d", line)
}