NDK 17 malloc debug wrap shell

551 views
Skip to first unread message

Memo Rodriguez

unread,
Jun 1, 2018, 2:14:30 PM6/1/18
to android-ndk
hi 

I want to start using the new memory debugging tools that were introduced with the ndk toolchain. I've been looking into how to use the malloc debug in combination with the wrap shell script. I've created a simple toy example that allocates 1MB without freeing the memory but unfortunately I haven't been able to see the leak being reported in my adb logs.


extern "C"
JNIEXPORT jstring

JNICALL
Java_android_apple_com_memorydebuggingtest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
char* ptr = (char*) malloc(1024 * 1024 * sizeof(char*));
ptr = nullptr;
return env->NewStringUTF(hello.c_str());
}


I'm using a non-rooted pixel 2 with android 8.1, the documentation doesn't mentioned that the device should be rooted. Can you guys confirm?, do you guys have any toy examples  that could enforce and report a leak?

thx
Memo

Dan Albert

unread,
Jun 1, 2018, 2:43:19 PM6/1/18
to android-ndk, Christopher Ferris
+cferris

What does your wrap.sh look like? Is your APK debuggable?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/65e537a0-2e28-4291-8267-ca8983e15e2e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Memo Rodriguez

unread,
Jun 1, 2018, 2:57:27 PM6/1/18
to andro...@googlegroups.com, Christopher Ferris
hi Dan

These are the contents of my wrap.sh

#!/system/bin/sh
LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper leak_track $@
Yes, my apk is debugable
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.apple.com.memorydebuggingtest">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:debuggable="true"



On Fri, Jun 1, 2018 at 11:42 AM, 'Dan Albert' via android-ndk <andro...@googlegroups.com> wrote:
+cferris

What does your wrap.sh look like? Is your APK debuggable?
On Fri, Jun 1, 2018, 11:14 Memo Rodriguez <memo...@gmail.com> wrote:
hi 

I want to start using the new memory debugging tools that were introduced with the ndk toolchain. I've been looking into how to use the malloc debug in combination with the wrap shell script. I've created a simple toy example that allocates 1MB without freeing the memory but unfortunately I haven't been able to see the leak being reported in my adb logs.


extern "C"
JNIEXPORT jstring

JNICALL
Java_android_apple_com_memorydebuggingtest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
char* ptr = (char*) malloc(1024 * 1024 * sizeof(char*));
ptr = nullptr;
return env->NewStringUTF(hello.c_str());
}


I'm using a non-rooted pixel 2 with android 8.1, the documentation doesn't mentioned that the device should be rooted. Can you guys confirm?, do you guys have any toy examples  that could enforce and report a leak?

thx
Memo

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Dan Albert

unread,
Jun 1, 2018, 3:04:07 PM6/1/18
to android-ndk, Christopher Ferris
It may be that malloc debug didn't work on unrooted devices until P. I'm not very familiar with malloc debug, but I've CC'd the guy that is.

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Memo Rodriguez

unread,
Jun 1, 2018, 3:14:03 PM6/1/18
to andro...@googlegroups.com, Christopher Ferris
hi Dan

thx, I guess I can try running the same code on my emulator running P.

Memo

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Memo Rodriguez

unread,
Jun 1, 2018, 5:06:30 PM6/1/18
to Christopher Ferris, andro...@googlegroups.com
Hi Christopher

I made the changes that you mentioned in your previous email but unfortunately I don't see the "malloc debug enabled" msg in logcat. I tried with both options "backtrace leak_track" and with each of them individually.

Do you have any other ideas?

thx
Memo

On Fri, Jun 1, 2018 at 12:30 PM, Christopher Ferris <cfe...@google.com> wrote:
I think this should work on a non-rooted phone. If you have the ability to see the logcat output, you should see a message something with a "malloc debug enabled" message.

However, looking at your wrap.sh, I believe the environment variable is a bit off.
  LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper leak_track $@
This should be:
  LIBC_DEBUG_MALLOC_OPTIONS="backtrace leak_track" logwrapper $@
You can also try just leak_track if you suspect that even that's not working properly, but I think this should be good to go. I think we should probably add some extra documentation showing how to use multiple options since it can be tricky.

Christopher

Christopher Ferris

unread,
Jun 1, 2018, 5:16:17 PM6/1/18
to memo...@gmail.com, andro...@googlegroups.com
I think this should work on a non-rooted phone. If you have the ability to see the logcat output, you should see a message something with a "malloc debug enabled" message.

However, looking at your wrap.sh, I believe the environment variable is a bit off.
  LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper leak_track $@
This should be:
  LIBC_DEBUG_MALLOC_OPTIONS="backtrace leak_track" logwrapper $@
You can also try just leak_track if you suspect that even that's not working properly, but I think this should be good to go. I think we should probably add some extra documentation showing how to use multiple options since it can be tricky.

Christopher

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Christopher Ferris

unread,
Jun 1, 2018, 5:37:54 PM6/1/18
to memo...@gmail.com, andro...@googlegroups.com
I verified that on a Pixel 2 device running a user 8.1 build, I was able to set the environment variable at the shell level and enable malloc debug. Therefore, I think something is going on with the usage of wrap.sh.

Here are a couple of things to check:

- Is wrap.sh executable?
- Where is the script located? It should be in lib/<arch>/wrap.sh, if your app is 64 bit, then lib/arm64/wrap.sh. To be thorough, you could add it in lib/arm/ too.

Here's the official documentation on wrap.sh, in case you didn't find it:


If that doesn't work, I don't think I have any other ideas.

Christopher

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Nithin Malik

unread,
Jun 7, 2019, 10:38:39 AM6/7/19
to android-ndk
Hi,

I have the same issue, please share the working wrap.sh.


Above procedure is not working in pixel3 android 9.0.




Thanks
Nithin
To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Akansha Pal

unread,
Jul 11, 2019, 4:56:51 PM7/11/19
to android-ndk
Hi, I am trying to set malloc_debug property to backtrace in PIE but i am not able to do so.
Can anyone help??

Dan Albert

unread,
Jul 12, 2019, 12:56:59 AM7/12/19
to android-ndk, Christopher Ferris
+cferris

On Thu, Jul 11, 2019 at 1:56 PM Akansha Pal <akans...@gmail.com> wrote:
Hi, I am trying to set malloc_debug property to backtrace in PIE but i am not able to do so.
Can anyone help??

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
Reply all
Reply to author
Forward
0 new messages