Fix ndk-gdb and add --delay=<seconds> option.

Fix ndk-gdb to _not_ start the activity unless --start or --launch is used.

Add a new option --delay=<seconds> to change the wait delay between the
launch of an activity and its attachement with gdbserver. The default delay
is also changed from 1 to 2 seconds.

Document the system libraries update.

Change-Id: Ia7e8c47221e3741bdc2688e750dae366c5426e67
diff --git a/build/core/build-all.mk b/build/core/build-all.mk
index 5fe4169..4712467 100644
--- a/build/core/build-all.mk
+++ b/build/core/build-all.mk
@@ -23,28 +23,6 @@
 
 # ====================================================================
 #
-# Check the import path
-#
-# ====================================================================
-
-NDK_MODULE_PATH := $(strip $(NDK_MODULE_PATH))
-ifdef NDK_MODULE_PATH
-  ifneq ($(words $(NDK_MODULE_PATH)),1)
-    $(call __ndk_info,ERROR: You NDK_MODULE_PATH variable contains spaces)
-    $(call __ndk_info,Please fix the error and start again.)
-    $(call __ndk_error,Aborting)
-  endif
-endif
-
-$(call import-init)
-$(foreach __path,$(patsubst $(HOST_DIRSEP),$(space),$(NDK_MODULE_PATH)),\
-  $(call import-add-path,$(__path))\
-)
-$(call import-add-path-optional,$(NDK_ROOT)/sources)
-$(call import-add-path-optional,$(NDK_ROOT)/../development/ndk/sources)
-
-# ====================================================================
-#
 # Prepare the build for parsing Android.mk files
 #
 # ====================================================================
diff --git a/build/core/build-local.mk b/build/core/build-local.mk
index a6a2fd9..0880515 100644
--- a/build/core/build-local.mk
+++ b/build/core/build-local.mk
@@ -115,6 +115,8 @@
 DUMP_VAR     := $(patsubst DUMP_%,%,$(filter DUMP_%,$(MAKECMDGOALS)))
 MAKECMDGOALS := $(filter-out DUMP_$(DUMP_VAR),$(MAKECMDGOALS))
 
+include $(BUILD_SYSTEM)/setup-imports.mk
+
 ifneq (,$(DUMP_VAR))
 
 # We only support a single DUMP_XXX goal at a time for now.
diff --git a/build/core/main.mk b/build/core/main.mk
index e46b2a3..45fdbd7 100644
--- a/build/core/main.mk
+++ b/build/core/main.mk
@@ -134,4 +134,5 @@
 # Where all app-specific generated files will be stored
 NDK_APP_OUT := $(NDK_ROOT)/out/apps
 
+include $(BUILD_SYSTEM)/setup-imports.mk
 include $(BUILD_SYSTEM)/build-all.mk
diff --git a/build/core/setup-imports.mk b/build/core/setup-imports.mk
new file mode 100644
index 0000000..67501b9
--- /dev/null
+++ b/build/core/setup-imports.mk
@@ -0,0 +1,36 @@
+# Copyright (C) 2009-2010 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# ====================================================================
+#
+# Check the import path
+#
+# ====================================================================
+
+NDK_MODULE_PATH := $(strip $(NDK_MODULE_PATH))
+ifdef NDK_MODULE_PATH
+  ifneq ($(words $(NDK_MODULE_PATH)),1)
+    $(call __ndk_info,ERROR: You NDK_MODULE_PATH variable contains spaces)
+    $(call __ndk_info,Please fix the error and start again.)
+    $(call __ndk_error,Aborting)
+  endif
+endif
+
+$(call import-init)
+$(foreach __path,$(patsubst $(HOST_DIRSEP),$(space),$(NDK_MODULE_PATH)),\
+  $(call import-add-path,$(__path))\
+)
+$(call import-add-path-optional,$(NDK_ROOT)/sources)
+$(call import-add-path-optional,$(NDK_ROOT)/../development/ndk/sources)
diff --git a/docs/CHANGES.TXT b/docs/CHANGES.TXT
index 1c5d834..8db3bd5 100644
--- a/docs/CHANGES.TXT
+++ b/docs/CHANGES.TXT
@@ -9,6 +9,13 @@
   a shared library. This requires changes in both the C library and the
   build system. IMPORTANT: This feature is only available for API level 9.
 
+- Fix packaging error for system libraries of level 5 and 8. The libraries
+  bundled with the NDK and used at link time were not the correct version,
+  and this prevented linking against certain symbols appropriately.
+
+- ndk-gdb: do not start activity unless --start or --launch is used.
+  (the documentation was correct, the implementation wrong)
+
 IMPORTANT CHANGES:
 
 - Support for module exports: A module can now define a set of compiler or
@@ -61,13 +68,6 @@
   LOCAL_FILTER_ASM in docs/ANDROID-MK.TXT for more details. This can be useful
   for certain kinds of obfuscation tasks.
 
-- Easier native debugging support by using 'ndk-build NDK_DEBUG=1'. The main
-  idea is that if NDK_DEBUG is defined, its value overrides the value found
-  in the application's android:debuggable manifest attribute.
-
-  Valid values for NDK_DEBUG are '1', '0', 'true' or 'false' (anything else
-  will be ignored).
-
 - Switched to GCC 4.4.3. The binaries for GCC 4.4.0 are still available and
   can be used with NDK_TOOLCHAIN=arm-eabi-4.4.0 defined in your environment.
   The binaries and support files for arm-eabi-4.2.1 were removed.
@@ -106,6 +106,11 @@
 
 - ndk-gdb: properly launch activities which name does not contain any dot.
 
+- ndk-gdb: add --delay=<timeout> option to specify a delay in seconds between
+  activity launch and gdbserver attach. This is needed because certain
+  activities can take a long time to properly launch. The default delay is
+  also increased to 2 secondes (instead of 1).
+
 - build/tools/build-gcc.sh: copy the sysroot to the build directory. This
   avoids the generated toolchain binaries from hard-coding host build paths.
 
diff --git a/ndk-gdb b/ndk-gdb
index 6019a08..f2577dc 100755
--- a/ndk-gdb
+++ b/ndk-gdb
@@ -35,6 +35,11 @@
 
 DEBUG_PORT=5039
 
+# Delay in seconds between launching the activity and attaching gdbserver on it.
+# This is needed because there is no way to know when the activity has really
+# started, and sometimes this takes a few seconds.
+DELAY=2
+
 PARAMETERS=
 OPTION_HELP=no
 OPTION_PROJECT=
@@ -44,6 +49,7 @@
 OPTION_START=no
 OPTION_LAUNCH=
 OPTION_LAUNCH_LIST=no
+OPTION_DELAY=
 
 check_parameter ()
 {
@@ -178,6 +184,9 @@
         --start)
             OPTION_START=yes
             ;;
+        --delay=*)
+            OPTION_DELAY="$optarg"
+            ;;
         -*) # unknown options
             echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
             exit 1
@@ -207,6 +216,7 @@
     echo "    --start           Launch application instead of attaching to existing one"
     echo "    --launch=<name>   Same as --start, but specify activity name (see below)"
     echo "    --launch-list     List all launchable activity names from manifest"
+    echo "    --delay=<secs>    Delay in seconds between activity start and gdbserver attach."
     echo "    --project=<path>  Specify application project path"
     echo "    -p <path>         Same as --project=<path>"
     echo "    --port=<port>     Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
@@ -230,6 +240,10 @@
     fi
 fi
 
+if [ -n "$OPTION_DELAY" ] ; then
+    DELAY="$OPTION_DELAY"
+fi
+
 # Check ADB tool version
 if [ -n "$OPTION_ADB" ] ; then
     ADB_CMD="$OPTION_ADB"
@@ -459,7 +473,7 @@
 fi
 
 # Launch the activity if needed
-if [ -n "$OPTION_START" ] ; then
+if [ "$OPTION_START" = "yes" ] ; then
     # If --launch is used, ignore --start, otherwise extract the first
     # launchable activity name from the manifest and use it as if --launch=<name>
     # was used instead.
@@ -490,7 +504,7 @@
     fi
     # Sleep a bit, it sometimes take one second to start properly
     # Note that we use the 'sleep' command on the device here.
-    run $ADB_CMD shell sleep 1
+    run $ADB_CMD shell sleep $DELAY
 fi
 
 # Find the PID of the application being run
@@ -547,13 +561,18 @@
 # Get the app_server binary from the device
 APP_PROCESS=$APP_OUT/app_process
 run $ADB_CMD pull /system/bin/app_process `native_path $APP_PROCESS`
-log "Pulled $APP_BINARY from device/emulator."
+log "Pulled app_process from device/emulator."
+
+run $ADB_CMD pull /system/lib/libc.so `native_path $APP_OUT/libc.so`
+log "Pulled libc.so from device/emulator."
 
 # Now launch the appropriate gdb client with the right init commands
 #
 GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
 GDBSETUP=$APP_OUT/gdb.setup
 cp -f $GDBSETUP_INIT $GDBSETUP
+#uncomment the following to debug the remote connection only
+#echo "set debug remote 1" >> $GDBSETUP
 echo "target remote :$DEBUG_PORT" >> $GDBSETUP
 if [ -n "$OPTION_EXEC" ] ; then
     cat $OPTION_EXEC >> $GDBSETUP