blob: 5d8011a1a28cc35a4b3f1c98d0a5cd2a2bb36086 [file] [log] [blame]
Scott Anderson1a5fc952012-03-07 17:15:06 -08001function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07002cat <<EOF
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08003Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Ying Wang67f02922012-08-22 10:25:20 -07004- lunch: lunch <product_name>-<build_variant>
Ying Wangb541ab62014-05-29 17:57:40 -07005- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07006- croot: Changes directory to the top of the tree.
nebkat09d30562012-12-28 10:40:45 +00007- cout: Changes directory to out.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07008- m: Makes from the top of the tree.
Ying Wangb607f7b2013-02-08 18:01:04 -08009- mm: Builds all of the modules in the current directory, but not their dependencies.
10- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
Primiano Tucci6a8069d2014-02-26 11:09:19 +000011 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Ying Wangb607f7b2013-02-08 18:01:04 -080012- mma: Builds all of the modules in the current directory, and their dependencies.
Daniel Bateman87c5c5a2012-08-04 03:48:09 -050013- mmp: Builds all of the modules in the current directory and pushes them to the device.
14- mmmp: Builds all of the modules in the supplied directories and pushes them to the device.
Ying Wangb607f7b2013-02-08 18:01:04 -080015- mmma: Builds all of the modules in the supplied directories, and their dependencies.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070016- cgrep: Greps on all local C/C++ files.
Jon Boekenoogencbca56f2014-04-07 10:57:38 -070017- ggrep: Greps on all local Gradle files.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070018- jgrep: Greps on all local Java files.
19- resgrep: Greps on all local res/*.xml files.
Jeff Brown46cbd202014-07-26 15:15:41 -070020- sgrep: Greps on all local source files.
The Android Open Source Project88b60792009-03-03 19:28:42 -080021- godir: Go to the directory containing a file.
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -060022- cmremote: Add git remote for CM Gerrit Review
23- cmgerrit: A Git wrapper that fetches/pushes patch from/to CM Gerrit Review
24- cmrebase: Rebase a Gerrit change and push it again
Steve Kondikb99aa572012-09-17 11:33:18 -070025- aospremote: Add git remote for matching AOSP repository
Steve Kondik263c0412013-10-27 13:34:36 -070026- cafremote: Add git remote for matching CodeAurora repository.
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -060027- mka: Builds using SCHED_BATCH on all processors
Chirayu Desaic2cd6e72012-09-28 11:56:02 +053028- mkap: Builds the module(s) using mka and pushes them to the device.
29- cmka: Cleans and builds using mka.
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -060030- reposync: Parallel repo sync using ionice and SCHED_BATCH
Chirayu Desai1a07b002013-06-05 20:14:33 +053031- repopick: Utility to fetch changes from Gerrit.
Steve Kondik0a9a0712012-10-24 16:40:42 -070032- installboot: Installs a boot.img to the connected device.
33- installrecovery: Installs a recovery.img to the connected device.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070034
35Look at the source to view more functions. The complete list is:
36EOF
37 T=$(gettop)
38 local A
39 A=""
Jeff Brown46cbd202014-07-26 15:15:41 -070040 for i in `cat $T/build/envsetup.sh | sed -n "/^[ \t]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070041 A="$A $i"
42 done
43 echo $A
44}
45
46# Get the value of a build variable as an absolute path.
47function get_abs_build_var()
48{
49 T=$(gettop)
50 if [ ! "$T" ]; then
51 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
52 return
53 fi
Ying Wang9cd17642012-12-13 10:52:07 -080054 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070055 command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070056}
57
58# Get the exact value of a build variable.
59function get_build_var()
60{
61 T=$(gettop)
62 if [ ! "$T" ]; then
63 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
64 return
65 fi
Andrew Boie6905e212013-12-19 13:21:46 -080066 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070067 command make --no-print-directory -f build/core/config.mk dumpvar-$1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080068}
69
70# check to see if the supplied product is one we can build
71function check_product()
72{
73 T=$(gettop)
74 if [ ! "$T" ]; then
75 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
76 return
77 fi
Ricardo Cerqueirab4afc9d2011-11-25 15:30:36 +000078
79 if (echo -n $1 | grep -q -e "^cm_") ; then
80 CM_BUILD=$(echo -n $1 | sed -e 's/^cm_//g')
Ricardo Cerqueira136f0e52013-09-26 00:10:20 +010081 export BUILD_NUMBER=$((date +%s%N ; echo $CM_BUILD; hostname) | openssl sha1 | sed -e 's/.*=//g; s/ //g' | cut -c1-10)
Ricardo Cerqueirab4afc9d2011-11-25 15:30:36 +000082 else
83 CM_BUILD=
84 fi
85 export CM_BUILD
86
Jeff Browne33ba4c2011-07-11 22:11:46 -070087 TARGET_PRODUCT=$1 \
88 TARGET_BUILD_VARIANT= \
89 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -070090 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080091 get_build_var TARGET_DEVICE > /dev/null
92 # hide successful answers, but allow the errors to show
93}
94
95VARIANT_CHOICES=(user userdebug eng)
96
97# check to see if the supplied variant is valid
98function check_variant()
99{
100 for v in ${VARIANT_CHOICES[@]}
101 do
102 if [ "$v" = "$1" ]
103 then
104 return 0
105 fi
106 done
107 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700108}
109
110function setpaths()
111{
112 T=$(gettop)
113 if [ ! "$T" ]; then
114 echo "Couldn't locate the top of the tree. Try setting TOP."
115 return
116 fi
117
118 ##################################################################
119 # #
120 # Read me before you modify this code #
121 # #
122 # This function sets ANDROID_BUILD_PATHS to what it is adding #
123 # to PATH, and the next time it is run, it removes that from #
124 # PATH. This is required so lunch can be run more than once #
125 # and still have working paths. #
126 # #
127 ##################################################################
128
Raphael Mollc639c782011-06-20 17:25:01 -0700129 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
130 # due to "C:\Program Files" being in the path.
131
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700132 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700133 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700134 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
135 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700136 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700137 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800138 # strip leading ':', if any
139 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500140 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700141
142 # and in with the new
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700143 prebuiltdir=$(getprebuilt)
Jing Yuf5172c72012-03-29 20:45:50 -0700144 gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700145
Ben Cheng8bc4c432012-11-16 13:29:13 -0800146 # defined in core/config.mk
147 targetgccversion=$(get_build_var TARGET_GCC_VERSION)
Colin Cross03b424a2014-05-22 11:57:43 -0700148 targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800149 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800150
Raphael Mollc639c782011-06-20 17:25:01 -0700151 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800152 export ANDROID_TOOLCHAIN=
153 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200154 local ARCH=$(get_build_var TARGET_ARCH)
155 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400156 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700157 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400158 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
159 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800160 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200161 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800162 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700163 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700164 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700165 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000166 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200167 *)
168 echo "Can't find toolchain for unknown architecture: $ARCH"
169 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700170 ;;
171 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700172 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800173 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700174 fi
Raphael732936d2011-06-22 14:35:32 -0700175
Ben Chengfba67bf2014-02-25 10:27:07 -0800176 if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
177 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
178 fi
179
180 unset ANDROID_KERNEL_TOOLCHAIN_PATH
Ying Wang08f5e9a2012-04-17 18:10:11 -0700181 case $ARCH in
Bruce Beare42ced6d2012-07-17 21:40:01 -0700182 arm)
Ben Chengfba67bf2014-02-25 10:27:07 -0800183 # Legacy toolchain configuration used for ARM kernel compilation
Ben Cheng8bc4c432012-11-16 13:29:13 -0800184 toolchaindir=arm/arm-eabi-$targetgccversion/bin
Bruce Beare42ced6d2012-07-17 21:40:01 -0700185 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Torne (Richard Coles)f24c3562014-04-25 16:24:22 +0100186 export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
187 ANDROID_KERNEL_TOOLCHAIN_PATH="$ARM_EABI_TOOLCHAIN":
Bruce Beare42ced6d2012-07-17 21:40:01 -0700188 fi
Ying Wang08f5e9a2012-04-17 18:10:11 -0700189 ;;
190 *)
Bruce Beare42ced6d2012-07-17 21:40:01 -0700191 # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
Ying Wang08f5e9a2012-04-17 18:10:11 -0700192 ;;
193 esac
Ying Wang08f5e9a2012-04-17 18:10:11 -0700194
Ying Wang564f9122013-03-29 17:40:14 -0700195 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
Ying Wang2a859d52014-06-30 10:25:33 -0700196 export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN:$ANDROID_TOOLCHAIN_2ND_ARCH:$ANDROID_KERNEL_TOOLCHAIN_PATH$ANDROID_DEV_SCRIPTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200197
198 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
199 # to ensure that the corresponding 'emulator' binaries are used.
200 case $(uname -s) in
201 Darwin)
202 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
203 ;;
204 Linux)
205 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
206 ;;
207 *)
208 ANDROID_EMULATOR_PREBUILTS=
209 ;;
210 esac
211 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Christopher Ferris7110f242014-05-20 13:56:00 -0700212 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200213 export ANDROID_EMULATOR_PREBUILTS
214 fi
215
Ying Wangaa1c9b52012-11-26 20:51:59 -0800216 export PATH=$ANDROID_BUILD_PATHS$PATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800217
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500218 unset ANDROID_JAVA_TOOLCHAIN
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900219 unset ANDROID_PRE_BUILD_PATHS
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500220 if [ -n "$JAVA_HOME" ]; then
221 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900222 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
223 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500224 fi
225
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800226 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700227 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
228 export OUT=$ANDROID_PRODUCT_OUT
229
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700230 unset ANDROID_HOST_OUT
231 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
232
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800233 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700234 # TODO: fix the path
235 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
236}
237
238function printconfig()
239{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800240 T=$(gettop)
241 if [ ! "$T" ]; then
242 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
243 return
244 fi
245 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700246}
247
248function set_stuff_for_environment()
249{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800250 settitle
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500251 set_java_home
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800252 setpaths
253 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700254
Ben Chengaac3f812013-08-13 14:38:15 -0700255 # With this environment variable new GCC can apply colors to warnings/errors
256 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700257}
258
259function set_sequence_number()
260{
Joe Onoratoaee4daa2010-06-23 14:03:13 -0700261 export BUILD_ENV_SEQUENCE_NUMBER=10
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700262}
263
264function settitle()
265{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800266 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
Raghu Gandham8da43102012-07-25 19:57:22 -0700267 local arch=$(gettargetarch)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700268 local product=$TARGET_PRODUCT
269 local variant=$TARGET_BUILD_VARIANT
270 local apps=$TARGET_BUILD_APPS
Steve Kondike53c87d2012-12-27 15:28:34 -0800271 if [ -z "$PROMPT_COMMAND" ]; then
272 # No prompts
273 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
274 elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
275 # Prompts exist, but no hardstatus
276 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700277 fi
Steve Kondike53c87d2012-12-27 15:28:34 -0800278 if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
Christopher Laiseff12542013-06-03 15:15:39 -0500279 PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')"
Steve Kondike53c87d2012-12-27 15:28:34 -0800280 fi
281
282 if [ -z "$apps" ]; then
283 ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
284 else
285 ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
286 fi
287 export ANDROID_PROMPT_PREFIX
288
289 # Inject build data into hardstatus
290 export PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/\\033]0;\(.*\)\\007/\\033]0;$ANDROID_PROMPT_PREFIX \1\\007/g')"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800291 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700292}
293
Kyle Ladddea4f872013-09-11 20:43:42 -0400294function check_bash_version()
Kenny Root52aa81c2011-07-15 11:07:06 -0700295{
Kenny Root52aa81c2011-07-15 11:07:06 -0700296 # Keep us from trying to run in something that isn't bash.
297 if [ -z "${BASH_VERSION}" ]; then
Kyle Ladddea4f872013-09-11 20:43:42 -0400298 return 1
Kenny Root52aa81c2011-07-15 11:07:06 -0700299 fi
300
301 # Keep us from trying to run in bash that's too old.
James Roberts-Thomson2e406782013-04-16 15:53:39 +1200302 if [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
Kyle Ladddea4f872013-09-11 20:43:42 -0400303 return 2
Kenny Root52aa81c2011-07-15 11:07:06 -0700304 fi
305
Kyle Ladddea4f872013-09-11 20:43:42 -0400306 return 0
Kenny Root52aa81c2011-07-15 11:07:06 -0700307}
308
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700309function choosetype()
310{
311 echo "Build type choices are:"
312 echo " 1. release"
313 echo " 2. debug"
314 echo
315
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800316 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700317 DEFAULT_NUM=1
318 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700319
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800320 export TARGET_BUILD_TYPE=
321 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700322 while [ -z $TARGET_BUILD_TYPE ]
323 do
324 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800325 if [ -z "$1" ] ; then
326 read ANSWER
327 else
328 echo $1
329 ANSWER=$1
330 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700331 case $ANSWER in
332 "")
333 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
334 ;;
335 1)
336 export TARGET_BUILD_TYPE=release
337 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800338 release)
339 export TARGET_BUILD_TYPE=release
340 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700341 2)
342 export TARGET_BUILD_TYPE=debug
343 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800344 debug)
345 export TARGET_BUILD_TYPE=debug
346 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700347 *)
348 echo
349 echo "I didn't understand your response. Please try again."
350 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700351 ;;
352 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800353 if [ -n "$1" ] ; then
354 break
355 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700356 done
357
358 set_stuff_for_environment
359}
360
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800361#
362# This function isn't really right: It chooses a TARGET_PRODUCT
363# based on the list of boards. Usually, that gets you something
364# that kinda works with a generic product, but really, you should
365# pick a product by name.
366#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700367function chooseproduct()
368{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700369 if [ "x$TARGET_PRODUCT" != x ] ; then
370 default_value=$TARGET_PRODUCT
371 else
Jeff Browne33ba4c2011-07-11 22:11:46 -0700372 default_value=full
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700373 fi
374
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800375 export TARGET_PRODUCT=
376 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700377 while [ -z "$TARGET_PRODUCT" ]
378 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700379 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800380 if [ -z "$1" ] ; then
381 read ANSWER
382 else
383 echo $1
384 ANSWER=$1
385 fi
386
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700387 if [ -z "$ANSWER" ] ; then
388 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800389 else
390 if check_product $ANSWER
391 then
392 export TARGET_PRODUCT=$ANSWER
393 else
394 echo "** Not a valid product: $ANSWER"
395 fi
396 fi
397 if [ -n "$1" ] ; then
398 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700399 fi
400 done
401
402 set_stuff_for_environment
403}
404
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800405function choosevariant()
406{
407 echo "Variant choices are:"
408 local index=1
409 local v
410 for v in ${VARIANT_CHOICES[@]}
411 do
412 # The product name is the name of the directory containing
413 # the makefile we found, above.
414 echo " $index. $v"
415 index=$(($index+1))
416 done
417
418 local default_value=eng
419 local ANSWER
420
421 export TARGET_BUILD_VARIANT=
422 while [ -z "$TARGET_BUILD_VARIANT" ]
423 do
424 echo -n "Which would you like? [$default_value] "
425 if [ -z "$1" ] ; then
426 read ANSWER
427 else
428 echo $1
429 ANSWER=$1
430 fi
431
432 if [ -z "$ANSWER" ] ; then
433 export TARGET_BUILD_VARIANT=$default_value
434 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
435 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800436 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800437 fi
438 else
439 if check_variant $ANSWER
440 then
441 export TARGET_BUILD_VARIANT=$ANSWER
442 else
443 echo "** Not a valid variant: $ANSWER"
444 fi
445 fi
446 if [ -n "$1" ] ; then
447 break
448 fi
449 done
450}
451
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700452function choosecombo()
453{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700454 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700455
456 echo
457 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700458 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700459
460 echo
461 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700462 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800463
464 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800466 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467}
468
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800469# Clear this variable. It will be built up again when the vendorsetup.sh
470# files are included at the end of this file.
471unset LUNCH_MENU_CHOICES
472function add_lunch_combo()
473{
474 local new_combo=$1
475 local c
476 for c in ${LUNCH_MENU_CHOICES[@]} ; do
477 if [ "$new_combo" = "$c" ] ; then
478 return
479 fi
480 done
481 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
482}
483
484# add the default one here
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700485add_lunch_combo aosp_arm-eng
Colin Cross4f0eb7d2014-01-21 19:35:38 -0800486add_lunch_combo aosp_arm64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000487add_lunch_combo aosp_mips-eng
Chris Dearman1efd9e42014-02-03 15:01:24 -0800488add_lunch_combo aosp_mips64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000489add_lunch_combo aosp_x86-eng
490add_lunch_combo aosp_x86_64-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800491
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700492function print_lunch_menu()
493{
494 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700495 echo
496 echo "You're building on" $uname
Ricardo Cerqueira105bb222011-01-31 00:49:49 +0000497 if [ "$(uname)" = "Darwin" ] ; then
498 echo " (ohai, koush!)"
499 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700500 echo
Ricardo Cerqueira105bb222011-01-31 00:49:49 +0000501 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
502 echo "Breakfast menu... pick a combo:"
503 else
504 echo "Lunch menu... pick a combo:"
505 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800506
507 local i=1
508 local choice
509 for choice in ${LUNCH_MENU_CHOICES[@]}
510 do
cybojenix2cede272013-06-28 20:30:00 +0100511 echo " $i. $choice "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800512 i=$(($i+1))
cybojenix2cede272013-06-28 20:30:00 +0100513 done | column
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800514
Ricardo Cerqueira105bb222011-01-31 00:49:49 +0000515 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
516 echo "... and don't forget the bacon!"
517 fi
518
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700519 echo
520}
521
Ricardo Cerqueira105bb222011-01-31 00:49:49 +0000522function brunch()
523{
524 breakfast $*
525 if [ $? -eq 0 ]; then
526 mka bacon
527 else
528 echo "No such item in brunch menu. Try 'breakfast'"
529 return 1
530 fi
531 return $?
532}
533
534function breakfast()
535{
536 target=$1
JustArchi144278a2014-06-22 14:37:30 +0200537 local variant=$2
Ricardo Cerqueira105bb222011-01-31 00:49:49 +0000538 CM_DEVICES_ONLY="true"
539 unset LUNCH_MENU_CHOICES
540 add_lunch_combo full-eng
541 for f in `/bin/ls vendor/cm/vendorsetup.sh 2> /dev/null`
542 do
543 echo "including $f"
544 . $f
545 done
546 unset f
547
548 if [ $# -eq 0 ]; then
549 # No arguments, so let's have the full menu
550 lunch
551 else
552 echo "z$target" | grep -q "-"
553 if [ $? -eq 0 ]; then
554 # A buildtype was specified, assume a full device name
555 lunch $target
556 else
557 # This is probably just the CM model name
JustArchi144278a2014-06-22 14:37:30 +0200558 if [ -z "$variant" ]; then
559 variant="userdebug"
560 fi
561 lunch cm_$target-$variant
Ricardo Cerqueira105bb222011-01-31 00:49:49 +0000562 fi
563 fi
564 return $?
565}
566
567alias bib=breakfast
568
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700569function lunch()
570{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800571 local answer
572
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700573 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800574 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700575 else
576 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700577 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 fi
580
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800581 local selection=
582
583 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700585 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
587 then
588 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
589 then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800590 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800591 fi
592 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
593 then
594 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700595 fi
596
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800597 if [ -z "$selection" ]
598 then
599 echo
600 echo "Invalid lunch combo: $answer"
601 return 1
602 fi
603
Joe Onoratoda12daf2010-06-09 18:18:31 -0700604 export TARGET_BUILD_APPS=
605
Jeff Browne33ba4c2011-07-11 22:11:46 -0700606 local product=$(echo -n $selection | sed -e "s/-.*$//")
607 check_product $product
608 if [ $? -ne 0 ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800609 then
Koushik Duttad8308d92011-11-26 18:51:42 -0800610 # if we can't find a product, try to grab it off the CM github
611 T=$(gettop)
612 pushd $T > /dev/null
613 build/tools/roomservice.py $product
614 popd > /dev/null
615 check_product $product
Diogo Ferreira01ab00d2012-03-18 21:18:29 +0000616 else
617 build/tools/roomservice.py $product true
Koushik Duttad8308d92011-11-26 18:51:42 -0800618 fi
619 if [ $? -ne 0 ]
620 then
Jeff Browne33ba4c2011-07-11 22:11:46 -0700621 echo
622 echo "** Don't have a product spec for: '$product'"
623 echo "** Do you have the right repo manifest?"
624 product=
625 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626
Jeff Browne33ba4c2011-07-11 22:11:46 -0700627 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
628 check_variant $variant
629 if [ $? -ne 0 ]
630 then
631 echo
632 echo "** Invalid variant: '$variant'"
633 echo "** Must be one of ${VARIANT_CHOICES[@]}"
634 variant=
635 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636
Jeff Browne33ba4c2011-07-11 22:11:46 -0700637 if [ -z "$product" -o -z "$variant" ]
638 then
639 echo
640 return 1
641 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800642
Jeff Browne33ba4c2011-07-11 22:11:46 -0700643 export TARGET_PRODUCT=$product
644 export TARGET_BUILD_VARIANT=$variant
645 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700646
647 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800648
Chirayu Desai069ff052013-06-30 10:04:25 +0530649 fixup_common_out_dir
650
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700651 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800652 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700653}
654
Jeff Davidson513d7a42010-08-02 10:00:44 -0700655# Tab completion for lunch.
656function _lunch()
657{
658 local cur prev opts
659 COMPREPLY=()
660 cur="${COMP_WORDS[COMP_CWORD]}"
661 prev="${COMP_WORDS[COMP_CWORD-1]}"
662
663 COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
664 return 0
665}
Emilio López53d37382013-11-03 13:02:13 -0300666complete -F _lunch lunch 2>/dev/null
Jeff Davidson513d7a42010-08-02 10:00:44 -0700667
Joe Onoratoda12daf2010-06-09 18:18:31 -0700668# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700669# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700670function tapas()
671{
Ying Wangb541ab62014-05-29 17:57:40 -0700672 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700673 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700674 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
675 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|armv5|arm64|x86_64|mips64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700676
Ying Wang67f02922012-08-22 10:25:20 -0700677 if [ $(echo $arch | wc -w) -gt 1 ]; then
678 echo "tapas: Error: Multiple build archs supplied: $arch"
679 return
680 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700681 if [ $(echo $variant | wc -w) -gt 1 ]; then
682 echo "tapas: Error: Multiple build variants supplied: $variant"
683 return
684 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700685 if [ $(echo $density | wc -w) -gt 1 ]; then
686 echo "tapas: Error: Multiple densities supplied: $density"
687 return
688 fi
Ying Wang67f02922012-08-22 10:25:20 -0700689
690 local product=full
691 case $arch in
Ying Wangb541ab62014-05-29 17:57:40 -0700692 x86) product=full_x86;;
693 mips) product=full_mips;;
694 armv5) product=generic_armv5;;
695 arm64) product=aosp_arm64;;
696 x86_64) product=aosp_x86_64;;
697 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700698 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700699 if [ -z "$variant" ]; then
700 variant=eng
701 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700702 if [ -z "$apps" ]; then
703 apps=all
704 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600705 if [ -z "$density" ]; then
706 density=alldpi
707 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700708
Ying Wang67f02922012-08-22 10:25:20 -0700709 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700710 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700711 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700712 export TARGET_BUILD_TYPE=release
713 export TARGET_BUILD_APPS=$apps
714
715 set_stuff_for_environment
716 printconfig
717}
718
Ricardo Cerqueiraf5e379c2011-08-19 20:37:12 +0100719function eat()
720{
721 if [ "$OUT" ] ; then
Chirayu Desaiaa3b2072013-05-04 17:59:18 +0530722 MODVERSION=$(get_build_var CM_VERSION)
Ricardo Cerqueiraf5e379c2011-08-19 20:37:12 +0100723 ZIPFILE=cm-$MODVERSION.zip
724 ZIPPATH=$OUT/$ZIPFILE
725 if [ ! -f $ZIPPATH ] ; then
726 echo "Nothing to eat"
727 return 1
728 fi
729 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
730 if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
731 echo "No device is online. Waiting for one..."
732 echo "Please connect USB and/or enable USB debugging"
733 until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
734 sleep 1
735 done
736 echo "Device Found.."
737 fi
Chirayu Desaiccf13932012-12-26 10:53:58 +0530738 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
739 then
Ricardo Cerqueiraf5e379c2011-08-19 20:37:12 +0100740 # if adbd isn't root we can't write to /cache/recovery/
741 adb root
742 sleep 1
743 adb wait-for-device
Steve Kondik0ab0e7f2013-04-13 07:19:52 -0700744 cat << EOF > /tmp/command
745--sideload
Ricardo Cerqueiraf5e379c2011-08-19 20:37:12 +0100746EOF
Steve Kondik0ab0e7f2013-04-13 07:19:52 -0700747 if adb push /tmp/command /cache/recovery/ ; then
748 echo "Rebooting into recovery for sideload installation"
749 adb reboot recovery
750 adb wait-for-sideload
751 adb sideload $ZIPPATH
Ricardo Cerqueiraf5e379c2011-08-19 20:37:12 +0100752 fi
Steve Kondik0ab0e7f2013-04-13 07:19:52 -0700753 rm /tmp/command
Ricardo Cerqueiraf5e379c2011-08-19 20:37:12 +0100754 else
755 echo "Nothing to eat"
756 return 1
757 fi
758 return $?
Chirayu Desaiccf13932012-12-26 10:53:58 +0530759 else
760 echo "The connected device does not appear to be $CM_BUILD, run away!"
761 fi
Ricardo Cerqueiraf5e379c2011-08-19 20:37:12 +0100762}
763
Nebojsa Cvetkovic7990b7f2012-11-09 23:02:54 +0000764function omnom
765{
766 brunch $*
767 eat
768}
769
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700770function gettop
771{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800772 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700773 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700774 # The following circumlocution ensures we remove symlinks from TOP.
775 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700776 else
777 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800778 # The following circumlocution (repeated below as well) ensures
779 # that we record the true directory name and not one that is
780 # faked up with symlink names.
781 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700782 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800783 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700784 T=
785 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800786 \cd ..
synergyb112a402013-07-05 19:47:47 -0700787 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700788 done
Ying Wang9cd17642012-12-13 10:52:07 -0800789 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700790 if [ -f "$T/$TOPFILE" ]; then
791 echo $T
792 fi
793 fi
794 fi
795}
796
Andrew Hsieh906cb782013-09-10 17:37:14 +0800797# Return driver for "make", if any (eg. static analyzer)
798function getdriver()
799{
800 local T="$1"
801 test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
802 if [ -n "$WITH_STATIC_ANALYZER" ]; then
803 echo "\
Andrew Hsiehc4f7fba2014-03-03 16:53:17 +0800804$T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
805--use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
Andrew Hsieh906cb782013-09-10 17:37:14 +0800806--status-bugs \
807--top=$T"
808 fi
809}
810
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700811function m()
812{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800813 local T=$(gettop)
814 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700815 if [ "$T" ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800816 $DRV make -C $T -f build/core/main.mk $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700817 else
818 echo "Couldn't locate the top of the tree. Try setting TOP."
819 fi
820}
821
822function findmakefile()
823{
824 TOPFILE=build/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800825 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700826 T=
827 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700828 T=`PWD= /bin/pwd`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700829 if [ -f "$T/Android.mk" ]; then
830 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800831 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700832 return
833 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800834 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700835 done
Ying Wang9cd17642012-12-13 10:52:07 -0800836 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700837}
838
839function mm()
840{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800841 local T=$(gettop)
842 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700843 # If we're sitting in the root of the build tree, just do a
844 # normal make.
845 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800846 $DRV make $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700847 else
848 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800849 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700850 local MODULES=
851 local GET_INSTALL_PATH=
852 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700853 # Remove the path to top as the makefilepath needs to be relative
854 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700855 if [ ! "$T" ]; then
856 echo "Couldn't locate the top of the tree. Try setting TOP."
857 elif [ ! "$M" ]; then
858 echo "Couldn't locate a makefile from the current directory."
859 else
Ying Wanga7deb082013-08-16 13:24:47 -0700860 for ARG in $@; do
861 case $ARG in
862 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
863 esac
864 done
865 if [ -n "$GET_INSTALL_PATH" ]; then
866 MODULES=
867 ARGS=GET-INSTALL-PATH
868 else
869 MODULES=all_modules
870 ARGS=$@
871 fi
Andrew Hsieh246daf72013-09-10 18:07:23 -0700872 ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700873 fi
874 fi
875}
876
877function mmm()
878{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800879 local T=$(gettop)
880 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700881 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800882 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800883 local MODULES=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800884 local ARGS=
885 local DIR TO_CHOP
Ying Wanga7deb082013-08-16 13:24:47 -0700886 local GET_INSTALL_PATH=
The Android Open Source Project88b60792009-03-03 19:28:42 -0800887 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
888 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
889 for DIR in $DIRS ; do
Alon Albert68895a92011-11-30 12:40:19 -0800890 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
891 if [ "$MODULES" = "" ]; then
892 MODULES=all_modules
893 fi
894 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700895 if [ -f $DIR/Android.mk ]; then
Ying Wanga7deb082013-08-16 13:24:47 -0700896 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
897 local TO_CHOP=`expr $TO_CHOP + 1`
898 local START=`PWD= /bin/pwd`
899 local MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700900 if [ "$MFILE" = "" ] ; then
901 MFILE=$DIR/Android.mk
902 else
903 MFILE=$MFILE/$DIR/Android.mk
904 fi
905 MAKEFILE="$MAKEFILE $MFILE"
906 else
Ying Wanga7deb082013-08-16 13:24:47 -0700907 case $DIR in
908 showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
909 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
910 *) echo "No Android.mk in $DIR."; return 1;;
911 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700912 fi
913 done
Ying Wanga7deb082013-08-16 13:24:47 -0700914 if [ -n "$GET_INSTALL_PATH" ]; then
915 ARGS=$GET_INSTALL_PATH
916 MODULES=
917 fi
Andrew Hsieh906cb782013-09-10 17:37:14 +0800918 ONE_SHOT_MAKEFILE="$MAKEFILE" $DRV make -C $T -f build/core/main.mk $DASH_ARGS $MODULES $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700919 else
920 echo "Couldn't locate the top of the tree. Try setting TOP."
921 fi
922}
923
Ying Wangb607f7b2013-02-08 18:01:04 -0800924function mma()
925{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800926 local T=$(gettop)
927 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800928 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800929 $DRV make $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800930 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800931 if [ ! "$T" ]; then
932 echo "Couldn't locate the top of the tree. Try setting TOP."
933 fi
934 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
Andrew Hsieh906cb782013-09-10 17:37:14 +0800935 $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
Ying Wangb607f7b2013-02-08 18:01:04 -0800936 fi
937}
938
939function mmma()
940{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800941 local T=$(gettop)
942 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800943 if [ "$T" ]; then
944 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
945 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
946 local MY_PWD=`PWD= /bin/pwd`
947 if [ "$MY_PWD" = "$T" ]; then
948 MY_PWD=
949 else
950 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
951 fi
952 local DIR=
953 local MODULE_PATHS=
954 local ARGS=
955 for DIR in $DIRS ; do
956 if [ -d $DIR ]; then
957 if [ "$MY_PWD" = "" ]; then
958 MODULE_PATHS="$MODULE_PATHS $DIR"
959 else
960 MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
961 fi
962 else
963 case $DIR in
964 showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
965 *) echo "Couldn't find directory $DIR"; return 1;;
966 esac
967 fi
968 done
Andrew Hsieh906cb782013-09-10 17:37:14 +0800969 $DRV make -C $T -f build/core/main.mk $DASH_ARGS $ARGS all_modules BUILD_MODULES_IN_PATHS="$MODULE_PATHS"
Ying Wangb607f7b2013-02-08 18:01:04 -0800970 else
971 echo "Couldn't locate the top of the tree. Try setting TOP."
972 fi
973}
974
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700975function croot()
976{
977 T=$(gettop)
978 if [ "$T" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800979 \cd $(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700980 else
981 echo "Couldn't locate the top of the tree. Try setting TOP."
982 fi
983}
984
nebkat09d30562012-12-28 10:40:45 +0000985function cout()
986{
987 if [ "$OUT" ]; then
988 cd $OUT
989 else
990 echo "Couldn't locate out directory. Try setting OUT."
991 fi
992}
993
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700994function cproj()
995{
996 TOPFILE=build/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700997 local HERE=$PWD
998 T=
999 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1000 T=$PWD
1001 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001002 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001003 return
1004 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001005 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001006 done
Ying Wang9cd17642012-12-13 10:52:07 -08001007 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001008 echo "can't find Android.mk"
1009}
1010
Daniel Sandler47e0a882013-07-30 13:23:52 -04001011# simplified version of ps; output in the form
1012# <pid> <procname>
1013function qpid() {
1014 local prepend=''
1015 local append=''
1016 if [ "$1" = "--exact" ]; then
1017 prepend=' '
1018 append='$'
1019 shift
1020 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
1021 echo "usage: qpid [[--exact] <process name|pid>"
1022 return 255
1023 fi
1024
1025 local EXE="$1"
1026 if [ "$EXE" ] ; then
Mathias Agopian44583272013-08-27 14:15:50 -07001027 qpid | \grep "$prepend$EXE$append"
Daniel Sandler47e0a882013-07-30 13:23:52 -04001028 else
1029 adb shell ps \
1030 | tr -d '\r' \
1031 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1032 fi
1033}
1034
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001035function pid()
1036{
Daniel Sandler47e0a882013-07-30 13:23:52 -04001037 local prepend=''
1038 local append=''
1039 if [ "$1" = "--exact" ]; then
1040 prepend=' '
1041 append='$'
1042 shift
1043 fi
1044 local EXE="$1"
1045 if [ "$EXE" ] ; then
1046 local PID=`adb shell ps \
1047 | tr -d '\r' \
Mathias Agopian44583272013-08-27 14:15:50 -07001048 | \grep "$prepend$EXE$append" \
Daniel Sandler47e0a882013-07-30 13:23:52 -04001049 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1050 echo "$PID"
1051 else
1052 echo "usage: pid [--exact] <process name>"
1053 return 255
1054 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001055}
1056
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001057# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001058# that has the core-file-size limit set correctly
1059#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001060# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001061# if its core-file-size limit is not set already.
1062# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1063
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001064function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001065{
1066 echo "Getting root...";
1067 adb root;
1068 adb wait-for-device;
1069
1070 echo "Remounting root parition read-write...";
1071 adb shell mount -w -o remount -t rootfs rootfs;
1072 sleep 1;
1073 adb wait-for-device;
1074 adb shell mkdir -p /cores;
Nick Kralevicha94282c2014-11-04 11:17:20 -08001075 adb shell mount -t tmpfs tmpfs /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001076 adb shell chmod 0777 /cores;
1077
1078 echo "Granting SELinux permission to dump in /cores...";
1079 adb shell restorecon -R /cores;
1080
1081 echo "Set core pattern.";
1082 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
1083
1084 echo "Done."
1085}
1086
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001087# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001088# $1 = PID of process (e.g., $(pid mediaserver))
1089#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001090# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001091# dump to actually be generated.
1092
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001093function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001094{
1095 local PID=$1;
1096 if [ -z "$PID" ]; then
1097 printf "Expecting a PID!\n";
1098 return;
1099 fi;
1100 echo "Setting core limit for $PID to infinite...";
1101 adb shell prlimit $PID 4 -1 -1
1102}
1103
1104# core - send SIGV and pull the core for process
1105# $1 = PID of process (e.g., $(pid mediaserver))
1106#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001107# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001108# enabled globally.
1109
1110function core()
1111{
1112 local PID=$1;
1113
1114 if [ -z "$PID" ]; then
1115 printf "Expecting a PID!\n";
1116 return;
1117 fi;
1118
1119 local CORENAME=core.$PID;
1120 local COREPATH=/cores/$CORENAME;
1121 local SIG=SEGV;
1122
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001123 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001124
1125 local done=0;
1126 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1127 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1128 adb shell kill -$SIG $PID;
1129 sleep 1;
1130 done;
1131
1132 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1133 echo "Done: core is under $COREPATH on device.";
1134}
1135
Christopher Tate744ee802009-11-12 15:33:08 -08001136# systemstack - dump the current stack trace of all threads in the system process
1137# to the usual ANR traces file
1138function systemstack()
1139{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001140 stacks system_server
1141}
1142
1143function stacks()
1144{
1145 if [[ $1 =~ ^[0-9]+$ ]] ; then
1146 local PID="$1"
1147 elif [ "$1" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001148 local PIDLIST="$(pid $1)"
1149 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1150 local PID="$PIDLIST"
1151 elif [ "$PIDLIST" ] ; then
1152 echo "more than one process: $1"
1153 else
1154 echo "no such process: $1"
1155 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001156 else
1157 echo "usage: stacks [pid|process name]"
1158 fi
1159
1160 if [ "$PID" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001161 # Determine whether the process is native
1162 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1163 # Dump stacks of Dalvik process
1164 local TRACES=/data/anr/traces.txt
1165 local ORIG=/data/anr/traces.orig
1166 local TMP=/data/anr/traces.tmp
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001167
Jeff Brownb12c2e52013-08-19 15:14:16 -07001168 # Keep original traces to avoid clobbering
1169 adb shell mv $TRACES $ORIG
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001170
Jeff Brownb12c2e52013-08-19 15:14:16 -07001171 # Make sure we have a usable file
1172 adb shell touch $TRACES
1173 adb shell chmod 666 $TRACES
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001174
Jeff Brownb12c2e52013-08-19 15:14:16 -07001175 # Dump stacks and wait for dump to finish
1176 adb shell kill -3 $PID
1177 adb shell notify $TRACES >/dev/null
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001178
Jeff Brownb12c2e52013-08-19 15:14:16 -07001179 # Restore original stacks, and show current output
1180 adb shell mv $TRACES $TMP
1181 adb shell mv $ORIG $TRACES
1182 adb shell cat $TMP
1183 else
1184 # Dump stacks of native process
Michael Wrightaeed7212014-06-19 19:58:12 -07001185 local USE64BIT="$(is64bit $PID)"
1186 adb shell debuggerd$USE64BIT -b $PID
Jeff Brownb12c2e52013-08-19 15:14:16 -07001187 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001188 fi
Christopher Tate744ee802009-11-12 15:33:08 -08001189}
1190
John Michelau50200252012-11-09 11:48:04 -06001191function gdbwrapper()
1192{
Ben Chengfba67bf2014-02-25 10:27:07 -08001193 local GDB_CMD="$1"
1194 shift 1
1195 $GDB_CMD -x "$@"
John Michelau50200252012-11-09 11:48:04 -06001196}
1197
Brigid Smith0a2712d2014-05-28 14:36:43 -07001198function get_symbols_directory()
1199{
1200 echo $(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1201}
1202
Michael Wrightaeed7212014-06-19 19:58:12 -07001203# Read the ELF header from /proc/$PID/exe to determine if the process is
1204# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001205function is64bit()
1206{
1207 local PID="$1"
1208 if [ "$PID" ] ; then
Michael Wrightaeed7212014-06-19 19:58:12 -07001209 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001210 echo "64"
1211 else
1212 echo ""
1213 fi
1214 else
1215 echo ""
1216 fi
1217}
1218
Dmitriy Ivanovfd0f1c82014-10-30 22:00:17 -07001219function adb_get_product_device() {
1220 echo `adb shell getprop ro.product.device | sed s/.$//`
1221}
1222
1223# returns 0 when process is not traced
1224function adb_get_traced_by() {
1225 echo `adb shell cat /proc/$1/status | grep -e "^TracerPid:" | sed "s/^TracerPid:\t//" | sed s/.$//`
1226}
1227
1228function gdbclient() {
1229 # TODO:
1230 # 1. Check for ANDROID_SERIAL/multiple devices
1231 local PROCESS_NAME="n/a"
1232 local PID=$1
1233 local PORT=5039
1234 if [ -z "$PID" ]; then
1235 echo "Usage: gdbclient <pid|processname> [port number]"
1236 return -1
1237 fi
1238 local DEVICE=$(adb_get_product_device)
1239
1240 if [ -z "$DEVICE" ]; then
1241 echo "Error: Unable to get device name. Please check if device is connected and ANDROID_SERIAL is set."
1242 return -2
1243 fi
1244
1245 if [ -n "$2" ]; then
1246 PORT=$2
1247 fi
1248
1249 local ROOT=$(gettop)
1250 if [ -z "$ROOT" ]; then
1251 # This is for the situation with downloaded symbols (from the build server)
1252 # we check if they are available.
1253 ROOT=`realpath .`
1254 fi
1255
1256 local OUT_ROOT="$ROOT/out/target/product/$DEVICE"
1257 local SYMBOLS_DIR="$OUT_ROOT/symbols"
1258
1259 if [ ! -d $SYMBOLS_DIR ]; then
1260 echo "Error: couldn't find symbols: $SYMBOLS_DIR does not exist or is not a directory."
1261 return -3
1262 fi
1263
1264 # let's figure out which executable we are about to debug
1265
1266 # check if user specified a name -> resolve to pid
1267 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1268 PROCESS_NAME=$PID
1269 PID=$(pid --exact $PROCESS_NAME)
1270 if [ -z "$PID" ]; then
1271 echo "Error: couldn't resolve pid by process name: $PROCESS_NAME"
1272 return -4
1273 fi
1274 fi
1275
1276 local EXE=`adb shell readlink /proc/$PID/exe | sed s/.$//`
1277 # TODO: print error in case there is no such pid
1278 local LOCAL_EXE_PATH=$SYMBOLS_DIR$EXE
1279
1280 if [ ! -f $LOCAL_EXE_PATH ]; then
1281 echo "Error: unable to find symbols for executable $EXE: file $LOCAL_EXE_PATH does not exist"
1282 return -5
1283 fi
1284
1285 local USE64BIT=""
1286
1287 if [[ "$(file $LOCAL_EXE_PATH)" =~ 64-bit ]]; then
1288 USE64BIT="64"
1289 fi
1290
1291 local GDB=
1292 local GDB64=
1293 local CPU_ABI=`adb shell getprop ro.product.cpu.abilist | sed s/.$//`
1294 # TODO: we assume these are available via $PATH
1295 if [[ $CPU_ABI =~ (^|,)arm64 ]]; then
1296 GDB=arm-linux-androideabi-gdb
1297 GDB64=aarch64-linux-android-gdb
1298 elif [[ $CPU_ABI =~ (^|,)arm ]]; then
1299 GDB=arm-linux-androideabi-gdb
1300 elif [[ $CPU_ABI =~ (^|,)x86_64 ]]; then
1301 GDB=x86_64-linux-androideabi-gdb
1302 elif [[ $CPU_ABI =~ (^|,)x86 ]]; then
1303 GDB=x86_64-linux-androideabi-gdb
1304 elif [[ $CPU_ABI =~ (^|,)mips64 ]]; then
1305 GDB=mipsel-linux-android-gdb
1306 GDB64=mips64el-linux-android-gdb
1307 elif [[ $CPU_ABI =~ (^|,)mips ]]; then
1308 GDB=mipsel-linux-android-gdb
1309 else
1310 echo "Error: unrecognized cpu.abilist: $CPU_ABI"
1311 return -6
1312 fi
1313
1314 # TODO: check if tracing process is gdbserver and not some random strace...
1315 if [ $(adb_get_traced_by $PID) -eq 0 ]; then
1316 # start gdbserver
1317 echo "Starting gdbserver..."
1318 # TODO: check if adb is already listening $PORT
1319 # to avoid unnecessary calls
1320 echo ". adb forward for port=$PORT..."
1321 adb forward tcp:$PORT tcp:$PORT
1322 echo ". starting gdbserver to attach to pid=$PID..."
1323 adb shell gdbserver$USE64BIT :$PORT --attach $PID &
1324 echo ". give it couple of seconds to start..."
1325 sleep 2
1326 echo ". done"
1327 else
1328 echo "It looks like gdbserver is already attached to $PID (process is traced), trying to connect to it using local port=$PORT"
1329 fi
1330
1331 local OUT_SO_SYMBOLS=$SYMBOLS_DIR/system/lib$USE64BIT
1332 local OUT_VENDOR_SO_SYMBOLS=$SYMBOLS_DIR/vendor/lib$USE64BIT
1333 local ART_CMD=""
1334
1335 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $SYMBOLS_DIR"
1336 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
1337 local DALVIK_GDB_SCRIPT=$ROOT/development/scripts/gdb/dalvik.gdb
1338 if [ -f $DALVIK_GDB_SCRIPT ]; then
1339 echo >>"$OUT_ROOT/gdbclient.cmds" "source $DALVIK_GDB_SCRIPT"
1340 ART_CMD="art-on"
1341 else
1342 echo "Warning: couldn't find $DALVIK_GDB_SCRIPT - ART debugging options will not be available"
1343 fi
1344 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote :$PORT"
1345 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
1346 echo >> "$OUT_ROOT/gdbclient.cmds" $ART_CMD
1347 fi
1348
1349 echo >>"$OUT_ROOT/gdbclient.cmds" ""
1350
1351 local WHICH_GDB=$GDB
1352
1353 if [ -n "$USE64BIT" -a -n "$GDB64" ]; then
1354 WHICH_GDB=$GDB64
1355 fi
1356
1357 gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$LOCAL_EXE_PATH"
1358}
1359
Ben Chengfba67bf2014-02-25 10:27:07 -08001360# gdbclient now determines whether the user wants to debug a 32-bit or 64-bit
1361# executable, set up the approriate gdbserver, then invokes the proper host
1362# gdb.
Dmitriy Ivanovfd0f1c82014-10-30 22:00:17 -07001363function gdbclient_old()
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001364{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001365 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
1366 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1367 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
Colin Cross3655a682014-05-22 11:59:10 -07001368 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
Brigid Smith0a2712d2014-05-28 14:36:43 -07001369 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001370 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
Nick Kralevich0ab21d32011-11-11 09:02:01 -08001371 local ARCH=$(get_build_var TARGET_ARCH)
1372 local GDB
1373 case "$ARCH" in
Nick Kralevich0ab21d32011-11-11 09:02:01 -08001374 arm) GDB=arm-linux-androideabi-gdb;;
Ben Chengfba67bf2014-02-25 10:27:07 -08001375 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
Duane Sand3c4fcd82014-07-22 14:34:00 -07001376 mips|mips64) GDB=mips64el-linux-android-gdb;;
Serban Constantinescu9b68fb22014-01-14 10:33:53 +00001377 x86) GDB=x86_64-linux-android-gdb;;
1378 x86_64) GDB=x86_64-linux-android-gdb;;
Nick Kralevich0ab21d32011-11-11 09:02:01 -08001379 *) echo "Unknown arch $ARCH"; return 1;;
1380 esac
1381
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001382 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
1383 local EXE="$1"
1384 if [ "$EXE" ] ; then
1385 EXE=$1
Brigid Smith7a569a62014-06-20 14:12:12 -07001386 if [[ $EXE =~ ^[^/].* ]] ; then
1387 EXE="system/bin/"$EXE
1388 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001389 else
1390 EXE="app_process"
1391 fi
1392
1393 local PORT="$2"
1394 if [ "$PORT" ] ; then
1395 PORT=$2
1396 else
1397 PORT=":5039"
1398 fi
1399
Chris Craik20c136a2012-11-09 18:02:19 -08001400 local PID="$3"
1401 if [ "$PID" ] ; then
1402 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
Grace Klobae9d04bf2011-04-30 11:04:05 -07001403 PID=`pid $3`
Chris Craik20c136a2012-11-09 18:02:19 -08001404 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1405 # that likely didn't work because of returning multiple processes
1406 # try again, filtering by root processes (don't contain colon)
Mathias Agopian44583272013-08-27 14:15:50 -07001407 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
Chris Craik20c136a2012-11-09 18:02:19 -08001408 if [[ ! "$PID" =~ ^[0-9]+$ ]]
1409 then
1410 echo "Couldn't resolve '$3' to single PID"
1411 return 1
1412 else
1413 echo ""
1414 echo "WARNING: multiple processes matching '$3' observed, using root process"
1415 echo ""
1416 fi
1417 fi
Grace Klobae9d04bf2011-04-30 11:04:05 -07001418 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001419 adb forward "tcp$PORT" "tcp$PORT"
Ben Chengfba67bf2014-02-25 10:27:07 -08001420 local USE64BIT="$(is64bit $PID)"
1421 adb shell gdbserver$USE64BIT $PORT --attach $PID &
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001422 sleep 2
1423 else
1424 echo ""
1425 echo "If you haven't done so already, do this first on the device:"
1426 echo " gdbserver $PORT /system/bin/$EXE"
1427 echo " or"
Chris Craik20c136a2012-11-09 18:02:19 -08001428 echo " gdbserver $PORT --attach <PID>"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001429 echo ""
1430 fi
1431
Colin Cross84480ad2014-04-10 12:51:39 -07001432 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
Colin Crossa58f8e02014-06-20 13:33:36 -07001433 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
Colin Cross84480ad2014-04-10 12:51:39 -07001434
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001435 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
Colin Cross3655a682014-05-22 11:59:10 -07001436 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
Ben Cheng72398162013-06-24 14:36:21 -07001437 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001438 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
Christopher Ferris855d27f2014-10-03 17:29:27 -07001439 # Enable special debugging for ART processes.
1440 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
1441 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
1442 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001443 echo >>"$OUT_ROOT/gdbclient.cmds" ""
1444
Ben Chengfba67bf2014-02-25 10:27:07 -08001445 local WHICH_GDB=
1446 # 64-bit exe found
1447 if [ "$USE64BIT" != "" ] ; then
1448 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1449 # 32-bit exe / 32-bit platform
1450 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1451 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1452 # 32-bit exe / 64-bit platform
1453 else
1454 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1455 fi
Brigid Smith0a2712d2014-05-28 14:36:43 -07001456
Ben Chengfba67bf2014-02-25 10:27:07 -08001457 gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001458 else
1459 echo "Unable to determine build system output dir."
1460 fi
1461
1462}
1463
Clark Scheff2764d0d2014-12-23 13:30:51 -08001464function dddclient()
1465{
1466 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
1467 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1468 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
1469 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
1470 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
1471 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
1472 local ARCH=$(get_build_var TARGET_ARCH)
1473 local GDB
1474 case "$ARCH" in
1475 arm) GDB=arm-linux-androideabi-gdb;;
1476 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
1477 mips|mips64) GDB=mips64el-linux-android-gdb;;
1478 x86) GDB=x86_64-linux-android-gdb;;
1479 x86_64) GDB=x86_64-linux-android-gdb;;
1480 *) echo "Unknown arch $ARCH"; return 1;;
1481 esac
1482
1483 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
1484 local EXE="$1"
1485 if [ "$EXE" ] ; then
1486 EXE=$1
1487 if [[ $EXE =~ ^[^/].* ]] ; then
1488 EXE="system/bin/"$EXE
1489 fi
1490 else
1491 EXE="app_process"
1492 fi
1493
1494 local PORT="$2"
1495 if [ "$PORT" ] ; then
1496 PORT=$2
1497 else
1498 PORT=":5039"
1499 fi
1500
1501 local PID="$3"
1502 if [ "$PID" ] ; then
1503 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1504 PID=`pid $3`
1505 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1506 # that likely didn't work because of returning multiple processes
1507 # try again, filtering by root processes (don't contain colon)
1508 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
1509 if [[ ! "$PID" =~ ^[0-9]+$ ]]
1510 then
1511 echo "Couldn't resolve '$3' to single PID"
1512 return 1
1513 else
1514 echo ""
1515 echo "WARNING: multiple processes matching '$3' observed, using root process"
1516 echo ""
1517 fi
1518 fi
1519 fi
1520 adb forward "tcp$PORT" "tcp$PORT"
1521 local USE64BIT="$(is64bit $PID)"
1522 adb shell gdbserver$USE64BIT $PORT --attach $PID &
1523 sleep 2
1524 else
1525 echo ""
1526 echo "If you haven't done so already, do this first on the device:"
1527 echo " gdbserver $PORT /system/bin/$EXE"
1528 echo " or"
1529 echo " gdbserver $PORT --attach <PID>"
1530 echo ""
1531 fi
1532
1533 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
1534 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
1535
1536 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
1537 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
1538 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
1539 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
1540 # Enable special debugging for ART processes.
1541 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
1542 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
1543 fi
1544 echo >>"$OUT_ROOT/gdbclient.cmds" ""
1545
1546 local WHICH_GDB=
1547 # 64-bit exe found
1548 if [ "$USE64BIT" != "" ] ; then
1549 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1550 # 32-bit exe / 32-bit platform
1551 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1552 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1553 # 32-bit exe / 64-bit platform
1554 else
1555 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1556 fi
1557
1558 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
1559 else
1560 echo "Unable to determine build system output dir."
1561 fi
1562}
1563
1564
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001565case `uname -s` in
1566 Darwin)
1567 function sgrep()
1568 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001569 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|S|java|xml|sh|mk|aidl)' -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001570 }
1571
1572 ;;
1573 *)
1574 function sgrep()
1575 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001576 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|S\|java\|xml\|sh\|mk\|aidl\)' -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001577 }
1578 ;;
1579esac
1580
Raghu Gandham8da43102012-07-25 19:57:22 -07001581function gettargetarch
1582{
1583 get_build_var TARGET_ARCH
1584}
1585
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001586function ggrep()
1587{
1588 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
1589}
1590
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001591function jgrep()
1592{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001593 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001594}
1595
1596function cgrep()
1597{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001598 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001599}
1600
1601function resgrep()
1602{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001603 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001604}
1605
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001606function mangrep()
1607{
1608 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1609}
1610
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001611function sepgrep()
1612{
1613 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d -print0 | xargs -0 grep --color -n -r --exclude-dir=\.git "$@"
1614}
1615
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001616case `uname -s` in
1617 Darwin)
1618 function mgrep()
1619 {
Ying Wang324c2b22012-08-17 15:03:20 -07001620 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001621 }
1622
1623 function treegrep()
1624 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001625 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001626 }
1627
1628 ;;
1629 *)
1630 function mgrep()
1631 {
Ying Wang324c2b22012-08-17 15:03:20 -07001632 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001633 }
1634
1635 function treegrep()
1636 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001637 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001638 }
1639
1640 ;;
1641esac
1642
1643function getprebuilt
1644{
1645 get_abs_build_var ANDROID_PREBUILTS
1646}
1647
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001648function tracedmdump()
1649{
1650 T=$(gettop)
1651 if [ ! "$T" ]; then
1652 echo "Couldn't locate the top of the tree. Try setting TOP."
1653 return
1654 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001655 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001656 local arch=$(gettargetarch)
1657 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001658
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001659 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001660 if [ ! "$TRACE" ] ; then
1661 echo "usage: tracedmdump tracename"
1662 return
1663 fi
1664
Jack Veenstra60116fc2009-04-09 18:12:34 -07001665 if [ ! -r "$KERNEL" ] ; then
1666 echo "Error: cannot find kernel: '$KERNEL'"
1667 return
1668 fi
1669
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001670 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001671 if [ "$BASETRACE" = "$TRACE" ] ; then
1672 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1673 fi
1674
1675 echo "post-processing traces..."
1676 rm -f $TRACE/qtrace.dexlist
1677 post_trace $TRACE
1678 if [ $? -ne 0 ]; then
1679 echo "***"
1680 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1681 echo "***"
1682 return
1683 fi
1684 echo "generating dexlist output..."
1685 /bin/ls $ANDROID_PRODUCT_OUT/system/framework/*.jar $ANDROID_PRODUCT_OUT/system/app/*.apk $ANDROID_PRODUCT_OUT/data/app/*.apk 2>/dev/null | xargs dexlist > $TRACE/qtrace.dexlist
1686 echo "generating dmtrace data..."
1687 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1688 echo "generating html file..."
1689 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1690 echo "done, see $TRACE/dmtrace.html for details"
1691 echo "or run:"
1692 echo " traceview $TRACE/dmtrace"
1693}
1694
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001695# communicate with a running device or emulator, set up necessary state,
1696# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001697function runhat()
1698{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001699 # process standard adb options
1700 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001701 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001702 adbTarget=$1
1703 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001704 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001705 adbTarget="$1 $2"
1706 shift 2
1707 fi
1708 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001709 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001710
1711 # runhat options
1712 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001713
1714 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001715 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001716 return
1717 fi
1718
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001719 # confirm hat is available
1720 if [ -z $(which hat) ]; then
1721 echo "hat is not available in this configuration."
1722 return
1723 fi
1724
Andy McFaddenb6289852010-07-12 08:00:19 -07001725 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001726 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001727 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001728 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001729 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001730 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001731 echo -n "> "
1732 read
1733
The Android Open Source Project88b60792009-03-03 19:28:42 -08001734 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001735
The Android Open Source Project88b60792009-03-03 19:28:42 -08001736 echo "Retrieving file $devFile..."
1737 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001738
The Android Open Source Project88b60792009-03-03 19:28:42 -08001739 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001740
The Android Open Source Project88b60792009-03-03 19:28:42 -08001741 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001742 echo "View the output by pointing your browser at http://localhost:7000/"
1743 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001744 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001745}
1746
1747function getbugreports()
1748{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001749 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001750
1751 if [ ! "$reports" ]; then
1752 echo "Could not locate any bugreports."
1753 return
1754 fi
1755
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001756 local report
1757 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001758 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001759 echo "/sdcard/bugreports/${report}"
1760 adb pull /sdcard/bugreports/${report} ${report}
1761 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001762 done
1763}
1764
Victoria Lease1b296b42012-08-21 15:44:06 -07001765function getsdcardpath()
1766{
1767 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1768}
1769
1770function getscreenshotpath()
1771{
1772 echo "$(getsdcardpath)/Pictures/Screenshots"
1773}
1774
1775function getlastscreenshot()
1776{
1777 local screenshot_path=$(getscreenshotpath)
1778 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1779 if [ "$screenshot" = "" ]; then
1780 echo "No screenshots found."
1781 return
1782 fi
1783 echo "${screenshot}"
1784 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1785}
1786
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001787function startviewserver()
1788{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001789 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001790 if [ $# -gt 0 ]; then
1791 port=$1
1792 fi
1793 adb shell service call window 1 i32 $port
1794}
1795
1796function stopviewserver()
1797{
1798 adb shell service call window 2
1799}
1800
1801function isviewserverstarted()
1802{
1803 adb shell service call window 3
1804}
1805
Romain Guyb84049a2010-10-04 16:56:11 -07001806function key_home()
1807{
1808 adb shell input keyevent 3
1809}
1810
1811function key_back()
1812{
1813 adb shell input keyevent 4
1814}
1815
1816function key_menu()
1817{
1818 adb shell input keyevent 82
1819}
1820
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001821function smoketest()
1822{
1823 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1824 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1825 return
1826 fi
1827 T=$(gettop)
1828 if [ ! "$T" ]; then
1829 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1830 return
1831 fi
1832
Ying Wang9cd17642012-12-13 10:52:07 -08001833 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001834 adb uninstall com.android.smoketest > /dev/null &&
1835 adb uninstall com.android.smoketest.tests > /dev/null &&
1836 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1837 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1838 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1839}
1840
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001841# simple shortcut to the runtest command
1842function runtest()
1843{
1844 T=$(gettop)
1845 if [ ! "$T" ]; then
1846 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1847 return
1848 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001849 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001850}
1851
The Android Open Source Project88b60792009-03-03 19:28:42 -08001852function godir () {
1853 if [[ -z "$1" ]]; then
1854 echo "Usage: godir <regex>"
1855 return
1856 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -08001857 T=$(gettop)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001858 if [[ ! -f $T/filelist ]]; then
1859 echo -n "Creating index..."
Ying Wang9cd17642012-12-13 10:52:07 -08001860 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001861 echo " Done"
1862 echo ""
1863 fi
1864 local lines
Jeff Hamilton293f9392011-11-18 17:15:25 -06001865 lines=($(\grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001866 if [[ ${#lines[@]} = 0 ]]; then
1867 echo "Not found"
1868 return
1869 fi
1870 local pathname
1871 local choice
1872 if [[ ${#lines[@]} > 1 ]]; then
1873 while [[ -z "$pathname" ]]; do
1874 local index=1
1875 local line
1876 for line in ${lines[@]}; do
1877 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001878 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001879 done
1880 echo
1881 echo -n "Select one: "
1882 unset choice
1883 read choice
1884 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1885 echo "Invalid choice"
1886 continue
1887 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001888 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001889 done
1890 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001891 pathname=${lines[0]}
1892 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001893 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001894}
1895
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -06001896function cmremote()
1897{
1898 git remote rm cmremote 2> /dev/null
Chirayu Desai48b3f8e2014-12-16 18:22:55 +05301899 GERRIT_REMOTE=$(git config --get remote.github.projectname)
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -06001900 if [ -z "$GERRIT_REMOTE" ]
1901 then
Chirayu Desai48b3f8e2014-12-16 18:22:55 +05301902 echo Unable to set up the git remote, are you under a git repo?
1903 return 0
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -06001904 fi
Chirayu Desai48b3f8e2014-12-16 18:22:55 +05301905 CMUSER=$(git config --get review.review.cyanogenmod.org.username)
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -06001906 if [ -z "$CMUSER" ]
1907 then
Pawit Pornkitprasan7a25fe92012-12-11 16:41:07 +07001908 git remote add cmremote ssh://review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -06001909 else
Pawit Pornkitprasan7a25fe92012-12-11 16:41:07 +07001910 git remote add cmremote ssh://$CMUSER@review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandc7ca3b92011-11-18 00:45:55 -06001911 fi
1912 echo You can now push to "cmremote".
1913}
Koushik Duttaa650e472012-05-14 16:14:36 -07001914
Steve Kondikb99aa572012-09-17 11:33:18 -07001915function aospremote()
1916{
1917 git remote rm aosp 2> /dev/null
1918 if [ ! -d .git ]
1919 then
1920 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1921 fi
Steve Kondik8d976152013-11-16 03:48:30 -08001922 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondikb99aa572012-09-17 11:33:18 -07001923 if (echo $PROJECT | grep -qv "^device")
1924 then
1925 PFX="platform/"
1926 fi
1927 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
1928 echo "Remote 'aosp' created"
1929}
Steve Kondikb99aa572012-09-17 11:33:18 -07001930
Steve Kondik263c0412013-10-27 13:34:36 -07001931function cafremote()
1932{
1933 git remote rm caf 2> /dev/null
1934 if [ ! -d .git ]
1935 then
1936 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1937 fi
Steve Kondik8d976152013-11-16 03:48:30 -08001938 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik263c0412013-10-27 13:34:36 -07001939 if (echo $PROJECT | grep -qv "^device")
1940 then
1941 PFX="platform/"
1942 fi
1943 git remote add caf git://codeaurora.org/$PFX$PROJECT
1944 echo "Remote 'caf' created"
1945}
Steve Kondik263c0412013-10-27 13:34:36 -07001946
Steve Kondikf8374f72012-09-23 23:46:55 -07001947function installboot()
1948{
1949 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1950 then
1951 echo "No recovery.fstab found. Build recovery first."
1952 return 1
1953 fi
1954 if [ ! -e "$OUT/boot.img" ];
1955 then
1956 echo "No boot.img found. Run make bootimage first."
1957 return 1
1958 fi
1959 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1960 if [ -z "$PARTITION" ];
1961 then
Ricardo Cerqueirac72f67f2013-07-27 13:51:56 +01001962 # Try for RECOVERY_FSTAB_VERSION = 2
1963 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1964 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1965 if [ -z "$PARTITION" ];
1966 then
1967 echo "Unable to determine boot partition."
1968 return 1
1969 fi
Steve Kondikf8374f72012-09-23 23:46:55 -07001970 fi
1971 adb start-server
Steve Kondik6cb90ec2013-08-30 17:34:37 -07001972 adb wait-for-online
Steve Kondikf8374f72012-09-23 23:46:55 -07001973 adb root
1974 sleep 1
Steve Kondik8a385c22013-04-13 13:23:35 -07001975 adb wait-for-online shell mount /system 2>&1 > /dev/null
1976 adb wait-for-online remount
Steve Kondikf8374f72012-09-23 23:46:55 -07001977 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
1978 then
1979 adb push $OUT/boot.img /cache/
1980 for i in $OUT/system/lib/modules/*;
1981 do
1982 adb push $i /system/lib/modules/
1983 done
1984 adb shell dd if=/cache/boot.img of=$PARTITION
1985 adb shell chmod 644 /system/lib/modules/*
1986 echo "Installation complete."
1987 else
1988 echo "The connected device does not appear to be $CM_BUILD, run away!"
1989 fi
1990}
1991
Steve Kondik0a9a0712012-10-24 16:40:42 -07001992function installrecovery()
1993{
1994 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1995 then
1996 echo "No recovery.fstab found. Build recovery first."
1997 return 1
1998 fi
1999 if [ ! -e "$OUT/recovery.img" ];
2000 then
2001 echo "No recovery.img found. Run make recoveryimage first."
2002 return 1
2003 fi
2004 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
2005 if [ -z "$PARTITION" ];
2006 then
Steve Kondikca387552013-08-09 21:03:42 -07002007 # Try for RECOVERY_FSTAB_VERSION = 2
Steve Kondik7e874f62013-08-10 21:02:09 -07002008 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
2009 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
Steve Kondikca387552013-08-09 21:03:42 -07002010 if [ -z "$PARTITION" ];
2011 then
2012 echo "Unable to determine recovery partition."
2013 return 1
2014 fi
Steve Kondik0a9a0712012-10-24 16:40:42 -07002015 fi
2016 adb start-server
Steve Kondik6cb90ec2013-08-30 17:34:37 -07002017 adb wait-for-online
Steve Kondik0a9a0712012-10-24 16:40:42 -07002018 adb root
2019 sleep 1
Steve Kondik8a385c22013-04-13 13:23:35 -07002020 adb wait-for-online shell mount /system 2>&1 >> /dev/null
2021 adb wait-for-online remount
Steve Kondik0a9a0712012-10-24 16:40:42 -07002022 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
2023 then
2024 adb push $OUT/recovery.img /cache/
2025 adb shell dd if=/cache/recovery.img of=$PARTITION
2026 echo "Installation complete."
2027 else
2028 echo "The connected device does not appear to be $CM_BUILD, run away!"
2029 fi
2030}
Steve Kondikf8374f72012-09-23 23:46:55 -07002031
Koushik Duttaa650e472012-05-14 16:14:36 -07002032function makerecipe() {
2033 if [ -z "$1" ]
2034 then
2035 echo "No branch name provided."
2036 return 1
2037 fi
2038 cd android
2039 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
2040 git commit -a -m "$1"
2041 cd ..
2042
2043 repo forall -c '
2044