On Dec 17, 2008, at 4:22 PM, radj wrote:
Yes, looking at the stack trace can help me identify which is which but what if the threads all look the same? ex: I've launched several threads of the
same method. But that's another question.

What I needed is:
I have my own debug log, my program writes to a file. How do I print out the current thread's number from within my code that will match with crash log's
thread numbers? :D

ex:
fwrite(filePointer, "[thread:%d] a trivial sample debug log line showing the
thread number\n", ________ ); // what should go in the blank?

The crash log's thread ID is not fixed. If you run ten helper threads and nine of them exit before you crash, the surviving helper thread will always be "Thread 1" in the crash log. Any scheme that tries to predict the crash log's thread ID will be unreliable - a thread could always be created or destroyed between the time you fetch the thread ID and the time you crash.

Having said that, it is possible to get the number that the crash report would contain were the program to crash right now. The number happens to be the thread's index in the array returned by task_threads (). This may change in the future, and it's not reliable as described above, but it may be helpful for debugging in the meantime.

#include <mach/mach.h>
#include <pthread.h>

int CrashLogThreadID(pthread_t thread)
{
    thread_act_port_array_t threads;
    unsigned count, i, result;
    kern_return_t kr;
    mach_port_t mach_thread;

    kr = task_threads(mach_task_self(), &threads, &count);
    if (kr != KERN_SUCCESS) return -1;

    mach_thread = pthread_mach_thread_np(thread);
    result = -1;
    for (i = 0; i < count; i++) {
        if (threads[i] == mach_thread) result = i;
        mach_port_deallocate(mach_task_self(), threads[i]);
    }
vm_deallocate(mach_task_self(), (vm_address_t)threads, count*sizeof(threads[0]));

    return result;
}


// Test program

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void *fn(void *arg __unused) {
fprintf(stderr, "Killed by thread %d\n", CrashLogThreadID (pthread_self()));
    __builtin_trap();
    return NULL;
}

int main() {
#define COUNT 16
    int i;
    pthread_t th[COUNT];

    // Create some threads
    for (i = 0; i < COUNT; i++) {
        pthread_create_suspended_np(&th[i], NULL, fn, NULL);
    }

    // Wake a random thread to crash
    srandomdev();
    i = random() % COUNT;
    fprintf(stderr, "Resuming thread %d\n", CrashLogThreadID(th[i]));
    thread_resume(pthread_mach_thread_np(th[i]));

    pthread_join(th[i], NULL);
    return 0;
}


--
Greg Parker     [email protected]     Runtime Wrangler


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to