Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adb: Use uid to limit logcat to the current application #131

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions xbuild/src/devices/adb.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::AndroidDebugConfig;
use crate::devices::{Backend, Device};
use crate::{Arch, Platform};
use anyhow::Result;
use anyhow::{Context, Result};
use apk::Apk;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -206,32 +206,35 @@ impl Adb {
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: {}",
std::str::from_utf8(&output.stderr)?.trim()
);
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.contains(' ') {
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
}
println!("pid of {} is {}", id, pid);
return Ok(pid.parse()?);
}
fn uidof(&self, device: &str, id: &str) -> Result<u32> {
let output = self
.shell(device, None)
.arg("pm")
.arg("list")
.arg("package")
.arg("-U")
.arg(id)
.output()?;
anyhow::ensure!(
output.status.success(),
"failed to get uid: {}",
std::str::from_utf8(&output.stderr)?.trim()
);
let output = std::str::from_utf8(&output.stdout)?;
let uid = output
.split_whitespace()
.find_map(|kv| kv.strip_prefix("uid:"))
.with_context(|| format!("Could not find `uid:`` in output `{output}`"))?;
Ok(uid.parse()?)
}

fn logcat(&self, device: &str, pid: u32, last_timestamp: &str) -> Result<Logcat> {
fn logcat(&self, device: &str, uid: u32, last_timestamp: &str) -> Result<Logcat> {
let child = self
.shell(device, None)
.arg("logcat")
.arg("-T")
.arg(format!("'{}'", last_timestamp))
.arg(format!("--pid={}", pid))
.arg(format!("--uid={}", uid))
.stdin(Stdio::null())
.stdout(Stdio::piped())
.spawn()?;
Expand Down Expand Up @@ -343,8 +346,8 @@ impl Adb {
self.forward_reverse(device, debug_config)?;
let last_timestamp = self.logcat_last_timestamp(device)?;
self.start(device, package, activity)?;
let pid = self.pidof(device, package)?;
let logcat = self.logcat(device, pid, &last_timestamp)?;
let uid = self.uidof(device, package)?;
let logcat = self.logcat(device, uid, &last_timestamp)?;
for line in logcat {
println!("{}", line);
}
Expand Down