| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
Retrieves the counts of how many objects have a particular field null on all running processes. |
| 19 |
|
| 20 |
Prints a json map from pid -> (log-tag, field-name, null-count, total-count). |
| 21 |
""" |
| 22 |
|
| 23 |
|
| 24 |
import adb |
| 25 |
import argparse |
| 26 |
import concurrent.futures |
| 27 |
import itertools |
| 28 |
import json |
| 29 |
import logging |
| 30 |
import os |
| 31 |
import os.path |
| 32 |
import signal |
| 33 |
import subprocess |
| 34 |
import time |
| 35 |
|
| 36 |
def main(): |
| 37 |
parser = argparse.ArgumentParser(description="Get counts of null fields from a device.") |
| 38 |
parser.add_argument("-S", "--serial", metavar="SERIAL", type=str, |
| 39 |
required=False, |
| 40 |
default=os.environ.get("ANDROID_SERIAL", None), |
| 41 |
help="Android serial to use. Defaults to ANDROID_SERIAL") |
| 42 |
parser.add_argument("-p", "--pid", required=False, |
| 43 |
default=[], action="append", |
| 44 |
help="Specific pids to check. By default checks all running dalvik processes") |
| 45 |
has_out = "OUT" in os.environ |
| 46 |
def_32 = os.path.join(os.environ.get("OUT", ""), "system", "lib", "libfieldnull.so") |
| 47 |
def_64 = os.path.join(os.environ.get("OUT", ""), "system", "lib64", "libfieldnull.so") |
| 48 |
has_32 = has_out and os.path.exists(def_32) |
| 49 |
has_64 = has_out and os.path.exists(def_64) |
| 50 |
def pushable_lib(name): |
| 51 |
if os.path.isfile(name): |
| 52 |
return name |
| 53 |
else: |
| 54 |
raise argparse.ArgumentTypeError(name + " is not a file!") |
| 55 |
parser.add_argument('--lib32', type=pushable_lib, |
| 56 |
required=not has_32, |
| 57 |
action='store', |
| 58 |
default=def_32, |
| 59 |
help="Location of 32 bit agent to push") |
| 60 |
parser.add_argument('--lib64', type=pushable_lib, |
| 61 |
required=not has_64, |
| 62 |
action='store', |
| 63 |
default=def_64 if has_64 else None, |
| 64 |
help="Location of 64 bit agent to push") |
| 65 |
parser.add_argument("fields", nargs="+", |
| 66 |
help="fields to check") |
| 67 |
|
| 68 |
out = parser.parse_args() |
| 69 |
|
| 70 |
device = adb.device.get_device(out.serial) |
| 71 |
print("getting root") |
| 72 |
device.root() |
| 73 |
|
| 74 |
print("Disabling selinux") |
| 75 |
device.shell("setenforce 0".split()) |
| 76 |
|
| 77 |
print("Pushing libraries") |
| 78 |
lib32 = device.shell("mktemp".split())[0].strip() |
| 79 |
lib64 = device.shell("mktemp".split())[0].strip() |
| 80 |
|
| 81 |
print(out.lib32 + " -> " + lib32) |
| 82 |
device.push(out.lib32, lib32) |
| 83 |
|
| 84 |
print(out.lib64 + " -> " + lib64) |
| 85 |
device.push(out.lib64, lib64) |
| 86 |
|
| 87 |
cmd32 = "'{}={}'".format(lib32, ','.join(out.fields)) |
| 88 |
cmd64 = "'{}={}'".format(lib64, ','.join(out.fields)) |
| 89 |
|
| 90 |
if len(out.pid) == 0: |
| 91 |
print("Getting jdwp pids") |
| 92 |
new_env = dict(os.environ) |
| 93 |
new_env["ANDROID_SERIAL"] = device.serial |
| 94 |
p = subprocess.Popen([device.adb_path, "jdwp"], env=new_env, stdout=subprocess.PIPE) |
| 95 |
|
| 96 |
with concurrent.futures.ProcessPoolExecutor() as ppe: |
| 97 |
ppe.submit(kill_it, p.pid).result() |
| 98 |
out.pid = p.communicate()[0].strip().split() |
| 99 |
p.wait() |
| 100 |
print(out.pid) |
| 101 |
print("Clearing logcat") |
| 102 |
device.shell("logcat -c".split()) |
| 103 |
final = {} |
| 104 |
print("Getting info from every process dumped to logcat") |
| 105 |
for p in out.pid: |
| 106 |
res = check_single_process(p, device, cmd32, cmd64); |
| 107 |
if res is not None: |
| 108 |
final[p] = res |
| 109 |
device.shell('rm {}'.format(lib32).split()) |
| 110 |
device.shell('rm {}'.format(lib64).split()) |
| 111 |
print(json.dumps(final, indent=2)) |
| 112 |
|
| 113 |
def kill_it(p): |
| 114 |
time.sleep(1) |
| 115 |
os.kill(p, signal.SIGINT) |
| 116 |
|
| 117 |
def check_single_process(pid, device, bit32, bit64): |
| 118 |
try: |
| 119 |
|
| 120 |
device.shell(['am', 'attach-agent', str(pid), bit32]) |
| 121 |
device.shell(['am', 'attach-agent', str(pid), bit64]) |
| 122 |
time.sleep(0.5) |
| 123 |
device.shell('kill -3 {}'.format(pid).split()) |
| 124 |
time.sleep(0.5) |
| 125 |
out = [] |
| 126 |
all_fields = [] |
| 127 |
lc_cmd = "logcat -d -b main --pid={} -e '^\\t.*\\t[0-9]*\\t[0-9]*$'".format(pid).split(' ') |
| 128 |
for l in device.shell(lc_cmd)[0].strip().split('\n'): |
| 129 |
|
| 130 |
data = l.strip().split()[5:] |
| 131 |
if len(data) < 4: |
| 132 |
continue |
| 133 |
|
| 134 |
|
| 135 |
field = data[1] |
| 136 |
if field not in all_fields: |
| 137 |
out.append((str(data[0]), str(data[1]), int(data[2]), int(data[3]))) |
| 138 |
all_fields.append(field) |
| 139 |
if len(out) != 0: |
| 140 |
print("pid: " + pid + " -> " + str(out)) |
| 141 |
return out |
| 142 |
else: |
| 143 |
return None |
| 144 |
except adb.device.ShellError as e: |
| 145 |
print("failed on pid " + repr(pid) + " because " + repr(e)) |
| 146 |
return None |
| 147 |
|
| 148 |
if __name__ == '__main__': |
| 149 |
main() |