12

I'm trying to start a Java program under Valgring like this (in adb shell):

valgrind am start -a android.intent.action.MAIN -n com.me.myapp/.MainActivity

I'm getting:

==2362== Memcheck, a memory error detector
==2362== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==2362== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==2362== Command: am
==2362== 
/system/bin/sh: am: No such file or directory
Alex Bitek
  • 6,529
  • 5
  • 47
  • 77
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • 1
    `am start ..` would start an activity and return immediately. I don't think that you should valgrind that (the am command itself). http://stackoverflow.com/questions/9123124/how-to-start-an-android-app-with-valgrind seems to be the way to do it – zapl Nov 23 '12 at 15:03
  • @zapl Unfortunately I wasn't able to understand the procedure you indicated. Could you give me a step by step instructions if possible? – Alexander Kulyakhtin Nov 23 '12 at 15:09
  • 1
    Sry, I don't know how that works either. They seem to create a script (`/data/local/val.sh`) which in magical ways combined with setting a `wrap.` property results in log-output. – zapl Nov 23 '12 at 15:18
  • I'd love to know how to do this too. I've installed valgrind after much effort, but now I have no idea how to debug NDK apps with it. >:-( – Matthew Mitchell Sep 25 '13 at 17:33

2 Answers2

20

You have to create a script, lets call it start_valgrind.sh

#!/system/bin/sh

PACKAGE="com.example.hellojni"

# Callgrind tool
#VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/valgrind.log.%p --tool=callgrind --callgrind-out-file=/sdcard/callgrind.out.%p'

# Memcheck tool
VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/valgrind.log.%p --tool=memcheck --leak-check=full --show-reachable=yes'

export TMPDIR=/data/data/$PACKAGE

exec /data/local/Inst/bin/valgrind $VGPARAMS $* 

that should be copied to the device.

Once you have the above script in the start_valgrind.sh file somewhere on your local filesystem you can just use the below script (lets call it bootstrap_valgrind.sh) to do the all the work (copies the start_valgrind.sh script to the phone, runs it, starts your app through Valgrind).

#!/usr/bin/env bash

PACKAGE="com.example.hellojni"

adb push start_valgrind.sh /data/local/
adb shell chmod 777 /data/local/start_valgrind.sh 

adb root
adb shell setprop wrap.$PACKAGE "logwrapper /data/local/start_valgrind.sh"

echo "wrap.$PACKAGE: $(adb shell getprop wrap.$PACKAGE)"

adb shell am force-stop $PACKAGE
adb shell am start -a android.intent.action.MAIN -n $PACKAGE/.HelloJni

adb logcat -c
adb logcat

exit 0 

WARNING: Make sure the property name set with setprop i.e. (wrap.com.yourcompany.yourapp) has a length of less than 31 characters.
Otherwise, you'll get the error "could not set property" because you CANNOT set a property name with a length greater than 31, which is the number maximum allowed characters in the property name.
Also the property value should be <= 91 characters: https://stackoverflow.com/a/5068818/313113


For how to build Valgrind for Android (ARM) see my script from here: https://stackoverflow.com/a/19255251/313113

Community
  • 1
  • 1
Alex Bitek
  • 6,529
  • 5
  • 47
  • 77
  • 1
    This works indeed, but is extremely slow to be of any practical use. Any ideas? – Ayberk Özgür Sep 08 '14 at 14:30
  • 1
    Valgrind is slow by nature. I recommend either building your own Android OS and installing it on your phone with Valgrind included from either https://android.googlesource.com/platform/external/valgrind or https://github.com/CyanogenMod/android_external_valgrind OR try Address Sanitizer https://code.google.com/p/address-sanitizer/wiki/Android , http://dev.chromium.org/developers/testing/addresssanitizer – Alex Bitek Sep 08 '14 at 20:30
  • Thanks for your extensive reply. Valgrind included in Cyanogenmod sure seems like a good idea. – Ayberk Özgür Sep 09 '14 at 08:15
  • Also if your code runs on Android it will almost certainly run on GNU/Linux x86 or x86_64 system, so instead of debugging on the phone write a simple UI for your app in the Qt framework and test your code with the tools you have on a regular desktop and GNU userland. – Alex Bitek Sep 10 '14 at 18:51
  • I get this error on logcat: 10-31 16:30:47.949: I/start_valgrind.sh(15977): /data/data/start_valgrind.sh[14]: /data/local/Inst/bin/valgrind: not found. I rooted the samsung s3 through towelroot and i have root permissions. I did chmod 777 on /data/local/Inst/* – ssk Oct 31 '14 at 23:31
  • @ssk What's the ouput of /data/local/Inst/bin/valgrind --version ? You probably don't have Valgrind installed correctly at the installation path. – Alex Bitek Nov 01 '14 at 13:18
  • @Dr.SkyLizard, I get this error when I do ./valgrind --version1|root@d2spr:/data/local/Inst/bin # pwd /data/local/Inst/bin root@d2spr:/data/local/Inst/bin # ./valgrind --version valgrind: failed to start tool 'memcheck' for platform 'arm-linux': Permission denied When I run the same thing from my sdcard, it works fine. I feel that the app doesn't have permission to run valgrind from sdcard on runtime. Any thoughts? – ssk Nov 03 '14 at 16:58
  • @ssk You cannot run NDK apps from the SD card. SD cards on Android are usually formatted with a FAT filesystem or a variant of it which doesn't support Linux permissions. – Alex Bitek Nov 03 '14 at 18:51
  • @Dr.SkyLizard I couldn't type elaborately on the comments section. Please check this http://pastebin.com/ZnQdG0w2 for more information on my problem. – ssk Nov 03 '14 at 19:29
  • @ssk Make sure things under /data/local/Inst/lib/valgrind/ are executable i.e. /data/local/Inst/lib/valgrind/memcheck-arm-linux (but they should be by default) – Alex Bitek Nov 03 '14 at 19:37
  • yes, it is executable: -rwxrwxrwx root root 18767972 2014-10-31 15:22 memcheck-arm-linux I rooted my samsung s3 phone using https://towelroot.com/, wonder that would affect. Also, I have SuperSu installed on my phone. – ssk Nov 03 '14 at 19:41
  • @ssk I'm not sure what might be the problem. Here are my permissions for the Valgrind installation: https://gist.githubusercontent.com/DrSkyLizard/7becccf4e0e5c9c79c51/raw/ca2b68cad36aca1c8ae3e33faea442e38495e63f/valgrind_permissions_android.txt Related SO questions: http://stackoverflow.com/q/19641111/313113 && http://stackoverflow.com/q/19121524/313113 – Alex Bitek Nov 03 '14 at 19:55
  • @Dr.SkyLizard Thanks for the help. I will update this post with what I find. – ssk Nov 03 '14 at 20:06
  • @bitek I dont know why we can call start_valgrind.sh throgh bootstrap_valgrind. Can you explain for me. Thanks. Im trying to start app through valgrind but it is not work. I can check version valgrind in device. And I see I can not call start_valgrind.... What is wrong. – huy nguyen Oct 20 '17 at 08:07
  • @bitek this is myscript. https://stackoverflow.com/questions/46828477/memcheck-do-not-show-anylog Thank you very much again. – huy nguyen Oct 20 '17 at 08:08
  • Hi, can I do it *without* root permission? I do not want to root my phone, you know... – ch271828n Sep 30 '21 at 13:38
2

1) I had Used the Following script to Generate the Inst Folder Android valgrind build fails

2) The Mistake i was Doing is I have not Given write Permission to all the Folders under inst the MemCheck Tool is Under lib/valgrind .

My findings Copy all the Folders Under Generated Inst(bin,share,inclide,lib) Folder to /data/local/Inst Traverse through each folder and set the Permission to CHMOD 777 *

was facing the Issue Like Memcheck Tool Not Found for arm-linux if i didnot copy all the these Folders 1 folder /Inst facing memcheck permission if dont set the permission to chmod 777 to all the Folders in the Hirearchy

Community
  • 1
  • 1
surya
  • 607
  • 5
  • 18