-
Notifications
You must be signed in to change notification settings - Fork 134
/
trace.go
296 lines (259 loc) · 7.77 KB
/
trace.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
// Copyright (C) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package desktop
import (
"bytes"
"context"
"fmt"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/google/gapid/core/app"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/core/os/device/bind"
"github.com/google/gapid/core/os/process"
"github.com/google/gapid/core/vulkan/loader"
gapii "github.com/google/gapid/gapii/client"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/api/sync"
perfetto "github.com/google/gapid/gapis/perfetto/desktop"
"github.com/google/gapid/gapis/service"
gapis_path "github.com/google/gapid/gapis/service/path"
"github.com/google/gapid/gapis/trace/tracer"
)
const (
// captureProcessNameEnvVar is the environment variable holding the name of
// the process to capture. Mirrored in gapii/cc/spy.cpp.
captureProcessNameEnvVar = "GAPID_CAPTURE_PROCESS_NAME"
)
type DesktopTracer struct {
b bind.Desktop
}
func NewTracer(dev bind.Device) *DesktopTracer {
return &DesktopTracer{dev.(bind.Desktop)}
}
func (t *DesktopTracer) GetDevice() bind.Device {
return t.b
}
func (t *DesktopTracer) ProcessProfilingData(ctx context.Context, buffer *bytes.Buffer,
capture *gapis_path.Capture, staticAnalysisResult chan *api.StaticAnalysisProfileData,
handleMapping map[uint64][]service.VulkanHandleMappingItem, syncData *sync.Data) (*service.ProfilingData, error) {
<-staticAnalysisResult // Ignore the static analysis result.
return nil, log.Err(ctx, nil, "Desktop replay profiling is unsupported.")
}
func (t *DesktopTracer) Validate(ctx context.Context, enableLocalFiles bool) (*service.DeviceValidationResult, error) {
return &service.DeviceValidationResult{}, nil
}
// TraceConfiguration returns the device's supported trace configuration.
func (t *DesktopTracer) TraceConfiguration(ctx context.Context) (*service.DeviceTraceConfiguration, error) {
types := make([]*service.TraceTypeCapabilities, 0, 1)
if len(t.b.Instance().GetConfiguration().GetDrivers().GetVulkan().GetPhysicalDevices()) > 0 {
types = append(types, tracer.VulkanTraceOptions())
}
preferredRoot, err := t.b.GetWorkingDirectory(ctx)
if err != nil {
return nil, err
}
isLocal, err := t.b.IsLocal(ctx)
if err != nil {
return nil, err
}
if t.b.SupportsPerfetto(ctx) {
types = append(types, tracer.PerfettoTraceOptions())
}
return &service.DeviceTraceConfiguration{
Types: types,
ServerLocalPath: isLocal,
CanSpecifyCwd: true,
CanUploadApplication: false,
CanSpecifyEnv: true,
PreferredRootUri: preferredRoot,
HasCache: false,
}, nil
}
// JoinPath provides a path.Join() for this specific target
func (t *DesktopTracer) JoinPath(paths []string) string {
if len(paths) == 0 {
return ""
}
if t.b.Instance().GetConfiguration().GetOS().GetKind() == device.Windows {
if paths[0] == "" {
return path.Clean(strings.Join(paths[1:], "\\"))
}
return path.Clean(strings.Join(paths, "\\"))
} else {
return path.Join(paths...)
}
}
// SplitPath performs a path.Split() operation for this specific target
func (t *DesktopTracer) SplitPath(p string) (string, string) {
if t.b.Instance().GetConfiguration().GetOS().GetKind() == device.Windows {
if runtime.GOOS == "windows" {
return filepath.Split(p)
} else {
lastSlash := strings.LastIndex(p, "\\")
if lastSlash == -1 {
return "", p
}
return p[0 : lastSlash+1], p[lastSlash+1:]
}
} else {
return path.Split(p)
}
}
func (t *DesktopTracer) FindTraceTargets(ctx context.Context, str string) ([]*tracer.TraceTargetTreeNode, error) {
isFile, err := t.b.IsFile(ctx, str)
if err != nil {
return nil, err
}
if !isFile {
return nil, fmt.Errorf("Trace target is not an executable file %+v", str)
}
dir, file := filepath.Split(str)
if dir == "" {
dir = "."
if t.b.Instance().GetConfiguration().GetOS().GetKind() == device.Windows {
str = ".\\" + file
} else {
str = "./" + file
}
}
node := &tracer.TraceTargetTreeNode{
Name: file,
Icon: nil,
URI: str,
TraceURI: str,
Children: nil,
Parent: dir,
ApplicationName: "",
ExecutableName: file,
}
return []*tracer.TraceTargetTreeNode{node}, nil
}
func (t *DesktopTracer) GetTraceTargetNode(ctx context.Context, uri string, iconDensity float32) (*tracer.TraceTargetTreeNode, error) {
dirs := []string{}
files := []string{}
var err error
traceUri := ""
if uri == "" {
uri = t.b.GetURIRoot()
}
isFile, err := t.b.IsFile(ctx, uri)
if err != nil {
return nil, err
}
if !isFile {
dirs, err = t.b.ListDirectories(ctx, uri)
if err != nil {
return nil, err
}
files, err = t.b.ListExecutables(ctx, uri)
if err != nil {
return nil, err
}
} else {
traceUri = uri
}
dir, file := t.SplitPath(uri)
name := file
if name == "" {
name = dir
}
children := append(dirs, files...)
for i := range children {
children[i] = t.JoinPath([]string{uri, children[i]})
if uri == "." {
if t.b.Instance().GetConfiguration().GetOS().GetKind() == device.Windows {
children[i] = ".\\" + children[i]
} else {
children[i] = "./" + children[i]
}
}
}
tttn := &tracer.TraceTargetTreeNode{
Name: name,
Icon: nil,
URI: uri,
TraceURI: traceUri,
Children: children,
Parent: dir,
ApplicationName: "",
ExecutableName: file,
}
return tttn, nil
}
func (t *DesktopTracer) SetupTrace(ctx context.Context, o *service.TraceOptions) (tracer.Process, app.Cleanup, error) {
env, err := t.b.GetEnv(ctx)
if err != nil {
return nil, nil, err
}
var portFile string
var cleanup app.Cleanup
ignorePort := true
if o.Type == service.TraceType_Graphics {
if o.LoadValidationLayer {
// We don't ship desktop builds of the VVL, and we have no guarantee
// that they are present on the desktop.
log.W(ctx, "Loading Vulkan validation layer at capture time is not supported on desktop")
}
cleanup, portFile, err = loader.SetupTrace(ctx, t.b, t.b.Instance().Configuration.ABIs[0], env)
if err != nil {
cleanup.Invoke(ctx)
return nil, nil, err
}
ignorePort = false
}
r := regexp.MustCompile("'.+'|\".+\"|\\S+")
args := r.FindAllString(o.AdditionalCommandLineArgs, -1)
for _, x := range o.Environment {
env.Add(x)
}
if o.ProcessName != "" {
env.Add(captureProcessNameEnvVar + "=" + o.ProcessName)
}
var p tracer.Process
var boundPort int
if o.Type == service.TraceType_Perfetto {
layers := tracer.LayersFromOptions(ctx, o)
c, err := loader.SetupLayers(ctx, layers, false, t.b, t.b.Instance().Configuration.ABIs[0], env)
if err != nil {
cleanup.Invoke(ctx)
}
cleanup = cleanup.Then(c)
p, err = perfetto.Start(ctx, t.b, t.b.Instance().Configuration.ABIs[0], o)
}
if o.GetUri() != "" {
log.D(ctx, "URI provided for trace: %+v", o.GetUri())
boundPort, err = process.StartOnDevice(ctx, o.GetUri(), process.StartOptions{
Env: env,
Args: args,
PortFile: portFile,
WorkingDir: o.Cwd,
Device: t.b,
IgnorePort: ignorePort,
})
}
if err != nil {
cleanup.Invoke(ctx)
return nil, nil, err
}
if p != nil {
return p, cleanup, nil
}
process := &gapii.Process{Port: boundPort, Device: t.b, Options: tracer.GapiiOptions(o)}
return process, cleanup, nil
}