2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <kern/assert.h>
30 #include <mach/mach_types.h>
31 #include <mach/boolean.h>
32 #include <mach/vm_param.h>
33 #include <kern/kern_types.h>
34 #include <kern/mach_param.h>
35 #include <kern/thread.h>
36 #include <kern/task.h>
37 #include <kern/kern_cdata.h>
38 #include <kern/kalloc.h>
39 #include <mach/mach_vm.h>
43 * The format for data is setup in a generic format as follows
45 * Layout of data structure:
48 * | type = MAGIC | LENGTH |
53 * |___________data____________|
56 * |___________data____________|
57 * | type = END | size=0 |
61 * The type field describes what kind of data is passed. For example type = TASK_CRASHINFO_UUID means the following data is a uuid.
62 * These types need to be defined in task_corpses.h for easy consumption by userspace inspection tools.
64 * Some range of types is reserved for special types like ints, longs etc. A cool new functionality made possible with this
65 * extensible data format is that kernel can decide to put more information as required without requiring user space tools to
66 * re-compile to be compatible. The case of rusage struct versions could be introduced without breaking existing tools.
68 * Feature description: Generic data with description
70 * Further more generic data with description is very much possible now. For example
72 * - kcdata_add_uint64_with_description(cdatainfo, 0x700, "NUM MACH PORTS");
73 * - and more functions that allow adding description.
74 * The userspace tools can then look at the description and print the data even if they are not compiled with knowledge of the field apriori.
77 * 0000 57 f1 ad de 00 00 00 00 00 00 00 00 00 00 00 00 W...............
78 * 0010 01 00 00 00 00 00 00 00 30 00 00 00 00 00 00 00 ........0.......
79 * 0020 50 49 44 00 00 00 00 00 00 00 00 00 00 00 00 00 PID.............
80 * 0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
81 * 0040 9c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
82 * 0050 01 00 00 00 00 00 00 00 30 00 00 00 00 00 00 00 ........0.......
83 * 0060 50 41 52 45 4e 54 20 50 49 44 00 00 00 00 00 00 PARENT PID......
84 * 0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
85 * 0080 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
88 * Feature description: Container markers for compound data
90 * If a given kernel data type is complex and requires adding multiple optional fields inside a container
91 * object for a consumer to understand arbitrary data, we package it using container markers.
93 * For example, the stackshot code gathers information and describes the state of a given task with respect
94 * to many subsystems. It includes data such as io stats, vm counters, process names/flags and syscall counts.
96 * kcdata_add_container_marker(kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN, STACKSHOT_KCCONTAINER_TASK, task_uniqueid);
97 * // add multiple data, or add_<type>_with_description()s here
99 * kcdata_add_container_marker(kcdata_p, KCDATA_TYPE_CONTAINER_END, STACKSHOT_KCCONTAINER_TASK, task_uniqueid);
101 * Feature description: Custom Data formats on demand
102 * --------------------
103 * With the self describing nature of format, the kernel provider can describe a data type (uniquely identified by a number) and use
104 * it in the buffer for sending data. The consumer can parse the type information and have knowledge of describing incoming data.
105 * Following is an example of how we can describe a kernel specific struct sample_disk_io_stats in buffer.
107 * struct sample_disk_io_stats {
108 * uint64_t disk_reads_count;
109 * uint64_t disk_reads_size;
110 * uint64_t io_priority_count[4];
111 * uint64_t io_priority_size;
112 * } __attribute__ ((packed));
115 * struct kcdata_subtype_descriptor disk_io_stats_def[] = {
116 * {KCS_SUBTYPE_FLAGS_NONE, KC_ST_UINT64, 0 * sizeof(uint64_t), sizeof(uint64_t), "disk_reads_count"},
117 * {KCS_SUBTYPE_FLAGS_NONE, KC_ST_UINT64, 1 * sizeof(uint64_t), sizeof(uint64_t), "disk_reads_size"},
118 * {KCS_SUBTYPE_FLAGS_ARRAY, KC_ST_UINT64, 2 * sizeof(uint64_t), KCS_SUBTYPE_PACK_SIZE(4, sizeof(uint64_t)), "io_priority_count"},
119 * {KCS_SUBTYPE_FLAGS_ARRAY, KC_ST_UINT64, (2 + 4) * sizeof(uint64_t), sizeof(uint64_t), "io_priority_size"},
122 * Now you can add this custom type definition into the buffer as
123 * kcdata_add_type_definition(kcdata_p, KCTYPE_SAMPLE_DISK_IO_STATS, "sample_disk_io_stats",
124 * &disk_io_stats_def[0], sizeof(disk_io_stats_def)/sizeof(struct kcdata_subtype_descriptor));
128 static kern_return_t
kcdata_get_memory_addr_with_flavor(kcdata_descriptor_t data
, uint32_t type
, uint32_t size
, uint64_t flags
, mach_vm_address_t
*user_addr
);
130 kcdata_descriptor_t
kcdata_memory_alloc_init(mach_vm_address_t buffer_addr_p
, unsigned data_type
, unsigned size
, unsigned flags
)
132 kcdata_descriptor_t data
= NULL
;
133 mach_vm_address_t user_addr
= 0;
135 data
= kalloc(sizeof(struct kcdata_descriptor
));
139 bzero(data
, sizeof(struct kcdata_descriptor
));
140 data
->kcd_addr_begin
= buffer_addr_p
;
141 data
->kcd_addr_end
= buffer_addr_p
;
142 data
->kcd_flags
= (flags
& KCFLAG_USE_COPYOUT
)? KCFLAG_USE_COPYOUT
: KCFLAG_USE_MEMCOPY
;
143 data
->kcd_length
= size
;
145 /* Initialize the BEGIN header */
146 if (KERN_SUCCESS
!= kcdata_get_memory_addr(data
, data_type
, 0, &user_addr
)){
147 kcdata_memory_destroy(data
);
154 kern_return_t
kcdata_memory_static_init(kcdata_descriptor_t data
, mach_vm_address_t buffer_addr_p
, unsigned data_type
, unsigned size
, unsigned flags
)
156 mach_vm_address_t user_addr
= 0;
159 return KERN_INVALID_ARGUMENT
;
161 bzero(data
, sizeof(struct kcdata_descriptor
));
162 data
->kcd_addr_begin
= buffer_addr_p
;
163 data
->kcd_addr_end
= buffer_addr_p
;
164 data
->kcd_flags
= (flags
& KCFLAG_USE_COPYOUT
)? KCFLAG_USE_COPYOUT
: KCFLAG_USE_MEMCOPY
;
165 data
->kcd_length
= size
;
167 /* Initialize the BEGIN header */
168 return kcdata_get_memory_addr(data
, data_type
, 0, &user_addr
);
171 uint64_t kcdata_memory_get_used_bytes(kcdata_descriptor_t kcd
)
174 return ((uint64_t)kcd
->kcd_addr_end
- (uint64_t)kcd
->kcd_addr_begin
) + sizeof(struct kcdata_item
);
178 * Free up the memory associated with kcdata
180 kern_return_t
kcdata_memory_destroy(kcdata_descriptor_t data
)
183 return KERN_INVALID_ARGUMENT
;
187 * data->kcd_addr_begin points to memory in not tracked by
188 * kcdata lib. So not clearing that here.
190 kfree(data
, sizeof(struct kcdata_descriptor
));
197 * Routine: kcdata_get_memory_addr
198 * Desc: get memory address in the userspace memory for corpse info
199 * NOTE: The caller is responsible to zero the resulting memory or
200 * user other means to mark memory if it has failed populating the
201 * data in middle of operation.
202 * params: data - pointer describing the crash info allocation
203 * type - type of data to be put. See corpse.h for defined types
204 * size - size requested. The header describes this size
205 * returns: mach_vm_address_t address in user memory for copyout().
207 kern_return_t
kcdata_get_memory_addr(
208 kcdata_descriptor_t data
,
211 mach_vm_address_t
*user_addr
)
213 return kcdata_get_memory_addr_with_flavor(data
, type
, size
, 0, user_addr
);
217 * Routine: kcdata_get_memory_addr_with_flavor
218 * Desc: internal function with flags field. See documentation for kcdata_get_memory_addr for details
221 static kern_return_t
kcdata_get_memory_addr_with_flavor(
222 kcdata_descriptor_t data
,
226 mach_vm_address_t
*user_addr
)
228 struct kcdata_item info
;
231 if (user_addr
== NULL
|| data
== NULL
) {
232 return KERN_INVALID_ARGUMENT
;
235 /* make sure 16 byte aligned */
237 size
+= (0x10 - (size
& 0xf));
240 bzero(&info
, sizeof(info
));
241 KCDATA_ITEM_TYPE(&info
) = type
;
242 KCDATA_ITEM_SIZE(&info
) = size
;
243 KCDATA_ITEM_FLAGS(&info
) = flags
;
244 total_size
= size
+ sizeof(info
);
246 /* check available memory, including trailer size for KCDATA_TYPE_BUFFER_END */
247 if (data
->kcd_length
< ((data
->kcd_addr_end
- data
->kcd_addr_begin
) + total_size
+ sizeof(info
))) {
248 return KERN_RESOURCE_SHORTAGE
;
251 if (data
->kcd_flags
& KCFLAG_USE_COPYOUT
) {
252 if (copyout(&info
, data
->kcd_addr_end
, sizeof(info
)))
253 return KERN_NO_ACCESS
;
255 memcpy((void *)data
->kcd_addr_end
, &info
, sizeof(info
));
258 data
->kcd_addr_end
+= sizeof(info
);
259 *user_addr
= data
->kcd_addr_end
;
260 data
->kcd_addr_end
+= size
;
262 /* setup the end header as well */
263 bzero(&info
, sizeof(info
));
264 KCDATA_ITEM_TYPE(&info
) = KCDATA_TYPE_BUFFER_END
;
265 KCDATA_ITEM_SIZE(&info
) = 0;
267 if (data
->kcd_flags
& KCFLAG_USE_COPYOUT
) {
268 if (copyout(&info
, data
->kcd_addr_end
, sizeof(info
)))
269 return KERN_NO_ACCESS
;
271 memcpy((void *)data
->kcd_addr_end
, &info
, sizeof(info
));
278 * Routine: kcdata_get_memory_addr_for_array
279 * Desc: get memory address in the userspace memory for corpse info
280 * NOTE: The caller is responsible to zero the resulting memory or
281 * user other means to mark memory if it has failed populating the
282 * data in middle of operation.
283 * params: data - pointer describing the crash info allocation
284 * type_of_element - type of data to be put. See kern_cdata.h for defined types
285 * size_of_element - size of element. The header describes this size
286 * count - num of elements in array.
287 * returns: mach_vm_address_t address in user memory for copyout().
290 kern_return_t
kcdata_get_memory_addr_for_array(
291 kcdata_descriptor_t data
,
292 uint32_t type_of_element
,
293 uint32_t size_of_element
,
295 mach_vm_address_t
*user_addr
)
297 uint64_t flags
= type_of_element
;
298 flags
= (flags
<< 32) | count
;
299 uint32_t total_size
= count
* size_of_element
;
300 return kcdata_get_memory_addr_with_flavor(data
, KCDATA_TYPE_ARRAY
, total_size
, flags
, user_addr
);
304 * Routine: kcdata_add_container_marker
305 * Desc: Add a container marker in the buffer for type and identifier.
306 * params: data - pointer describing the crash info allocation
307 * header_type - one of (KCDATA_TYPE_CONTAINER_BEGIN ,KCDATA_TYPE_CONTAINER_END)
308 * container_type - type of data to be put. See kern_cdata.h for defined types
309 * identifier - unique identifier. This is required to match nested containers.
310 * returns: return value of kcdata_get_memory_addr()
313 kern_return_t
kcdata_add_container_marker(
314 kcdata_descriptor_t data
,
315 uint32_t header_type
,
316 uint32_t container_type
,
319 mach_vm_address_t user_addr
;
321 assert(header_type
== KCDATA_TYPE_CONTAINER_END
|| header_type
== KCDATA_TYPE_CONTAINER_BEGIN
);
322 uint32_t data_size
= (header_type
== KCDATA_TYPE_CONTAINER_BEGIN
)? sizeof(uint32_t): 0;
323 kr
= kcdata_get_memory_addr_with_flavor(data
, header_type
, data_size
, identifier
, &user_addr
);
324 if (kr
!= KERN_SUCCESS
)
328 kr
= kcdata_memcpy(data
, user_addr
, &container_type
, data_size
);
333 * Routine: kcdata_memcpy
334 * Desc: a common function to copy data out based on either copyout or memcopy flags
335 * params: data - pointer describing the kcdata buffer
336 * dst_addr - destination address
337 * src_addr - source address
338 * size - size in bytes to copy.
339 * returns: KERN_NO_ACCESS if copyout fails.
342 kern_return_t
kcdata_memcpy(kcdata_descriptor_t data
, mach_vm_address_t dst_addr
, void *src_addr
, uint32_t size
)
344 if (data
->kcd_flags
& KCFLAG_USE_COPYOUT
) {
345 if (copyout(src_addr
, dst_addr
, size
))
346 return KERN_NO_ACCESS
;
348 memcpy((void *)dst_addr
, src_addr
, size
);
354 * Routine: kcdata_add_type_definition
355 * Desc: add type definition to kcdata buffer.
356 * see feature description in documentation above.
357 * params: data - pointer describing the kcdata buffer
358 * type_id - unique type identifier for this data
359 * type_name - a string of max KCDATA_DESC_MAXLEN size for name of type
360 * elements_array - address to descriptors for each field in struct
361 * elements_count - count of how many fields are there in struct.
362 * returns: return code from kcdata_get_memory_addr in case of failure.
365 kern_return_t
kcdata_add_type_definition(
366 kcdata_descriptor_t data
,
369 struct kcdata_subtype_descriptor
*elements_array_addr
,
370 uint32_t elements_count
)
372 kern_return_t kr
= KERN_SUCCESS
;
373 struct kcdata_type_definition kc_type_definition
;
374 mach_vm_address_t user_addr
;
375 uint32_t total_size
= sizeof(struct kcdata_type_definition
);
377 if (strnlen(type_name
, KCDATA_DESC_MAXLEN
+ 1) >= KCDATA_DESC_MAXLEN
)
378 return KERN_INVALID_ARGUMENT
;
379 strlcpy(&kc_type_definition
.kct_name
[0], type_name
, KCDATA_DESC_MAXLEN
);
380 kc_type_definition
.kct_num_elements
= elements_count
;
381 kc_type_definition
.kct_type_identifier
= type_id
;
383 total_size
+= elements_count
* sizeof(struct kcdata_subtype_descriptor
);
384 if (KERN_SUCCESS
!= (kr
= kcdata_get_memory_addr_with_flavor(data
, KCDATA_TYPE_TYPEDEFINTION
, total_size
, 0, &user_addr
)))
386 if (KERN_SUCCESS
!= (kr
= kcdata_memcpy(data
, user_addr
, (void *)&kc_type_definition
, sizeof(struct kcdata_type_definition
))))
388 user_addr
+= sizeof(struct kcdata_type_definition
);
389 if (KERN_SUCCESS
!= (kr
= kcdata_memcpy(data
, user_addr
, (void *)elements_array_addr
, elements_count
* sizeof(struct kcdata_subtype_descriptor
))))
396 /* Internal structs for convenience */
397 struct _uint64_with_description_data
{
398 char desc
[KCDATA_DESC_MAXLEN
];
402 struct _uint32_with_description_data
{
403 char desc
[KCDATA_DESC_MAXLEN
];
409 kern_return_t
kcdata_add_uint64_with_description(
410 kcdata_descriptor_t data_desc
,
412 const char *description
)
414 if (strnlen(description
, KCDATA_DESC_MAXLEN
+ 1) >= KCDATA_DESC_MAXLEN
)
415 return KERN_INVALID_ARGUMENT
;
417 kern_return_t kr
= 0;
418 mach_vm_address_t user_addr
;
419 struct _uint64_with_description_data save_data
;
420 const uint64_t size_req
= sizeof(save_data
);
421 bzero(&save_data
, size_req
);
423 strlcpy(&(save_data
.desc
[0]), description
, sizeof(save_data
.desc
));
424 save_data
.data
= data
;
426 kr
= kcdata_get_memory_addr(data_desc
, KCDATA_TYPE_UINT64_DESC
, size_req
, &user_addr
);
427 if (kr
!= KERN_SUCCESS
)
430 if (data_desc
->kcd_flags
& KCFLAG_USE_COPYOUT
) {
431 if (copyout(&save_data
, user_addr
, size_req
))
432 return KERN_NO_ACCESS
;
434 memcpy((void *)user_addr
, &save_data
, size_req
);
439 kern_return_t
kcdata_add_uint32_with_description(
440 kcdata_descriptor_t data_desc
,
442 const char *description
)
444 assert(strlen(description
) < KCDATA_DESC_MAXLEN
);
445 if (strnlen(description
, KCDATA_DESC_MAXLEN
+ 1) >= KCDATA_DESC_MAXLEN
)
446 return KERN_INVALID_ARGUMENT
;
447 kern_return_t kr
= 0;
448 mach_vm_address_t user_addr
;
449 struct _uint32_with_description_data save_data
;
450 const uint64_t size_req
= sizeof(save_data
);
452 bzero(&save_data
, size_req
);
453 strlcpy(&(save_data
.desc
[0]), description
, sizeof(save_data
.desc
));
454 save_data
.data
= data
;
456 kr
= kcdata_get_memory_addr(data_desc
, KCDATA_TYPE_UINT32_DESC
, size_req
, &user_addr
);
457 if (kr
!= KERN_SUCCESS
)
459 if (data_desc
->kcd_flags
& KCFLAG_USE_COPYOUT
) {
460 if (copyout(&save_data
, user_addr
, size_req
))
461 return KERN_NO_ACCESS
;
463 memcpy((void *)user_addr
, &save_data
, size_req
);
469 /* end buffer management api */