0

I have an Android command whose output is as follows:

$adb shell "head -20 /d/dma_buf/bufinfo"

Dma-buf Objects:
size            flags           mode            count           exp_name        buf name
00020480        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf210
        Attached Devices:
Total 0 devices attached

09469952        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf209
        Attached Devices:
        kgsl-3d0
Total 1 devices attached

00020480        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf208
        Attached Devices:
Total 0 devices attached

09469952        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf207
        Attached Devices:
        kgsl-3d0
Total 1 devices attached

There are similar entries in the file. Now what I want is to extract the pid from the exp_name field (kernel doesn't have access to the complete name that's greater than 16 bytes) and get that process's complete name from the ps output with ps pid -o comm= cf. [1] and print it along with $1 of the awk output. I am stuck at how to retrieve that. I started with the following command

$adb shell "sed -n '/dmabuf/p' /d/dma_buf/bufinfo|head -10"|awk '
BEGIN { touch temp} {echo $5 > temp; pid = $(cut -d '-' -f 3,3 temp);}
END { rm temp}
'

But this yields syntax errors as:

awk: cmd. line:2: BEGIN { touch temp} {echo $5 > temp; pid = $(cut -d - -f 3,3 temp);}
awk: cmd. line:2:                                                           ^ syntax error

[1] There's a problem with Android awk due to which I'm resorting to the host Linux machine's utilities.

$adb shell "awk '/dmabuf/{print}' /d/dma_buf/bufinfo|head -3"
00020480        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf210
09469952        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf209
00020480        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf208

adb shell "awk '/dmabuf/{print NF}' /d/dma_buf/bufinfo|head -3"
6
6
6

So this output has 6 fields (that are tab separated). Yet when I try to print the individual fields it doesn’t do that and still prints the entire line.

$adb shell "awk '/dmabuf/{print $4}' /d/dma_buf/bufinfo|head -3"
00020480        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf210
09469952        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf209
00020480        00000002        00000007        00000003        ion-system-660-vendor.qti.hard  dmabuf208
  • a) there is no problem with android's awk; the $4 in your last command will be expanded (to nothing) by the shell on the PC, because it's inside double quotes. b) you cannot do shell-like command expansion inside awk (use split() instead of $(cut -d ...) c) android has pgrep -l – mosvy Oct 25 '18 at 18:55
  • @mosvy What would you recommend then for fixing my command to work using the Android awk ? I tried adb shell awk /dmabuf/'{print $4}' /d/dma_buf/bufinfo|head -10 but that gives an error awk: illegal statement at source line 1 missing }. – Zoso Oct 26 '18 at 6:12
  • adb shell 'awk '\''/dmabuf/{print $4}'\'' /d/dma_buf/bufinfo'. I cannot give you a turnkey answer, because on my all android devices, that bufinfo file has a completely different format. – mosvy Oct 26 '18 at 11:53
  • @mosvycan you please explain the syntax you've used or direct me to something that might clarify my queries since I suspect there's something basic that I'm missing. – Zoso Oct 27 '18 at 14:18
  • yes you are missing something basic -- the fact that variables (eg. $4) are expanded by the shell inside double quotes, and that the argument to adb shell will be expanded twice (by the shell on the PC and by the shell on the android gadget). I suggest you use set -x liberally until you figure out what happens: (set -x; adb shell 'set -x; ...'). – mosvy Oct 27 '18 at 15:17
0

I'm able to extract the pids with:

$ awk 'NF == 6 {print $5}' inputfile | grep -Eo '[0-9]+'
660
660
660
660

Since Android uses procfs, you can see the actual binary as a symlink in /proc/$pid/exe. But, since this is a magic (i. e. procfs) symlink that doesn't obey the normal POSIX rules, you can't use the more common methods to find its target. You can, however, use stat:

$ stat -c '%N' /proc/$$/exe
`/proc/4811/exe' -> `/system/bin/sh'
[...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.