crash-reporter: remove gflags dependency

We are switching to using chromeos/flag_helper.h instead to standardize the
code everywhere.

BUG=chromium:402631
TEST=`FEATURES=test emerge-panther crash-reporter`
TEST=`test_that -b panther <ip> e:logging_.*`

Change-Id: I62b911a54f89d853235d0540460cbea119f66e9e
Reviewed-on: https://chromium-review.googlesource.com/212140
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Steve Fung <stevefung@chromium.org>
Commit-Queue: Steve Fung <stevefung@chromium.org>
diff --git a/crash_reporter/crash-reporter.gyp b/crash_reporter/crash-reporter.gyp
index 79faf97..974d8d0 100644
--- a/crash_reporter/crash-reporter.gyp
+++ b/crash_reporter/crash-reporter.gyp
@@ -29,11 +29,6 @@
             '<@(exported_deps)',
           ],
         },
-        'link_settings': {
-          'libraries': [
-            '-lgflags',
-          ],
-        },
       },
       'sources': [
         'chrome_collector.cc',
diff --git a/crash_reporter/crash_reporter.cc b/crash_reporter/crash_reporter.cc
index 1da71d4..c5824aa 100644
--- a/crash_reporter/crash_reporter.cc
+++ b/crash_reporter/crash_reporter.cc
@@ -8,14 +8,13 @@
 #include <string>
 #include <vector>
 
-#include <base/command_line.h>
 #include <base/files/file_util.h>
 #include <base/logging.h>
 #include <base/strings/string_split.h>
 #include <base/strings/string_util.h>
 #include <base/strings/stringprintf.h>
+#include <chromeos/flag_helper.h>
 #include <chromeos/syslog_logging.h>
-#include <gflags/gflags.h>
 #include <metrics/metrics_library.h>
 
 #include "crash-reporter/chrome_collector.h"
@@ -25,22 +24,6 @@
 #include "crash-reporter/unclean_shutdown_collector.h"
 #include "crash-reporter/user_collector.h"
 
-#pragma GCC diagnostic ignored "-Wstrict-aliasing"
-DEFINE_bool(init, false, "Initialize crash logging");
-DEFINE_bool(clean_shutdown, false, "Signal clean shutdown");
-DEFINE_string(generate_kernel_signature, "",
-              "Generate signature from given kcrash file");
-DEFINE_bool(crash_test, false, "Crash test");
-DEFINE_string(user, "", "User crash info (pid:signal:exec_name)");
-DEFINE_bool(unclean_check, true, "Check for unclean shutdown");
-DEFINE_string(udev, "", "Udev event description (type:device:subsystem)");
-DEFINE_bool(kernel_warning, false, "Report collected kernel warning");
-DEFINE_string(chrome, "", "Chrome crash dump file");
-DEFINE_string(pid, "", "PID of crashing process");
-DEFINE_string(uid, "", "UID of crashing process");
-DEFINE_string(exe, "", "Executable name of crashing process");
-#pragma GCC diagnostic error "-Wstrict-aliasing"
-
 static const char kCrashCounterHistogram[] = "Logging.CrashCounter";
 static const char kUserCrashSignal[] =
     "org.chromium.CrashReporter.UserCrash";
@@ -124,8 +107,10 @@
 
 static int Initialize(KernelCollector *kernel_collector,
                       UserCollector *user_collector,
-                      UncleanShutdownCollector *unclean_shutdown_collector) {
-  CHECK(!FLAGS_clean_shutdown) << "Incompatible options";
+                      UncleanShutdownCollector *unclean_shutdown_collector,
+                      const bool unclean_check,
+                      const bool clean_shutdown) {
+  CHECK(!clean_shutdown) << "Incompatible options";
 
   bool was_kernel_crash = false;
   bool was_unclean_shutdown = false;
@@ -134,7 +119,7 @@
     was_kernel_crash = kernel_collector->Collect();
   }
 
-  if (FLAGS_unclean_check) {
+  if (unclean_check) {
     was_unclean_shutdown = unclean_shutdown_collector->Collect();
   }
 
@@ -158,13 +143,14 @@
   return 0;
 }
 
-static int HandleUserCrash(UserCollector *user_collector) {
+static int HandleUserCrash(UserCollector *user_collector,
+                           const std::string& user, const bool crash_test) {
   // Handle a specific user space crash.
-  CHECK(!FLAGS_user.empty()) << "--user= must be set";
+  CHECK(!user.empty()) << "--user= must be set";
 
   // Make it possible to test what happens when we crash while
   // handling a crash.
-  if (FLAGS_crash_test) {
+  if (crash_test) {
     *(volatile char *)0 = 0;
     return 0;
   }
@@ -172,35 +158,40 @@
   // Accumulate logs to help in diagnosing failures during user collection.
   chromeos::LogToString(true);
   // Handle the crash, get the name of the process from procfs.
-  bool handled = user_collector->HandleCrash(FLAGS_user, nullptr);
+  bool handled = user_collector->HandleCrash(user, nullptr);
   chromeos::LogToString(false);
   if (!handled)
     return 1;
   return 0;
 }
 
-static int HandleChromeCrash(ChromeCollector *chrome_collector) {
-  CHECK(!FLAGS_chrome.empty()) << "--chrome= must be set";
-  CHECK(!FLAGS_pid.empty()) << "--pid= must be set";
-  CHECK(!FLAGS_uid.empty()) << "--uid= must be set";
-  CHECK(!FLAGS_exe.empty()) << "--exe= must be set";
+static int HandleChromeCrash(ChromeCollector *chrome_collector,
+                             const std::string& chrome_dump_file,
+                             const std::string& pid,
+                             const std::string& uid,
+                             const std::string& exe) {
+  CHECK(!chrome_dump_file.empty()) << "--chrome= must be set";
+  CHECK(!pid.empty()) << "--pid= must be set";
+  CHECK(!uid.empty()) << "--uid= must be set";
+  CHECK(!exe.empty()) << "--exe= must be set";
 
   chromeos::LogToString(true);
-  bool handled = chrome_collector->HandleCrash(FilePath(FLAGS_chrome),
-                                               FLAGS_pid, FLAGS_uid, FLAGS_exe);
+  bool handled = chrome_collector->HandleCrash(FilePath(chrome_dump_file),
+                                               pid, uid, exe);
   chromeos::LogToString(false);
   if (!handled)
     return 1;
   return 0;
 }
 
-static int HandleUdevCrash(UdevCollector *udev_collector) {
+static int HandleUdevCrash(UdevCollector *udev_collector,
+                           const std::string& udev_event) {
   // Handle a crash indicated by a udev event.
-  CHECK(!FLAGS_udev.empty()) << "--udev= must be set";
+  CHECK(!udev_event.empty()) << "--udev= must be set";
 
   // Accumulate logs to help in diagnosing failures during user collection.
   chromeos::LogToString(true);
-  bool handled = udev_collector->HandleCrash(FLAGS_udev);
+  bool handled = udev_collector->HandleCrash(udev_event);
   chromeos::LogToString(false);
   if (!handled)
     return 1;
@@ -219,10 +210,11 @@
 }
 
 // Interactive/diagnostics mode for generating kernel crash signatures.
-static int GenerateKernelSignature(KernelCollector *kernel_collector) {
+static int GenerateKernelSignature(KernelCollector *kernel_collector,
+                                   const std::string& kernel_signature_file) {
   std::string kcrash_contents;
   std::string signature;
-  if (!base::ReadFileToString(FilePath(FLAGS_generate_kernel_signature),
+  if (!base::ReadFileToString(FilePath(kernel_signature_file),
                               &kcrash_contents)) {
     fprintf(stderr, "Could not read file.\n");
     return 1;
@@ -258,11 +250,28 @@
 }
 
 int main(int argc, char *argv[]) {
+  DEFINE_bool(init, false, "Initialize crash logging");
+  DEFINE_bool(clean_shutdown, false, "Signal clean shutdown");
+  DEFINE_string(generate_kernel_signature, "",
+                "Generate signature from given kcrash file");
+  DEFINE_bool(crash_test, false, "Crash test");
+  DEFINE_string(user, "", "User crash info (pid:signal:exec_name)");
+  DEFINE_bool(unclean_check, true, "Check for unclean shutdown");
+  DEFINE_string(udev, "", "Udev event description (type:device:subsystem)");
+  DEFINE_bool(kernel_warning, false, "Report collected kernel warning");
+  DEFINE_string(chrome, "", "Chrome crash dump file");
+  DEFINE_string(pid, "", "PID of crashing process");
+  DEFINE_string(uid, "", "UID of crashing process");
+  DEFINE_string(exe, "", "Executable name of crashing process");
+  DEFINE_bool(core2md_failure, false, "Core2md failure test");
+  DEFINE_bool(directory_failure, false, "Spool directory failure test");
+  DEFINE_string(filter_in, "",
+                "Ignore all crashes but this for testing");
+
   OpenStandardFileDescriptors();
-  google::ParseCommandLineFlags(&argc, &argv, true);
   FilePath my_path = base::MakeAbsoluteFilePath(FilePath(argv[0]));
   s_metrics_lib.Init();
-  CommandLine::Init(argc, argv);
+  chromeos::FlagHelper::Init(argc, argv, "Chromium OS Crash Reporter");
   chromeos::OpenLog(my_path.BaseName().value().c_str(), true);
   chromeos::InitLog(chromeos::kLogToSyslog);
 
@@ -274,7 +283,10 @@
   user_collector.Initialize(CountUserCrash,
                             my_path.value(),
                             IsFeedbackAllowed,
-                            true);  // generate_diagnostics
+                            true,  // generate_diagnostics
+                            FLAGS_core2md_failure,
+                            FLAGS_directory_failure,
+                            FLAGS_filter_in);
   UncleanShutdownCollector unclean_shutdown_collector;
   unclean_shutdown_collector.Initialize(CountUncleanShutdown,
                                         IsFeedbackAllowed);
@@ -289,7 +301,9 @@
   if (FLAGS_init) {
     return Initialize(&kernel_collector,
                       &user_collector,
-                      &unclean_shutdown_collector);
+                      &unclean_shutdown_collector,
+                      FLAGS_unclean_check,
+                      FLAGS_clean_shutdown);
   }
 
   if (FLAGS_clean_shutdown) {
@@ -299,11 +313,12 @@
   }
 
   if (!FLAGS_generate_kernel_signature.empty()) {
-    return GenerateKernelSignature(&kernel_collector);
+    return GenerateKernelSignature(&kernel_collector,
+                                   FLAGS_generate_kernel_signature);
   }
 
   if (!FLAGS_udev.empty()) {
-    return HandleUdevCrash(&udev_collector);
+    return HandleUdevCrash(&udev_collector, FLAGS_udev);
   }
 
   if (FLAGS_kernel_warning) {
@@ -311,8 +326,12 @@
   }
 
   if (!FLAGS_chrome.empty()) {
-    return HandleChromeCrash(&chrome_collector);
+    return HandleChromeCrash(&chrome_collector,
+                             FLAGS_chrome,
+                             FLAGS_pid,
+                             FLAGS_uid,
+                             FLAGS_exe);
   }
 
-  return HandleUserCrash(&user_collector);
+  return HandleUserCrash(&user_collector, FLAGS_user, FLAGS_crash_test);
 }
diff --git a/crash_reporter/user_collector.cc b/crash_reporter/user_collector.cc
index 9056a5c..302b130 100644
--- a/crash_reporter/user_collector.cc
+++ b/crash_reporter/user_collector.cc
@@ -26,14 +26,6 @@
 #include <base/strings/stringprintf.h>
 #include <chromeos/process.h>
 #include <chromeos/syslog_logging.h>
-#include <gflags/gflags.h>
-
-#pragma GCC diagnostic ignored "-Wstrict-aliasing"
-DEFINE_bool(core2md_failure, false, "Core2md failure test");
-DEFINE_bool(directory_failure, false, "Spool directory failure test");
-DEFINE_string(filter_in, "",
-              "Ignore all crashes but this for testing");
-#pragma GCC diagnostic error "-Wstrict-aliasing"
 
 static const char kCollectionErrorSignature[] =
     "crash_reporter-user-collection";
@@ -69,12 +61,18 @@
     UserCollector::CountCrashFunction count_crash_function,
     const std::string &our_path,
     UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function,
-    bool generate_diagnostics) {
+    bool generate_diagnostics,
+    bool core2md_failure,
+    bool directory_failure,
+    const std::string &filter_in) {
   CrashCollector::Initialize(count_crash_function,
                              is_feedback_allowed_function);
   our_path_ = our_path;
   initialized_ = true;
   generate_diagnostics_ = generate_diagnostics;
+  core2md_failure_ = core2md_failure;
+  directory_failure_ = directory_failure;
+  filter_in_ = filter_in;
 }
 
 UserCollector::~UserCollector() {
@@ -301,7 +299,7 @@
                                              bool *out_of_capacity) {
   FilePath process_path = GetProcessPath(pid);
   std::string status;
-  if (FLAGS_directory_failure) {
+  if (directory_failure_) {
     LOG(ERROR) << "Purposefully failing to create spool directory";
     return false;
   }
@@ -368,7 +366,7 @@
   core2md.AddArg(core_path.value());
   core2md.AddArg(procfs_directory.value());
 
-  if (!FLAGS_core2md_failure) {
+  if (!core2md_failure_) {
     core2md.AddArg(minidump_path.value());
   } else {
     // To test how core2md errors are propagaged, cause an error
@@ -635,13 +633,13 @@
 
   // Allow us to test the crash reporting mechanism successfully even if
   // other parts of the system crash.
-  if (!FLAGS_filter_in.empty() &&
-      (FLAGS_filter_in == "none" ||
-       FLAGS_filter_in != exec)) {
+  if (!filter_in_.empty() &&
+      (filter_in_ == "none" ||
+       filter_in_ != exec)) {
     // We use a different format message to make it more obvious in tests
     // which crashes are test generated and which are real.
     LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while "
-                 << "filter_in=" << FLAGS_filter_in << ".";
+                 << "filter_in=" << filter_in_ << ".";
     return true;
   }
 
diff --git a/crash_reporter/user_collector.h b/crash_reporter/user_collector.h
index fe2c00b..aac94cb 100644
--- a/crash_reporter/user_collector.h
+++ b/crash_reporter/user_collector.h
@@ -30,7 +30,10 @@
   void Initialize(CountCrashFunction count_crash,
                   const std::string &our_path,
                   IsFeedbackAllowedFunction is_metrics_allowed,
-                  bool generate_diagnostics);
+                  bool generate_diagnostics,
+                  bool core2md_failure,
+                  bool directory_failure,
+                  const std::string &filter_in);
 
   ~UserCollector() override;
 
@@ -176,6 +179,10 @@
   std::string our_path_;
   bool initialized_;
 
+  bool core2md_failure_;
+  bool directory_failure_;
+  std::string filter_in_;
+
   static const char *kUserId;
   static const char *kGroupId;
 
diff --git a/crash_reporter/user_collector_test.cc b/crash_reporter/user_collector_test.cc
index 778c061..4350f18 100644
--- a/crash_reporter/user_collector_test.cc
+++ b/crash_reporter/user_collector_test.cc
@@ -42,7 +42,10 @@
     collector_.Initialize(CountCrash,
                           kFilePath,
                           IsMetrics,
-                          false);
+                          false,
+                          false,
+                          false,
+                          "");
     base::DeleteFile(FilePath("test"), true);
     mkdir("test", 0777);
     collector_.set_core_pattern_file("test/core_pattern");