Related
I want to capture a system trace, using the systrace tool.
I have this device (not-rooted) :
Pixi 4.5 Alcatel
Android 6.0 (API 23)
And I use Android Studio 4.2.2 and I have installed Python 2.7.18, as required (the version 3+ does not match).
Here is the command line to view the list of available categories on my device :
C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace>C:\Users\myUser\AppData\Local\Programs\Python27\python systrace.py -l
I chose this command with the -l option for my here question because it results (see below) in the expected list plus an extra set of lines which suggests that the error CommandFailedError is related to an exception on HasRoot :
gfx - Graphics
input - Input
view - View System
webview - WebView
wm - Window Manager
am - Activity Manager
sm - Sync Manager
audio - Audio
video - Video
camera - Camera
hal - Hardware Modules
app - Application
res - Resource Loading
dalvik - Dalvik VM
rs - RenderScript
hwui - HWUI
perf - Performance
bionic - Bionic C Library
power - Power Management
sched - CPU Scheduling
freq - CPU Frequency
idle - CPU Idle
load - CPU Load
CRITICAL:root:(TimeoutThread-1-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 1 of 4: CommandFailedError("(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']",)
CRITICAL:root:(TimeoutThread-2-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 2 of 4: CommandFailedError("(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']",)
CRITICAL:root:(TimeoutThread-3-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 3 of 4: CommandFailedError("(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']",)
Traceback (most recent call last):
File "systrace.py", line 49, in <module>
sys.exit(run_systrace.main())
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 205, in main
main_impl(sys.argv)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 177, in main_impl
atrace_agent.list_categories(options)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_agents\atrace_agent.py", line 65, in list_categories
if not devutils.HasRoot():
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 63, in timeout_r
etry_wrapper
impl, timeout, retries, desc=desc, retry_if_func=retry_if_func)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 164, in Run
error_log_func=error_log_func)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 199, in JoinA
ll
self._JoinAll(watcher, timeout)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 171, in _Join
All
thread.ReraiseIfException()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 94, in run
self._ret = self._func(*self._args, **self._kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 156, in <lambda
>
lambda: func(*args, **kwargs), name=thread_name)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 51, in impl
return f(*args, **kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py", line 562, in HasRoo
t
output = self.RunShellCommand(['id'], single_line=True)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 56, in timeout_r
etry_wrapper
return impl()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 51, in impl
return f(*args, **kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py", line 1556, in RunSh
ellCommand
raise device_errors.CommandFailedError(msg % output, str(self))
devil.android.device_errors.CommandFailedError: (device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(
shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']
By comparison, here is the error CommandFailedError obtained with a command which should generate an html report :
C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace>C:\Users\myUser\AppData\Local\Programs\Python27\python systrace.py
Traceback (most recent call last):
File "systrace.py", line 49, in <module>
sys.exit(run_systrace.main())
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 205, in main
main_impl(sys.argv)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 185, in main_impl
controller.StartTracing()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\systrace_runner.py", line 46, in StartTracing
self._tracing_controller.StartTracing()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_controller.py", line 156, in StartTracing
timeout=self._controller_config.timeout):
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\common\py_utils\py_utils\__init__.py", line 103, in RunWithTimeout
return timeout_retry.Run(func, timeout, 0, args=args)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 164, in Run
error_log_func=error_log_func)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 199, in JoinA
ll
self._JoinAll(watcher, timeout)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 171, in _Join
All
thread.ReraiseIfException()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 94, in run
self._ret = self._func(*self._args, **self._kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 156, in <lambda
>
lambda: func(*args, **kwargs), name=thread_name)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_agents\android_cgroup_agent.py", line 59, in StartAgent
Tracing
if not self._device_utils.HasRoot():
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 56, in timeout_r
etry_wrapper
return impl()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 51, in impl
return f(*args, **kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py", line 562, in HasRoo
t
output = self.RunShellCommand(['id'], single_line=True)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 56, in timeout_r
etry_wrapper
return impl()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 51, in impl
return f(*args, **kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py", line 1556, in RunSh
ellCommand
raise device_errors.CommandFailedError(msg % output, str(self))
devil.android.device_errors.CommandFailedError: (device: deviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(
shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']
I am sure that there is a proper way to run systrace on an old unrooted device but I just didn't find it.
Does anyone have a clue or a solution? Thanks.
EDIT :
Thanks a lot for your analysis and your answer #yi-yang.
As you recommended, I set single_line to False in the RunShellCommand call. Furthermore, I also modified the return line, which gives the final code in the device_utils.py file, lines 562-563 :
output = self.RunShellCommand(['id'], single_line=False)
return output[0].startswith('uid=0(root)')
Note that I select the first line because there are still two lines, which means that some optional newline at the end needs to be stripped somehow...
Now it works but only for a few categories, as in the following example:
systrace.py -o mynewtrace.html perf idle load
However, for any atrace data, including the very helpful "sched" (see atrace_agent.py), the cpu profiling not possible, as shown by the following messages:
Warning: Only 2 of 3 tracing agents started.
IOError('Unable to get atrace data. Did you forget adb root?')
So the root privileges do matter, despite what the file comments (l. 556-558) claim :-(.
I guess rooting is a must for the full experience!
I am trying to write a script for termux. And i need to use psutil with python. And it's working fine. But every time i've tried to run the script it's giving me some errors, which not effects my code.
I've tried to use "try", "except" to catch the error. But it doesn’t work.
Note: See the last line on the error message. Script working fine. If you have other modules or solution to provide, remember 'I can't use os.kill on my script'.
Code:
try:
import psutil, os, signal
except Exception as e:
pass
print ("killing python")
proc = psutil.Process(os.getpid())
proc.send_signal(signal.SIGTERM)
Error + output :
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 202, in <module>
scputimes = set_scputimes_ntuple("/proc")
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1435, in <module>
_last_cpu_times = cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1429, in cpu_times
return _psplatform.cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 301, in cpu_times
set_scputimes_ntuple(procfs_path)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1442, in <module>
_last_per_cpu_times = cpu_times(percpu=True)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1431, in cpu_times
return _psplatform.per_cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 314, in per_cpu_times
set_scputimes_ntuple(procfs_path)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
killing python
Terminated
Today I had the very same issue when importing psutil in termux on android. The solution / workaround that Mr. lindroid found is helping to avoid that annoying error message. Thanks for sharing!
I want to propose a small improvement:
import os,sys
sys.stderr = open(os.devnull, "w")
try:
import psutil
finally:
sys.stderr = sys.__stderr__
This way, all exceptions which actually prevent the "import psutil" line from execution will get shown. just in case there is a real problem with psutil (i.e. dependency module missing).
Do you know why the error posted originally cannot be caught by a normal try/except statement? Is it raised in a second thread maybe? I already tried manipulating the Thread class to handle the error but it didn't work. What else could be a reason?
If i want to ignore the error messages. I can use following code, because it doesn’t interfere with my actual program.
import os, signal, sys
# set stderr to dev/null
sys.stderr = open(os.devnull, "w")
import psutil
# after importing, set stderr to original
sys.stderr = sys.__stderr__
print ("killing python")
proc = psutil.Process(os.getpid())
proc.send_signal(signal.SIGTERM)
Output :
killing python
Terminated
It can also be handled in a proper way as #TheoRet mentioned:
import os,sys
sys.stderr = open(os.devnull, "w")
try:
import psutil
#except:
#handle module not found
finally:
sys.stderr = sys.__stderr__
This way, all exceptions which actually prevent the "import psutil" line from execution will get shown. just in case there is a real problem with psutil (i.e. dependency module missing).
I am trying to use the MavenToAndroidAnt Python3 to SMACK 4.1.1, but I am getting some errors
I have installed setuptools-15.0b1, python-gnupg-master, httplib2-master
I can't post image yet... i will simulate my cmd prompt
c:\Windows\system32>cd C:\lib
C:\lib>python getMavenArtifactsNg.py -f artifacts.csv -p C:\eclipse\Works\TEsteX
mPP
Downloading smack-android-4.1.1.jar to C:\eclipse\Works\TEsteXmPP/libs/
Downloading smack-android-4.1.1.jar.asc to C:\eclipse\Works\TEsteXmPP/libs/
Downloading smack-android-4.1.1-sources.jar to C:\eclipse\Works\TEsteXmPP/libs-sources/
Traceback (most recent call last):
File "getMavenArtifactsNg.py", line 204, in
a.installIn(project)
File "getMavenArtifactsNg.py", line 147, in installIn
if self.verifySignature(jarSigDest, jarDest):
File "getMavenArtifactsNg.py", line 158, in verifySignature
gpg = gnupg.GPG()
File "c:\python34\lib\site-packages\gnupg-unknown-py3.4.egg\gnupg\gnupg.py", l
ine 125, in init
ignore_homedir_permissions=ignore_homedir_permissions,
File "c:\python34\lib\site-packages\gnupg-unknown-py3.4.egg\gnupg_meta.py", l
ine 176, in init
self.binary = _util._find_binary(binary)
File "c:\python34\lib\site-packages\gnupg-unknown-py3.4.egg\gnupg_util.py", l
ine 427, in _find_binary
raise RuntimeError("GnuPG is not installed!")
RuntimeError: GnuPG is not installed!
C:\lib>
____________ artifacts.csv file
org.igniterealtime.smack,smack-android,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-android-extensions,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-core,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-tcp,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-extensions,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-experimental,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-resolver-minidns,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.igniterealtime.smack,smack-sasl-provided,4.1.1,1357B01865B2503C18453D208CAC2A9678548E35
org.jxmpp,jxmpp-core,0.4.2-beta1,1357B01865B2503C18453D208CAC2A9678548E35
org.jxmpp,jxmpp-util-cache,0.4.2-beta1,1357B01865B2503C18453D208CAC2A9678548E35
de.measite.minidns,minidns,0.1.1,4677EF84C286721DA33C09C98D2028BA8AF1E192
EDIT>>>
C:\lib>python getMavenArtifactsNg.py -f artifacts.csv -p C:\eclipse\Works\TEsteX
mPP
Downloading smack-android-4.1.1.jar to C:\eclipse\Works\TEsteXmPP/libs/
Downloading smack-android-4.1.1.jar.asc to C:\eclipse\Works\TEsteXmPP/libs/
Downloading smack-android-4.1.1-sources.jar to C:\eclipse\Works\TEsteXmPP/libs-s
ources/
Exception in thread Thread-8:
Traceback (most recent call last):
File "c:\python34\lib\threading.py", line 920, in _bootstrap_inner
self.run()
File "c:\python34\lib\threading.py", line 868, in run
self._target(*self._args, **self._kwargs)
File "c:\python34\lib\site-packages\gnupg.py", line 753, in _read_response
result.handle_status(keyword, value)
File "c:\python34\lib\site-packages\gnupg.py", line 284, in handle_status
raise ValueError("Unknown status message: %r" % key)
ValueError: Unknown status message: 'NEWSIG'
Traceback (most recent call last):
File "getMavenArtifactsNg.py", line 204, in
a.installIn(project)
File "getMavenArtifactsNg.py", line 150, in installIn
raise Exception("Could not verify signature for " + jarDest)
Exception: Could not verify signature for C:\eclipse\Works\TEsteXmPP/libs/smack-
android-4.1.1.jar
<
I am using Windows 8.1 64X, eclipse SDK 4.2.2 JUNO + ADT
I really want to study the XMPP Smack 4.1.1 documentation, please help me and sorry my english.
I wrote only this 3 lines in python using SL4A:
import android
droid = android.Android()
droid.makeToast(u"ascc4r")
When this code runs, I get the following error:
pydev debugger: starting
Traceback (most recent call last):
File "C:\Users\Tibi\Desktop\adt-bundle-windows-x86_64-20130917\eclipse\plugins \org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1446, in <module>
debugger.run(setup['file'], None, None)
File "C:\Users\Tibi\Desktop\adt-bundle-windows-x86_64-20130917\eclipse\plugins org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1092, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "C:\Users\Tibi\workspace\26\src\26module.py", line 7, in <module>
droid = android.Android()
File "C:\Python26\lib\android.py", line 34, in __init__
self.conn = socket.create_connection(addr)
File "C:\Python26\lib\socket.py", line 547, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
Environment setup:
- Python 2.6.6
- set ap_port=9999
- adb forward tcp:9999 tcp:xxxx (xxxx where I started the server on the phone)
My android.py is in the Python/Lib folder.
Update:
I tried this 3 instruction in CMD and it's work, making a Toast. So I think the fault is in the ADT bundle, or Eclipse Python plugin.
What is this Errno 11001?
What is this Errno 11001?
I don't know about that error, but I suggest to use the correct syntax which is
import android
droid = android.Android()
droid.makeToast('my text to print should be inside the quotes')
see also the API Overview
I am trying to learn creating the android application using python. I am going through the tutorials.
when I am running the command :
adb forward tcp:9999 tcp:4321
set AP_PORT=9999
and then running the code,
import android
droid = android.Android()
droid.makeToast("Hello from my computer!")
It gives me error as :
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
droid.makeToast("Hello from my computer!")
File "C:\Python27\lib\site-packages\android.py", line 58, in rpc_call
return self._rpc(name, *args)
File "C:\Python27\lib\site-packages\android.py", line 47, in _rpc
response = self.client.readline()
File "C:\Python27\lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10053] An established connection was aborted by the software in your host machine
how can I solve the problem?