-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
adb.rs
402 lines (374 loc) · 12.3 KB
/
adb.rs
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use crate::devices::{Backend, Device, PartialRunner};
use crate::{Arch, Platform};
use anyhow::Result;
use apk::Apk;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdout, Command, Stdio};
use std::time::Duration;
#[derive(Clone, Debug)]
pub(crate) struct Adb(PathBuf);
impl Adb {
pub fn which() -> Result<Self> {
Ok(Self(which::which(exe!("adb"))?))
}
fn adb(&self, device: &str) -> Command {
let mut cmd = Command::new(&self.0);
cmd.arg("-s").arg(device);
cmd
}
fn push(&self, device: &str, path: &Path) -> Result<()> {
let status = self
.adb(device)
.arg("push")
.arg("--sync")
.arg(path)
.arg("/data/local/tmp")
.status()?;
anyhow::ensure!(status.success(), "adb push failed");
Ok(())
}
fn shell(&self, device: &str, run_as: Option<&str>) -> Command {
let mut cmd = self.adb(device);
cmd.arg("shell").arg("-x");
if let Some(package) = run_as {
cmd.arg("run-as").arg(package);
}
cmd
}
pub fn devices(&self, devices: &mut Vec<Device>) -> Result<()> {
let output = Command::new(&self.0).arg("devices").output()?;
anyhow::ensure!(
output.status.success(),
"adb devices exited with code {:?}",
output.status.code()
);
let mut lines = std::str::from_utf8(&output.stdout)?.lines();
lines.next();
for line in lines {
if let Some(id) = line.split_whitespace().next() {
devices.push(Device {
backend: Backend::Adb(self.clone()),
id: id.to_string(),
});
}
}
Ok(())
}
fn getprop(&self, device: &str, prop: &str) -> Result<String> {
let output = self.shell(device, None).arg("getprop").arg(prop).output()?;
anyhow::ensure!(
output.status.success(),
"adb getprop exited with code {:?}",
output.status.code()
);
Ok(std::str::from_utf8(&output.stdout)?.trim().to_string())
}
fn install(&self, device: &str, path: &Path) -> Result<()> {
let file_name = path.file_name().unwrap().to_str().unwrap();
self.push(device, path)?;
let status = self
.shell(device, None)
.arg("pm")
.arg("install")
.arg(format!("/data/local/tmp/{}", file_name))
.status()?;
anyhow::ensure!(
status.success(),
"adb pm install exited with code {:?}",
status.code()
);
Ok(())
}
/// To run a native activity use "android.app.NativeActivity" as the activity name
fn start(&self, device: &str, package: &str, activity: &str) -> Result<()> {
let status = self
.shell(device, None)
.arg("am")
.arg("start")
.arg("-a")
.arg("android.intent.action.RUN")
.arg("-n")
.arg(format!("{}/{}", package, activity))
.status()?;
anyhow::ensure!(
status.success(),
"adb shell am start exited with code {:?}",
status.code()
);
Ok(())
}
fn stop(&self, device: &str, id: &str) -> Result<()> {
let status = self
.shell(device, None)
.arg("am")
.arg("force-stop")
.arg(id)
.status()?;
anyhow::ensure!(
status.success(),
"adb shell am force-stop exited with code {:?}",
status.code()
);
Ok(())
}
fn set_debug_app(&self, device: &str, package: &str) -> Result<()> {
let status = self
.shell(device, None)
.arg("am")
.arg("set-debug-app")
.arg("-w")
.arg(package)
.status()?;
anyhow::ensure!(
status.success(),
"adb shell am set-debug-app exited with code {:?}",
status.code()
);
Ok(())
}
fn clear_debug_app(&self, device: &str) -> Result<()> {
let status = self
.shell(device, None)
.arg("am")
.arg("clear-debug-app")
.status()?;
anyhow::ensure!(
status.success(),
"adb shell am clear-debug-app exited with code {:?}",
status.code()
);
Ok(())
}
fn logcat_last_timestamp(&self, device: &str) -> Result<String> {
let output = self
.shell(device, None)
.arg("logcat")
.arg("-v")
.arg("time")
.arg("-t")
.arg("1")
.output()?;
anyhow::ensure!(
output.status.success(),
"adb logcat exited with code {:?}",
output.status.code()
);
let line = std::str::from_utf8(&output.stdout)?.lines().nth(1).unwrap();
Ok(line[..18].to_string())
}
fn pidof(&self, device: &str, id: &str) -> Result<u32> {
loop {
let output = self.shell(device, None).arg("pidof").arg(id).output()?;
anyhow::ensure!(output.status.success(), "failed to get pid");
let pid = std::str::from_utf8(&output.stdout)?.trim();
// may return multiple space separated pids if the old process hasn't exited yet.
if pid.is_empty() || pid.split_once(' ').is_some() {
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
}
println!("pid of {} is {}", id, pid);
return Ok(pid.parse()?);
}
}
fn logcat(&self, device: &str, pid: u32, last_timestamp: &str) -> Result<Logcat> {
let child = self
.shell(device, None)
.arg("logcat")
.arg("-T")
.arg(format!("'{}'", last_timestamp))
.arg(format!("--pid={}", pid))
.stdin(Stdio::null())
.stdout(Stdio::piped())
.spawn()?;
Ok(Logcat::new(child))
}
pub fn forward(&self, device: &str, port: u16) -> Result<u16> {
let output = self
.adb(device)
.arg("forward")
.arg("tcp:0")
.arg(format!("tcp:{}", port))
.output()?;
anyhow::ensure!(
output.status.success(),
"adb forward exited with code {:?}",
output.status.code()
);
Ok(std::str::from_utf8(&output.stdout)?.trim().parse()?)
}
/*fn app_dir(&self, device: &str, package: &str) -> Result<PathBuf> {
let output = self
.shell(device, Some(package))
.arg("sh")
.arg("-c")
.arg("pwd")
.output()?;
anyhow::ensure!(output.status.success(), "failed to get app dir");
Ok(Path::new(std::str::from_utf8(&output.stdout)?.trim()).to_path_buf())
}*/
pub fn lldb(&self, device: &str, lldb_server: &Path, executable: &Path) -> Result<()> {
/*let package = env.manifest().android().package.as_ref().unwrap();
let app_dir = self.app_dir(device, package)?;
self.shell(device, Some(package))
.arg("chmod")
.arg("a+x")
.arg(&app_dir)
.status()?;
let dest = app_dir.join("lldb-server");*/
self.push(device, lldb_server)?;
/*self.shell(device, None)
.arg("cat")
.arg("/data/local/tmp/lldb-server")
.arg("|")
.arg("run-as")
.arg(package)
.arg("sh")
.arg("-c")
.arg(format!("'cat > {}'", dest.display()))
.status()?;
self.shell(device, Some(package))
.arg("chmod")
.arg("700")
.arg(&dest)
.status()?;*/
let mut lldb_server = self
.shell(device, None)
.arg("cd")
.arg("/data/local/tmp")
.arg("&&")
.arg("./lldb-server")
.arg("platform")
.arg("--listen")
.arg("*:10086")
.arg("--server")
.stdin(Stdio::null())
.spawn()?;
std::thread::sleep(Duration::from_millis(100));
self.forward(device, 10086)?;
let status = Command::new("lldb")
.arg("-O")
.arg("platform select remote-android")
//.arg("-O")
//.arg(format!("platform settings -w {}", app_dir.display()))
//.arg("platform settings -w /data/local/tmp")
.arg("-O")
.arg(format!("platform connect connect://{}:10086", device))
.arg(executable)
.status()?;
anyhow::ensure!(status.success(), "lldb exited with nonzero exit code.");
lldb_server.kill()?;
Ok(())
}
pub fn run(
&self,
device: &str,
path: &Path,
flutter_attach: bool,
debug: bool,
) -> Result<PartialRunner> {
let entry_point = Apk::entry_point(path)?;
let package = &entry_point.package;
let activity = &entry_point.activity;
self.stop(device, package)?;
if debug {
self.set_debug_app(device, package)?;
} else {
self.clear_debug_app(device)?;
}
self.install(device, path)?;
let last_timestamp = self.logcat_last_timestamp(device)?;
self.start(device, package, activity)?;
let pid = self.pidof(device, package)?;
let mut logcat = self.logcat(device, pid, &last_timestamp)?;
let url = if flutter_attach {
let url = loop {
if let Some(line) = logcat.next() {
if let Some((_, url)) = line.rsplit_once(' ') {
if url.starts_with("http") {
break url.trim().to_string();
}
}
println!("{}", line);
}
};
Some(url)
} else {
None
};
Ok(PartialRunner {
url,
logger: Box::new(move || {
for line in logcat {
println!("{}", line);
}
}),
child: None,
})
}
pub fn name(&self, device: &str) -> Result<String> {
self.getprop(device, "ro.product.device")
}
pub fn platform(&self, _device: &str) -> Result<Platform> {
Ok(Platform::Android)
}
pub fn arch(&self, device: &str) -> Result<Arch> {
let arch = match self.getprop(device, "ro.product.cpu.abi")?.as_str() {
"arm64-v8a" => Arch::Arm64,
//"armeabi-v7a" => Arch::Arm,
"x86_64" => Arch::X64,
//"x86" => Arch::X86,
abi => anyhow::bail!("unrecognized abi {}", abi),
};
Ok(arch)
}
pub fn details(&self, device: &str) -> Result<String> {
let release = self.getprop(device, "ro.build.version.release")?;
let sdk = self.getprop(device, "ro.build.version.sdk")?;
Ok(format!("Android {} (API {})", release, sdk))
}
}
pub struct Logcat {
child: Child,
reader: BufReader<ChildStdout>,
line: String,
}
impl Logcat {
fn new(mut child: Child) -> Self {
let stdout = child.stdout.take().expect("child missing stdout");
let reader = BufReader::new(stdout);
Self {
child,
reader,
line: String::with_capacity(1024),
}
}
}
impl Iterator for Logcat {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
loop {
self.line.clear();
match self.reader.read_line(&mut self.line) {
Ok(0) => return None,
Ok(_) => {
let line = self.line.trim();
if let Some((date, line)) = line.split_once(' ') {
if let Some((time, line)) = line.split_once(' ') {
if date.len() == 5 && time.len() == 12 {
return Some(line.to_string());
}
}
}
return Some(self.line.clone());
}
Err(_) => {}
}
}
}
}
impl Drop for Logcat {
fn drop(&mut self) {
self.child.kill().ok();
}
}