Re-land "[dev-util] Add symbolicate_dump endpoint to dev server"

This reverts commit 50fe427cb8cebf7dca441e92463c407cb90fddf3.

Add an endpoint to the dev server that will symbolicate a minidump.

BUG=chromium-os:29850
TEST=unit
TEST=run dev server, use curl to make it download some artifacts; check to
TEST=see that debug symbols are staged in static/archive
TEST=once symbols are staged, run the dev server in your
TEST=chroot and use curl with a minidump file like this:
TEST=  curl -F minidump=@/home/cmasone/chromeos/phooey/powerd.20120424.141235.1005.dmp http://localhost:8080/symbolicate_dump

Change-Id: Ib9c11afd780aea5a665358b43f03a210ecc83482
Reviewed-on: https://gerrit.chromium.org/gerrit/21541
Tested-by: Chris Masone <cmasone@chromium.org>
Reviewed-by: Scott Zawalski <scottz@chromium.org>
Tested-by: Scott Zawalski <scottz@chromium.org>
Commit-Ready: Scott Zawalski <scottz@chromium.org>
diff --git a/devserver.py b/devserver.py
index bb22e22..07c85ee 100755
--- a/devserver.py
+++ b/devserver.py
@@ -7,11 +7,14 @@
 """A CherryPy-based webserver to host images and build packages."""
 
 import cherrypy
+import cStringIO
 import logging
 import optparse
 import os
 import re
 import sys
+import subprocess
+import tempfile
 
 import autoupdate
 import devserver_util
@@ -240,6 +243,41 @@
     return return_obj
 
   @cherrypy.expose
+  def symbolicate_dump(self, minidump):
+    """Symbolicates a minidump using pre-downloaded symbols, returns it.
+
+    Callers will need to POST to this URL with a body of MIME-type
+    "multipart/form-data".
+    The body should include a single argument, 'minidump', containing the
+    binary-formatted minidump to symbolicate.
+
+    It is up to the caller to ensure that the symbols they want are currently
+    staged.
+
+    Args:
+      minidump: The binary minidump file to symbolicate.
+    """
+    to_return = ''
+    with tempfile.NamedTemporaryFile() as local:
+      while True:
+        data = minidump.file.read(8192)
+        if not data:
+          break
+        local.write(data)
+      local.flush()
+      stackwalk = subprocess.Popen(['minidump_stackwalk',
+                                    local.name,
+                                    updater.static_dir + '/debug/breakpad'],
+                                   stdout=subprocess.PIPE,
+                                   stderr=subprocess.PIPE)
+      to_return, error_text = stackwalk.communicate()
+      if stackwalk.returncode != 0:
+        raise DevServerError("Can't generate stack trace: %s (rc=%d)" % (
+            error_text, stackwalk.returncode))
+
+    return to_return
+
+  @cherrypy.expose
   def wait_for_status(self, **kwargs):
     """Waits for background artifacts to be downloaded from Google Storage.