• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022, 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 <android-base/unique_fd.h>
20 #include <binder/Parcel.h>
21 #include <mutex>
22 
23 namespace android {
24 
25 namespace binder::debug {
26 
27 // Warning: Transactions are sequentially recorded to the file descriptor in a
28 // non-stable format. A detailed description of the recording format can be found in
29 // RecordedTransaction.cpp.
30 
31 class RecordedTransaction {
32 public:
33     // Filled with the first transaction from fd.
34     static std::optional<RecordedTransaction> fromFile(const android::base::unique_fd& fd);
35     // Filled with the arguments.
36     static std::optional<RecordedTransaction> fromDetails(const String16& interfaceName,
37                                                           uint32_t code, uint32_t flags,
38                                                           timespec timestamp, const Parcel& data,
39                                                           const Parcel& reply, status_t err);
40     RecordedTransaction(RecordedTransaction&& t) noexcept;
41 
42     [[nodiscard]] status_t dumpToFile(const android::base::unique_fd& fd) const;
43 
44     const std::string& getInterfaceName() const;
45     uint32_t getCode() const;
46     uint32_t getFlags() const;
47     int32_t getReturnedStatus() const;
48     timespec getTimestamp() const;
49     uint32_t getVersion() const;
50     const Parcel& getDataParcel() const;
51     const Parcel& getReplyParcel() const;
52 
53 private:
54     RecordedTransaction() = default;
55 
56     android::status_t writeChunk(const android::base::borrowed_fd, uint32_t chunkType,
57                                  size_t byteCount, const uint8_t* data) const;
58 
59 #pragma clang diagnostic push
60 #pragma clang diagnostic error "-Wpadded"
61     struct TransactionHeader {
62         uint32_t code = 0;
63         uint32_t flags = 0;
64         int32_t statusReturned = 0;
65         uint32_t version = 0; // !0 iff Rpc
66         int64_t timestampSeconds = 0;
67         int32_t timestampNanoseconds = 0;
68         int32_t reserved = 0;
69     };
70 #pragma clang diagnostic pop
71     static_assert(sizeof(TransactionHeader) == 32);
72     static_assert(sizeof(TransactionHeader) % 8 == 0);
73 
74     struct MovableData { // movable
75         TransactionHeader mHeader;
76         std::string mInterfaceName;
77     };
78     MovableData mData;
79     Parcel mSent;
80     Parcel mReply;
81 };
82 
83 } // namespace binder::debug
84 
85 } // namespace android
86