Add env var for producer/consumer socket.

Bug: 76169489
Change-Id: I952baff9aeeb0e68fc203cc9abf4749c6bdd09a6
diff --git a/Android.bp b/Android.bp
index 92426f0..e8c900f 100644
--- a/Android.bp
+++ b/Android.bp
@@ -177,6 +177,7 @@
     "src/tracing/core/trace_packet.cc",
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/ipc/consumer/consumer_ipc_client_impl.cc",
+    "src/tracing/ipc/default_socket.cc",
     "src/tracing/ipc/posix_shared_memory.cc",
   ],
   shared_libs: [
@@ -3181,6 +3182,7 @@
     "src/tracing/core/trace_packet.cc",
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/ipc/consumer/consumer_ipc_client_impl.cc",
+    "src/tracing/ipc/default_socket.cc",
     "src/tracing/ipc/posix_shared_memory.cc",
     "src/tracing/ipc/producer/producer_ipc_client_impl.cc",
     "src/tracing/ipc/service/consumer_ipc_service.cc",
@@ -3418,6 +3420,7 @@
     "src/tracing/core/trace_writer_impl.cc",
     "src/tracing/core/trace_writer_impl_unittest.cc",
     "src/tracing/ipc/consumer/consumer_ipc_client_impl.cc",
+    "src/tracing/ipc/default_socket.cc",
     "src/tracing/ipc/posix_shared_memory.cc",
     "src/tracing/ipc/posix_shared_memory_unittest.cc",
     "src/tracing/test/aligned_buffer_test.cc",
diff --git a/include/perfetto/traced/traced.h b/include/perfetto/traced/traced.h
index 5311131..df70e93 100644
--- a/include/perfetto/traced/traced.h
+++ b/include/perfetto/traced/traced.h
@@ -21,14 +21,6 @@
 
 namespace perfetto {
 
-#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
-#define PERFETTO_PRODUCER_SOCK_NAME "/dev/socket/traced_producer"
-#define PERFETTO_CONSUMER_SOCK_NAME "/dev/socket/traced_consumer"
-#else
-#define PERFETTO_PRODUCER_SOCK_NAME "/tmp/perfetto-producer"
-#define PERFETTO_CONSUMER_SOCK_NAME "/tmp/perfetto-consumer"
-#endif
-
 int ServiceMain(int argc, char** argv);
 int ProbesMain(int argc, char** argv);
 int PerfettoCmdMain(int argc, char** argv);
diff --git a/src/perfetto_cmd/perfetto_cmd.cc b/src/perfetto_cmd/perfetto_cmd.cc
index 0b61965..fd9dc50 100644
--- a/src/perfetto_cmd/perfetto_cmd.cc
+++ b/src/perfetto_cmd/perfetto_cmd.cc
@@ -42,6 +42,8 @@
 
 #include "perfetto/config/trace_config.pb.h"
 
+#include "src/tracing/ipc/default_socket.h"
+
 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
@@ -231,8 +233,8 @@
   if (!limiter.ShouldTrace(args))
     return 1;
 
-  consumer_endpoint_ = ConsumerIPCClient::Connect(PERFETTO_CONSUMER_SOCK_NAME,
-                                                  this, &task_runner_);
+  consumer_endpoint_ =
+      ConsumerIPCClient::Connect(GetConsumerSocket(), this, &task_runner_);
   SetupCtrlCSignalHandler();
   task_runner_.Run();
 
diff --git a/src/traced/probes/BUILD.gn b/src/traced/probes/BUILD.gn
index 47d5963..976657c 100644
--- a/src/traced/probes/BUILD.gn
+++ b/src/traced/probes/BUILD.gn
@@ -19,6 +19,7 @@
   deps = [
     ":probes_src",
     "../../../gn:default_deps",
+    "../../tracing:ipc",
   ]
   sources = [
     "probes.cc",
diff --git a/src/traced/probes/probes.cc b/src/traced/probes/probes.cc
index f8dea99..d2c29cc 100644
--- a/src/traced/probes/probes.cc
+++ b/src/traced/probes/probes.cc
@@ -25,6 +25,7 @@
 
 #include "src/ftrace_reader/ftrace_procfs.h"
 #include "src/traced/probes/probes_producer.h"
+#include "src/tracing/ipc/default_socket.h"
 
 namespace perfetto {
 
@@ -67,7 +68,7 @@
 
   base::UnixTaskRunner task_runner;
   ProbesProducer producer;
-  producer.ConnectWithRetries(PERFETTO_PRODUCER_SOCK_NAME, &task_runner);
+  producer.ConnectWithRetries(GetProducerSocket(), &task_runner);
   task_runner.Run();
   return 0;
 }
diff --git a/src/traced/service/service.cc b/src/traced/service/service.cc
index b3f7812..ac2dbfd 100644
--- a/src/traced/service/service.cc
+++ b/src/traced/service/service.cc
@@ -18,6 +18,7 @@
 #include "perfetto/base/watchdog.h"
 #include "perfetto/traced/traced.h"
 #include "perfetto/tracing/ipc/service_ipc_host.h"
+#include "src/tracing/ipc/default_socket.h"
 
 namespace perfetto {
 
@@ -37,9 +38,9 @@
     base::ScopedFile consumer_fd(atoi(env_cons));
     svc->Start(std::move(producer_fd), std::move(consumer_fd));
   } else {
-    unlink(PERFETTO_PRODUCER_SOCK_NAME);
-    unlink(PERFETTO_CONSUMER_SOCK_NAME);
-    svc->Start(PERFETTO_PRODUCER_SOCK_NAME, PERFETTO_CONSUMER_SOCK_NAME);
+    unlink(GetProducerSocket());
+    unlink(GetConsumerSocket());
+    svc->Start(GetProducerSocket(), GetConsumerSocket());
   }
 
   // Set the CPU limit and start the watchdog running. The memory limit will
@@ -49,8 +50,8 @@
   watchdog->SetCpuLimit(75, 30 * 1000);
   watchdog->Start();
 
-  PERFETTO_ILOG("Started traced, listening on %s %s",
-                PERFETTO_PRODUCER_SOCK_NAME, PERFETTO_CONSUMER_SOCK_NAME);
+  PERFETTO_ILOG("Started traced, listening on %s %s", GetProducerSocket(),
+                GetConsumerSocket());
   task_runner.Run();
   return 0;
 }
diff --git a/src/tracing/BUILD.gn b/src/tracing/BUILD.gn
index cc59dd2..32c8a26 100644
--- a/src/tracing/BUILD.gn
+++ b/src/tracing/BUILD.gn
@@ -71,6 +71,8 @@
   sources = [
     "ipc/consumer/consumer_ipc_client_impl.cc",
     "ipc/consumer/consumer_ipc_client_impl.h",
+    "ipc/default_socket.cc",
+    "ipc/default_socket.h",
     "ipc/posix_shared_memory.cc",
     "ipc/posix_shared_memory.h",
     "ipc/producer/producer_ipc_client_impl.cc",
@@ -101,6 +103,8 @@
   sources = [
     "ipc/consumer/consumer_ipc_client_impl.cc",
     "ipc/consumer/consumer_ipc_client_impl.h",
+    "ipc/default_socket.cc",
+    "ipc/default_socket.h",
     "ipc/posix_shared_memory.cc",
     "ipc/posix_shared_memory.h",
   ]
diff --git a/src/tracing/ipc/default_socket.cc b/src/tracing/ipc/default_socket.cc
new file mode 100644
index 0000000..236e2cf
--- /dev/null
+++ b/src/tracing/ipc/default_socket.cc
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "src/tracing/ipc/default_socket.h"
+
+#include "perfetto/base/build_config.h"
+
+#include <stdlib.h>
+
+namespace perfetto {
+
+const char* GetProducerSocket() {
+  static const char* name = getenv("PERFETTO_PRODUCER_SOCK_NAME");
+  if (name == nullptr) {
+#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
+    name = "/dev/socket/traced_producer";
+#else
+    name = "/tmp/perfetto-producer";
+#endif
+  }
+  return name;
+}
+
+const char* GetConsumerSocket() {
+  static const char* name = getenv("PERFETTO_CONSUMER_SOCK_NAME");
+  if (name == nullptr) {
+#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
+    name = "/dev/socket/traced_consumer";
+#else
+    name = "/tmp/perfetto-consumer";
+#endif
+  }
+  return name;
+}
+
+}  // namespace perfetto
diff --git a/src/tracing/ipc/default_socket.h b/src/tracing/ipc/default_socket.h
new file mode 100644
index 0000000..0cfa00e
--- /dev/null
+++ b/src/tracing/ipc/default_socket.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_TRACING_IPC_DEFAULT_SOCKET_H_
+#define SRC_TRACING_IPC_DEFAULT_SOCKET_H_
+
+namespace perfetto {
+
+const char* GetConsumerSocket();
+const char* GetProducerSocket();
+
+}  // namespace perfetto
+
+#endif  // SRC_TRACING_IPC_DEFAULT_SOCKET_H_
diff --git a/test/BUILD.gn b/test/BUILD.gn
index de7c72c..72e9c4a 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -95,6 +95,9 @@
 
 source_set("test_helper") {
   testonly = true
+  public_deps = [
+    "../src/tracing:ipc",
+  ]
   deps = [
     ":task_runner_thread",
     ":task_runner_thread_delegates",
diff --git a/test/cts/producer/jni/fake_producer_jni.cc b/test/cts/producer/jni/fake_producer_jni.cc
index 2341a63..f7cfe4e 100644
--- a/test/cts/producer/jni/fake_producer_jni.cc
+++ b/test/cts/producer/jni/fake_producer_jni.cc
@@ -19,6 +19,7 @@
 #include "perfetto/traced/traced.h"
 
 #include "src/base/test/test_task_runner.h"
+#include "src/tracing/ipc/default_socket.h"
 
 #include "test/fake_producer.h"
 
@@ -27,7 +28,7 @@
 void ListenAndRespond(const std::string& name) {
   base::TestTaskRunner task_runner;
   FakeProducer producer(name);
-  producer.Connect(PERFETTO_PRODUCER_SOCK_NAME, &task_runner,
+  producer.Connect(GetProducerSocket(), &task_runner,
                    [&producer]() { producer.ProduceEventBatch([] {}); });
   task_runner.Run();
 }
diff --git a/test/end_to_end_integrationtest.cc b/test/end_to_end_integrationtest.cc
index b765014..2a8e1f0 100644
--- a/test/end_to_end_integrationtest.cc
+++ b/test/end_to_end_integrationtest.cc
@@ -28,6 +28,7 @@
 #include "perfetto/tracing/core/trace_config.h"
 #include "perfetto/tracing/core/trace_packet.h"
 #include "src/base/test/test_task_runner.h"
+#include "src/tracing/ipc/default_socket.h"
 #include "test/task_runner_thread.h"
 #include "test/task_runner_thread_delegates.h"
 #include "test/test_helper.h"
@@ -43,7 +44,7 @@
     PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
 #define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer"
 #else
-#define TEST_PRODUCER_SOCK_NAME PERFETTO_PRODUCER_SOCK_NAME
+#define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket()
 #endif
 
 // TODO(b/73453011): reenable this on more platforms (including standalone
diff --git a/test/end_to_end_shared_memory_fuzzer.cc b/test/end_to_end_shared_memory_fuzzer.cc
index a05800d..9ca5430 100644
--- a/test/end_to_end_shared_memory_fuzzer.cc
+++ b/test/end_to_end_shared_memory_fuzzer.cc
@@ -31,6 +31,7 @@
 #include "perfetto/tracing/ipc/producer_ipc_client.h"
 #include "perfetto/tracing/ipc/service_ipc_host.h"
 #include "src/base/test/test_task_runner.h"
+#include "src/tracing/ipc/default_socket.h"
 #include "test/task_runner_thread.h"
 #include "test/task_runner_thread_delegates.h"
 #include "test/test_helper.h"
@@ -47,7 +48,7 @@
     PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
 #define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer"
 #else
-#define TEST_PRODUCER_SOCK_NAME PERFETTO_PRODUCER_SOCK_NAME
+#define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket()
 #endif
 
 // Fake producer writing a protozero message of data into shared memory
diff --git a/test/test_helper.cc b/test/test_helper.cc
index 9b2b39e..8055b7b 100644
--- a/test/test_helper.cc
+++ b/test/test_helper.cc
@@ -21,6 +21,8 @@
 #include "perfetto/tracing/core/trace_packet.h"
 #include "test/task_runner_thread_delegates.h"
 
+#include "src/tracing/ipc/default_socket.h"
+
 #include "perfetto/trace/trace_packet.pb.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
 
@@ -33,8 +35,8 @@
 #define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer"
 #define TEST_CONSUMER_SOCK_NAME "/data/local/tmp/traced_consumer"
 #else
-#define TEST_PRODUCER_SOCK_NAME PERFETTO_PRODUCER_SOCK_NAME
-#define TEST_CONSUMER_SOCK_NAME PERFETTO_CONSUMER_SOCK_NAME
+#define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket()
+#define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket()
 #endif
 
 TestHelper::TestHelper(base::TestTaskRunner* task_runner)