xref: /xnu-11417.121.6/libkdd/README.md (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS DistributionsKernel Data Descriptors
2*a1e26a70SApple OSS Distributions=======================
3*a1e26a70SApple OSS Distributions
4*a1e26a70SApple OSS DistributionsThis project allows for dynamic data to be passed from the kernel to userspace tools without binding them to particular version of
5*a1e26a70SApple OSS Distributionsstruct definition. The `libkdd` library provides convenient API for parsing and interpreting `kernel chunked data`.
6*a1e26a70SApple OSS Distributions
7*a1e26a70SApple OSS DistributionsThe libkdd APIs are defined in [kdd.h](./kdd.h)
8*a1e26a70SApple OSS Distributions
9*a1e26a70SApple OSS DistributionsThe `KCDATA` format
10*a1e26a70SApple OSS Distributions===================
11*a1e26a70SApple OSS Distributions
12*a1e26a70SApple OSS DistributionsThe format for data is setup in a generic format as follows
13*a1e26a70SApple OSS Distributions
14*a1e26a70SApple OSS DistributionsLayout of data structure
15*a1e26a70SApple OSS Distributions------------------------
16*a1e26a70SApple OSS Distributions
17*a1e26a70SApple OSS Distributions	|         8 - bytes         |
18*a1e26a70SApple OSS Distributions	|---------------------------|  ------ offset = 00
19*a1e26a70SApple OSS Distributions	|  type = MAGIC |  LENGTH   |  # BEGIN Header
20*a1e26a70SApple OSS Distributions	|            0              |
21*a1e26a70SApple OSS Distributions	|---------------------------|  ------ offset = 16
22*a1e26a70SApple OSS Distributions	|      type     |  size     |  # chunk header
23*a1e26a70SApple OSS Distributions	|          flags            |
24*a1e26a70SApple OSS Distributions	|---------------------------|  ------ offset = 32
25*a1e26a70SApple OSS Distributions	|           data            |  # arbitrary data (len=16)
26*a1e26a70SApple OSS Distributions	|___________data____________|
27*a1e26a70SApple OSS Distributions	|---------------------------|  ------ offset = 48
28*a1e26a70SApple OSS Distributions	|      type     |   size    |  # chunk header
29*a1e26a70SApple OSS Distributions	|          flags            |
30*a1e26a70SApple OSS Distributions	|---------------------------|  ------ offset = 64
31*a1e26a70SApple OSS Distributions	|           data            |  # arbitrary data (len=32)
32*a1e26a70SApple OSS Distributions	|           data            |
33*a1e26a70SApple OSS Distributions	|           data            |
34*a1e26a70SApple OSS Distributions	|___________data____________|
35*a1e26a70SApple OSS Distributions	|---------------------------|  ------ offset = 96
36*a1e26a70SApple OSS Distributions	|  type = END   |  size=0   |  # chunk header
37*a1e26a70SApple OSS Distributions	|            0              |
38*a1e26a70SApple OSS Distributions
39*a1e26a70SApple OSS Distributions
40*a1e26a70SApple OSS DistributionsThe type field describes what kind of data is passed. For example type = `TASK_CRASHINFO_UUID` means the following data is a uuid.
41*a1e26a70SApple OSS DistributionsThese types need to be defined in task_corpses.h for easy consumption by userspace inspection tools.
42*a1e26a70SApple OSS Distributions
43*a1e26a70SApple OSS DistributionsSome range of types is reserved for special types like ints, longs etc. A cool new functionality made possible with this
44*a1e26a70SApple OSS Distributionsextensible data format is that kernel can decide to put more information as required without requiring user space tools to
45*a1e26a70SApple OSS Distributionsre-compile to be compatible. The case of `rusage` struct versions could be introduced without breaking existing tools.
46*a1e26a70SApple OSS Distributions
47*a1e26a70SApple OSS DistributionsFeature description: Generic data with description
48*a1e26a70SApple OSS Distributions-------------------
49*a1e26a70SApple OSS DistributionsFurther more generic data with description is very much possible now. For example
50*a1e26a70SApple OSS Distributions
51*a1e26a70SApple OSS Distributions	- kcdata_add_uint64_with_description(cdatainfo, 0x700, "NUM MACH PORTS");
52*a1e26a70SApple OSS Distributions	- and more functions that allow adding description.
53*a1e26a70SApple OSS Distributions
54*a1e26a70SApple OSS DistributionsThe userspace tools can then look at the description and print the data even if they are not compiled with knowledge of the field apriori.
55*a1e26a70SApple OSS Distributions
56*a1e26a70SApple OSS Distributions	Example data:
57*a1e26a70SApple OSS Distributions	0000  57 f1 ad de 00 00 00 00 00 00 00 00 00 00 00 00  W...............
58*a1e26a70SApple OSS Distributions	0010  01 00 00 00 00 00 00 00 30 00 00 00 00 00 00 00  ........0.......
59*a1e26a70SApple OSS Distributions	0020  50 49 44 00 00 00 00 00 00 00 00 00 00 00 00 00  PID.............
60*a1e26a70SApple OSS Distributions	0030  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
61*a1e26a70SApple OSS Distributions	0040  9c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
62*a1e26a70SApple OSS Distributions	0050  01 00 00 00 00 00 00 00 30 00 00 00 00 00 00 00  ........0.......
63*a1e26a70SApple OSS Distributions	0060  50 41 52 45 4e 54 20 50 49 44 00 00 00 00 00 00  PARENT PID......
64*a1e26a70SApple OSS Distributions	0070  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
65*a1e26a70SApple OSS Distributions	0080  01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
66*a1e26a70SApple OSS Distributions	0090  ed 58 91 f1
67*a1e26a70SApple OSS Distributions
68*a1e26a70SApple OSS Distributions
69*a1e26a70SApple OSS DistributionsFeature description: Container markers for compound data
70*a1e26a70SApple OSS Distributions------------------
71*a1e26a70SApple OSS Distributions
72*a1e26a70SApple OSS DistributionsIf a given kernel data type is complex and requires adding multiple optional fields inside a container
73*a1e26a70SApple OSS Distributionsobject for a consumer to understand arbitrary data, we package it using container markers.
74*a1e26a70SApple OSS Distributions
75*a1e26a70SApple OSS DistributionsFor example, the stackshot code gathers information and describes the state of a given task with respect
76*a1e26a70SApple OSS Distributionsto many subsystems. It includes data such as io stats, vm counters, process names/flags and syscall counts.
77*a1e26a70SApple OSS Distributions
78*a1e26a70SApple OSS Distributions	kcdata_add_container_marker(kcdata_p, KCDATA_TYPE_CONTAINER_BEGIN, STACKSHOT_KCCONTAINER_TASK, task_uniqueid);
79*a1e26a70SApple OSS Distributions	// add multiple data, or add_<type>_with_description()s here
80*a1e26a70SApple OSS Distributions
81*a1e26a70SApple OSS Distributions	kcdata_add_container_marker(kcdata_p, KCDATA_TYPE_CONTAINER_END, STACKSHOT_KCCONTAINER_TASK, task_uniqueid);
82*a1e26a70SApple OSS Distributions
83*a1e26a70SApple OSS Distributions
84*a1e26a70SApple OSS DistributionsFeature description: Custom Data formats on demand
85*a1e26a70SApple OSS Distributions--------------------
86*a1e26a70SApple OSS Distributions
87*a1e26a70SApple OSS DistributionsWith the self describing nature of format, the kernel provider can describe a data type (uniquely identified by a number) and use
88*a1e26a70SApple OSS Distributionsit in the buffer for sending data. The consumer can parse the type information and have knowledge of describing incoming data.
89*a1e26a70SApple OSS DistributionsFollowing is an example of how we can describe a kernel specific struct sample_disk_io_stats in buffer.
90*a1e26a70SApple OSS Distributions
91*a1e26a70SApple OSS Distributions	struct sample_disk_io_stats {
92*a1e26a70SApple OSS Distributions	    uint64_t        disk_reads_count;
93*a1e26a70SApple OSS Distributions	    uint64_t        disk_reads_size;
94*a1e26a70SApple OSS Distributions	    uint64_t        io_priority_count[4];
95*a1e26a70SApple OSS Distributions	    uint64_t        io_priority_size;
96*a1e26a70SApple OSS Distributions	} __attribute__ ((packed));
97*a1e26a70SApple OSS Distributions
98*a1e26a70SApple OSS Distributions
99*a1e26a70SApple OSS Distributions	struct kcdata_subtype_descriptor disk_io_stats_def[] = {
100*a1e26a70SApple OSS Distributions	    {KCS_SUBTYPE_FLAGS_NONE, KC_ST_UINT64, 0 * sizeof(uint64_t), sizeof(uint64_t), "disk_reads_count"},
101*a1e26a70SApple OSS Distributions	    {KCS_SUBTYPE_FLAGS_NONE, KC_ST_UINT64, 1 * sizeof(uint64_t), sizeof(uint64_t), "disk_reads_size"},
102*a1e26a70SApple OSS Distributions	    {KCS_SUBTYPE_FLAGS_ARRAY, KC_ST_UINT64, 2 * sizeof(uint64_t), KCS_SUBTYPE_PACK_SIZE(4, sizeof(uint64_t)), "io_priority_count"},
103*a1e26a70SApple OSS Distributions	    {KCS_SUBTYPE_FLAGS_ARRAY, KC_ST_UINT64, (2 + 4) * sizeof(uint64_t), sizeof(uint64_t), "io_priority_size"},
104*a1e26a70SApple OSS Distributions	};
105*a1e26a70SApple OSS Distributions
106*a1e26a70SApple OSS DistributionsNow you can add this custom type definition into the buffer as
107*a1e26a70SApple OSS Distributions	kcdata_add_type_definition(kcdata_p, KCTYPE_SAMPLE_DISK_IO_STATS, "sample_disk_io_stats",
108*a1e26a70SApple OSS Distributions	         &disk_io_stats_def[0], sizeof(disk_io_stats_def)/sizeof(struct kcdata_subtype_descriptor));
109*a1e26a70SApple OSS Distributions
110