1 // Copyright 2010 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Helper program for the linux_dumper class, which creates a bunch of
30 // threads. The first word of each thread's stack is set to the thread
31 // id.
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h> // Must come first
35 #endif
36
37 #include <pthread.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/syscall.h>
42 #include <unistd.h>
43
44 #include "common/scoped_ptr.h"
45 #include "third_party/lss/linux_syscall_support.h"
46
47 #if defined(__ARM_EABI__)
48 #define TID_PTR_REGISTER "r3"
49 #elif defined(__aarch64__)
50 #define TID_PTR_REGISTER "x3"
51 #elif defined(__i386)
52 #define TID_PTR_REGISTER "ecx"
53 #elif defined(__x86_64)
54 #define TID_PTR_REGISTER "rcx"
55 #elif defined(__mips__)
56 #define TID_PTR_REGISTER "$1"
57 #elif defined(__riscv)
58 #define TID_PTR_REGISTER "x4"
59 #else
60 #error This test has not been ported to this platform.
61 #endif
62
thread_function(void * data)63 void* thread_function(void* data) {
64 int pipefd = *static_cast<int*>(data);
65 volatile pid_t* thread_id = new pid_t;
66 *thread_id = syscall(__NR_gettid);
67 // Signal parent that a thread has started.
68 uint8_t byte = 1;
69 if (write(pipefd, &byte, sizeof(byte)) != sizeof(byte)) {
70 perror("ERROR: parent notification failed");
71 return NULL;
72 }
73 register volatile pid_t* thread_id_ptr asm(TID_PTR_REGISTER) = thread_id;
74 while (true)
75 asm volatile ("" : : "r" (thread_id_ptr));
76 return NULL;
77 }
78
main(int argc,char * argv[])79 int main(int argc, char* argv[]) {
80 if (argc < 3) {
81 fprintf(stderr,
82 "usage: linux_dumper_unittest_helper <pipe fd> <# of threads>\n");
83 return 1;
84 }
85 int pipefd = atoi(argv[1]);
86 int num_threads = atoi(argv[2]);
87 if (num_threads < 1) {
88 fprintf(stderr, "ERROR: number of threads is 0");
89 return 1;
90 }
91 google_breakpad::scoped_array<pthread_t> threads(new pthread_t[num_threads]);
92 pthread_attr_t thread_attributes;
93 pthread_attr_init(&thread_attributes);
94 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED);
95 for (int i = 1; i < num_threads; i++) {
96 pthread_create(&threads[i], &thread_attributes, &thread_function, &pipefd);
97 }
98 thread_function(&pipefd);
99 return 0;
100 }
101