[lib/inferior_control] Pass log settings to test helper

... plus use fxl logging more.

Change-Id: If6b930615fa2eef547ae49a440b18ddf6115b76f
diff --git a/garnet/lib/inferior_control/test_helper.cc b/garnet/lib/inferior_control/test_helper.cc
index 60635fe..4f84e8a 100644
--- a/garnet/lib/inferior_control/test_helper.cc
+++ b/garnet/lib/inferior_control/test_helper.cc
@@ -6,12 +6,15 @@
 #include <cstring>
 #include <thread>
 
+#include <lib/fxl/command_line.h>
+#include <lib/fxl/log_settings.h>
+#include <lib/fxl/log_settings_command_line.h>
+#include <lib/zx/event.h>
+#include <lib/zx/port.h>
 #include <zircon/process.h>
 #include <zircon/processargs.h>
 #include <zircon/syscalls.h>
 #include <zircon/syscalls/port.h>
-#include <lib/zx/event.h>
-#include <lib/zx/port.h>
 
 #include "garnet/lib/debugger_utils/breakpoints.h"
 #include "garnet/lib/debugger_utils/util.h"
@@ -113,29 +116,31 @@
 }
 
 int main(int argc, char* argv[]) {
-  if (argc > 2) {
-    fprintf(stderr, "Usage: %s [command]\n", argv[0]);
+  auto cl = fxl::CommandLineFromArgcArgv(argc, argv);
+  if (!fxl::SetLogSettingsFromCommandLine(cl)) {
     return 1;
   }
 
-  zx_handle_t channel = zx_take_startup_handle(PA_HND(PA_USER0, 0));
-  // If no channel was passed we're running standalone.
-  if (channel == ZX_HANDLE_INVALID) {
-    FXL_LOG(INFO) << "No handle provided";
-  }
+  const std::vector<std::string>& args = cl.positional_args();
 
-  if (argc == 2) {
-    const char* cmd = argv[1];
-    if (strcmp(cmd, "wait-peer-closed") == 0) {
+  if (!args.empty()) {
+    zx_handle_t channel = zx_take_startup_handle(PA_HND(PA_USER0, 0));
+    // If no channel was passed we're running standalone.
+    if (channel == ZX_HANDLE_INVALID) {
+      FXL_LOG(WARNING) << "No handle provided";
+    }
+
+    const std::string& cmd = args[0];
+    if (cmd == "wait-peer-closed") {
       return PerformWaitPeerClosed(channel);
     }
-    if (strcmp(cmd, "trigger-sw-bkpt") == 0) {
+    if (cmd == "trigger-sw-bkpt") {
       return TriggerSoftwareBreakpoint(channel, false);
     }
-    if (strcmp(cmd, "trigger-sw-bkpt-with-handler") == 0) {
+    if (cmd == "trigger-sw-bkpt-with-handler") {
       return TriggerSoftwareBreakpoint(channel, true);
     }
-    fprintf(stderr, "Unrecognized command: %s\n", cmd);
+    FXL_LOG(ERROR) << "Unrecognized command: " << cmd;
     return 1;
   }
 
diff --git a/garnet/lib/inferior_control/test_server.cc b/garnet/lib/inferior_control/test_server.cc
index 05ab0ce..33bd0ab 100644
--- a/garnet/lib/inferior_control/test_server.cc
+++ b/garnet/lib/inferior_control/test_server.cc
@@ -9,6 +9,7 @@
 #include <string>
 #include <vector>
 
+#include <lib/fxl/log_settings_command_line.h>
 #include <lib/fxl/logging.h>
 #include <lib/fxl/strings/string_printf.h>
 #include <lib/sys/cpp/service_directory.h>
@@ -35,6 +36,13 @@
 }
 
 void TestServer::TearDown() {
+  // Before we close the exception port, make sure we've detached.
+  Process* inferior = current_process();
+  if (inferior && inferior->IsAttached()) {
+    inferior->Kill();
+    inferior->Detach();
+  }
+
   if (exception_port_started_) {
     // Tell the exception port to quit and wait for it to finish.
     exception_port_.Quit();
@@ -46,9 +54,10 @@
 
 bool TestServer::Run() {
   // Start the main loop.
-  message_loop_.Run();
+  zx_status_t status = message_loop_.Run();
 
-  FXL_LOG(INFO) << "Main loop exited";
+  FXL_LOG(INFO) << "Main loop exited, status "
+                << debugger_utils::ZxErrorString(status);
 
   // |run_status_| is checked by TearDown().
   return true;
@@ -56,7 +65,14 @@
 
 bool TestServer::SetupInferior(const std::vector<std::string>& argv) {
   auto inferior = new Process(this, this, services_);
-  inferior->set_argv(argv);
+
+  // Transfer our log settings to the inferior.
+  std::vector<std::string> inferior_argv{argv};
+  for (auto arg : fxl::LogSettingsToArgv(fxl::GetLogSettings())) {
+    inferior_argv.push_back(arg);
+  }
+  inferior->set_argv(inferior_argv);
+
   // We take over ownership of |inferior| here.
   set_current_process(inferior);
   return true;
@@ -101,6 +117,8 @@
     return false;
   }
   if (inferior->IsAttached()) {
+    // The program should have terminated some how, in which case we would
+    // have detached. So it's likely the program is still running.
     FXL_LOG(ERROR) << "inferior still attached";
     return false;
   }
@@ -126,6 +144,8 @@
     return false;
   }
   if (inferior->IsAttached()) {
+    // The program should have terminated some how, in which case we would
+    // have detached. So it's likely the program is still running.
     FXL_LOG(ERROR) << "inferior still attached";
     return false;
   }
@@ -171,8 +191,9 @@
 void TestServer::OnProcessTermination(Process* process) {
   FXL_DCHECK(process);
 
-  printf("Process %s is gone, rc %d\n", process->GetName().c_str(),
-         process->return_code());
+  FXL_LOG(INFO) << fxl::StringPrintf("Process %s is gone, rc %d",
+                                     process->GetName().c_str(),
+                                     process->return_code());
 
   // Process is gone, exit main loop.
   PostQuitMessageLoop(true);