crash-reporter: Add "boot_mode" key to crash reports for dev mode

For crash reports sent when a machine is in developer mode, annotate them
with a "boot_mode" key set to "dev".  This is so other people know the
system may have been mucked with.

BUG=chromium-os:33947
TEST=Ran logging_CrashSender autotest

Change-Id: I30d58885e41f2c32e32b7b0f691ed2e5d14f2e3c
Reviewed-on: https://gerrit.chromium.org/gerrit/33246
Tested-by: Michael Krebs <mkrebs@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
Commit-Ready: Michael Krebs <mkrebs@chromium.org>
diff --git a/crash_reporter/crash_sender b/crash_reporter/crash_sender
index 07302fb..ab1898f 100644
--- a/crash_reporter/crash_sender
+++ b/crash_reporter/crash_sender
@@ -47,6 +47,10 @@
 # crash sending was successful, otherwise unsuccessful.
 MOCK_CRASH_SENDING="/tmp/mock-crash-sending"
 
+# Set this to 1 in the environment to pretend to have booted in developer
+# mode.  This is used by autotests.
+MOCK_DEVELOPER_MODE=${MOCK_DEVELOPER_MODE:-0}
+
 # Ignore PAUSE_CRASH_SENDING file if set.
 OVERRIDE_PAUSE_SENDING=${OVERRIDE_PAUSE_SENDING:-0}
 
@@ -151,6 +155,15 @@
   return 1
 }
 
+# Returns 0 if the machine booted up in developer mode.
+is_developer_mode() {
+  [ ${MOCK_DEVELOPER_MODE} -ne 0 ] && return 0
+  # If we're testing crash reporter itself, we don't want to special-case
+  # for developer mode.
+  is_crash_test_in_progress && return 1
+  crossystem "devsw_boot?1"  # exit status will be accurate
+}
+
 # Generate a uniform random number in 0..max-1.
 generate_uniform_random() {
   local max=$1
@@ -256,6 +269,15 @@
     image_type="mock-fail"
   fi
 
+  local boot_mode
+  if ! crossystem "cros_debug" > /dev/null 2>&1; then
+    # Sanity-check failed that makes sure crossystem exists.
+    lecho "Cannot determine boot mode due to error running crossystem command"
+    boot_mode="missing-crossystem"
+  elif is_developer_mode; then
+    boot_mode="dev"
+  fi
+
   local extra_key1="write_payload_size"
   local extra_value1="${write_payload_size}"
   local extra_key2="send_payload_size"
@@ -280,6 +302,7 @@
   lecho "  Payload: ${report_payload}"
   lecho "  Version: ${chromeos_version}"
   [ -n "${image_type}" ] && lecho "  Image type: ${image_type}"
+  [ -n "${boot_mode}" ] && lecho "  Boot mode: ${boot_mode}"
   if is_mock; then
     lecho "  Product: ${CHROMEOS_PRODUCT}"
     lecho "  URL: ${url}"
@@ -324,6 +347,7 @@
     -F "hwclass=${hwclass}" \
     -F "exec_name=${exec_name}" \
     ${image_type:+-F "image_type=${image_type}"} \
+    ${boot_mode:+-F "boot_mode=${boot_mode}"} \
     ${error_type:+-F "error_type=${error_type}"} \
     -F "${extra_key1}=${extra_value1}" \
     -F "${extra_key2}=${extra_value2}" \