Skip to content

Commit 680caf4

Browse files
committed
Re-add the ability to connect to a server-local local port
This makes it easier to start up applications apart from gapit.
1 parent 36beb54 commit 680caf4

File tree

10 files changed

+125
-66
lines changed

10 files changed

+125
-66
lines changed

cmd/gapit/flags.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,12 @@ type (
211211
No struct {
212212
Buffer bool `help:"Do not buffer the output, this helps if the application crashes"`
213213
}
214-
API string `help:"only capture the given API valid options are gles and vulkan"`
214+
API string `help:"only capture the given API valid options are gles and vulkan"`
215+
Local struct {
216+
Port int `help:"connect to an application already running on the server using this port"`
217+
}
215218
}
219+
216220
PackagesFlags struct {
217221
DeviceFlags
218222
Gapis GapisFlags

cmd/gapit/trace.go

+73-48
Original file line numberDiff line numberDiff line change
@@ -54,80 +54,95 @@ func (verb *traceVerb) Run(ctx context.Context, flags flag.FlagSet) error {
5454

5555
traceURI := verb.URI
5656

57-
if traceURI == "" {
57+
if traceURI == "" && verb.Local.Port == 0 {
5858
if flags.NArg() != 1 {
5959
app.Usage(ctx, "Expected application name")
6060
return nil
6161
}
6262
traceURI = flags.Arg(0)
6363
}
6464

65-
uris := []string{}
66-
traceDevices := []string{}
6765
var traceDevice *path.Device
68-
names := []string{}
69-
// Find the actual trace URI from all of the devices
66+
out := "capture.gfxtrace"
67+
var port uint32
68+
var uri string
7069

71-
devices, err := filterDevices(ctx, &verb.DeviceFlags, client)
72-
if err != nil {
73-
return err
74-
}
75-
76-
if len(devices) == 0 {
77-
return fmt.Errorf("Could not find matching device")
78-
}
79-
80-
for _, dev := range devices {
81-
tttn, err := client.FindTraceTarget(ctx, &service.FindTraceTargetRequest{
82-
Device: dev,
83-
Uri: traceURI,
84-
})
70+
if verb.Local.Port != 0 {
71+
serverInfo, err := client.GetServerInfo(ctx)
8572
if err != nil {
86-
continue
73+
return err
8774
}
88-
89-
dd, err := client.Get(ctx, dev.Path())
75+
traceDevice = serverInfo.GetServerLocalDevice()
76+
if traceDevice.GetID() == nil {
77+
return fmt.Errorf("The server was not started with a local device for tracing")
78+
}
79+
port = uint32(verb.Local.Port)
80+
} else {
81+
uris := []string{}
82+
traceDevices := []string{}
83+
names := []string{}
84+
// Find the actual trace URI from all of the devices
85+
86+
devices, err := filterDevices(ctx, &verb.DeviceFlags, client)
9087
if err != nil {
9188
return err
9289
}
93-
d := dd.(*device.Instance)
94-
95-
uris = append(uris, tttn.Uri)
96-
traceDevices = append(traceDevices, d.Name)
97-
traceDevice = dev
98-
if tttn.FriendlyApplication != "" {
99-
names = append(names, tttn.FriendlyApplication)
100-
} else if tttn.FriendlyExecutable != "" {
101-
names = append(names, tttn.FriendlyExecutable)
102-
} else {
103-
names = append(names, tttn.Name)
90+
91+
if len(devices) == 0 {
92+
return fmt.Errorf("Could not find matching device")
10493
}
105-
}
10694

107-
if len(uris) == 0 {
108-
return fmt.Errorf("Could not find %+v to trace on any device", traceURI)
109-
}
95+
for _, dev := range devices {
96+
tttn, err := client.FindTraceTarget(ctx, &service.FindTraceTargetRequest{
97+
Device: dev,
98+
Uri: traceURI,
99+
})
100+
if err != nil {
101+
continue
102+
}
110103

111-
if len(uris) > 1 {
112-
output := fmt.Sprintf("Found %+v on multiple devices: \n", traceURI)
113-
for i := range uris {
114-
output += fmt.Sprintf(" %+v: %+v\n", traceDevices[i], uris[i])
104+
dd, err := client.Get(ctx, dev.Path())
105+
if err != nil {
106+
return err
107+
}
108+
d := dd.(*device.Instance)
109+
110+
uris = append(uris, tttn.Uri)
111+
traceDevices = append(traceDevices, d.Name)
112+
traceDevice = dev
113+
if tttn.FriendlyApplication != "" {
114+
names = append(names, tttn.FriendlyApplication)
115+
} else if tttn.FriendlyExecutable != "" {
116+
names = append(names, tttn.FriendlyExecutable)
117+
} else {
118+
names = append(names, tttn.Name)
119+
}
120+
}
121+
122+
if len(uris) == 0 {
123+
return fmt.Errorf("Could not find %+v to trace on any device", traceURI)
124+
}
125+
126+
if len(uris) > 1 {
127+
output := fmt.Sprintf("Found %+v on multiple devices: \n", traceURI)
128+
for i := range uris {
129+
output += fmt.Sprintf(" %+v: %+v\n", traceDevices[i], uris[i])
130+
}
131+
return fmt.Errorf("%s", output)
115132
}
116-
return fmt.Errorf("%s", output)
133+
134+
fmt.Printf("Tracing %+v", uris)
135+
out = names[0] + ".gfxtrace"
136+
uri = uris[0]
117137
}
118138

119-
fmt.Printf("Tracing %+v", uris)
120-
out := names[0] + ".gfxtrace"
121139
if verb.Out != "" {
122140
out = verb.Out
123141
}
124142

125143
options := &service.TraceOptions{
126144
Device: traceDevice,
127-
App: &service.TraceOptions_Uri{
128-
uris[0],
129-
},
130-
Apis: []string{},
145+
Apis: []string{},
131146
AdditionalCommandLineArgs: verb.AdditionalArgs,
132147
Cwd: verb.WorkingDir,
133148
Environment: verb.Env,
@@ -145,6 +160,16 @@ func (verb *traceVerb) Run(ctx context.Context, flags flag.FlagSet) error {
145160
ServerLocalSavePath: out,
146161
}
147162

163+
if uri != "" {
164+
options.App = &service.TraceOptions_Uri{
165+
uri,
166+
}
167+
} else {
168+
options.App = &service.TraceOptions_Port{
169+
port,
170+
}
171+
}
172+
148173
switch verb.API {
149174
case "vulkan":
150175
options.Apis = []string{"Vulkan"}

gapis/server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ func optionsToTraceOptions(opts *service.TraceOptions) tracer.TraceOptions {
415415
return tracer.TraceOptions{
416416
URI: opts.GetUri(),
417417
UploadApplication: opts.GetUploadApplication(),
418+
Port: opts.GetPort(),
418419
ClearCache: opts.ClearCache,
419420
APIs: opts.Apis,
420421
WriteFile: opts.ServerLocalSavePath,

gapis/service/service.proto

+18-16
Original file line numberDiff line numberDiff line change
@@ -946,39 +946,41 @@ message TraceOptions {
946946
string uri = 2;
947947
// The application to upload and trace
948948
bytes upload_application = 3;
949+
// The server port that should be connected to for tracing
950+
uint32 port = 4;
949951
}
950952
// What APIs should we trace
951-
repeated string apis = 4;
953+
repeated string apis = 5;
952954
// Any additional command-line args to pass to the app
953-
string additional_command_line_args = 5;
955+
string additional_command_line_args = 6;
954956
// What directory should be used for tracing
955-
string cwd = 6;
957+
string cwd = 7;
956958
// Additional environment variables to add in the form 'X=Y'
957-
repeated string environment = 7;
959+
repeated string environment = 8;
958960
// How long should we trace for
959-
float duration = 8;
961+
float duration = 9;
960962
// What frames should we insert into the trace
961-
uint32 observe_frame_frequency = 9;
963+
uint32 observe_frame_frequency = 10;
962964
// What draw calls should we insert into the trace
963-
uint32 observe_draw_frequency = 10;
965+
uint32 observe_draw_frequency = 11;
964966
// What frame should we start tracing.
965-
uint32 start_frame = 11;
967+
uint32 start_frame = 12;
966968
// How many frames should we capture
967-
uint32 frames_to_capture = 12;
969+
uint32 frames_to_capture = 13;
968970
// Disable disable pre-compiled shaders.
969-
bool disable_pcs = 13;
971+
bool disable_pcs = 14;
970972
// Insert extra commands to record error state
971-
bool record_error_state = 14;
973+
bool record_error_state = 15;
972974
// Wait for an event to start tracing
973-
bool defer_start = 15;
975+
bool defer_start = 16;
974976
// Disable buffering. (In case of a crash)
975-
bool no_buffer = 16;
977+
bool no_buffer = 17;
976978
// Clear the application cache
977-
bool clear_cache = 17;
979+
bool clear_cache = 18;
978980
// Hide unknown extensions
979-
bool hide_unknown_extensions = 18;
981+
bool hide_unknown_extensions = 19;
980982
// Where should we save the capture file.
981-
string server_local_save_path = 19;
983+
string server_local_save_path = 20;
982984
}
983985

984986
enum TraceEvent {

gapis/trace/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ go_library(
3131
"//core/log:go_default_library",
3232
"//core/os/device:go_default_library",
3333
"//core/os/device/bind:go_default_library",
34+
"//gapii/client:go_default_library",
3435
"//gapis/service/path:go_default_library",
3536
"//gapis/trace/android:go_default_library",
3637
"//gapis/trace/desktop:go_default_library",

gapis/trace/android/trace.go

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ type androidTracer struct {
4747
lastPackageUpdate time.Time
4848
}
4949

50+
func (t *androidTracer) GetDevice() bind.Device {
51+
return t.b
52+
}
53+
5054
func (t *androidTracer) GetPackages(ctx context.Context, isRoot bool, iconDensityScale float32) (*pkginfo.PackageList, error) {
5155
refresh := time.Since(t.lastPackageUpdate).Seconds() > packageUpdateTime ||
5256
t.lastIconDensityScale != iconDensityScale

gapis/trace/desktop/trace.go

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func NewTracer(dev bind.Device) *DesktopTracer {
3939
return &DesktopTracer{dev.(bind.Device)}
4040
}
4141

42+
func (t *DesktopTracer) GetDevice() bind.Device {
43+
return t.b
44+
}
45+
4246
// IsServerLocal returns true if all paths on this device can be server-local
4347
func (t *DesktopTracer) IsServerLocal() bool {
4448
return true

gapis/trace/trace.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ import (
2222

2323
"github.com/google/gapid/core/event/task"
2424
"github.com/google/gapid/core/log"
25+
gapii "github.com/google/gapid/gapii/client"
2526
"github.com/google/gapid/gapis/service/path"
2627
"github.com/google/gapid/gapis/trace/tracer"
2728
)
2829

2930
func Trace(ctx context.Context, device *path.Device, start task.Signal, options *tracer.TraceOptions, written *int64) error {
31+
var process *gapii.Process
32+
cleanup := func() {}
33+
var err error
3034
mgr := GetManager(ctx)
3135
if device == nil {
3236
return log.Errf(ctx, nil, "Invalid device path")
@@ -36,7 +40,15 @@ func Trace(ctx context.Context, device *path.Device, start task.Signal, options
3640
return log.Errf(ctx, nil, "Could not find tracer for device %d", device.ID.ID())
3741
}
3842

39-
process, cleanup, err := tracer.SetupTrace(ctx, options)
43+
if options.Port != 0 {
44+
if !tracer.IsServerLocal() {
45+
return log.Errf(ctx, nil, "Cannot attach to a remote device by port")
46+
}
47+
process = &gapii.Process{Port: int(options.Port), Device: tracer.GetDevice(), Options: options.GapiiOptions()}
48+
} else {
49+
process, cleanup, err = tracer.SetupTrace(ctx, options)
50+
}
51+
4052
if err != nil {
4153
return log.Errf(ctx, err, "Could not start trace")
4254
}

gapis/trace/tracer/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ go_library(
2323
importpath = "github.com/google/gapid/gapis/trace/tracer",
2424
visibility = ["//visibility:public"],
2525
deps = [
26+
"//core/os/device/bind:go_default_library",
2627
"//gapii/client:go_default_library",
2728
"//gapis/service:go_default_library",
2829
],

gapis/trace/tracer/tracer.go

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package tracer
1717
import (
1818
"context"
1919

20+
"github.com/google/gapid/core/os/device/bind"
2021
gapii "github.com/google/gapid/gapii/client"
2122
"github.com/google/gapid/gapis/service"
2223
)
@@ -47,6 +48,7 @@ type TraceTargetTreeNode struct {
4748
type TraceOptions struct {
4849
URI string // What application should be traced
4950
UploadApplication []byte // This application should be uploaded and traced
51+
Port uint32 // Connect to a local port instead of launching anything
5052
ClearCache bool // Should the cache be cleared on the device
5153
PortFile string // What port file should be written to
5254
APIs []string // What apis should be traced
@@ -97,6 +99,9 @@ type Tracer interface {
9799
// for the trace to be started. It returns the process that was created, as
98100
// well as a function that can be used to clean up the device
99101
SetupTrace(ctx context.Context, o *TraceOptions) (*gapii.Process, func(), error)
102+
103+
// GetDevice returns the device associated with this tracer
104+
GetDevice() bind.Device
100105
}
101106

102107
// GapiiOptions converts the given TraceOptions to gapii.Options.

0 commit comments

Comments
 (0)