LLDB mainline
AdbClient.h
Go to the documentation of this file.
1//===-- AdbClient.h ---------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H
10#define LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H
11
12#include "lldb/Utility/Status.h"
13#include <chrono>
14#include <functional>
15#include <list>
16#include <memory>
17#include <string>
18#include <vector>
19
20namespace lldb_private {
21
22class FileSpec;
23
24namespace platform_android {
25
26class AdbClient {
27public:
31 };
32
33 using DeviceIDList = std::list<std::string>;
34
36 friend class AdbClient;
37
38 public:
39 virtual ~SyncService();
40
41 virtual Status PullFile(const FileSpec &remote_file,
42 const FileSpec &local_file);
43
44 Status PushFile(const FileSpec &local_file, const FileSpec &remote_file);
45
46 virtual Status Stat(const FileSpec &remote_file, uint32_t &mode,
47 uint32_t &size, uint32_t &mtime);
48
49 bool IsConnected() const;
50
51 protected:
52 explicit SyncService(std::unique_ptr<Connection> &&conn);
53
54 private:
55 Status SendSyncRequest(const char *request_id, const uint32_t data_len,
56 const void *data);
57
58 Status ReadSyncHeader(std::string &response_id, uint32_t &data_len);
59
60 Status PullFileChunk(std::vector<char> &buffer, bool &eof);
61
62 Status ReadAllBytes(void *buffer, size_t size);
63
64 Status internalPullFile(const FileSpec &remote_file,
65 const FileSpec &local_file);
66
67 Status internalPushFile(const FileSpec &local_file,
68 const FileSpec &remote_file);
69
70 Status internalStat(const FileSpec &remote_file, uint32_t &mode,
71 uint32_t &size, uint32_t &mtime);
72
73 Status executeCommand(const std::function<Status()> &cmd);
74
75 std::unique_ptr<Connection> m_conn;
76 };
77
78 static Status CreateByDeviceID(const std::string &device_id, AdbClient &adb);
79
81 explicit AdbClient(const std::string &device_id);
82
83 virtual ~AdbClient();
84
85 const std::string &GetDeviceID() const;
86
87 Status GetDevices(DeviceIDList &device_list);
88
89 Status SetPortForwarding(const uint16_t local_port,
90 const uint16_t remote_port);
91
92 Status SetPortForwarding(const uint16_t local_port,
93 llvm::StringRef remote_socket_name,
94 const UnixSocketNamespace socket_namespace);
95
96 Status DeletePortForwarding(const uint16_t local_port);
97
98 Status Shell(const char *command, std::chrono::milliseconds timeout,
99 std::string *output);
100
101 virtual Status ShellToFile(const char *command,
102 std::chrono::milliseconds timeout,
103 const FileSpec &output_file_spec);
104
105 virtual std::unique_ptr<SyncService> GetSyncService(Status &error);
106
108
109private:
110 Status Connect();
111
112 void SetDeviceID(const std::string &device_id);
113
114 Status SendMessage(const std::string &packet, const bool reconnect = true);
115
116 Status SendDeviceMessage(const std::string &packet);
117
118 Status ReadMessage(std::vector<char> &message);
119
120 Status ReadMessageStream(std::vector<char> &message,
121 std::chrono::milliseconds timeout);
122
123 Status GetResponseError(const char *response_id);
124
126
127 Status Sync();
128
130
131 Status internalShell(const char *command, std::chrono::milliseconds timeout,
132 std::vector<char> &output_buf);
133
134 Status ReadAllBytes(void *buffer, size_t size);
135
136 std::string m_device_id;
137 std::unique_ptr<Connection> m_conn;
138};
139
140} // namespace platform_android
141} // namespace lldb_private
142
143#endif // LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H
static llvm::raw_ostream & error(Stream &strm)
A file utility class.
Definition: FileSpec.h:56
An error handling class.
Definition: Status.h:115
Status internalPushFile(const FileSpec &local_file, const FileSpec &remote_file)
Definition: AdbClient.cpp:465
Status ReadAllBytes(void *buffer, size_t size)
Definition: AdbClient.cpp:665
Status SendSyncRequest(const char *request_id, const uint32_t data_len, const void *data)
Definition: AdbClient.cpp:600
Status executeCommand(const std::function< Status()> &cmd)
Definition: AdbClient.cpp:587
Status PushFile(const FileSpec &local_file, const FileSpec &remote_file)
Definition: AdbClient.cpp:565
Status PullFileChunk(std::vector< char > &buffer, bool &eof)
Definition: AdbClient.cpp:633
Status ReadSyncHeader(std::string &response_id, uint32_t &data_len)
Definition: AdbClient.cpp:618
virtual Status PullFile(const FileSpec &remote_file, const FileSpec &local_file)
Definition: AdbClient.cpp:558
Status internalPullFile(const FileSpec &remote_file, const FileSpec &local_file)
Definition: AdbClient.cpp:430
virtual Status Stat(const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime)
Definition: AdbClient.cpp:572
Status internalStat(const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime)
Definition: AdbClient.cpp:521
const std::string & GetDeviceID() const
Definition: AdbClient.cpp:128
void SetDeviceID(const std::string &device_id)
Definition: AdbClient.cpp:124
std::list< std::string > DeviceIDList
Definition: AdbClient.h:33
Status internalShell(const char *command, std::chrono::milliseconds timeout, std::vector< char > &output_buf)
Definition: AdbClient.cpp:350
virtual std::unique_ptr< SyncService > GetSyncService(Status &error)
Definition: AdbClient.cpp:421
Status DeletePortForwarding(const uint16_t local_port)
Definition: AdbClient.cpp:202
static Status CreateByDeviceID(const std::string &device_id, AdbClient &adb)
Definition: AdbClient.cpp:91
Status Shell(const char *command, std::chrono::milliseconds timeout, std::string *output)
Definition: AdbClient.cpp:386
Status ReadAllBytes(void *buffer, size_t size)
Definition: AdbClient.cpp:346
Status SetPortForwarding(const uint16_t local_port, const uint16_t remote_port)
Definition: AdbClient.cpp:170
Status SendDeviceMessage(const std::string &packet)
Definition: AdbClient.cpp:235
virtual Status ShellToFile(const char *command, std::chrono::milliseconds timeout, const FileSpec &output_file_spec)
Definition: AdbClient.cpp:398
Status GetResponseError(const char *response_id)
Definition: AdbClient.cpp:301
Status SendMessage(const std::string &packet, const bool reconnect=true)
Definition: AdbClient.cpp:213
Status ReadMessage(std::vector< char > &message)
Definition: AdbClient.cpp:241
Status GetDevices(DeviceIDList &device_list)
Definition: AdbClient.cpp:143
std::unique_ptr< Connection > m_conn
Definition: AdbClient.h:137
Status ReadMessageStream(std::vector< char > &message, std::chrono::milliseconds timeout)
Definition: AdbClient.cpp:262
A class that represents a running process on the host machine.