aboutsummaryrefslogtreecommitdiffstats
path: root/system_cmds/lsmp.tproj/task_details.c
blob: 123f8aac9694c6b596024f86cb41ba2111674d5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
 * Copyright (c) 2002-2016 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * The contents of this file constitute Original Code as defined in and
 * are subject to the Apple Public Source License Version 1.1 (the
 * "License").  You may not use this file except in compliance with the
 * License.  Please obtain a copy of the License at
 * http://www.apple.com/publicsource and read it before using this file.
 *
 * This Original Code and all software distributed under the License are
 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */

#include <unistd.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <mach_debug/ipc_info.h>
#include <stdio.h>
#include <stdlib.h>
#include <libproc.h>
#include <assert.h>

#include "common.h"

#pragma mark kobject to name hash table implementation

#if (K2N_TABLE_SIZE & (K2N_TABLE_SIZE - 1) != 0)
#error K2N_TABLE_SIZE must be a power of two
#endif

#define K2N_TABLE_MASK	(K2N_TABLE_SIZE - 1)

static uint32_t k2n_hash(natural_t kobject) {
    return (uint64_t)kobject * 2654435761 >> 32;
}

static struct k2n_table_node *k2n_table_lookup_next_internal(struct k2n_table_node *node, natural_t kobject) {
    while (node) {
        if (kobject == node->kobject)
            return node;

        node = node->next;
    }

    return NULL;
}

struct k2n_table_node *k2n_table_lookup_next(struct k2n_table_node *node, natural_t kobject) {
    if (!node) {
        return NULL;
    }
    return k2n_table_lookup_next_internal(node->next, kobject);
}

struct k2n_table_node *k2n_table_lookup(struct k2n_table_node **table, natural_t kobject) {
    uint32_t hv;
    struct k2n_table_node *node;

    hv = k2n_hash(kobject);

    node = table[hv & K2N_TABLE_MASK];

    return k2n_table_lookup_next_internal(node, kobject);
}

static void k2n_table_enter(struct k2n_table_node **table, natural_t kobject, ipc_info_name_t *info_name) {
    uint32_t hv;
    struct k2n_table_node *node;

    hv = k2n_hash(kobject);

    node = malloc(sizeof (struct k2n_table_node));
    assert(node);

    node->kobject = kobject;
    node->info_name = info_name;
    assert(kobject == info_name->iin_object);

    node->next = table[hv & K2N_TABLE_MASK];
    table[hv & K2N_TABLE_MASK] = node;
}

#pragma mark -

static my_per_task_info_t NOT_FOUND_TASK_INFO = {
    .task = NULL,
    .task_kobject = NULL,
    .pid = -1,
    .info = {0,0,0,0,0,0},
    .table = NULL,
    .tableCount = 0,
    .tree = NULL,
    .treeCount = 0,
    .valid = FALSE,
    .k2ntable = {0},
    .processName = "Unknown",
    .exceptionInfo = {0},
    .threadInfos = NULL,
    .threadCount = 0,
    .threadExceptionInfos = NULL
};

char * get_task_name_by_pid(pid_t pid);

static void proc_pid_to_name(int pid, char *pname){
    if (0 == proc_name(pid, pname, PROC_NAME_LEN)) {
        strcpy(pname, "Unknown Process");
    }
}

static my_per_task_info_t *global_taskinfo = NULL;
static uint32_t global_taskcount = 0;

my_per_task_info_t * allocate_taskinfo_memory(uint32_t taskCount)
{
    my_per_task_info_t * retval = malloc(taskCount * sizeof(my_per_task_info_t));
    if (retval) {
        bzero((void *)retval, taskCount * sizeof(my_per_task_info_t));
    }
    global_taskcount = taskCount;
    global_taskinfo = retval;
    return retval;
}

void deallocate_taskinfo_memory(my_per_task_info_t *data){
    if (data) {
        free(data);
        global_taskinfo = NULL;
        global_taskcount = 0;
    }
}

kern_return_t collect_per_task_info(my_per_task_info_t *taskinfo, task_read_t target_task)
{
    int i;
    kern_return_t ret = KERN_SUCCESS;
    unsigned int kotype = 0;
    vm_offset_t kobject = (vm_offset_t)0;

    taskinfo->task = target_task;
    pid_for_task(target_task, &taskinfo->pid);

    ret = task_get_exception_ports_info(taskinfo->task, EXC_MASK_ALL, taskinfo->exceptionInfo.masks, &taskinfo->exceptionInfo.count, taskinfo->exceptionInfo.ports_info, taskinfo->exceptionInfo.behaviors, taskinfo->exceptionInfo.flavors);

    if (ret != KERN_SUCCESS) {
        fprintf(stderr, "task_get_exception_ports_info() failed: pid:%d error: %s\n",taskinfo->pid, mach_error_string(ret));
        taskinfo->pid = 0;
    }

    /* collect threads port as well */
    taskinfo->threadCount = 0;
    thread_act_array_t threadPorts;
    ret = task_threads(taskinfo->task, &threadPorts, &taskinfo->threadCount);

    if (ret != KERN_SUCCESS) {
        fprintf(stderr, "task_threads() failed: pid:%d error: %s\n",taskinfo->pid, mach_error_string(ret));
        taskinfo->threadCount = 0;
    } else {
        /* collect the thread information */
        taskinfo->threadInfos = (struct my_per_thread_info *)malloc(sizeof(struct my_per_thread_info) * taskinfo->threadCount);
        bzero(taskinfo->threadInfos, sizeof(struct my_per_thread_info) * taskinfo->threadCount);

        /* now collect exception ports for each of those threads as well */
        taskinfo->threadExceptionInfos = (struct exc_port_info *) malloc(sizeof(struct exc_port_info) * taskinfo->threadCount);
        boolean_t found_exception = false;
        for (int i = 0; i < taskinfo->threadCount; i++) {
            unsigned th_kobject = 0;
            unsigned th_kotype = 0;
            ipc_voucher_t th_voucher = IPC_VOUCHER_NULL;
            thread_identifier_info_data_t th_info;
            mach_msg_type_number_t th_info_count = THREAD_IDENTIFIER_INFO_COUNT;
            struct exc_port_info *excinfo = &(taskinfo->threadExceptionInfos[i]);

            ret = thread_get_exception_ports_info(threadPorts[i], EXC_MASK_ALL, excinfo->masks, &excinfo->count, excinfo->ports_info, excinfo->behaviors, excinfo->flavors);
            if (ret != KERN_SUCCESS){
                fprintf(stderr, "thread_get_exception_ports_info() failed: pid: %d thread: %d error %s\n", taskinfo->pid, threadPorts[i], mach_error_string(ret));
            }

            if (excinfo->count != 0) {
                found_exception = true;
            }

            taskinfo->threadInfos[i].thread = threadPorts[i];

            if (KERN_SUCCESS == mach_port_kernel_object(mach_task_self(), threadPorts[i], &th_kotype, &th_kobject)) {
                taskinfo->threadInfos[i].th_kobject = th_kobject;
                if (KERN_SUCCESS == thread_info(threadPorts[i], THREAD_IDENTIFIER_INFO, (thread_info_t)&th_info, &th_info_count)) {
                    taskinfo->threadInfos[i].th_id = th_info.thread_id;
                }
            }

            if (KERN_SUCCESS == thread_get_mach_voucher(threadPorts[i], 0, &th_voucher) && th_voucher != IPC_VOUCHER_NULL) {
                char *detail = copy_voucher_detail(mach_task_self(), th_voucher, NULL);
                taskinfo->threadInfos[i].voucher_detail = strndup(detail, VOUCHER_DETAIL_MAXLEN);
                free(detail);

                mach_port_deallocate(mach_task_self(), th_voucher);
            }

            mach_port_deallocate(mach_task_self(), threadPorts[i]);
            threadPorts[i] = MACH_PORT_NULL;
        }

        if (found_exception == false) {
            free(taskinfo->threadExceptionInfos);
            taskinfo->threadExceptionInfos = NULL;
        }

    }

    vm_deallocate(mach_task_self(), threadPorts, taskinfo->threadCount * sizeof(thread_act_t));
    threadPorts = NULL;

    ret = mach_port_space_info(target_task, &taskinfo->info, &taskinfo->table, &taskinfo->tableCount, &taskinfo->tree, &taskinfo->treeCount);

    if (ret != KERN_SUCCESS) {
        fprintf(stderr, "mach_port_space_info() failed: pid:%d error: %s\n",taskinfo->pid, mach_error_string(ret));
        taskinfo->pid = 0;
        return ret;
    }

    bzero(taskinfo->k2ntable, K2N_TABLE_SIZE * sizeof (struct k2n_table_node *));
    for (i = 0; i < taskinfo->tableCount; i++) {
        k2n_table_enter(taskinfo->k2ntable, taskinfo->table[i].iin_object, &taskinfo->table[i]);
    }

    proc_pid_to_name(taskinfo->pid, taskinfo->processName);

    ret = mach_port_kernel_object(mach_task_self(), taskinfo->task, &kotype, (unsigned *)&kobject);
    
    /* Now that we are using read ports, kotype should be checked against IKOT_TASK_READ */
    if (ret == KERN_SUCCESS && kotype == IKOT_TASK_READ) {
        taskinfo->task_kobject = kobject;
        taskinfo->valid = TRUE;
    }

    return ret;
}

void get_exc_behavior_string(exception_behavior_t b, char *out_string, size_t len)
{
    out_string[0]='\0';

    if (b & MACH_EXCEPTION_CODES)
        strlcat(out_string, "MACH +", len);
    switch (b & ~MACH_EXCEPTION_CODES) {
        case EXCEPTION_DEFAULT:
            strlcat(out_string, " DEFAULT", len);
            break;
        case EXCEPTION_STATE:
            strlcat(out_string, " STATE", len);
            break;
        case EXCEPTION_STATE_IDENTITY:
            strlcat(out_string, " IDENTITY", len);
            break;
        default:
            strlcat(out_string, " UNKNOWN", len);
    }
}

void get_exc_mask_string(exception_mask_t m, char *out_string, size_t len)
{
    out_string[0]='\0';

    if (m & (1<<EXC_BAD_ACCESS))
        strlcat(out_string, " BAD_ACCESS", len);
    if (m & (1<<EXC_BAD_INSTRUCTION))
        strlcat(out_string," BAD_INSTRUCTION", len);
    if (m & (1<<EXC_ARITHMETIC))
        strlcat(out_string," ARITHMETIC", len);
    if (m & (1<<EXC_EMULATION))
        strlcat(out_string," EMULATION", len);
    if (m & (1<<EXC_SOFTWARE))
        strlcat(out_string," SOFTWARE", len);
    if (m & (1<<EXC_BREAKPOINT))
        strlcat(out_string," BREAKPOINT", len);
    if (m & (1<<EXC_SYSCALL))
        strlcat(out_string," SYSCALL", len);
    if (m & (1<<EXC_MACH_SYSCALL))
        strlcat(out_string," MACH_SYSCALL", len);
    if (m & (1<<EXC_RPC_ALERT))
        strlcat(out_string," RPC_ALERT", len);
    if (m & (1<<EXC_CRASH))
        strlcat(out_string," CRASH", len);
    if (m & (1<<EXC_RESOURCE))
        strlcat(out_string," RESOURCE", len);
    if (m & (1<<EXC_GUARD))
        strlcat(out_string," GUARD", len);
}

kern_return_t print_task_exception_info(my_per_task_info_t *taskinfo, JSON_t json)
{

    char behavior_string[30];
    char mask_string[200];

    JSON_KEY(json, exception_ports);
    JSON_ARRAY_BEGIN(json);

    boolean_t header_required = TRUE;
    for (int i = 0; i < taskinfo->exceptionInfo.count; i++) {
        if (taskinfo->exceptionInfo.ports_info[i].iip_port_object != 0) {
            my_per_task_info_t * _found_task;
            
            if (header_required) {

                printf("    exc_port_object    receiver_task    flavor  <behaviors>           mask   \n");
                header_required = FALSE;
            }
            get_exc_behavior_string(taskinfo->exceptionInfo.behaviors[i], behavior_string, sizeof(behavior_string));
            get_exc_mask_string(taskinfo->exceptionInfo.masks[i], mask_string, sizeof(mask_string));

            JSON_OBJECT_BEGIN(json);
            JSON_OBJECT_SET(json, port_object, "0x%08x", taskinfo->exceptionInfo.ports_info[i].iip_port_object);
            JSON_OBJECT_SET(json, receiver_object, "0x%08x", taskinfo->exceptionInfo.ports_info[i].iip_receiver_object);
            JSON_OBJECT_SET(json, flavor, "0x%03x", taskinfo->exceptionInfo.flavors[i]);
            JSON_OBJECT_SET(json, behavior, "%s", behavior_string);
            JSON_OBJECT_SET(json, mask, "%s", mask_string);
            JSON_OBJECT_END(json); // exception port
            
            _found_task = get_taskinfo_by_kobject((natural_t)taskinfo->exceptionInfo.ports_info[i].iip_receiver_object);
            
            printf("    0x%08x         (%d) %s       0x%03x  <%s>         %s  \n",
                   taskinfo->exceptionInfo.ports_info[i].iip_port_object,
                   _found_task->pid,
                   _found_task->processName,
                   taskinfo->exceptionInfo.flavors[i], behavior_string, mask_string);
        }

    }

    JSON_ARRAY_END(json); // exception ports

    return KERN_SUCCESS;
}


kern_return_t print_task_threads_special_ports(my_per_task_info_t *taskinfo, JSON_t json)
{
    kern_return_t kret = KERN_SUCCESS;
    mach_msg_type_number_t threadcount = taskinfo->threadCount;
    boolean_t header_required = TRUE;
    boolean_t newline_required = TRUE;
    struct my_per_thread_info * info = NULL;

    JSON_KEY(json, threads);
    JSON_ARRAY_BEGIN(json);

    for (int i = 0; i < threadcount; i++) {
        JSON_OBJECT_BEGIN(json);

        info = &taskinfo->threadInfos[i];
        if (header_required) {
            printf("Thread_KObject  Thread-ID     Port Description.");
            header_required = FALSE;
        }

        if (newline_required) {
            printf("\n");
        }
        newline_required = TRUE;

        if (info->th_kobject != 0) {
            /* TODO: Should print tid and stuff */
            printf("0x%08x       ", info->th_kobject);
            printf("0x%llx  ", info->th_id);

            JSON_OBJECT_SET(json, kobject, "0x%08x", info->th_kobject);
            JSON_OBJECT_SET(json, tid, "0x%llx", info->th_id);
        }

        if (info->voucher_detail != NULL) {
            /* TODO: include voucher detail in JSON */
            printf("%s\n", info->voucher_detail);
        }

        JSON_KEY(json, exception_ports);
        JSON_ARRAY_BEGIN(json);

        /* print the thread exception ports also */
        if (taskinfo->threadExceptionInfos != NULL)
        {

            struct exc_port_info *excinfo = &taskinfo->threadExceptionInfos[i];
            char behavior_string[30];
            char mask_string[200];

            if (excinfo->count > 0) {
                boolean_t header_required = TRUE;
                for (int i = 0; i < excinfo->count; i++) {
                    JSON_OBJECT_BEGIN(json);

                    if (excinfo->ports_info[i].iip_port_object != 0) {
                        if (header_required) {
                            printf("\n    exc_port_object    exc_port_receiver    flavor <behaviors>           mask   -> name    owner\n");
                            header_required = FALSE;
                        }
                        get_exc_behavior_string(excinfo->behaviors[i], behavior_string, sizeof(behavior_string));
                        get_exc_mask_string(excinfo->masks[i], mask_string, sizeof(mask_string));

                        JSON_OBJECT_SET(json, port_object, "0x%08x", excinfo->ports_info[i].iip_port_object);
                        JSON_OBJECT_SET(json, receiver_object, "0x%08x", excinfo->ports_info[i].iip_receiver_object);
                        JSON_OBJECT_SET(json, flavor, "0x%03x", excinfo->flavors[i]);
                        JSON_OBJECT_SET(json, behavior, "%s", behavior_string);
                        JSON_OBJECT_SET(json, mask, "%s", mask_string);

                        printf("    0x%08x  0x%08x  0x%03x  <%s>           %s  " , excinfo->ports_info[i].iip_port_object, excinfo->ports_info[i].iip_receiver_object, excinfo->flavors[i], behavior_string, mask_string);

                        my_per_task_info_t *recv_holder_taskinfo;
                        mach_port_name_t recv_name = MACH_PORT_NULL;
                        if (KERN_SUCCESS == get_taskinfo_of_receiver_by_send_right_info(excinfo->ports_info[i], &recv_holder_taskinfo, &recv_name)) {
                            JSON_OBJECT_SET(json, name, "0x%08x", recv_name);
                            JSON_OBJECT_SET(json, ipc-object, "0x%08x", excinfo->ports_info[i].iip_port_object);
                            JSON_OBJECT_SET(json, pid, %d, recv_holder_taskinfo->pid);
                            JSON_OBJECT_SET(json, process, "%s", recv_holder_taskinfo->processName);

                            printf("   -> 0x%08x  0x%08x  (%d) %s\n",
                                    recv_name,
                                    excinfo->ports_info[i].iip_port_object,
                                    recv_holder_taskinfo->pid,
                                    recv_holder_taskinfo->processName);
                        } else {
                            fprintf(stderr, "failed to find");
                        }

                        printf("\n");

                    }
                    JSON_OBJECT_END(json); // exception port
                }
            }
        }
        JSON_ARRAY_END(json); // exception ports
        JSON_OBJECT_END(json); // thread
    }

    JSON_ARRAY_END(json); // threads
    printf("\n");
    return kret;
}

char * get_task_name_by_pid(pid_t pid) {
    char * retval = "Unknown";
    for (int i = 0; i < global_taskcount; i++) {
        if (pid == global_taskinfo[i].pid) {
            return global_taskinfo[i].processName;
        }
    }
    return retval;
}

my_per_task_info_t * get_taskinfo_by_kobject(natural_t kobj) {
    my_per_task_info_t *retval = &NOT_FOUND_TASK_INFO;
    for (int j = 0; j < global_taskcount; j++) {
        if (global_taskinfo[j].task_kobject == kobj) {
            retval = &global_taskinfo[j];
            break;
        }
    }
    return retval;
}

static kern_return_t _get_taskinfo_of_receiver_by_send_right(natural_t kobject, my_per_task_info_t **out_taskinfo, mach_port_name_t *out_recv_info)
{
    *out_taskinfo = &NOT_FOUND_TASK_INFO;
    struct k2n_table_node *k2nnode;

    for (unsigned int j = 0; j < global_taskcount; j++) {
        if ((k2nnode = k2n_table_lookup(global_taskinfo[j].k2ntable, kobject))) {
            assert(k2nnode->info_name->iin_object == kobject);

            if (k2nnode->info_name->iin_type & MACH_PORT_TYPE_RECEIVE) {
                *out_taskinfo = &global_taskinfo[j];
                *out_recv_info = k2nnode->info_name->iin_name;
                return KERN_SUCCESS;
            }
        }
    }

    return KERN_FAILURE;
}

kern_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)
{
    return _get_taskinfo_of_receiver_by_send_right(sendright.iin_object, out_taskinfo, out_recv_info);
}

kern_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)
{
    return _get_taskinfo_of_receiver_by_send_right(sendright_info.iip_port_object, out_taskinfo, out_recv_info);
}