-
Notifications
You must be signed in to change notification settings - Fork 69
/
instruments.go
284 lines (232 loc) · 7.45 KB
/
instruments.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
package giDevice
import (
"encoding/json"
"fmt"
"github.com/electricbubble/gidevice/pkg/libimobiledevice"
)
var _ Instruments = (*instruments)(nil)
func newInstruments(client *libimobiledevice.InstrumentsClient) *instruments {
return &instruments{
client: client,
}
}
type instruments struct {
client *libimobiledevice.InstrumentsClient
}
func (i *instruments) notifyOfPublishedCapabilities() (err error) {
_, err = i.client.NotifyOfPublishedCapabilities()
return
}
func (i *instruments) requestChannel(channel string) (id uint32, err error) {
return i.client.RequestChannel(channel)
}
func (i *instruments) AppLaunch(bundleID string, opts ...AppLaunchOption) (pid int, err error) {
opt := new(appLaunchOption)
opt.appPath = ""
opt.options = map[string]interface{}{
"StartSuspendedKey": uint64(0),
"KillExisting": uint64(0),
}
if len(opts) != 0 {
for _, optFunc := range opts {
optFunc(opt)
}
}
var id uint32
if id, err = i.requestChannel("com.apple.instruments.server.services.processcontrol"); err != nil {
return 0, err
}
args := libimobiledevice.NewAuxBuffer()
if err = args.AppendObject(opt.appPath); err != nil {
return 0, err
}
if err = args.AppendObject(bundleID); err != nil {
return 0, err
}
if err = args.AppendObject(opt.environment); err != nil {
return 0, err
}
if err = args.AppendObject(opt.arguments); err != nil {
return 0, err
}
if err = args.AppendObject(opt.options); err != nil {
return 0, err
}
var result *libimobiledevice.DTXMessageResult
selector := "launchSuspendedProcessWithDevicePath:bundleIdentifier:environment:arguments:options:"
if result, err = i.client.Invoke(selector, args, id, true); err != nil {
return 0, err
}
if nsErr, ok := result.Obj.(libimobiledevice.NSError); ok {
return 0, fmt.Errorf("%s", nsErr.NSUserInfo.(map[string]interface{})["NSLocalizedDescription"])
}
return int(result.Obj.(uint64)), nil
}
func (i *instruments) appProcess(bundleID string) (err error) {
var id uint32
if id, err = i.requestChannel("com.apple.instruments.server.services.processcontrol"); err != nil {
return err
}
args := libimobiledevice.NewAuxBuffer()
if err = args.AppendObject(bundleID); err != nil {
return err
}
selector := "processIdentifierForBundleIdentifier:"
if _, err = i.client.Invoke(selector, args, id, true); err != nil {
return err
}
return
}
func (i *instruments) startObserving(pid int) (err error) {
var id uint32
if id, err = i.requestChannel("com.apple.instruments.server.services.processcontrol"); err != nil {
return err
}
args := libimobiledevice.NewAuxBuffer()
if err = args.AppendObject(pid); err != nil {
return err
}
var result *libimobiledevice.DTXMessageResult
selector := "startObservingPid:"
if result, err = i.client.Invoke(selector, args, id, true); err != nil {
return err
}
if nsErr, ok := result.Obj.(libimobiledevice.NSError); ok {
return fmt.Errorf("%s", nsErr.NSUserInfo.(map[string]interface{})["NSLocalizedDescription"])
}
return
}
func (i *instruments) AppKill(pid int) (err error) {
var id uint32
if id, err = i.requestChannel("com.apple.instruments.server.services.processcontrol"); err != nil {
return err
}
args := libimobiledevice.NewAuxBuffer()
if err = args.AppendObject(pid); err != nil {
return err
}
selector := "killPid:"
if _, err = i.client.Invoke(selector, args, id, false); err != nil {
return err
}
return
}
func (i *instruments) AppRunningProcesses() (processes []Process, err error) {
var id uint32
if id, err = i.requestChannel("com.apple.instruments.server.services.deviceinfo"); err != nil {
return nil, err
}
selector := "runningProcesses"
var result *libimobiledevice.DTXMessageResult
if result, err = i.client.Invoke(selector, libimobiledevice.NewAuxBuffer(), id, true); err != nil {
return nil, err
}
objs := result.Obj.([]interface{})
processes = make([]Process, 0, len(objs))
for _, v := range objs {
m := v.(map[string]interface{})
var data []byte
if data, err = json.Marshal(m); err != nil {
debugLog(fmt.Sprintf("process marshal: %v\n%v\n", err, m))
err = nil
continue
}
var tp Process
if err = json.Unmarshal(data, &tp); err != nil {
debugLog(fmt.Sprintf("process unmarshal: %v\n%v\n", err, m))
err = nil
continue
}
processes = append(processes, tp)
}
return
}
func (i *instruments) AppList(opts ...AppListOption) (apps []Application, err error) {
opt := new(appListOption)
opt.updateToken = ""
opt.appsMatching = make(map[string]interface{})
if len(opts) != 0 {
for _, optFunc := range opts {
optFunc(opt)
}
}
var id uint32
if id, err = i.requestChannel("com.apple.instruments.server.services.device.applictionListing"); err != nil {
return nil, err
}
args := libimobiledevice.NewAuxBuffer()
if err = args.AppendObject(opt.appsMatching); err != nil {
return nil, err
}
if err = args.AppendObject(opt.updateToken); err != nil {
return nil, err
}
selector := "installedApplicationsMatching:registerUpdateToken:"
var result *libimobiledevice.DTXMessageResult
if result, err = i.client.Invoke(selector, args, id, true); err != nil {
return nil, err
}
objs := result.Obj.([]interface{})
for _, v := range objs {
m := v.(map[string]interface{})
var data []byte
if data, err = json.Marshal(m); err != nil {
debugLog(fmt.Sprintf("application marshal: %v\n%v\n", err, m))
err = nil
continue
}
var app Application
if err = json.Unmarshal(data, &app); err != nil {
debugLog(fmt.Sprintf("application unmarshal: %v\n%v\n", err, m))
err = nil
continue
}
apps = append(apps, app)
}
return
}
func (i *instruments) DeviceInfo() (devInfo *DeviceInfo, err error) {
var id uint32
if id, err = i.requestChannel("com.apple.instruments.server.services.deviceinfo"); err != nil {
return nil, err
}
selector := "systemInformation"
var result *libimobiledevice.DTXMessageResult
if result, err = i.client.Invoke(selector, libimobiledevice.NewAuxBuffer(), id, true); err != nil {
return nil, err
}
data, err := json.Marshal(result.Obj)
if err != nil {
return nil, err
}
devInfo = new(DeviceInfo)
err = json.Unmarshal(data, devInfo)
return
}
func (i *instruments) registerCallback(obj string, cb func(m libimobiledevice.DTXMessageResult)) {
i.client.RegisterCallback(obj, cb)
}
type Application struct {
AppExtensionUUIDs []string `json:"AppExtensionUUIDs,omitempty"`
BundlePath string `json:"BundlePath"`
CFBundleIdentifier string `json:"CFBundleIdentifier"`
ContainerBundleIdentifier string `json:"ContainerBundleIdentifier,omitempty"`
ContainerBundlePath string `json:"ContainerBundlePath,omitempty"`
DisplayName string `json:"DisplayName"`
ExecutableName string `json:"ExecutableName,omitempty"`
Placeholder bool `json:"Placeholder,omitempty"`
PluginIdentifier string `json:"PluginIdentifier,omitempty"`
PluginUUID string `json:"PluginUUID,omitempty"`
Restricted int `json:"Restricted"`
Type string `json:"Type"`
Version string `json:"Version"`
}
type DeviceInfo struct {
Description string `json:"_deviceDescription"`
DisplayName string `json:"_deviceDisplayName"`
Identifier string `json:"_deviceIdentifier"`
Version string `json:"_deviceVersion"`
ProductType string `json:"_productType"`
ProductVersion string `json:"_productVersion"`
XRDeviceClassName string `json:"_xrdeviceClassName"`
}