1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <stdbool.h>
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <zircon/syscalls.h>
12 #include <zircon/syscalls/debug.h>
13 #include <zircon/syscalls/exception.h>
14 
15 enum message {
16     // Force the type to be signed, avoids mismatch clashes in unittest macros.
17     MSG_FORCE_SIGNED = -1,
18     MSG_DONE,
19     MSG_PING,
20     MSG_PONG,
21     MSG_CRASH_AND_RECOVER_TEST,
22     MSG_RECOVERED_FROM_CRASH,
23     MSG_START_EXTRA_THREADS,
24     MSG_EXTRA_THREADS_STARTED,
25     MSG_GET_THREAD_HANDLE,
26     MSG_THREAD_HANDLE,
27 };
28 
29 typedef struct {
30     zx_koid_t tid;
31     zx_handle_t handle;
32 } thread_data_t;
33 
34 typedef struct {
35     // Koid of the inferior process.
36     zx_koid_t pid;
37     // Borrowed handle of the inferior process.
38     zx_handle_t inferior;
39     // Borrowed handle of the exception port.
40     zx_handle_t eport;
41     // #entries in |threads|.
42     size_t max_num_threads;
43     // The array is unsorted, and there can be holes (tid,handle = invalid).
44     thread_data_t* threads;
45 } inferior_data_t;
46 
47 extern const char* program_path;
48 
49 extern uint32_t get_uint32(char* buf);
50 
51 extern uint64_t get_uint64(char* buf);
52 
53 extern void set_uint64(char* buf, uint64_t value);
54 
55 extern uint32_t get_uint32_property(zx_handle_t handle, uint32_t prop);
56 
57 extern void send_msg(zx_handle_t handle, enum message msg);
58 
59 extern bool recv_msg(zx_handle_t handle, enum message* msg);
60 
61 extern void dump_gregs(zx_handle_t thread_handle, const zx_thread_state_general_regs_t* regs);
62 
63 extern void dump_inferior_regs(zx_handle_t thread);
64 
65 extern void read_inferior_gregs(zx_handle_t thread, zx_thread_state_general_regs_t* out);
66 
67 extern void write_inferior_gregs(zx_handle_t thread, const zx_thread_state_general_regs_t* out);
68 
69 extern size_t read_inferior_memory(zx_handle_t proc, uintptr_t vaddr, void* buf, size_t len);
70 
71 extern size_t write_inferior_memory(zx_handle_t proc, uintptr_t vaddr, const void* buf, size_t len);
72 
73 extern zx_status_t create_inferior(const char* name, int argc, const char* const* argv,
74                                    const char* const* envp, size_t hnds_count, zx_handle_t* handles,
75                                    uint32_t* ids, launchpad_t** out_launchpad);
76 
77 extern bool setup_inferior(const char* name, launchpad_t** out_lp, zx_handle_t* out_inferior,
78                            zx_handle_t* out_channel);
79 
80 extern inferior_data_t* attach_inferior(zx_handle_t inferior, zx_handle_t eport,
81                                         size_t max_threads);
82 
83 extern void detach_inferior(inferior_data_t* data, bool unbind_eport);
84 
85 extern bool start_inferior(launchpad_t* lp);
86 
87 extern bool verify_inferior_running(zx_handle_t channel);
88 
89 extern bool get_inferior_thread_handle(zx_handle_t channel, zx_handle_t* thread);
90 
91 extern bool resume_inferior(zx_handle_t inferior, zx_handle_t port, zx_koid_t tid);
92 
93 extern bool shutdown_inferior(zx_handle_t channel, zx_handle_t inferior);
94 
95 extern bool read_exception(zx_handle_t eport, zx_port_packet_t* packet);
96 
97 extern bool wait_thread_suspended(zx_handle_t proc, zx_handle_t thread, zx_handle_t eport);
98 
99 extern bool get_vdso_exec_range(uintptr_t* start, uintptr_t* end);
100