xref: /aosp_15_r20/system/core/debuggerd/tombstoned/intercept_manager.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1 /*
2  * Copyright 2016, 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 #pragma once
18 
19 #include <sys/types.h>
20 
21 #include <unordered_map>
22 
23 #include <event2/event.h>
24 #include <event2/listener.h>
25 
26 #include <android-base/unique_fd.h>
27 
28 #include "dump_type.h"
29 
30 struct InterceptManager;
31 struct InterceptRequest;
32 struct InterceptResponse;
33 
34 struct Intercept {
35   ~Intercept();
36 
37   InterceptManager* intercept_manager = nullptr;
38   event* intercept_event = nullptr;
39   android::base::unique_fd sockfd;
40 
41   pid_t pid = -1;
42   android::base::unique_fd output_fd;
43   bool registered = false;
44   DebuggerdDumpType dump_type = kDebuggerdNativeBacktrace;
45 };
46 
47 struct InterceptManager {
48   event_base* base;
49   std::unordered_map<pid_t, std::unordered_map<DebuggerdDumpType, Intercept*>> intercepts;
50   evconnlistener* listener = nullptr;
51 
52   InterceptManager(event_base* _Nonnull base, int intercept_socket);
53   InterceptManager(InterceptManager& copy) = delete;
54   InterceptManager(InterceptManager&& move) = delete;
55 
56   bool CanRegister(const InterceptRequest& request, InterceptResponse& response);
57   Intercept* Get(const pid_t pid, const DebuggerdDumpType dump_type);
58   void Register(Intercept* intercept);
59   void Unregister(Intercept* intercept);
60 
61   bool FindIntercept(pid_t pid, DebuggerdDumpType dump_type, android::base::unique_fd* out_fd);
62 };
63