Allow mojo_debug gdb attach to search symbols in multiple directories.

This fixes domokit/devtools#38.

R=qsr@google.com

Review URL: https://codereview.chromium.org/1325593004 .

Cr-Mirrored-From: https://github.com/domokit/mojo
Cr-Mirrored-Commit: 1e331d28863d4b6a4084c22dd2447c5718c40462
diff --git a/android_gdb/session.py b/android_gdb/session.py
index e425c40..44bc17d 100644
--- a/android_gdb/session.py
+++ b/android_gdb/session.py
@@ -71,10 +71,11 @@
 
 
 class DebugSession(object):
-  def __init__(self, build_directory, package_name, pyelftools_dir, adb):
-    self._build_directory = build_directory
-    if not os.path.exists(self._build_directory):
-      logging.fatal("Please pass a valid build directory")
+  def __init__(self, build_directory_list, package_name, pyelftools_dir, adb):
+    build_directories = build_directory_list.split(',')
+    if len(build_directories) == 0 or not all(map(os.path.exists,
+                                                  build_directories)):
+      logging.fatal("Please pass a list of valid build directories")
       sys.exit(1)
     self._package_name = package_name
     self._adb = adb
@@ -92,7 +93,7 @@
 
     self._elffile_module = elffile
 
-    self._libraries = self._find_libraries(build_directory)
+    self._libraries = self._find_libraries(build_directories)
     self._rfc = RemoteFileConnection('localhost', 10000)
     self._remote_file_reader_process = None
     if not os.path.exists(self._remote_file_cache):
@@ -111,15 +112,16 @@
     if self._remote_file_reader_process != None:
       self._remote_file_reader_process.kill()
 
-  def _find_libraries(self, lib_dir):
-    """Finds all libraries in |lib_dir| and key them by their signatures.
+  def _find_libraries(self, lib_dirs):
+    """Finds all libraries in |lib_dirs| and key them by their signatures.
     """
     res = {}
-    for fn in glob.glob('%s/*.so' % lib_dir):
-      with open(fn, 'r') as f:
-        s = get_signature(f, self._elffile_module)
-        if s is not None:
-          res[s] = fn
+    for lib_dir in lib_dirs:
+      for fn in glob.glob('%s/*.so' % lib_dir):
+        with open(fn, 'r') as f:
+          s = get_signature(f, self._elffile_module)
+          if s is not None:
+            res[s] = fn
     return res
 
   def _associate_symbols(self, mapping, local_file):
diff --git a/mojo_debug b/mojo_debug
index cc1aa0b..b3b786d 100755
--- a/mojo_debug
+++ b/mojo_debug
@@ -143,7 +143,8 @@
   devtools_dir = os.path.dirname(os.path.abspath(__file__))
   stack_command = [os.path.join(devtools_dir, 'android_stack_parser', 'stack')]
   if args.build_dir:
-    stack_command.append('--build-dir=' + os.path.abspath(args.build_dir))
+    stack_command.append('--build-dir=' +
+            os.path.abspath(','.join(args.build_dir)))
   if args.ndk_dir:
     stack_command.append('--ndk-dir=' + os.path.abspath(args.ndk_dir))
   stack_command.append('-')
@@ -222,12 +223,12 @@
                                         'android_gdb', 'session.py')
   debug_session_arguments = {}
   if args.build_dir:
-    debug_session_arguments["build_directory"] = args.build_dir
+    debug_session_arguments["build_directory_list"] = ','.join(args.build_dir)
   else:
     try:
-      debug_session_arguments["build_directory"] = os.path.join(
+      debug_session_arguments["build_directory_list"] = os.path.join(
           _get_dir_above('out'), 'out', 'android_Debug')
-      if not os.path.exists(debug_session_arguments["build_directory"]):
+      if not os.path.exists(debug_session_arguments["build_directory_list"]):
         raise DirectoryNotFoundException()
     except DirectoryNotFoundException:
       logging.fatal("Unable to find the build directory, please specify it "
@@ -290,8 +291,8 @@
       help='symbolize the crash stacktraces from the device log')
   device_stack_parser.add_argument('--ndk-dir', type=str,
       help='path to the directory containing the Android NDK')
-  device_stack_parser.add_argument('--build-dir', type=str,
-      help='list paths to the build directories, separated by commas')
+  device_stack_parser.add_argument('--build-dir', type=str, action='append',
+      help='paths to the build directory, may be repeated')
   device_stack_parser.set_defaults(func=_device_stack)
 
 
@@ -307,8 +308,8 @@
       help='path to the adb tool from the Android SDK (optional)')
   gdb_attach_parser.add_argument('--ndk-dir', type=str,
       help='path to the directory containing the Android NDK')
-  gdb_attach_parser.add_argument('--build-dir', type=str,
-      help='path to the build directory')
+  gdb_attach_parser.add_argument('--build-dir', type=str, action='append',
+      help='Paths to the build directory, may be repeated')
   gdb_attach_parser.add_argument('--pyelftools-dir', type=str,
       help='Path to a directory containing third party libraries')
   gdb_attach_parser.add_argument('--gsutil-dir', type=str,