-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
72 lines (64 loc) · 1.92 KB
/
process.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
package rpz
import (
"database/sql"
"fmt"
"io"
"github.com/cirss/geist/pkg/rdf"
)
// Process represents a row in the processes table of trace.sqlite3
type Process struct {
ID int64
RunID int64
Parent sql.NullInt64
Timestamp int64
IsThread bool
ExitCode int64
}
// GetProcesses returns all rows in the processes table of trace.sqlite3
func GetProcesses(db *sql.DB) []Process {
var processes []Process
rows, err := db.Query("SELECT id, run_id, parent, timestamp, is_thread, exitcode FROM processes")
if err != nil {
panic(err)
}
for rows.Next() {
var p Process
err := rows.Scan(&p.ID, &p.RunID, &p.Parent, &p.Timestamp, &p.IsThread, &p.ExitCode)
if err != nil {
fmt.Println(err)
return processes
}
processes = append(processes, p)
}
return processes
}
func WriteProcessFacts(writer io.Writer, processes []Process) {
printRowHeader(writer, "cpr_process(ProcessID, ParentID, RunID, IsThread, ExitCode, TimeStamp).")
for _, p := range processes {
fmt.Fprintln(writer, p)
}
}
// String prints one row of the processes table of trace.sqlite3 as a Prolog fact
func (p Process) String() string {
return fmt.Sprintf("cpr_process(%s, %s, %s, %t, %d, %s).",
P(p.ID), int64OrNil("p", p.Parent), R(p.RunID), p.IsThread, p.ExitCode, timestampInt64(p.Timestamp))
}
func AddProcessTriples(g *rdf.Graph, processes []Process) {
for _, p := range processes {
processURI := ProcessUri(g, p.ID)
if p.IsThread {
continue
//g.AddNewTriple(processURI, "rdf:type", g.NewUri("cpr:Thread"))
} else {
g.AddNewTriple(processURI, "rdf:type", g.NewUri("os:Process"))
g.AddNewTriple(processURI, "os:exitCode", p.ExitCode)
}
if p.Parent.Valid {
g.AddNewTriple(processURI, "os:childProcessOf", ProcessUri(g, p.Parent.Int64))
}
g.AddNewTriple(processURI, "os:startTime", timestampInt64(p.Timestamp))
}
}
func ProcessUri(g *rdf.Graph, id int64) rdf.Uri {
return g.NewUri(fmt.Sprintf("process/%d", id))
}