xref: /aosp_15_r20/external/perfetto/src/profiling/memory/client_api_factory_android.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/profiling/memory/client_api_factory.h"
18 
19 #include <string>
20 
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26 #include <optional>
27 
28 #include "perfetto/base/build_config.h"
29 #include "perfetto/ext/base/unix_socket.h"
30 #include "src/profiling/memory/client.h"
31 
32 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
33 #error "Must be built on Android."
34 #endif
35 
36 namespace perfetto {
37 namespace profiling {
38 
StartHeapprofdIfStatic()39 void StartHeapprofdIfStatic() {}
40 
ConstructClient(UnhookedAllocator<perfetto::profiling::Client> unhooked_allocator)41 std::shared_ptr<Client> ConstructClient(
42     UnhookedAllocator<perfetto::profiling::Client> unhooked_allocator) {
43   PERFETTO_LOG("Constructing client for central daemon.");
44   using perfetto::profiling::Client;
45 
46   std::optional<perfetto::base::UnixSocketRaw> sock =
47       Client::ConnectToHeapprofd(perfetto::profiling::kHeapprofdSocketFile);
48   if (!sock) {
49     PERFETTO_ELOG("Failed to connect to %s.",
50                   perfetto::profiling::kHeapprofdSocketFile);
51     return nullptr;
52   }
53   return Client::CreateAndHandshake(std::move(sock.value()),
54                                     unhooked_allocator);
55 }
56 
57 }  // namespace profiling
58 }  // namespace perfetto
59