]> git.saurik.com Git - apple/system_cmds.git/blame - lsmp.tproj/common.h
system_cmds-880.100.5.tar.gz
[apple/system_cmds.git] / lsmp.tproj / common.h
CommitLineData
1a7e3f61 1/*
cf37c299 2 * Copyright (c) 2002-2016 Apple Inc. All rights reserved.
1a7e3f61
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23
24#ifndef system_cmds_common_h
25#define system_cmds_common_h
26
ac27e6b4 27#include <mach/mach.h>
00bf83c0 28#include <mach_debug/ipc_info.h>
887d5eed 29#include "json.h"
ac27e6b4 30
1a7e3f61
A
31#define PROC_NAME_LEN 100
32#define BUFSTR_LEN 30
ac27e6b4 33#define VOUCHER_DETAIL_MAXLEN 1024
1a7e3f61
A
34
35/* common struct to hold all configurations, static and args based */
36struct prog_configs {
37 boolean_t show_all_tasks;
38 boolean_t show_voucher_details;
39 boolean_t verbose;
40 int voucher_detail_length;
41 pid_t pid; /* if user focusing only one pid */
887d5eed 42 JSON_t json_output;
1a7e3f61
A
43};
44
45extern struct prog_configs lsmp_config;
46
ac27e6b4
A
47/* exception port information */
48struct exc_port_info {
49 mach_msg_type_number_t count;
00bf83c0 50 ipc_info_port_t ports_info[EXC_TYPES_COUNT];
ac27e6b4
A
51 exception_mask_t masks[EXC_TYPES_COUNT];
52 exception_behavior_t behaviors[EXC_TYPES_COUNT];
53 thread_state_flavor_t flavors[EXC_TYPES_COUNT];
54};
55
56/* private structure to hold thread specific information */
57struct my_per_thread_info {
58 mach_port_t thread;
59 uint32_t th_kobject;
60 uint64_t th_id;
61 char * voucher_detail;
62};
63
cf37c299
A
64/* kobject to name hash table declarations */
65#define K2N_TABLE_SIZE 256
66
67struct k2n_table_node {
68 natural_t kobject; /* kobject referred to by the name -- the key into the table */
69 ipc_info_name_t *info_name; /* info about the name that refers to the key kobject -- value of the table */
70 struct k2n_table_node *next;
71};
72
73struct k2n_table_node *k2n_table_lookup_next(struct k2n_table_node *node, natural_t kobject);
74struct k2n_table_node *k2n_table_lookup(struct k2n_table_node **table, natural_t kobject);
75
1a7e3f61
A
76/* private structure to wrap up per-task info */
77typedef struct my_per_task_info {
00bf83c0 78 task_read_t task;
1a7e3f61
A
79 pid_t pid;
80 vm_address_t task_kobject;
81 ipc_info_space_t info;
82 ipc_info_name_array_t table;
83 mach_msg_type_number_t tableCount;
84 ipc_info_tree_name_array_t tree;
85 mach_msg_type_number_t treeCount;
86 boolean_t valid; /* TRUE if all data is accurately collected */
cf37c299 87 struct k2n_table_node *k2ntable[K2N_TABLE_SIZE];
1a7e3f61 88 char processName[PROC_NAME_LEN];
ac27e6b4
A
89 struct exc_port_info exceptionInfo;
90 struct my_per_thread_info * threadInfos; /* dynamically allocated in collect_per_task_info */
91 unsigned int threadCount;
92 struct exc_port_info *threadExceptionInfos; /* this is 2 dimensional array with threadCount X struct exc_port_info of ports */
1a7e3f61
A
93} my_per_task_info_t;
94
95
96/*
97 * WARNING - these types are copied from xnu/osfmk/kern/ipc_kobject.h
98 * Need to stay in sync to print accurate results.
99 */
100#define IKOT_NONE 0
c0bbac3a
A
101#define IKOT_THREAD_CONTROL 1
102#define IKOT_TASK_CONTROL 2
1a7e3f61
A
103#define IKOT_HOST 3
104#define IKOT_HOST_PRIV 4
105#define IKOT_PROCESSOR 5
106#define IKOT_PSET 6
107#define IKOT_PSET_NAME 7
108#define IKOT_TIMER 8
109#define IKOT_PAGING_REQUEST 9
110#define IKOT_MIG 10
111#define IKOT_MEMORY_OBJECT 11
112#define IKOT_XMM_PAGER 12
113#define IKOT_XMM_KERNEL 13
114#define IKOT_XMM_REPLY 14
115#define IKOT_UND_REPLY 15
116#define IKOT_HOST_NOTIFY 16
117#define IKOT_HOST_SECURITY 17
118#define IKOT_LEDGER 18
119#define IKOT_MASTER_DEVICE 19
120#define IKOT_TASK_NAME 20
121#define IKOT_SUBSYSTEM 21
122#define IKOT_IO_DONE_QUEUE 22
123#define IKOT_SEMAPHORE 23
124#define IKOT_LOCK_SET 24
125#define IKOT_CLOCK 25
126#define IKOT_CLOCK_CTRL 26
570be395 127#define IKOT_IOKIT_IDENT 27
1a7e3f61
A
128#define IKOT_NAMED_ENTRY 28
129#define IKOT_IOKIT_CONNECT 29
130#define IKOT_IOKIT_OBJECT 30
131#define IKOT_UPL 31
132#define IKOT_MEM_OBJ_CONTROL 32
133#define IKOT_AU_SESSIONPORT 33
134#define IKOT_FILEPORT 34
135#define IKOT_LABELH 35
136#define IKOT_TASK_RESUME 36
137#define IKOT_VOUCHER 37
138#define IKOT_VOUCHER_ATTR_CONTROL 38
c0bbac3a
A
139#define IKOT_WORK_INTERVAL 39
140#define IKOT_UX_HANDLER 40
141#define IKOT_UEXT_OBJECT 41
142#define IKOT_ARCADE_REG 42
143#define IKOT_EVENTLINK 43
144#define IKOT_TASK_INSPECT 44
145#define IKOT_TASK_READ 45
146#define IKOT_THREAD_INSPECT 46
147#define IKOT_THREAD_READ 47
148#define IKOT_SUID_CRED 48
149#define IKOT_HYPERVISOR 49
150
151#define IKOT_UNKNOWN 50 /* magic catchall */
1a7e3f61
A
152#define IKOT_MAX_TYPE (IKOT_UNKNOWN+1) /* # of IKOT_ types */
153
887d5eed
A
154
155#define PORT_FLAG_TO_INDEX(flag) ( __builtin_ctz(flag) ) /* count trailing zeros */
156#define INDEX_TO_PORT_FLAG(idx) ( 1 << idx )
157typedef struct port_status_flag_info {
158 natural_t flag; /* MACH_PORT_STATUS_FLAG_* */
159 const char *compact_name; /* Single character name for compact representation */
160 const char *name; /* human readable long name */
161} port_status_flag_info_t;
162
163/*
164 * list of names for possible MACH_PORT_STATUS_FLAG_*
165 * indexed by PORT_FLAG_TO_INDEX(MACH_PORT_STATUS_FLAG_*)
166 */
167extern const port_status_flag_info_t port_status_flags[];
168
169#define _SHOW_PORT_STATUS_FLAG(flags, flag) \
170 (flags & flag) ? port_status_flags[PORT_FLAG_TO_INDEX(flag)].compact_name : "-"
1a7e3f61 171#define SHOW_PORT_STATUS_FLAGS(flags) \
887d5eed
A
172 _SHOW_PORT_STATUS_FLAG(flags, MACH_PORT_STATUS_FLAG_TEMPOWNER), \
173 _SHOW_PORT_STATUS_FLAG(flags, MACH_PORT_STATUS_FLAG_GUARDED), \
174 _SHOW_PORT_STATUS_FLAG(flags, MACH_PORT_STATUS_FLAG_STRICT_GUARD), \
175 _SHOW_PORT_STATUS_FLAG(flags, MACH_PORT_STATUS_FLAG_IMP_DONATION), \
176 _SHOW_PORT_STATUS_FLAG(flags, MACH_PORT_STATUS_FLAG_REVIVE), \
177 _SHOW_PORT_STATUS_FLAG(flags, MACH_PORT_STATUS_FLAG_TASKPTR)
1a7e3f61
A
178
179
887d5eed
A
180uint32_t show_recipe_detail(mach_voucher_attr_recipe_t recipe, char * voucher_outstr, uint32_t maxlen, JSON_t json);
181char *copy_voucher_detail(mach_port_t task, mach_port_name_t voucher, JSON_t json);
1a7e3f61
A
182
183/* mach port related functions */
184const char * kobject_name(natural_t kotype);
00bf83c0
A
185void get_receive_port_context(task_read_t taskp, mach_port_name_t portname, mach_port_context_t *context);
186int get_recieve_port_status(task_read_t taskp, mach_port_name_t portname, mach_port_info_ext_t *info);
887d5eed 187void show_task_mach_ports(my_per_task_info_t *taskinfo, uint32_t taskCount, my_per_task_info_t *allTaskInfos, JSON_t json);
1a7e3f61
A
188
189/* task and thread related helper functions */
00bf83c0 190kern_return_t collect_per_task_info(my_per_task_info_t *taskinfo, task_read_t target_task);
1a7e3f61
A
191my_per_task_info_t * allocate_taskinfo_memory(uint32_t taskCount);
192void deallocate_taskinfo_memory(my_per_task_info_t *data);
887d5eed
A
193kern_return_t print_task_exception_info(my_per_task_info_t *taskinfo, JSON_t json);
194kern_return_t print_task_threads_special_ports(my_per_task_info_t *taskinfo, JSON_t json);
1a7e3f61
A
195my_per_task_info_t * get_taskinfo_by_kobject(natural_t kobj);
196
197void get_exc_behavior_string(exception_behavior_t b, char *out_string, size_t len);
198void get_exc_mask_string(exception_mask_t m, char *out_string, size_t len);
00bf83c0
A
199kern_return_t get_taskinfo_of_receiver_by_send_right(ipc_info_name_t sendright, my_per_task_info_t **out_taskinfo, mach_port_name_t *out_recv_info);
200kern_return_t get_taskinfo_of_receiver_by_send_right_info(ipc_info_port_t sendright_info, my_per_task_info_t **out_taskinfo, mach_port_name_t *out_recv_info);
1a7e3f61
A
201
202/* basic util functions */
530d02b6 203uint32_t print_hex_data(char *outstr, uint32_t maxlen, char *prefix, char *desc, void *addr, int len);
1a7e3f61
A
204
205#endif