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.
Related
Basically, I have a C++ program that finds the sum of two numbers given. I need to provide the two numbers to the C++ program as input using my android app and then display the result in my android app. I guess I need to use parcelable class. Can someone please tell me the steps to be followed?
Edit: I forgot to mention that the C++ program that I intend to communicate with is an executable program (sum.exe)
To run a C++ executable on Android, you can use something like Runtime.exec("sum 1 2"). There are a lot of tutorials, e.g. https://www.mkyong.com/java/how-to-execute-shell-command-from-java/. The output (stdout and stderr) can be parsed, too. A more sophisticated way is to use ProcessBuilder, but the idea is the same.
If you want your executable to keep running in background, and send the numbers to crunch once in a while, you can either use input pipe, or some IPC protocol. Shared memory works well, see e.g. How to use Shared Memory (IPC) in Android.
You can use JNI code, take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
where you can find super simple code with C++ being called via JNI wrapper from Java.
I'm compiling in debug mode a certain .so in the Android-Native layer which by default outputs to the default logging file.
However, I do not want this data to be saved to the logs in the first place because it will overwhelm the logging file; I plan on streaming it off of the device.
POSSIBLE SOLUTIONS
Modify the .so code to output the debug info to NOT the logging file.
Definitely the most straightforward way but I'd prefer not to do this since this'll require modification of the .so. I agree this should be trivial modification but I have a requirement to modify the .so as little as possible.
Is there a way to create an alias for a file, pipe all writes to it through my app (like tee) and selectively allow writes to the real file?
Is there a built-in logcat filtering tool that can do this filtering for me with some regexs?
First of all, the first solution is indeed the trivial and the better one - since it remains in the code unlike the logcat filtering that might get deleted.
If the requirement is to modify the .so as little as possible, there is no real problem - you modify the code using #ifdef for debug only, so the code changes for debug mode, but the generated .so for release does not change. This will give you a very good control - but in case you have a lot of 'special' debug code, it will make your code a little ugly...
Edit:
If you want to write to you own log, you can run logcat using Runtime class, with your own parameters - look here: Using GREP command to filter logcat in Android.
Regarding the logcat filtering - you can use regex in the filtering in Android Studio. You have a 'regex' check box next to the input box. See here for more details: How to exclude certain messages by TAG name using Android adb logcat?
I want to implement __android_log_write() functionality using write() or some api available in libc(actually ulibc). The reason being that i cannot use any libraries associated with android as that would increase the memory required. I have very limited amount of memory as my code is running in separate memory region reserved during boot up. Main goal is to attach my debugging logs to logcat.
I am looking something similar to this:
write(1,"sandy",6);
The abovce code i can directly write to stdout. Similarly, i want to use write() or something else and write to logcat. What is the clean way to do it.
Hope i am clear. Thanks.
Got the answer. We need to open /dev/radio and write into them.
Thanks
I want to know who called *sys_reboot* when the phone(android) reboot unexpectly.
Is there a way to dump the call stack in syscall(android kernel)?
If all you want it a kernel call trace, you can get that via dump_stack(). panic() calls that, amongst other things. The BUG() / BUG_ON() wrappers give it a more descriptive message and an optional conditional test.
A userland stacktrace, particularly a symbolic one, though, cannot reliably be obtained from within the kernel directly. It's possible to copy the stack memory into kernel space and log the contents, or even heuristically walk SP/FP linkage if framepointers aren't optimized out, but to resolve symbols, it'd need to access and parse ELF information. I'm unsure anyone has done that as pure kernel-side implementation; an easier solution there would be to stop the program from your syscall hook, spawn a userspace debugger attaching to it, extracting a stacktrace, continuing the program when done.
See this SO posting, call_usermodehelper / call_usermodehelperpipe usage for how to do this.
See also this SO posting: How to print the userspace stack trace in linux kernelspace for another reference to the same question.
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.