* src/threads/threads-common.c (threads_print_stacktrace): Pass
[cacao.git] / src / threads / threads-common.c
1 /* src/threads/threads-common.c - machine independent thread functions
2
3    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: signal.c 7246 2007-01-29 18:49:05Z twisti $
26
27 */
28
29
30 #include "config.h"
31 #include "vm/types.h"
32
33
34 #include "native/jni.h"
35 #include "native/include/java_lang_Object.h"
36 #include "native/include/java_lang_Thread.h"
37
38 #if defined(WITH_CLASSPATH_GNU)
39 # include "native/include/java_lang_VMThread.h"
40 #endif
41
42 #include "threads/threads-common.h"
43
44 #include "threads/native/threads.h"
45
46 #include "vm/builtin.h"
47 #include "vm/stringlocal.h"
48
49 #include "vm/jit/stacktrace.h"
50
51 #include "vmcore/class.h"
52 #include "vmcore/utf8.h"
53
54
55 /* threads_create_thread *******************************************************
56
57    Creates a thread object with the given name.
58
59 *******************************************************************************/
60
61 threadobject *threads_create_thread(utf *name)
62 {
63         threadobject       *thread;
64         java_lang_Thread   *t;
65 #if defined(WITH_CLASSPATH_GNU)
66         java_lang_VMThread *vmt;
67 #endif
68
69         /* create the vm internal thread object */
70
71         thread = NEW(threadobject);
72
73         if (thread == NULL)
74                 return NULL;
75
76         /* create the java thread object */
77
78         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
79
80         if (t == NULL)
81                 return NULL;
82
83 #if defined(WITH_CLASSPATH_GNU)
84         vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
85
86         if (vmt == NULL)
87                 return NULL;
88
89         vmt->thread = t;
90         vmt->vmdata = (java_lang_Object *) thread;
91
92         t->vmThread = vmt;
93 #elif defined(WITH_CLASSPATH_CLDC1_1)
94         t->vm_thread = (java_lang_Object *) thread;
95 #endif
96
97         thread->object     = t;
98         thread->flags      = THREAD_FLAG_DAEMON;
99
100         /* set java.lang.Thread fields */
101
102         t->name     = javastring_new(name);
103 #if defined(ENABLE_JAVASE)
104         t->daemon   = true;
105 #endif
106         t->priority = NORM_PRIORITY;
107
108         /* return the thread object */
109
110         return thread;
111 }
112
113
114 /* threads_get_current_tid *****************************************************
115
116    Return the tid of the current thread.
117    
118    RETURN VALUE:
119        the current tid
120
121 *******************************************************************************/
122
123 ptrint threads_get_current_tid(void)
124 {
125         threadobject *thread;
126
127         thread = THREADOBJECT;
128
129         /* this may happen during bootstrap */
130
131         if (thread == NULL)
132                 return 0;
133
134         return (ptrint) thread->tid;
135 }
136
137
138 /* threads_dump ****************************************************************
139
140    Dumps info for all threads running in the JVM.  This function is
141    called when SIGQUIT (<ctrl>-\) is sent to CACAO.
142
143 *******************************************************************************/
144
145 void threads_dump(void)
146 {
147         threadobject     *thread;
148         java_lang_Thread *t;
149         utf              *name;
150
151         thread = mainthreadobj;
152
153         /* XXX we should stop the world here */
154
155         printf("Full thread dump CACAO "VERSION":\n");
156
157         /* iterate over all started threads */
158
159         do {
160                 /* get thread object */
161
162                 t = thread->object;
163
164                 /* the thread may be currently in initalization, don't print it */
165
166                 if (t != NULL) {
167                         /* get thread name */
168
169 #if defined(ENABLE_JAVASE)
170                         name = javastring_toutf(t->name, false);
171 #elif defined(ENABLE_JAVAME_CLDC1_1)
172                         name = t->name;
173 #endif
174
175                         printf("\n\"");
176                         utf_display_printable_ascii(name);
177                         printf("\" ");
178
179                         if (thread->flags & THREAD_FLAG_DAEMON)
180                                 printf("daemon ");
181
182 #if SIZEOF_VOID_P == 8
183                         printf("prio=%d tid=0x%016lx\n", t->priority, (ptrint) thread->tid);
184 #else
185                         printf("prio=%d tid=0x%08lx\n", t->priority, (ptrint) thread->tid);
186 #endif
187
188                         /* print trace of thread */
189
190                         threads_print_stacktrace(thread);
191                 }
192
193                 thread = thread->next;
194         } while ((thread != NULL) && (thread != mainthreadobj));
195 }
196
197
198 /* threads_print_stacktrace ****************************************************
199
200    Print the current stacktrace of the given thread.
201
202 *******************************************************************************/
203
204 void threads_print_stacktrace(threadobject *thread)
205 {
206         stackframeinfo   *sfi;
207         stacktracebuffer *stb;
208         s4                dumpsize;
209
210         /* mark start of dump memory area */
211
212         dumpsize = dump_size();
213
214         /* create a stacktrace for the current thread */
215
216 #if defined(ENABLE_THREADS)
217         sfi = thread->_stackframeinfo;
218 #else
219         sfi = _no_threads_stackframeinfo;
220 #endif
221
222         stb = stacktrace_create(sfi);
223
224         /* print stacktrace */
225
226         if (stb != NULL)
227                 stacktrace_print_trace_from_buffer(stb);
228         else {
229                 puts("\t<<No stacktrace available>>");
230                 fflush(stdout);
231         }
232
233         dump_release(dumpsize);
234 }
235
236
237 /*
238  * These are local overrides for various environment variables in Emacs.
239  * Please do not remove this and leave it at the end of the file, where
240  * Emacs will automagically detect them.
241  * ---------------------------------------------------------------------
242  * Local variables:
243  * mode: c
244  * indent-tabs-mode: t
245  * c-basic-offset: 4
246  * tab-width: 4
247  * End:
248  * vim:noexpandtab:sw=4:ts=4:
249  */