Using systrace in application code android - android

The documentation in android says
you can use the methods of the Trace class to add instrumentation to
your application code and see the results in a Systrace report.
I have added the below methods to my code
Trace.beginSection("test-trace");
// block of code
Trace.endSection();
Now where can I check the results of this section. I start the systrace tool from the android device monitor and recorded it for 30 secs(performed the button click that executes the above the block). It generates the trace.html file but how do i get the above section information from this html file

It is there, I myself searched for it about one hour :D
If you have only one thread, it's shown in UI thread row, otherwise it's shown in your-defined thread row.
If you can not find it, use the search toolbox, in top right corner of page, type 'test-trace' there and it will show you the start time of that in detail
:)
this screenshot may help you

The systrace output only includes the tags that are listed on the command line. For app-specific tracing, that means adding a --app=package-name argument. This is necessary because systrace logs the entire system, and you wouldn't want it to automatically pick up the traces for every app and component.
You can find an example here. For a program with package name com.faddensoft.multicoretest, you would use a command line like:
python systrace.py --app=com.faddensoft.multicoretest gfx view sched dalvik
With that, your tracing should appear in the row of the thread that's issuing the trace calls. (Open the HTML file in a web browser; might need to use Chrome.)

Probably you recorded too long, make sure to increase buffer size with -b command, or simply follow this example:
python systrace.py -app=package_name sched freq idle am wm gfx view dalvik input binder_driver -t 30 -o test.html -b 30384

Related

How to save shell command for next time use (Terminal of Android Studio)

Suppose I run this command in Android Studio terminal
D:\.android\sdk\platform-tools/adb shell input text 'Some Text'
How do I save it (as macro) for next time use After closing and opening Android Studio instead of retyping the full command?
I know in the current session we can use the UP/DOWN arrows, I am looking at something like macros to be saved and be permanent...
I also faced this problem and reduce my 80% time and effort to run shell command by creating .bat files with just one letter like d.bat etc (single letter for reducing keyboard interaction to lessen time).
If you have no idea about What is bat file? and How to deal with it? please check this answer.

Exec(Linux). How it functions internally? Linux executable attributes(rlimit)

I would like to understand what is rlimit and pipe limit for an executable?
I am debugging an issue where dynamically built executables are crashing while loaded but statically built executables are fine.
WHen a dynamically built executable is loaded,
I see the following prints after the register dump
Process 2748(ls) has RLIMIT_CORE set to 1
Aborting Core
First of all some one please explain what is rlimit with perspective to exec. Also is there some thing called pipe limit? I see this proc entry.
Please point me to some resource that explains these attributes of an executable.
Question 2 :
When an executable is loaded, what exactly exec does? linker will load the shared libraries that are needed by my executable. So for this to happen linker(a binary inside /system/bin) should execute first.
Hence I understand that linker(system/bin/linker) and executable(ls) both are loaded by exec. Is my understanding correct? This is all theory. If someone, probably who is good at both fs and proc management, could point me to the relevant source, it would be great help. Thank you.
The RLIMIT_CORE is used to place a limit on the amount of info that coredump is allowed to produce, before it is aborted. Once this limit is hit, no more info is logged and the message Aborting Core is logged to the console.
From the man page of core :
A process can set its soft RLIMIT_CORE resource limit to place an upper limit on the size of the core dump file that will be produced if it receives a "core dump" signal.
Use setrlimit() to configure RLIMIT_CORE to a larger value to obtain complete coredumps.
The most common format of executables/shared-objects is ELF. On Linux, the dynamic loading and linking of these shared-objects is performed by ld.so
ld.so is loaded in the address space of a newly created process (by exec in this case) and executed first. This is possible as its path is embedded into the .interp section of the executable binary during compilation.
The dynamic linker then
loads the initial executable image
loads the dependent shared-objects(*.so files)
jumps to the entry-point of the executable
Reference : Dynamic linker on ELF-based Unix-like systems

Dalvik bytecode tracer

How can I get the trace of dalvik bytecode that is being executed while the device is running an app (like logcat does)?
There is no existing tool to do what you want, but it might be possible to implement such a tool without too much work.
The general idea is that you want to connect to dalvik's vm as a debugger, using the jdwp protocol, and single step the virtual machine, inspecting the state between each step and printing out the instruction. You should be able to get the... instruction index. Or maybe the instruction offset, via JDWP.
Additionally, you will need to read the dex file, and look up the exact instruction being executed, based on the instruction index/offset you get from JDWP.
You should be able to use AndBug to implement the first part of the above solution, and then use dexlib to access the dex file, in order to get the instruction.
You might want to check out traceview
It is realy too slow to trace the method not to speak the byte code,when you want to know what the app is doing the best way is to trace method first,then trace the byte code in it.

How to find this stack trace?

My program keeps crashing, but the logcat does not show any exceptions. I just get the following message, plus a lot of stats about CPU usage. Clearly I'm using too much of the CPU, but I don't know what part of my program is doing this. Where is the following file? I can't find it.
12-30 23:13:06.639: INFO/dalvikvm(7688): Wrote stack trace to
'/data/anr/traces.txt'
adb shell
-->
cat /data/anr/traces.txt
EDIT:
You need to have root access to modify the files in /data/ you should be able to access the file with an app like https://market.android.com/details?id=com.estrongs.android.pop
Once you have that app, open it --> Menu --> Settings --> Home Directory (Change from /sdcard/ to /) --> Exit the app --> Open it again
Then you should be able to browse to /data/anr/*
EDIT2 (Additional info, based on comments) for use with a published app after hands on testing:
Most developers rely on the developer console error reporting to see the stacktrace and error logs when a user submits an error report.
Others impliment their own or use a library like ACRA
Be warned some users do not install an app simply because it uses the permission to read sensitive log data.
For comfortable viewing without cutted head of file use following one-line command:
adb shell "cat /data/anr/traces.txt" | less
The ANR stands for "Android Not Responding" and it doesn't mean you're using too much of the CPU, it means that the UI main looper hadn't been called for a given amount of time. The UI looper takes care of user input, so from the point of view of your user, the app was unresponsive to input. Usually this is caused by doing long-running or blocking operations on the main UI thread. For example, downloading a file on the main thread could cause an ANR. Usually it's pretty easy to pick out the code that causes an ANR just from that information.
If you are on Windows with Android Studio, you can do the following:
In Andriod Studio:
View > Tool Windows > Device File Explorer
This should open a new window with all files on the device (where your stack trace is located), then you can navigate to '/data/anr/traces.txt', just double click on the file will open it up for you.

How to View Android Native Code Profiling?

I started my emulator with ./emulator -trace profile -avd emulator_15. I then tracked down the trace files to ~/.android/avd/rodgers_emulator_15.avd/traces/profile, where there are six files: qtrace.bb, qtrace.exc, qtrace.insn, qtrace.method, qtrace.pid, qtrace.static. I can't figure out what to do with these files. I've tried both dmtracedump and traceview on all of the files, but none seem to generate any output I can do anything with.
How can I view the proportion of time taken by native method calls on Android?
You need to use tracedmdump to convert the output. This is a shell function defined in build/envsetup.sh in the full Android sources. If you're using the SDK, rather than building from a full tree, I'm not sure this will work.
(If you don't have the sources and want to take a peek at the tracedmdump function, you can see it here.)
If you used emulator -trace profile, you'd run tracedmdump profile. This will dig through various binaries to retrieve symbolic information and associate it with the trace data, generating an HTML summary and a traceview-compatible trace file.
It's worth noting that the VM will execute more slowly with profiling enabled (the interpreter has overhead on every method call and return, and it's running in the slower "debug" interpreter), while native code continues to run at full speed, so you have to be careful when drawing conclusions.
General comment: don't forget to use F9 or one of the method calls to start/stop the tracing -- the -trace flag just enables the feature.
In order to use those six files, there are other scripts in the same directory as that of dmtracedump such as read_pid, read_trace, profile_trace etc. U should first run post_trace on the trace directory containing the six files, then you can use any one of them to get profile info such as how often each basic block executes, the pids they belong to etc.

Categories

Resources