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