• Home
Name Date Size #Lines LOC

..--

apex/03-May-2024-194178

client/03-May-2024-16,39712,065

coverage/03-May-2024-175109

crypto/03-May-2024-691425

daemon/03-May-2024-6,6684,716

fastdeploy/03-May-2024-2,3621,536

fdevent/03-May-2024-1,392977

libs/03-May-2024-686447

pairing_auth/03-May-2024-1,193726

pairing_connection/03-May-2024-2,2811,583

proto/03-May-2024-271222

sysdeps/03-May-2024-924499

tls/03-May-2024-1,6141,120

tools/03-May-2024-523399

.clang-formatD03-May-2024291 1413

Android.bpD03-May-202422.3 KiB984875

MODULE_LICENSE_APACHE2D03-May-20240

NOTICED03-May-202410.5 KiB192159

OVERVIEW.TXTD03-May-20245.2 KiB13690

OWNERSD03-May-2024123 53

PREUPLOAD.cfgD03-May-2024246 96

README.mdD03-May-20246.5 KiB9572

SERVICES.TXTD03-May-20249.6 KiB257193

SOCKET-ACTIVATION.txtD03-May-20241.4 KiB4337

SYNC.TXTD03-May-20243.4 KiB8163

TEST_MAPPINGD03-May-2024102 87

adb.bashD03-May-202412.9 KiB500403

adb.cppD03-May-202450.6 KiB1,4281,068

adb.hD03-May-20247.5 KiB262156

adb_auth.hD03-May-20241.9 KiB7134

adb_integration_test_adb.xmlD03-May-20241.4 KiB3014

adb_integration_test_device.xmlD03-May-20241.4 KiB3014

adb_io.cppD03-May-20245.9 KiB190121

adb_io.hD03-May-20243.1 KiB7917

adb_io_test.cppD03-May-20244.4 KiB15796

adb_listeners.cppD03-May-20248.2 KiB257189

adb_listeners.hD03-May-20241.5 KiB5023

adb_listeners_test.cppD03-May-20245.4 KiB167105

adb_mdns.cppD03-May-20244.7 KiB13990

adb_mdns.hD03-May-20244.1 KiB9947

adb_test.xmlD03-May-20241.7 KiB3919

adb_trace.cppD03-May-20246.1 KiB193132

adb_trace.hD03-May-20241.6 KiB6333

adb_unique_fd.cppD03-May-2024791 299

adb_unique_fd.hD03-May-20241.1 KiB4016

adb_utils.cppD03-May-202410.8 KiB381269

adb_utils.hD03-May-20243.5 KiB13082

adb_utils_test.cppD03-May-20247.9 KiB236172

adb_wifi.hD03-May-20241 KiB3914

benchmark_device.pyD03-May-20244.5 KiB157110

bugreport_test.cppD03-May-202419.2 KiB467332

compression_utils.hD03-May-202417.1 KiB487372

file_sync_protocol.hD03-May-20243.7 KiB145110

mdns_test.cppD03-May-20243.6 KiB10859

protocol.txtD03-May-202412 KiB300208

security_log_tags.hD03-May-20241 KiB297

services.cppD03-May-20249.4 KiB273210

services.hD03-May-20241.2 KiB3310

shell_protocol.hD03-May-20243.8 KiB11938

shell_service_protocol.cppD03-May-20241.7 KiB6333

shell_service_protocol_test.cppD03-May-20245.9 KiB199124

socket.hD03-May-20243.7 KiB12745

socket_spec.cppD03-May-202415.8 KiB468393

socket_spec.hD03-May-20241.3 KiB3612

socket_spec_test.cppD03-May-20247.3 KiB189149

socket_test.cppD03-May-202413.2 KiB387268

sockets.cppD03-May-202428 KiB921653

sockets.diaD03-May-20242.3 KiB

sysdeps.hD03-May-202423.7 KiB796530

sysdeps_test.cppD03-May-20246.6 KiB254192

sysdeps_unix.cppD03-May-20243 KiB9360

sysdeps_win32.cppD03-May-2024111.8 KiB3,1472,105

sysdeps_win32_test.cppD03-May-20247.1 KiB171100

test_adb.pyD03-May-202433.4 KiB813576

test_device.pyD03-May-202469.1 KiB1,7871,302

trace.shD03-May-2024651 1815

transfer_id.hD03-May-20242.1 KiB7444

transport.cppD03-May-202447.5 KiB1,5681,216

transport.hD03-May-202416.3 KiB498280

transport_benchmark.cppD03-May-20246.8 KiB195140

transport_fd.cppD03-May-20247.8 KiB242183

transport_test.cppD03-May-20245.7 KiB188128

types.cppD03-May-20246.5 KiB205164

types.hD03-May-20249.7 KiB359248

types_test.cppD03-May-20243.8 KiB13791

README.md

1# ADB Internals
2
3If you are new to adb source code, you should start by reading [OVERVIEW.TXT](OVERVIEW.TXT) which describes the three components of adb pipeline.
4
5This document is here to boost what can be achieved within a "window of naive interest". You will not find function or class documentation here but rather the "big picture" which should allow you to build a mental map to help navigate the code.
6
7## Three components of adb pipeline
8
9As outlined in the overview, this codebase generates three components (Client, Server (a.k.a Host), and Daemon (a.k.a adbd)). The central part is the Server which runs on the Host computer. On one side the Server exposes a "Smart Socket" to Clients such as adb or DDMLIB. On the other side, the Server continuously monitors for connecting Daemons (as USB devices or TCP emulator). Communication with a device is done with a Transport.
10
11```
12+----------+              +------------------------+
13|   ADB    +----------+   |      ADB SERVER        |                   +----------+
14|  CLIENT  |          |   |                        |              (USB)|   ADBD   |
15+----------+          |   |                     Transport+-------------+ (DEVICE) |
16                      |   |                        |                   +----------+
17+-----------          |   |                        |
18|   ADB    |          v   +                        |                   +----------+
19|  CLIENT  +--------->SmartSocket                  |              (USB)|   ADBD   |
20+----------+          ^   | (TCP/IP)            Transport+-------------+ (DEVICE) |
21                      |   |                        |                   +----------+
22+----------+          |   |                        |
23|  DDMLIB  |          |   |                     Transport+--+          +----------+
24|  CLIENT  +----------+   |                        |        |  (TCP/IP)|   ADBD   |
25+----------+              +------------------------+        +----------|(EMULATOR)|
26                                                                       +----------+
27```
28
29The Client and the Server are contained in the same executable and both run on the Host machine. Code sections specific to the Host is enclosed within `ADB_HOST` guard. adbd runs on the Android Device. Daemon specific code is enclosed in `!ADB_HOST` but also sometimes with-in `__ANDROID__` guard.
30
31
32## "SMART SOCKET" and TRANSPORT
33
34A smart socket is a simple TCP socket with a smart protocol built on top of it. This is what Clients connect onto from the Host side. The Client must always initiate communication via a human readable request but the response format varies. The smart protocol is documented in [SERVICES.TXT](SERVICES.TXT).
35
36On the other side, the Server communicate with a device via a Transport. adb initially targeted devices connecting over USB, which is restricted to a fixed number of data streams. Therefore, adb multiplexes multiple byte streams over a single pipe via Transport. When devices connecting over other mechanisms (e.g. emulators over TCP) were introduced, the existing transport protocol was maintained.
37
38## THREADING MODEL and FDEVENT system
39
40At the heart of both the Server and Daemon is a main thread running an fdevent loop, which is an platform-independent abstraction over poll/epoll/WSAPoll monitoring file descriptors events. Requests and services are usually server from the main thread but some service requests result in new threads being spawned.
41
42To allow for operations to run on the Main thread, fdevent features a RunQueue combined with an interrupt fd to force polling to return.
43
44```
45+------------+    +-------------------------^
46|  RUNQUEUE  |    |                         |
47+------------+    |  POLLING (Main thread)  |
48| Function<> |    |                         |
49+------------+    |                         |
50| Function<> |    ^-^-------^-------^------^^
51+------------+      |       |       |       |
52|    ...     |      |       |       |       |
53+------------+      |       |       |       |
54|            |      |       |       |       |
55|============|      |       |       |       |
56|Interrupt fd+------+  +----+  +----+  +----+
57+------------+         fd      Socket  Pipe
58```
59
60## ASOCKET, APACKET, and AMESSAGE
61
62The asocket, apacket, and amessage constructs exist only to wrap data while it transits on a Transport. An asocket handles a stream of apackets. An apacket consists in a amessage header featuring a command (`A_SYNC`, `A_OPEN`, `A_CLSE`, `A_WRTE`, `A_OKAY`, ...) followed by a payload (find more documentation in [protocol.txt](protocol.txt). There is no `A_READ` command because an asocket is unidirectional. To model a bi-directional stream, asocket have a peer which go in the opposite direction.
63
64An asocket features a buffer where the elemental unit is an apacket. Is traffic is inbound, the buffer stores apacket until they are consumed. If the traffic is oubound, the buffer store apackets until they are sent down the wire (with `A_WRTE` commands).
65
66```
67+---------------------ASocket------------------------+
68 |                                                   |
69 | +----------------APacket Queue------------------+ |
70 | |                                               | |
71 | |            APacket     APacket     APacket    | |
72 | |          +--------+  +--------+  +--------+   | |
73 | |          |AMessage|  |AMessage|  |AMessage|   | |
74 | |          +--------+  +--------+  +--------+   | |
75 | |          |        |  |        |  |        |   | |
76 | |  .....   |        |  |        |  |        |   | |
77 | |          |  Data  |  |  Data  |  |  Data  |   | |
78 | |          |        |  |        |  |        |   | |
79 | |          |        |  |        |  |        |   | |
80 | |          +--------+  +--------+  +--------+   | |
81 | |                                               | |
82 | +-----------------------------------------------+ |
83 +---------------------------------------------------+
84```
85
86This system allows to multiplex data streams on an unique byte stream.  Without entering too much into details, the amessage fields arg1 and arg2 are used alike in the TCP protocol where local and remote ports identify an unique stream. Note that unlike TCP which feature an "unacknowledged-send window", an apacket is sent only after the previous one has been confirmed to be received.
87
88The two types of asocket (Remote and Local) differentiate between outbound and inbound traffic.
89
90## adbd <-> APPPLICATION communication
91
92This pipeline is detailed in [services.cpp](services.cpp). The JDWP extension implemented by Dalvik/ART are documented in:
93- platform/dalvik/+/master/docs/debugmon.html
94- platform/dalvik/+/master/docs/debugger.html
95