1 /* src/native/jvmti/cacaodbg.c - contains entry points for debugging support
4 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
5 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
6 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
7 J. Wenninger, Institut f. Computersprachen - TU Wien
9 This file is part of CACAO.
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2, or (at
14 your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 Contact: cacao@complang.tuwien.ac.at
28 Authors: Martin Platter
30 Changes: Edwin Steiner
33 $Id: cacao.c,v 3.165 2006/01/03 23:44:38 twisti Exp $
37 #include "native/jvmti/jvmti.h"
38 #include "native/jvmti/cacaodbg.h"
39 #include "native/jvmti/cacaodbgserver.h"
40 #include "native/jvmti/dbg.h"
42 #include "vm/loader.h"
43 #include "vm/exceptions.h"
44 #include "vm/builtin.h"
45 #include "vm/jit/asmpart.h"
46 #include "vm/stringlocal.h"
47 #include "toolbox/logging.h"
48 #include "threads/native/threads.h"
50 #include <sys/types.h>
57 /* allthreads *****************************************************************
59 Gets an array of threadobjects of all threads
61 *******************************************************************************/
62 jvmtiError allthreads (jint * threads_count_ptr, threadobject*** threads_ptr) {
64 threadobject *thread, **tthreads;
66 #if defined(ENABLE_THREADS)
67 tthreads = MNEW(threadobject*, (sizeof(threadobject*) * cnt));
69 thread = mainthreadobj;
71 if(thread->o.thread != NULL) {
74 /* count and copy only live threads */
76 MREALLOC(tthreads,threadobject*,cnt,cnt+8);
82 thread = thread->info.prev;
84 /* repeat until we got the pointer to the mainthread twice */
85 } while (mainthreadobj != thread);
89 *threads_count_ptr = i;
90 *threads_ptr = tthreads;
92 return JVMTI_ERROR_NONE;
94 return JVMTI_ERROR_NOT_AVAILABLE;
99 /* getcurrentthread ***********************************************************
101 Get jthread structure of current thread.
103 *******************************************************************************/
104 jthread getcurrentthread() {
105 return (jthread)(threads_get_current_threadobject())->o.thread;
110 /* brktablecreator*************************************************************
112 helper function to enlarge the breakpoint table if needed
114 *******************************************************************************/
116 static void brktablecreator() {
118 struct brkpts *jvmtibrkpt;
120 jvmtibrkpt = &dbgcom->jvmtibrkpt;;
121 if (jvmtibrkpt->size == 0) {
122 jvmtibrkpt->brk = MNEW(struct _brkpt, 16);
123 memset(jvmtibrkpt->brk, 0, sizeof(struct _brkpt)*16);
124 jvmtibrkpt->size = 16;
125 jvmtibrkpt->num = BEGINUSERBRK;
127 jvmtibrkpt->size += 16;
128 tmp = jvmtibrkpt->brk;
129 jvmtibrkpt->brk = MNEW(struct _brkpt, jvmtibrkpt->size);
130 memset(jvmtibrkpt->brk, 0, sizeof(struct _brkpt)*(jvmtibrkpt->size));
131 memcpy((void*)jvmtibrkpt->brk,(void*)tmp,jvmtibrkpt->size);
132 MFREE(tmp,struct _brkpt,jvmtibrkpt->size-16);
137 /* setsysbrkpt ****************************************************************
139 sets a system breakpoint in breakpoint table and calls set breakpoint
141 *******************************************************************************/
143 void setsysbrkpt(int sysbrk, void* addr) {
144 struct brkpts *jvmtibrkpt;
146 pthread_mutex_lock(&dbgcomlock);
147 jvmtibrkpt = &dbgcom->jvmtibrkpt;;
149 if (jvmtibrkpt->size == jvmtibrkpt->num)
152 assert (sysbrk < BEGINUSERBRK);
153 jvmtibrkpt->brk[sysbrk].addr = addr;
156 dbgcom->setbrkpt = true;
157 dbgcom->brkaddr = addr;
158 jvmtibrkpt->brk[sysbrk].orig = dbgcom->brkorig;
159 pthread_mutex_unlock(&dbgcomlock);
161 /* call cacaodbgserver */
164 fprintf (stderr,"setsysbrk %d %X done\n",sysbrk, addr);
168 /* addbrkpt *******************************************************************
170 adds a breakpoint to breakpoint table and calls set breakpoint
172 *******************************************************************************/
174 void addbrkpt(void* addr, jmethodID method, jlocation location) {
175 struct brkpts *jvmtibrkpt;
177 pthread_mutex_lock(&dbgcomlock);
178 jvmtibrkpt = &dbgcom->jvmtibrkpt;;
180 if (jvmtibrkpt->size == jvmtibrkpt->num)
183 assert (jvmtibrkpt->size > jvmtibrkpt->num);
184 fprintf (stderr,"add brk add: %X\n",addr);
185 jvmtibrkpt->brk[jvmtibrkpt->num].addr = addr;
186 jvmtibrkpt->brk[jvmtibrkpt->num].method = method;
187 jvmtibrkpt->brk[jvmtibrkpt->num].location = location;
189 /* todo: set breakpoint */
190 /* jvmtibrkpt.brk[jvmtibrkpt.num].orig = */
192 pthread_mutex_unlock(&dbgcomlock);
194 fprintf (stderr,"add brk done\n");
198 /* setup_jdwp_thread *****************************************************
200 Helper function to start JDWP threads
202 *******************************************************************************/
204 void setup_jdwp_thread(char* transport) {
205 java_objectheader *o;
210 /* new gnu.classpath.jdwp.Jdwp() */
211 class = load_class_from_sysloader(
212 utf_new_char("gnu.classpath.jdwp.Jdwp"));
214 throw_main_exception_exit();
216 o = builtin_new(class);
219 throw_main_exception_exit();
221 m = class_resolveclassmethod(class,
224 class_java_lang_Object,
227 throw_main_exception_exit();
231 /* configure(transport,NULL) */
232 m = class_resolveclassmethod(
233 class, utf_new_char("configure"),
234 utf_new_char("(Ljava/lang/String;)V"),
235 class_java_lang_Object,
238 s = javastring_new_from_ascii(&transport[1]);
240 vm_call_method(m,o,s);
243 throw_main_exception_exit();
246 /* _doInitialization */
247 m = class_resolveclassmethod(class,
248 utf_new_char("_doInitialization"),
254 throw_main_exception_exit();
259 /* cacaobreakpointhandler **********************************************************
261 handles breakpoints. called by cacaodbgserver.
263 *******************************************************************************/
265 void cacaobreakpointhandler() {
267 genericEventData data;
270 /* XXX to be continued :-) */
272 fprintf(stderr,"breakpoint handler called\n");
273 log_text(" - signal %d", ev.signal);
276 /* search the breakpoint that has been triggered */
278 while ((ev.ip!=dbgcom->jvmtibrkpt.brk[i].addr) && (i<dbgcom->jvmtibrkpt.num)) i++;
280 fprintf(stderr,"cacaodbglisten SIGTRAP switch after while loop i %d\n",i);
283 case SETTHREADOBJECTBRK:
284 /* threads_set_current_threadobject */
285 fprintf(stderr,"IP %X == threads_set_current_threadobject\n",ev.ip);
286 data.ev=JVMTI_EVENT_THREAD_START;
290 if ((i >= BEGINUSERBRK) && (i<dbgcom->jvmtibrkpt.num)) {
291 log_text("todo: user defined breakpoints are not handled yet");
293 log_text("breakpoint not handled - continue anyway");
297 log_text("debugger process SIGQUIT");
298 data.ev=JVMTI_EVENT_VM_DEATH;
302 log_text("signal not handled");
308 * These are local overrides for various environment variables in Emacs.
309 * Please do not remove this and leave it at the end of the file, where
310 * Emacs will automagically detect them.
311 * ---------------------------------------------------------------------
314 * indent-tabs-mode: t
318 * vim:noexpandtab:sw=4:ts=4: