|
1 | 1 | use crate::config::AndroidDebugConfig; |
2 | 2 | use crate::devices::{Backend, Device}; |
3 | 3 | use crate::{Arch, Platform}; |
4 | | -use anyhow::Result; |
| 4 | +use anyhow::{Context, Result}; |
5 | 5 | use apk::Apk; |
6 | 6 | use std::io::{BufRead, BufReader}; |
7 | 7 | use std::path::{Path, PathBuf}; |
@@ -206,32 +206,35 @@ impl Adb { |
206 | 206 | Ok(line[..18].to_string()) |
207 | 207 | } |
208 | 208 |
|
209 | | - fn pidof(&self, device: &str, id: &str) -> Result<u32> { |
210 | | - loop { |
211 | | - let output = self.shell(device, None).arg("pidof").arg(id).output()?; |
212 | | - anyhow::ensure!( |
213 | | - output.status.success(), |
214 | | - "failed to get pid: {}", |
215 | | - std::str::from_utf8(&output.stderr)?.trim() |
216 | | - ); |
217 | | - let pid = std::str::from_utf8(&output.stdout)?.trim(); |
218 | | - // may return multiple space separated pids if the old process hasn't exited yet. |
219 | | - if pid.is_empty() || pid.contains(' ') { |
220 | | - std::thread::sleep(std::time::Duration::from_millis(100)); |
221 | | - continue; |
222 | | - } |
223 | | - println!("pid of {} is {}", id, pid); |
224 | | - return Ok(pid.parse()?); |
225 | | - } |
| 209 | + fn uidof(&self, device: &str, id: &str) -> Result<u32> { |
| 210 | + let output = self |
| 211 | + .shell(device, None) |
| 212 | + .arg("pm") |
| 213 | + .arg("list") |
| 214 | + .arg("package") |
| 215 | + .arg("-U") |
| 216 | + .arg(id) |
| 217 | + .output()?; |
| 218 | + anyhow::ensure!( |
| 219 | + output.status.success(), |
| 220 | + "failed to get uid: {}", |
| 221 | + std::str::from_utf8(&output.stderr)?.trim() |
| 222 | + ); |
| 223 | + let output = std::str::from_utf8(&output.stdout)?; |
| 224 | + let uid = output |
| 225 | + .split_whitespace() |
| 226 | + .find_map(|kv| kv.strip_prefix("uid:")) |
| 227 | + .with_context(|| format!("Could not find `uid:`` in output `{output}`"))?; |
| 228 | + Ok(uid.parse()?) |
226 | 229 | } |
227 | 230 |
|
228 | | - fn logcat(&self, device: &str, pid: u32, last_timestamp: &str) -> Result<Logcat> { |
| 231 | + fn logcat(&self, device: &str, uid: u32, last_timestamp: &str) -> Result<Logcat> { |
229 | 232 | let child = self |
230 | 233 | .shell(device, None) |
231 | 234 | .arg("logcat") |
232 | 235 | .arg("-T") |
233 | 236 | .arg(format!("'{}'", last_timestamp)) |
234 | | - .arg(format!("--pid={}", pid)) |
| 237 | + .arg(format!("--uid={}", uid)) |
235 | 238 | .stdin(Stdio::null()) |
236 | 239 | .stdout(Stdio::piped()) |
237 | 240 | .spawn()?; |
@@ -343,8 +346,8 @@ impl Adb { |
343 | 346 | self.forward_reverse(device, debug_config)?; |
344 | 347 | let last_timestamp = self.logcat_last_timestamp(device)?; |
345 | 348 | self.start(device, package, activity)?; |
346 | | - let pid = self.pidof(device, package)?; |
347 | | - let logcat = self.logcat(device, pid, &last_timestamp)?; |
| 349 | + let uid = self.uidof(device, package)?; |
| 350 | + let logcat = self.logcat(device, uid, &last_timestamp)?; |
348 | 351 | for line in logcat { |
349 | 352 | println!("{}", line); |
350 | 353 | } |
|
0 commit comments