Is it possible to also display the Log's Package Name in each line?
Using
logcat -v long
leaves exactly the package name field (after PID) empty.
I want to filter the Logs from a specific application with different Tags, just wondering if it is possible.
logcat record does not have a "package name field". Therefore there is no standard/built-in way to filter by it.
Although since Android 7.0 you can use logcat --pid option combined with pidof -s command to filter output by binary/package name:
adb shell "logcat --pid=$(pidof -s <package_name>)"
Replace " with ' for Linux/MacOS
This is my script. A little rusty but still working.
PID=`adb shell ps | grep -i <your package name> | cut -c10-15`;
adb logcat | grep $PID
Actually I have a python script to add more colours, and a while loop to keep monitoring for that process when it gets restarted, but I think you'll get the point.
Option 1
Enter these line by line:
adb shell
su
bash
PKG_PID_LIST_CACHE=$(eval $(pm list packages | cut -d ':' -f 2 | sed 's/^\(.*\)$/echo \"\$\(echo \1\) \$\(pidof \1\)\";/'))
PROC_PID_LIST_CACHE=$(ps -A -o NAME -o PID)
PID_LIST_CACHE=$(echo "$PKG_PID_LIST_CACHE\n$PROC_PID_LIST_CACHE")
function pid2pkg() { pkgName=$(echo "$PID_LIST_CACHE" | grep -w $1 | cut -d ' ' -f1 | head -1); if [ "$pkgName" != "" ] ; then echo $pkgName; else echo "*NOT RUNNING*"; fi }
eval "$(logcat -d | sed -r -e 's/([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +(.*)/\2 \$\(pid2pkg \3\) \5/g' | sed -r -e 's/(.+)/echo -e \"\1\"/g')"
The logcat output will be in the following format:
[Time] [Name] [Type] [Message]
Example:
14:34:59.386 wpa_supplicant E wpa_supplicant: nl80211: Failed to set IPv4 unicast in multicast filter
14:35:02.231 com.android.phone D TelephonyProvider: subIdString = 1 subId = 1
14:35:03.469 android.hardware.wifi#1.0-service E WifiHAL : wifi_get_logger_supported_feature_set: Error -3 happened.
14:35:03.518 system_server I WifiService: getWifiApEnabledState uid=10086
14:35:03.519 dev.ukanth.ufirewall D AFWall : isWifiApEnabled is false
14:35:03.520 system_server I GnssLocationProvider: WakeLock released by handleMessage(UPDATE_NETWORK_STATE, 0, 123)
14:35:03.522 dev.ukanth.ufirewall I AFWall : Now assuming wifi connection
Some system processes don't have packages. system_server and wpa_supplicant for instance. If a package name can't be found, the process name will be displayed instead.
Caveats:
If the process behind a certain PID is not running anymore, you can't get the package/process name anymore.
After a process exited itself or your device restarted, the PIDs may actually be assigned to completely different processes.
So you might want to check for how long the process has actually been running:
ps -e -o pid -o stime -o name
Option 2
If you want the formatting to be a bit more readable, you can copy my logging script to your device and execute it:
better_logging.sh
PKG_PID_LIST_CACHE=$(eval $(pm list packages | cut -d ':' -f 2 | sed 's/^\(.*\)$/echo \"\$\(echo \1\) \$\(pidof \1\)\";/'))
PROC_PID_LIST_CACHE=$(ps -A -o NAME -o PID)
PID_LIST_CACHE=$(echo "$PKG_PID_LIST_CACHE\n$PROC_PID_LIST_CACHE")
MAX_LEN=$(echo "$PID_LIST_CACHE" | cut -d ' ' -f1 | awk '{ if ( length > L ) { L=length} }END{ print L}')
function pid2pkg() {
pkgName=$(echo "$PID_LIST_CACHE" | grep -w $1 | cut -d ' ' -f1 | head -1);
if [ "$pkgName" != "" ] ; then
printf "%-${MAX_LEN}s" "$pkgName";
else
printf "%-${MAX_LEN}s" "<UNKNOWN (NOT RUNNING)>";
fi
}
eval "$(\
logcat -d | \
sed -r -e 's/([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +(.*)/\2\ $\(pid2pkg \3\) \5/g' | \
sed -r -e 's/(.+)/echo -e \"\1\"/g' \
)" | \
awk '
function color(c,s) {
printf("\033[%dm%s\033[0m\n",90+c,s)
}
/ E / {color(1,$0);next}
/ D / {color(2,$0);next}
/ W / {color(3,$0);next}
/ I / {color(4,$0);next}
{print}
'
To copy and run it, you can use:
adb push better_logging.sh /sdcard/better_logging.sh
adb shell "bash /sdcard/better_logging.sh"
Output will look like this:
This works with awk.
adb logcat | grep -F "`adb shell ps | grep com.yourpackage | awk '{print $2;}'`"
Related
I made a magisk module with a customize.sh like this and nothing else:
if [ $API -gt 23 ]
then
BROMITE=`curl -I --silent https://github.com/bromite/bromite/releases/latest | grep 'location:' | cut -d':' -f2,3 | tr -d '[:blank:]\r' | sed -e 's/tag/download/g'`
ui_print "Installing Bromite webview for $ARCH..."
mkdir -p $MODPATH/system/app/bromite/
wget $BROMITE/"$ARCH"_SystemWebView.apk -P $MODPATH
mv $MODPATH/"$ARCH"_SystemWebView.apk $MODPATH/system/app/bromite/bromite.apk
ui_print "Setting file permissions..."
set_perm_recursive $MODPATH/system/ 0 0 0755 0644
else
abort "Your Android version is too old."
fi
When trying to install it, after printing 'Installing Bromite webview for arm64...', it's immediately done and there's nothing except for the files already in the .zip file and the created directories in there.
I ran this on my PC:
The variables at the top (except for 'BROMITE') are set by the Magisk installer.
API=24
ARCH=arm64
MODPATH=/home/user/Documents/Module/test
if [ $API -gt 23 ]; then
BROMITE=`curl -I --silent https://github.com/bromite/bromite/releases/latest | grep 'location:' | cut -d':' -f2,3 | tr -d '[:blank:]\r' | sed -e 's/tag/download/g'`
mkdir -p $MODPATH/system/app/bromite/
wget $BROMITE/"$ARCH"_SystemWebView.apk -P $MODPATH
mv $MODPATH/"$ARCH"_SystemWebView.apk $MODPATH/system/app/bromite/bromite.apk
fi
And everything worked, the .apk file was where it's intended to be and I have no idea why it's not working on Android.
I'm developing an application that uses ADB Shell to interface with android devices, and I need some way of printing out the application name or label of an application, given maybe their package name.
In short, I need a way of getting app names (i.e. "Angry Birds v1.0.0") for user installed applications through adb shell.
Any light on the matter? Any help is appreciated on this.
adb shell pm list packages will give you a list of all installed package names.
You can then use dumpsys | grep -A18 "Package \[my.package\]" to grab the package information such as version identifiers etc
just enter the following command on command prompt after launching the app:
adb shell dumpsys window windows | find "mCurrentFocus"
if executing the command on linux terminal replace find by grep
If you know the app id of the package (like org.mozilla.firefox), it is easy.
First to get the path of actual package file of the appId,
$ adb shell pm list packages -f com.google.android.apps.inbox
package:/data/app/com.google.android.apps.inbox-1/base.apk=com.google.android.apps.inbox
Now you can do some grep|sed magic to extract the path : /data/app/com.google.android.apps.inbox-1/base.apk
After that aapt tool comes in handy :
$ adb shell aapt dump badging /data/app/com.google.android.apps.inbox-1/base.apk
...
application-label:'Inbox'
application-label-hi:'Inbox'
application-label-ru:'Inbox'
...
Again some grep magic to get the Label.
A shell script to accomplish this:
#!/bin/bash
# Remove whitespace
function remWS {
if [ -z "${1}" ]; then
cat | tr -d '[:space:]'
else
echo "${1}" | tr -d '[:space:]'
fi
}
for pkg in $(adb shell pm list packages -3 | cut -d':' -f2); do
apk_loc="$(adb shell pm path $(remWS $pkg) | cut -d':' -f2 | remWS)"
apk_name="$(adb shell aapt dump badging $apk_loc | pcregrep -o1 $'application-label:\'(.+)\'' | remWS)"
apk_info="$(adb shell aapt dump badging $apk_loc | pcregrep -o1 '\b(package: .+)')"
echo "$apk_name v$(echo $apk_info | pcregrep -io1 -e $'\\bversionName=\'(.+?)\'')"
done
Inorder to find an app's name (application label), you need to do the following:
(as shown in other answers)
Find the APK path of the app whose name you want to find.
Using aapt command, find the app label.
But devices don't ship with the aapt binary out-of-the-box.
So you will need to install it first. You can download it from here:
https://github.com/Calsign/APDE/tree/master/APDE/src/main/assets/aapt-binaries
Check this guide for complete steps:
How to find an app name using package name through ADB Android?
(Disclaimer: I am the author of that blog post)
This is what I just came up with. It gives a few errors but works well enough for my needs, matching package names to labels.
It pulls copies of all packages into subdirectories of $PWD, so keep that in mind if storage is a concern.
#!/bin/bash
TOOLS=~/Downloads/adt-bundle-linux-x86_64-20130717/sdk/build-tools/19.1.0
AAPT=$TOOLS/aapt
PMLIST=adb_shell_pm_list_packages_-f.txt
TEMP=$(echo $(adb shell mktemp -d -p /data/local/tmp) | sed 's/\r//')
mkdir -p packages
[ -f $PMLIST ] || eval $(echo $(basename $PMLIST) | tr '_' ' ') > $PMLIST
while read line; do
package=${line##*:}
apk=${package%%=*}
name=${package#*=}
copy=packages$apk
mkdir -p $(dirname $copy)
if [ ! -s $copy ]; then # copy it because `adb pull` doesn't see /mnt/expand/
adb shell cp -f $apk $TEMP/copy.apk
adb pull $TEMP/copy.apk $copy
fi
label=$($AAPT dump badging $copy || echo ERROR in $copy >&2 | \
sed -n 's/^application-label:\(.\)\(.*\)\1$/\2/p')
echo $name:$label
done < <(sed 's/\r//' $PMLIST)
adb shell rm -rf $TEMP
So I extremely grateful to jcomeau_ictx for providing the info on how to extract application-label info from apk and the idea to pull apk from phone directly!
However I had to make several alteration to script it self:
while read line; do done are breaking as a result of commands within while loop interacting with stdin/stdout and as a result while loop runs only once and then stops, as it is discussed in While loop stops reading after the first line in Bash - the comment from cmo I used solution provided and switched while loop to use unused file descriptor number 9.
All that the script really need is a package name and adb shell pm list packages -f is really excessive so I changed it to expect a file with packages list only and provided example on how one can get one from adb.
jcomeau_ictx script variant do not take in to account that some packages may have multiple apk associated with them which breaks the script.
And the least and last, I made every variable to start with underscore, it's just something that makes it easier to read script.
So here another variant of the same script:
#!/bin/bash
_TOOLS=/opt/android-sdk-update-manager/build-tools/29.0.3
_AAPT=${_TOOLS}/aapt
#adb shell pm list packages --user 0 | sed -e 's|^package:||' | sort >./packages_list.txt
_PMLIST=packages_list.txt
rm ./packages_list_with_names.txt
_TEMP=$(echo $(adb shell mktemp -d -p /data/local/tmp) | sed 's/\r//')
mkdir -p packages
[ -f ${_PMLIST} ] || eval $(echo $(basename ${_PMLIST}) | tr '_' ' ') > ${_PMLIST}
while read -u 9 _line; do
_package=${_line##*:}
_apkpath=$(adb shell pm path ${_package} | sed -e 's|^package:||' | head -n 1)
_apkfilename=$(basename "${_apkpath}")
adb shell cp -f ${_apkpath} ${_TEMP}/copy.apk
adb pull ${_TEMP}/copy.apk ./packages
_name=$(${_AAPT} dump badging ./packages/copy.apk | sed -n 's|^application-label:\(.\)\(.*\)\1$|\2|p' )
#'
echo "${_package} - ${_name}" >>./packages_list_with_names.txt
done 9< ${_PMLIST}
adb shell rm -rf $TEMP
When I perform command:
for el in $(adb shell ls /mnt/sdcard/ | grep mem_); do adb pull /mnt/sdcard/${el} android_mem; done
I get:
' does not existmnt/sdcard/mem_AI
' does not existmnt/sdcard/mem_Alarms
' does not existmnt/sdcard/mem_Android
' does not existmnt/sdcard/mem_Autodesk
' does not existmnt/sdcard/mem_Cardboard
' does not existmnt/sdcard/mem_DCIM
...
But if I perform this, for example, adb pull /mnt/sdcard/mem_DCIM android_mem I get that 0 KB/s (20 bytes in 0.080s), ie ok. Why is this happens??
The problem is that adb shell ls /mnt/sdcard/ | grep mem_ is returning a \r at the end, so it can't pull the file properly.
So you need to remove it with sed -r 's/[\r]+//g', for example:
for el in $(adb shell ls /mnt/sdcard/ | grep mem_ | sed -r 's/[\r]+//g'); do adb pull /mnt/sdcard/${el} android_mem; done
For example, in an android program, I can do it like this:
android.content.pm.Signature[] sigs = pkm.getPackageInfo(
"com.test", PackageManager.GET_SIGNATURES).signatures;
But, how can I do it via adb on PC?
You can do
service call package 2 s16 "com.test" i32 64
and parse the resulting Parcel dump
Alex has a great answer.
WARNING: Ugly code ahead
I was able to get the signature in adb shell as root with the following code:
package=com.test; b=false; while read line; do case $line in *\<package*${package}*) b=true ;; *\<cert*) if $b; then echo $line | sed -e 's|.*key="||' -e 's|".*||'; b=false; fi esac; done < /data/system/packages.xml
indented:
package=com.test
b=false
while read line; do
case $line in
*\<package*${package}*)
b=true ;;
*\<cert*)
if $b; then
echo $line | sed -e 's|.*key="||' -e 's|".*||'
b=false
fi
esac
done < /data/system/packages.xml
It is really annoying if you adb push/pull large files to the device that there's no way to now how far along it is. Is it possible to run adb push or adb pull and get a progress bar using the 'bar' utility?
The main issue here is I think that adb expects two file names, if the input file could be replaced by stdin you could pipe through the 'bar' utility and get a progress bar. So far I haven't succeeded in doing so, but I'm not really a shell guru which is why I'm asking here :)
Note that I'm on Linux using bash.
It looks like the latest adb has progress support.
Android Debug Bridge version 1.0.32
device commands:
adb push [-p] <local> <remote>
- copy file/dir to device
('-p' to display the transfer progress)
However, the answers above also work for 'adb install' which do not have a progress option. I modified the first answer's script to work this way:
Create "adb-install.sh" somewhere in your PATH and run "adb-install.sh " instead of "adb install -f "
#!/bin/bash
# adb install with progressbar displayed
# usage: <adb-install.sh> <file.apk>
# original code from: http://stackoverflow.com/questions/6595374/adb-push-pull-with-progress-bar
function usage()
{
echo "$0 <apk to install>"
exit 1
}
function progressbar()
{
bar="================================================================================"
barlength=${#bar}
n=$(($1*barlength/100))
printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
# echo -ne "\b$1"
}
export -f progressbar
[[ $# < 1 ]] && usage
SRC=$1
[ ! -f $SRC ] && { \
echo "source file not found"; \
exit 2; \
}
which adb >/dev/null 2>&1 || { \
echo "adb doesn't exist in your path"; \
exit 3; \
}
SIZE=$(ls -l $SRC | awk '{print $5}')
export ADB_TRACE=all
adb install -r $SRC 2>&1 \
| sed -n '/DATA/p' \
| awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
export ADB_TRACE=
echo
echo 'press any key'
read n
Currently I have this little piece of bash:
function adb_push {
# NOTE: 65544 is the max size adb seems to transfer in one go
TOTALSIZE=$(ls -Rl "$1" | awk '{ sum += sprintf("%.0f\n", ($5 / 65544)+0.5) } END { print sum }')
exp=$(($TOTALSIZE * 7)) # 7 bytes for every line we print - not really accurate if there's a lot of small files :(
# start bar in the background
ADB_TRACE=adb adb push "$1" "$2" 2>&1 | unbuffer -p awk '/DATA/ { split($3,a,"="); print a[2] }' | unbuffer -p cut -d":" -s -f1 | unbuffer -p bar -of /dev/null -s $exp
echo # Add a newline after the progressbar.
}
It works somewhat, it shows a progress bar going from 0 to 100 which is nice. However, it won't be correct if you do a lot of small files, and worse, the bytes/s and total bytes shown by 'bar' aren't correct.
I challenge you to improve on my script; it shouldn't be hard! ;)
Here is my solution, it will show a simple progressbar and current numeric progress
[==================================================] 100%
Usage
./progress_adb.sh source destination
progress_adb.sh
#!/bin/bash
function usage()
{
echo "$0 source destination"
exit 1
}
function progressbar()
{
bar="=================================================="
barlength=${#bar}
n=$(($1*barlength/100))
printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
# echo -ne "\b$1"
}
export -f progressbar
[[ $# < 2 ]] && usage
SRC=$1
DST=$2
[ ! -f $SRC ] && { \
echo "source file not found"; \
exit 2; \
}
which adb >/dev/null 2>&1 || { \
echo "adb doesn't exist in your path"; \
exit 3; \
}
SIZE=$(ls -l $SRC | awk '{print $5}')
ADB_TRACE=adb adb push $SRC $DST 2>&1 \
| sed -n '/DATA/p' \
| awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
echo
Testing on Ubuntu 14.04
$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
TODO
directory support
progressbar size change when screen size change
Well I can give you an Idea:
ADB_TRACE=adb adb push <source> <destination>
returns logs for any command, so for example the copy command, which looks like:
writex: fd=3 len=65544: 4441544100000100000000021efd DATA....#....b..
here you can get the total bytes length before, with ls -a, then parse the output of adb with grep or awk, increment an interneral counter and send the current progress to the bar utility.
When you succeeded, please post the script here.