-
- return (TRUE); // no, not really, we won't return
-}
-
-#define MAX_FRAMES 1000
-
-static int pid_from_task(task_t task)
-{
- int pid = -1;
-
- if (task->bsd_info)
- pid = proc_pid(task->bsd_info);
-
- return pid;
-}
-
-boolean_t
-kdp_copyin(pmap_t p, uint64_t uaddr, void *dest, size_t size) {
- size_t rem = size;
- char *kvaddr = dest;
-
- while (rem) {
- ppnum_t upn = pmap_find_phys(p, uaddr);
- uint64_t phys_src = (upn << PAGE_SHIFT) | (uaddr & PAGE_MASK);
- uint64_t phys_dest = kvtophys((vm_offset_t)kvaddr);
- uint64_t src_rem = PAGE_SIZE - (phys_src & PAGE_MASK);
- uint64_t dst_rem = PAGE_SIZE - (phys_dest & PAGE_MASK);
- size_t cur_size = (uint32_t) MIN(src_rem, dst_rem);
- cur_size = MIN(cur_size, rem);
-
- if (upn && pmap_valid_page(upn) && phys_dest) {
- bcopy_phys(phys_src, phys_dest, cur_size);
- }
- else
- break;
- uaddr += cur_size;
- kvaddr += cur_size;
- rem -= cur_size;
- }
- return (rem == 0);
-}
-
-int
-kdp_stackshot(int pid, void *tracebuf, uint32_t tracebuf_size, unsigned trace_options, uint32_t *pbytesTraced)
-{
- char *tracepos = (char *) tracebuf;
- char *tracebound = tracepos + tracebuf_size;
- uint32_t tracebytes = 0;
- int error = 0;
-
- task_t task = TASK_NULL;
- thread_t thread = THREAD_NULL;
- int nframes = trace_options;
- thread_snapshot_t tsnap = NULL;
- unsigned framesize = 2 * sizeof(vm_offset_t);
- boolean_t dispatch_p = ((trace_options & STACKSHOT_GET_DQ) != 0);
- uint16_t dispatch_offset = (trace_options & STACKSHOT_DISPATCH_OFFSET_MASK) >> STACKSHOT_DISPATCH_OFFSET_SHIFT;
- struct task ctask;
- struct thread cthread;
-
- if ((nframes <= 0) || nframes > MAX_FRAMES)
- nframes = MAX_FRAMES;
-
- queue_iterate(&tasks, task, task_t, tasks) {
- if ((task == NULL) || (ml_nofault_copy((vm_offset_t) task, (vm_offset_t) &ctask, sizeof(struct task)) != sizeof(struct task)))
- goto error_exit;
- /* Trace everything, unless a process was specified */
- if ((pid == -1) || (pid == pid_from_task(task)))
- queue_iterate(&task->threads, thread, thread_t, task_threads){
- if ((thread == NULL) || (ml_nofault_copy((vm_offset_t) thread, (vm_offset_t) &cthread, sizeof(struct thread)) != sizeof(struct thread)))
- goto error_exit;
- if (((tracepos + 4 * sizeof(struct thread_snapshot)) > tracebound)) {
- error = -1;
- goto error_exit;
- }
-/* Populate the thread snapshot header */
- tsnap = (thread_snapshot_t) tracepos;
- tsnap->thread_id = (uint64_t) (uintptr_t)thread;
- tsnap->state = thread->state;
- tsnap->wait_event = thread->wait_event;
- tsnap->continuation = (uint64_t) (uintptr_t) thread->continuation;
-/* Add the BSD process identifiers */
- if ((tsnap->pid = pid_from_task(task)) != -1)
- proc_name_kdp(task, tsnap->p_comm, sizeof(tsnap->p_comm));
- else
- tsnap->p_comm[0] = '\0';
-
- tsnap->snapshot_magic = 0xfeedface;
- tracepos += sizeof(struct thread_snapshot);
- tsnap->ss_flags = 0;
-
- if (dispatch_p && (task != kernel_task) && (task->active) && (task->map)) {
- uint64_t dqkeyaddr = thread_dispatchqaddr(thread);
- if (dqkeyaddr != 0) {
- boolean_t task64 = task_has_64BitAddr(task);
- uint64_t dqaddr = 0;
- if (kdp_copyin(task->map->pmap, dqkeyaddr, &dqaddr, (task64 ? 8 : 4)) && (dqaddr != 0)) {
- uint64_t dqserialnumaddr = dqaddr + dispatch_offset;
- uint64_t dqserialnum = 0;
- if (kdp_copyin(task->map->pmap, dqserialnumaddr, &dqserialnum, (task64 ? 8 : 4))) {
- tsnap->ss_flags |= kHasDispatchSerial;
- *(uint64_t *)tracepos = dqserialnum;
- tracepos += 8;
- }
- }
- }
- }
-/* Call through to the machine specific trace routines
- * Frames are added past the snapshot header.
- */
- if (thread->kernel_stack != 0) {
-#if defined(__LP64__)
- tracebytes = machine_trace_thread64(thread, tracepos, tracebound, nframes, FALSE);
- tsnap->ss_flags |= kKernel64_p;
- framesize = 16;
-#else
- tracebytes = machine_trace_thread(thread, tracepos, tracebound, nframes, FALSE);
- framesize = 8;
-#endif
- }
- tsnap->nkern_frames = tracebytes/framesize;
- tracepos += tracebytes;
- tracebytes = 0;
-/* Trace user stack, if any */
- if (thread->task->map != kernel_map) {
- /* 64-bit task? */
- if (task_has_64BitAddr(thread->task)) {
- tracebytes = machine_trace_thread64(thread, tracepos, tracebound, nframes, TRUE);
- tsnap->ss_flags |= kUser64_p;
- framesize = 16;
- }
- else {
- tracebytes = machine_trace_thread(thread, tracepos, tracebound, nframes, TRUE);
- framesize = 8;
- }
- }
- tsnap->nuser_frames = tracebytes/framesize;
- tracepos += tracebytes;
- tracebytes = 0;
- }
- }
-
-error_exit:
- /* Release stack snapshot wait indicator */
- kdp_snapshot_postflight();