[traceutil] Print warning if trace_manager is not in base package set

Tested: fx traceutil record <-- with/without running fx set with
--with-base=//garnet/packages/prod:tracing

PT-143

Change-Id: Ia211a7226a267a0ca2717393bd4bb15c41f7f203
diff --git a/garnet/bin/traceutil/check_build.go b/garnet/bin/traceutil/check_build.go
new file mode 100644
index 0000000..82c312a
--- /dev/null
+++ b/garnet/bin/traceutil/check_build.go
@@ -0,0 +1,55 @@
+// Copyright 2019 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"path"
+
+	"github.com/golang/glog"
+)
+
+const baseTargetListFile = "monolith_packages.list"
+const traceManagerPackageName = "trace_manager"
+
+// PT-143: If tracing isn't in the base configuration then a lot of services
+// will not have connected to trace-manager, resulting in potentially
+// confusing traces. Until a better solution is found, at least give the user
+// a heads-up to prevent wasting time wondering what the problem is.
+//
+// TODO(dje): It would make sense to only print this once a day or some such.
+
+func checkBuildConfiguration() {
+	if !traceManagerIsInBaseBuildTargets() {
+		fmt.Printf("WARNING: %s is not in the base package set.\n", traceManagerPackageName)
+		fmt.Printf("    Tracing will likely have missing data. PT-143\n")
+		fmt.Printf("    To fix this, add --with-base=//garnet/packages/prod:tracing to \"fx set\"\n")
+	}
+}
+
+func traceManagerIsInBaseBuildTargets() bool {
+	file, err := os.Open(getBaseTargetListFile())
+	if err != nil {
+		glog.V(1).Infof("%s not found\n", baseTargetListFile)
+		return false
+	}
+
+	scanner := bufio.NewScanner(file)
+
+	for scanner.Scan() {
+		package_name := scanner.Text()
+		if package_name == traceManagerPackageName {
+			return true
+		}
+	}
+
+	return false
+}
+
+func getBaseTargetListFile() string {
+	return path.Join(buildRoot, baseTargetListFile)
+}
diff --git a/garnet/bin/traceutil/cmd_record.go b/garnet/bin/traceutil/cmd_record.go
index 0f4e532..92eda1c 100644
--- a/garnet/bin/traceutil/cmd_record.go
+++ b/garnet/bin/traceutil/cmd_record.go
@@ -201,6 +201,8 @@
 
 func (cmd *cmdRecord) Execute(_ context.Context, f *flag.FlagSet,
 	_ ...interface{}) subcommands.ExitStatus {
+	checkBuildConfiguration()
+
 	// Flag errors in report type early.
 	reportGenerator := builtinReports[cmd.reportType]
 	generatorPath := ""