diff --git a/bin/adb b/bin/adb index 87ea217..fa8dbdc 100755 --- a/bin/adb +++ b/bin/adb @@ -69,8 +69,33 @@ if [ -z "${ANDROID_SERIAL:-}" ]; then devices | start-server | kill-server | version | help | connect | disconnect) ;; *) if [ "$named" = false ]; then - serial=$(avd_serial "$real" "$(project_avd)" || true) - [ -n "$serial" ] && target=(-s "$serial") + avd=$(project_avd) + serial=$(avd_serial "$real" "$avd" || true) + if [ -n "$serial" ]; then + target=(-s "$serial") + else + # This checkout's emulator is not running, and something + # else's is. Handing the call through would drive their + # device -- installing over the app they are looking at, + # or taking their foreground -- and it would look like it + # worked. A physical phone is not somebody's checkout and + # is left alone, which is why this asks for the AVD name + # rather than counting devices. + others=$(running_avds "$real" | awk -F'\t' -v mine="$avd" '$2 != mine') + if [ -n "$others" ]; then + { + echo "adb: '$avd' is not running, and these emulators belong to other checkouts:" + echo "$others" | while IFS=$'\t' read -r s n; do + printf ' %-16s %s\n' "$s" "$n" + done + echo + echo " emu up start this checkout's own" + echo " adb -s SERIAL ... or say which you meant" + echo " ANDROID_SERIAL=SERIAL for a run of them" + } >&2 + exit 1 + fi + fi fi ;; esac diff --git a/bin/emu b/bin/emu index 3449aa9..52630f4 100755 --- a/bin/emu +++ b/bin/emu @@ -59,8 +59,15 @@ cmd_list() { found=false while IFS=$'\t' read -r serial name; do found=true - port=${serial#emulator-} - rss=$(ps -eo rss,args | awk -v p="-port $port" '$0 ~ p && $0 ~ /qemu-system/ {print int($1/1024); exit}') + # Matched on the AVD name the emulator was started with, which is + # what its process actually carries -- there is no -port in that + # command line, and the console port in the serial does not appear + # in it. Filtered on the process *name* rather than on the whole + # line, so this cannot match the shell running it: that shell's + # command line contains this pattern, and a `ps | awk` that matches + # itself reports the size of the shell. + rss=$(ps -eo rss=,comm=,args= | + awk -v want="-avd $name" '$2 ~ /^qemu-system/ && index($0, want) {print int($1/1024); exit}') printf ' %-16s %-24s %s\n' "$serial" "$name" "${rss:+$rss MB}" done < <(running_avds "$adb") [ "$found" = true ] || echo " (none attached)"