Can't get malloc debug to turn on? - android

I'm trying to enable malloc debug for my android application, and for the life of me i can't get it working.
My wrap.sh scripts each contain this:
#!/system/bin/sh
LIBC_DEBUG_MALLOC_OPTIONS="free-track verbose"
logwrapper "$#"
and I've set up the following directory structure:
When I grep logcat for "malloc debug enabled", I get nothing.
If i change my wrap.sh scripts contents to this
#!/system/bin/sh
LIBC_DEBUG_MALLOC_OPTIONS=free-track logwrapper "$#"
which is exactly what the documentation for the wrap shell script says to add (although to me that seems like invalid shell script syntax), i can see the "malloc debug enabled" log message, but my application freezes at the splash screen and never actually launches.
I'm not sure what's going on and would love any type of input.
Other notes:
My project is using CMake.
My debug device is running Android P.

That is for running an executable binary on Android. I think you are running a Java app with JNI calls, and that trick does not apply to you. You will have to set those environment variables from JAVA as the app starts.
Try looking at OS.setenv() - https://developer.android.com/reference/android/system/Os.html. I hope that those environment variables can be set from Java before the JNI library is loaded, or that the library reads those environment variables on every call, and not just init.

If the accepted answer doesn't work for you then my solution might work:
#!/system/bin/sh
LIBC_DEBUG_MALLOC_OPTIONS=free-track logwrapper
exec "$#"
Make sure line endings are LF and not CRLF.
Then my issue was that malloc debug wasn't able to write the dump:
E/malloc_debug: Unable to create file: /data/local/tmp/backtrace_heap.16934.exit.txt
I thought this might be due missing permissions. By changing the output folder to the one of your app, this is circumvented:
LIBC_DEBUG_MALLOC_OPTIONS='backtrace backtrace_dump_on_exit backtrace_dump_prefix=data/data/com.your.app/files/dump' logwrapper
And dumping now works:
E/malloc_debug: Dumping to file: data/data/com.your.app/files/dump.17599.exit.txt
Also make sure you set
buildTypes{
debug {
debuggable true
}
}
in build.gradle and
android:extractNativeLibs="true"
android:debuggable="true"
in the AndroidManifest.xml file

Related

Run Python for Android (Qpython) in Android Terminal

Python for Android (Py4A 2.6) is wonderful!!! But QPython (2.7) is a lot more powerful and continuously developed. Now I try to integrate Qpython's Python into Android (5.1 CM12) Terminal / ADB described in this Tutorial:http://wiki.qpython.org/en/diveinto/
My Script to import the environment variables qpython needs looks like:
TERM=screen
PATH=/data/data/com.hipipal.qpyplus/files/bin:/system/bin:/system/xbin
LD_LIBRARY_PATH=.:/data/data/com.hipipal.qpyplus/files/lib/:/data/data/com.hipipal.qpyplus/files/:/data/data/com.hipipal.qpyplus/lib/
PYTHONHOME=/data/data/com.hipipal.qpyplus/files
ANDROID_PRIVATE=/data/data/com.hipipal.qpyplus/files
PYTHONPATH=/storage/emulated/0/com.hipipal.qpyplus/lib/python2.7/site-packages/:/data/data/com.hipipal.qpyplus/files/lib/python2.7/site-packages/:/data/data/com.hipipal.qpyplus/files/lib/python2.7/:/data/data/com.hipipal.qpyplus/files/lib/python27.zip:/data/data/com.hipipal.qpyplus/files/lib/python2.7/lib-dynload/:/storage/emulated/0/com.hipipal.qpyplus/scripts
PYTHONSTARTUP=/storage/emulated/0/com.hipipal.qpyplus/lib/python2.7/site-packages/qpythoninit.py
PYTHONOPTIMIZE=2
TMPDIR=/storage/emulated/0/com.hipipal.qpyplus/cache
AP_HOST=127.0.0.1
AP_PORT=58418
AP_HANDSHAKE=01805a68-c04c-4e21-bd54-ac8732f3a8da
ANDROID_PUBLIC=/storage/emulated/0/com.hipipal.qpyplus
ANDROID_PRIVATE=/data/data/com.hipipal.qpyplus/files
ANDROID_ARGUMENT=/storage/emulated/0/com.hipipal.qpyplus/scripts
Shell session:
u0_a101#C1905:/ $ source /sdcard/qpyenvnew.sh
u0_a101#C1905:/ $ $python
u0_a101#C1905:/ $ hmm do nothing?!
I get no output.
I have found here on Stackoverflow the same Question but the posted solution ist a link how is down.
Other Tutorial on throws only errors
http://dinostest.com/node/28
Try this command in your terminal:
$sh data/data/com.hipipal.qpyplus/files/bin/qpython.sh
Terminal will prompt a Python console.
If your script for the environment variables was obtained by running the code from the tutorial link, it should be correct.
If what you have pasted is exactly what you entered, the problem would be that you tried to run pyhton by typing $python when it should have just been python
Pre-fixing with $ is used to reference the value of a variable, so entering $python referenced the value of the variable python but does nothing with it, hence no feedback.
EDIT2: Although it's not a direct answer to this question I suggest having a look at the App 'Termux'. You can install Python2/3 via apt, virtualenv/-wrapper etc... Much more powerfull!
EDIT: Since /data/data... is only accessible as root, the following must be done as root!
Take care that the python executables path is in your $PATH. That wasn't the case even after having run the script from the tutorial (your script). If it's not present simply type:export PATH=/data/data/com.hipipal.qpyplus/files/bin:$PATH
or follow theses steps. In short: create a file in /sdcard (here ".bashrc") with the above line. After that type: source .bashrcsource init.sh (init.sh is located in /data/data/com.hipipal.qpyplus/files/bin as well) Then fire up Python with python-android5 which is the PIE compatible executable for Phone with Lollipop and above. But be aware: For whatever reason I couldn't exit() or quit() the Python prompt but I was able to run Script from the Terminal! Have fun!
EDIT: I spotted this: PythonForAndroid. It is a fork of PythonForAndroid that is still alive, and this versions are able to run on Android L (Python3 on 5.1.1 in my case). Also check the SL4A linked there, it works great on Lollipop.
Thanks to the answers above I made a super easy solution. If you have root: Create a file "python" and type the following line in it:
sh data/data/com.hipipal.qpyplus/files/bin/qpython-android5.sh
Place this file in your /system/bin directory and you can access python from everywhere using the command 'python'.

Libgit2 running on Android unable to perform clone operation - returns "failed to set permissions" error

I have just built Libgit2 (v0.20.0) for Android (target SDK version 18, debugging on a rooted device running Cyanogenmod 10.1.2, Android 4.2.2) and a simple function like getting the version number of Libgit2 works fine through the JNI. But when I use the git_clone function it stops right after the objects/info folder is created and returns this error:
Error -1 cloning repository - Failed to set permissions on '/storage/sdcard0/it/ptt/.git/objects/info': Operation not permitted
I have given the application the WRITE_EXTERNAL_STORAGE permission but I guess it still can't chmod unless owner of the file. When I use adb shell to check out the permission mode of the info folder I get:
d---rwxr-x system sdcard_rw 2014-05-15 09:31 info
And by using pwd.h functions I get the username that the c code (that is calling git_clone) is under to be u0_a92. How am I suppose to get pass this I suppose very Android related issue? Is there a simple way to stop Libgit2 from calling p_chmod or can I give it permissions to do so?
I ended up defining p_chmod as a method always returning true to get passed the error. In the bash script I use to build libgit2 I inserted the following lines that leaves the source files in an unmodified condition after building for android:
LIBGIT2_POSIX_PATH="$LIBGIT2_SOURCE_PATH/src/posix.h"
LIBGIT2_POSIX_BACKUP_PATH="$LIBGIT2_SOURCE_PATH/src/posix_original.h"
printf "#include \"always_success.h\"\nint always_success() { return 0; }" > "$LIBGIT2_SOURCE_PATH/src/always_success.c"
printf "int always_success();" > "$LIBGIT2_SOURCE_PATH/src/always_success.h"
cp $LIBGIT2_POSIX_PATH "$LIBGIT2_POSIX_BACKUP_PATH"
sed -i "s/^#define\sp_chmod(p, m).*$/#include \"always_success.h\"\n#define p_chmod(p, m) always_success()\nextern int always_success();\n/" $LIBGIT2_POSIX_PATH
# run the build process with cmake ...
# restore chmod manipulated source header
mv $LIBGIT2_POSIX_BACKUP_PATH $LIBGIT2_POSIX_PATH
There is probably a cleaner way to solve this but at least now I dont get that error anymore. Thanks to Carlos for his help!
UPDATE
Running adb shell mount | grep sdcard I could see that the sdcard which I am trying to clone the repository into uses the vfat file system which according to this forum thread doesn't support unix-style permissions.

Android apitrace failed to run

I would like to use the apitrace project on android. I followed the instructions from the readme file.
But get no trace where created.
I run this command
adb shell TRACE_FILE=/data/test.trace LD_PRELOAD=/data/egltrace.so am start -n APP_NAME
How can I make it work?
I tried following the instructions in Dalvik.markdown of the original distribution of apitrace, but without success.
The instructions say to set two properties: wrap._process_name_ and debug.apitrace.procname. The former has to be set, according to those instructions, to LD_PRELOAD=/data/egltrace.so. When launching the application I still wouldn't get any trace generated nor any apitrace-related message in the logcat.
I had more success by putting the LD_PRELOAD instruction in a script and using that as the wrapper. This is the script that I use, called /data/apitrace.sh:
LD_PRELOAD=/data/egltrace.so exec $#
You can also set the TRACE_FILE environment variable to specify the path to which the trace file should be written to. Otherwise it will be _/data/app_process.trace_. For example:
TRACE_FILE=/data/apitraces/mytrace.trace LD_PRELOAD=/data/egltrace.so exec $#
I believe apitrace takes care of adding numbers to the filename to prevent overwriting existing ones. So you'll have mytrace.trace, mytrace.1.trace, and so on.
So with this script in place I set the properties like so:
adb shell setprop wrap._process_name_ /data/apitrace.sh
adb shell setprop debug.apitrace.procname _process_name_
And then I launch the application. I see in the logcat something like the following:
I/dalvikvm( 5980): Exec: /system/bin/sh -c /data/apitrace.sh /system/bin/app_process /system/bin --application '--nice-name=_process_name_' com.android.internal.os.WrapperInit 25 17 'android.app.ActivityThread'
D/apitrace( 5991): apitrace: loaded
D/apitrace( 5991): apitrace[5991]: enabled for _process_name_
D/apitrace( 5991): apitrace: tracing to /data/app_process.trace
I'm using CyanogenMod 10.1.3, which is based on Android 4.2.2.

Makefile(s) debug: which file/line calls to a command?

I'm facing a bug in a makefile build system (Android built under Linux) - some files are removed by an 'rm' command, and I can see that command in the build log.
How can I find the exact line in the makefiles which calls the 'rm' ? Is there any automated method?
For GNU Make you can do the following trick:
__shell := $(SHELL)
SHELL = \
$(warning making '$#'$(if $^, from '$^')$(if $?, because of '$?'))$(__shell)
SHELL variable is expanded each time when Make invokes a sub-shell to execute a recipe. In these lines it is replaced so that on each expansion it will print a target, its prerequisites and prerequisites that are newer than the target. Also each debug message is prepended with the file and line number of the rule being executed.
The same technique is used in GMD to set breakpoints to certain targets.
Assuming your make is a Gnu make, you can also pass some debugging options, like --debug=b (basic debugging messages, very often enough) or --debug=all which is the same as -d
Some files may be removed because they are intermediate. Read also about secondary files and precious files in make
You may try make -d -w and then grep your file from huge amount of output lines.

Retrieve Logcat data through native (C) code

I was wondering if there is any way to access the logcat logging data from native code ?
Also if someone knows what kind of shell can be accessed on the native linux system directly on the device itself (so NOT by starting a shell through adb on a client pc !!!) ?
Thanks in advance
You can either read the raw data fom /dev/log/main or you can run the built-in shell command logcat and pipe the result to a file descriptor as usual. The logcat command is usually preferably because then the printout is easier to filter and format.
As for the built-in shell, it is called toolbox and the source can be found in the Android open source project. The shell is rather similar to bash. Toolbox contains lots more functionality than just a shell. It is very similar to Busybox, but released under another license.
You can set it up using a log tag in your source file and then include the library as so:
#define LOG_TAG "some.unique.identifier" //I usually use the class name
#include <utils/Log.h>
Then in the Android.mk file you will need to add the liblog library dependency to your LOCAL_SHARED_LIBRARIES.
LOCAL_SHARED_LIBRARIES += liblog
Also, take note that logcat looks for the LOG_TAG definition to include as part of the tagging so it makes searching for your logs easier. Then you can log as such:
int my_int = 0;
ALOGI("some logs.... print my int: %d", my_int);
ALOGI is for info, you can also use ALOGE, ALOGD, ALOGV and ALOGW for error, debug, verbose and warning logging, respectively.
ALOG* is analogous to printf. I interchange them at times if I need to debug on different platforms, say Linux.

Categories

Resources