crasher.c revision 4f6e8d7a00cbeda1e70cc15be9c4af1018bdad53
1
2//#include <cutils/misc.h>
3
4#include <unistd.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sched.h>
9#include <errno.h>
10
11#include <signal.h>
12#include <sys/ptrace.h>
13#include <sys/wait.h>
14#include <sys/socket.h>
15
16#include <pthread.h>
17
18#include <cutils/sockets.h>
19
20void crash1(void);
21void crashnostack(void);
22
23static void debuggerd_connect()
24{
25    char tmp[1];
26    int s;
27    sprintf(tmp, "%d", gettid());
28    s = socket_local_client("android:debuggerd",
29            ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
30    if(s >= 0) {
31        read(s, tmp, 1);
32        close(s);
33    }
34}
35
36void test_call1()
37{
38    *((int*) 32) = 1;
39}
40
41void *test_thread(void *x)
42{
43    printf("crasher: thread pid=%d tid=%d\n", getpid(), gettid());
44
45    sleep(1);
46    test_call1();
47    printf("goodbye\n");
48
49    return 0;
50}
51
52void *noisy(void *x)
53{
54    char c = (unsigned) x;
55    for(;;) {
56        usleep(250*1000);
57        write(2, &c, 1);
58        if(c == 'C') *((unsigned*) 0) = 42;
59    }
60    return 0;
61}
62
63int ctest()
64{
65    pthread_t thr;
66    pthread_attr_t attr;
67    pthread_attr_init(&attr);
68    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
69    pthread_create(&thr, &attr, noisy, (void*) 'A');
70    pthread_create(&thr, &attr, noisy, (void*) 'B');
71    pthread_create(&thr, &attr, noisy, (void*) 'C');
72    for(;;) ;
73    return 0;
74}
75
76int main(int argc, char **argv)
77{
78    pthread_t thr;
79    pthread_attr_t attr;
80
81    fprintf(stderr,"crasher: " __TIME__ "!@\n");
82    fprintf(stderr,"crasher: init pid=%d tid=%d\n", getpid(), gettid());
83
84    if(argc > 1) {
85        if(!strcmp(argv[1],"nostack")) crashnostack();
86        if(!strcmp(argv[1],"ctest")) return ctest();
87        if(!strcmp(argv[1],"exit")) exit(1);
88        if(!strcmp(argv[1],"abort")) maybeabort();
89
90        pthread_attr_init(&attr);
91        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
92        pthread_create(&thr, &attr, test_thread, 0);
93        while(1) sleep(1);
94    } else {
95        crash1();
96//        *((int*) 0) = 42;
97    }
98
99    return 0;
100}
101
102void maybeabort()
103{
104    if(time(0) != 42) abort();
105}
106