forked from google/agi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
238 lines (201 loc) · 6.45 KB
/
bind.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
// Copyright (C) 2021 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 ffx
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/google/gapid/core/event/task"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/device/bind"
"github.com/google/gapid/core/os/file"
"github.com/google/gapid/core/os/fuchsia"
"github.com/google/gapid/core/os/shell"
"github.com/google/gapid/gapis/service"
)
// binding represents an attached Fuchsia device.
type binding struct {
bind.Simple
}
// verify that binding implements Device
var _ fuchsia.Device = (*binding)(nil)
// FFX is the path to the ffx executable, or an empty string if the ffx
// executable was not found.
var FFX file.Path
func Ffx() (file.Path, error) {
if !FFX.IsEmpty() {
return FFX, nil
}
if ffx_env := os.Getenv("FUCHSIA_FFX_PATH"); ffx_env != "" {
FFX = file.Abs(ffx_env)
if !FFX.IsExecutable() {
return file.Path{}, fmt.Errorf("ffx path: %s is not executable", FFX)
}
return FFX, nil
}
return file.Path{}, fmt.Errorf("FUCHSIA_FFX_PATH is not set. " +
"The \"ffx\" tool, from the Fuchsia SDK, is required for Fuchsia device profiling.\n")
}
// DeviceInfo implements the fuchsia.Device interface.
func (b *binding) DeviceInfo(ctx context.Context) (map[string]string, error) {
stdout, err := b.Command("target", "show", "--json").Call(ctx)
if err != nil {
return nil, log.Errf(ctx, err, "FFX command failed: "+stdout)
}
info := []map[string]interface{}{}
if err := json.Unmarshal([]byte(stdout), &info); err != nil {
return nil, err
}
properties := map[string]string{}
warn := false
for _, parent := range info {
group, _ := parent["label"].(string)
if children, ok := parent["child"].([]interface{}); ok {
for _, child := range children {
if child, ok := child.(map[string]interface{}); ok {
label, _ := child["label"].(string)
value, _ := child["value"].(string)
if value = strings.TrimSpace(value); value != "" {
properties[group+"."+label] = value
}
} else {
warn = true
}
}
} else {
warn = true
}
}
if warn {
log.W(ctx, "Unexpected JSON output for device info command: \n%s", stdout)
}
return properties, nil
}
// Command implements the fuchsia.Device interface.
func (b *binding) Command(name string, args ...string) shell.Cmd {
return shell.Command(name, args...).On(deviceTarget{b})
}
func (b *binding) augmentFFXCommand(cmd shell.Cmd) (shell.Cmd, error) {
exe, err := Ffx()
if err != nil {
return cmd, err
}
// Adjust the ffx command to use a specific target:
// "ffx -t <target> <cmd.Name> <cmd.Args...>"
old := cmd.Args
cmd.Args = make([]string, 0, len(old)+4)
cmd.Args = append(cmd.Args, "-t", b.To.Serial)
cmd.Args = append(cmd.Args, cmd.Name)
cmd.Args = append(cmd.Args, old...)
cmd.Name = exe.System()
fmt.Println(cmd)
// And delegate to the normal local target
return cmd, nil
}
// TraceProviders implements the fuchsia.Device interface.
func (b *binding) TraceProviders(ctx context.Context) ([]string, error) {
exe, err := Ffx()
if err != nil {
return nil, err
}
cmd, err := b.augmentFFXCommand(shell.Command(exe.System(), "trace", "list-providers"))
if err != nil {
return nil, err
}
providersStdOut, err := cmd.Call(ctx)
if strings.Contains(providersStdOut, "No devices found") {
return nil, ErrNoDeviceList
}
lines := strings.Split(providersStdOut, "\n")
var providers []string
for _, line := range lines {
if strings.HasPrefix(line, "- ") {
tokens := strings.Split(line, " ")
if len(tokens) == 2 {
providers = append(providers, tokens[1])
} else {
return nil, ErrTraceProvidersFormat
}
}
}
return providers, nil
}
// StartTrace implements the fuchsia.Device interface and starts a Fuchsia trace.
func (b *binding) StartTrace(ctx context.Context, options *service.TraceOptions, traceFile file.Path, stop task.Signal, ready task.Task) error {
var cmd shell.Cmd
switch t := options.GetType(); t {
case service.TraceType_Graphics:
if fuchsiaConfig := options.GetFuchsiaTraceConfig(); fuchsiaConfig != nil {
// Initialize shell command.
cmd = b.Command("agis", "listen", strconv.FormatInt(int64(fuchsiaConfig.GetGlobalId()), 10))
} else {
return fmt.Errorf("fuchsiaConfig is nil for graphics trace")
}
case service.TraceType_Fuchsia:
var categoriesArg string
// Initialize shell command.
cmd = b.Command("trace", "start", "--output", traceFile.System())
// Extract trace options and append arguments to command.
if options != nil {
if durationSecs := int(options.Duration); durationSecs > 0 {
cmd = cmd.With("--duration", strconv.Itoa(durationSecs))
}
// ffx expects a comma delimited list of trace categories.
if fuchsiaConfig := options.GetFuchsiaTraceConfig(); fuchsiaConfig != nil {
categories := fuchsiaConfig.GetCategories()
categoriesArg = strings.Join(categories, ",")
if len(categoriesArg) > 0 {
cmd = cmd.With("--categories", categoriesArg)
}
}
}
default:
return fmt.Errorf("Unknown Fuchsia trace type: %d", t)
}
log.I(ctx, "StartTrace is calling `ffx agis listen <global_id>`")
stdout, err := cmd.Call(ctx)
if err != nil {
return log.Err(ctx, err, stdout)
}
if strings.Contains(stdout, "No devices found") {
return ErrNoDeviceList
}
return nil
}
// StopTrace implements the fuchsia.Device interface and stops a Fuchsia trace.
func (b *binding) StopTrace(ctx context.Context, traceFile file.Path) error {
cmd := b.Command("trace", "stop", "--output", traceFile.System())
stdout, err := cmd.Call(ctx)
if err != nil {
return log.Err(ctx, err, stdout)
}
if strings.Contains(stdout, "No devices found") {
return ErrNoDeviceList
}
return nil
}
type deviceTarget struct{ b *binding }
func (t deviceTarget) Start(cmd shell.Cmd) (shell.Process, error) {
cmd, err := t.b.augmentFFXCommand(cmd)
if err != nil {
return nil, err
}
return shell.LocalTarget.Start(cmd)
}
func (t deviceTarget) String() string {
return "command:" + t.b.String()
}