blob: 535c1176e52915ee0e090641163b09ffa2a3ffec [file] [log] [blame]
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project89c1feb2008-12-17 18:03:55 -080016
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070017/*
18 * Main interpreter entry point and support functions.
19 *
20 * The entry point selects the "standard" or "debug" interpreter and
21 * facilitates switching between them. The standard interpreter may
22 * use the "fast" or "portable" implementation.
23 *
24 * Some debugger support functions are included here. Ideally their
25 * entire existence would be "#ifdef WITH_DEBUGGER", but we're not that
26 * aggressive in other parts of the code yet.
27 */
28#include "Dalvik.h"
29#include "interp/InterpDefs.h"
30
31
32/*
33 * ===========================================================================
34 * Debugger support
35 * ===========================================================================
36 */
37
Andy McFadden96516932009-10-28 17:39:02 -070038// fwd
39static BreakpointSet* dvmBreakpointSetAlloc(void);
40static void dvmBreakpointSetFree(BreakpointSet* pSet);
41
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070042/*
Andy McFadden96516932009-10-28 17:39:02 -070043 * Initialize global breakpoint structures.
44 */
45bool dvmBreakpointStartup(void)
46{
47#ifdef WITH_DEBUGGER
48 gDvm.breakpointSet = dvmBreakpointSetAlloc();
49 return (gDvm.breakpointSet != NULL);
50#else
51 return true;
52#endif
53}
54
55/*
56 * Free resources.
57 */
58void dvmBreakpointShutdown(void)
59{
60#ifdef WITH_DEBUGGER
61 dvmBreakpointSetFree(gDvm.breakpointSet);
62#endif
63}
64
65
66#ifdef WITH_DEBUGGER
67/*
68 * This represents a breakpoint inserted in the instruction stream.
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -070069 *
Andy McFadden96516932009-10-28 17:39:02 -070070 * The debugger may ask us to create the same breakpoint multiple times.
71 * We only remove the breakpoint when the last instance is cleared.
72 */
73typedef struct {
Andy McFaddend22748a2010-04-22 17:08:11 -070074 Method* method; /* method we're associated with */
Andy McFadden96516932009-10-28 17:39:02 -070075 u2* addr; /* absolute memory address */
76 u1 originalOpCode; /* original 8-bit opcode value */
77 int setCount; /* #of times this breakpoint was set */
78} Breakpoint;
79
80/*
81 * Set of breakpoints.
82 */
83struct BreakpointSet {
84 /* grab lock before reading or writing anything else in here */
85 pthread_mutex_t lock;
86
87 /* vector of breakpoint structures */
88 int alloc;
89 int count;
90 Breakpoint* breakpoints;
91};
92
93/*
94 * Initialize a BreakpointSet. Initially empty.
95 */
96static BreakpointSet* dvmBreakpointSetAlloc(void)
97{
98 BreakpointSet* pSet = (BreakpointSet*) calloc(1, sizeof(*pSet));
99
100 dvmInitMutex(&pSet->lock);
101 /* leave the rest zeroed -- will alloc on first use */
102
103 return pSet;
104}
105
106/*
107 * Free storage associated with a BreakpointSet.
108 */
109static void dvmBreakpointSetFree(BreakpointSet* pSet)
110{
111 if (pSet == NULL)
112 return;
113
114 free(pSet->breakpoints);
115 free(pSet);
116}
117
118/*
119 * Lock the breakpoint set.
Andy McFaddend22748a2010-04-22 17:08:11 -0700120 *
121 * It's not currently necessary to switch to VMWAIT in the event of
122 * contention, because nothing in here can block. However, it's possible
123 * that the bytecode-updater code could become fancier in the future, so
124 * we do the trylock dance as a bit of future-proofing.
Andy McFadden96516932009-10-28 17:39:02 -0700125 */
126static void dvmBreakpointSetLock(BreakpointSet* pSet)
127{
Andy McFaddend22748a2010-04-22 17:08:11 -0700128 if (dvmTryLockMutex(&pSet->lock) != 0) {
129 Thread* self = dvmThreadSelf();
130 int oldStatus = dvmChangeStatus(self, THREAD_VMWAIT);
131 dvmLockMutex(&pSet->lock);
132 dvmChangeStatus(self, oldStatus);
133 }
Andy McFadden96516932009-10-28 17:39:02 -0700134}
135
136/*
137 * Unlock the breakpoint set.
138 */
139static void dvmBreakpointSetUnlock(BreakpointSet* pSet)
140{
141 dvmUnlockMutex(&pSet->lock);
142}
143
144/*
145 * Return the #of breakpoints.
146 */
147static int dvmBreakpointSetCount(const BreakpointSet* pSet)
148{
149 return pSet->count;
150}
151
152/*
153 * See if we already have an entry for this address.
154 *
155 * The BreakpointSet's lock must be acquired before calling here.
156 *
157 * Returns the index of the breakpoint entry, or -1 if not found.
158 */
159static int dvmBreakpointSetFind(const BreakpointSet* pSet, const u2* addr)
160{
161 int i;
162
163 for (i = 0; i < pSet->count; i++) {
164 Breakpoint* pBreak = &pSet->breakpoints[i];
165 if (pBreak->addr == addr)
166 return i;
167 }
168
169 return -1;
170}
171
172/*
173 * Retrieve the opcode that was originally at the specified location.
174 *
175 * The BreakpointSet's lock must be acquired before calling here.
176 *
177 * Returns "true" with the opcode in *pOrig on success.
178 */
179static bool dvmBreakpointSetOriginalOpCode(const BreakpointSet* pSet,
180 const u2* addr, u1* pOrig)
181{
182 int idx = dvmBreakpointSetFind(pSet, addr);
183 if (idx < 0)
184 return false;
185
186 *pOrig = pSet->breakpoints[idx].originalOpCode;
187 return true;
188}
189
190/*
Andy McFaddenda9dc842010-05-03 16:11:20 -0700191 * Check the opcode. If it's a "magic" NOP, indicating the start of
192 * switch or array data in the instruction stream, we don't want to set
193 * a breakpoint.
194 *
195 * This can happen because the line number information dx generates
196 * associates the switch data with the switch statement's line number,
197 * and some debuggers put breakpoints at every address associated with
198 * a given line. The result is that the breakpoint stomps on the NOP
199 * instruction that doubles as a data table magic number, and an explicit
200 * check in the interpreter results in an exception being thrown.
201 *
202 * We don't want to simply refuse to add the breakpoint to the table,
203 * because that confuses the housekeeping. We don't want to reject the
204 * debugger's event request, and we want to be sure that there's exactly
205 * one un-set operation for every set op.
206 */
207static bool instructionIsMagicNop(const u2* addr)
208{
209 u2 curVal = *addr;
210 return ((curVal & 0xff) == OP_NOP && (curVal >> 8) != 0);
211}
212
213/*
Andy McFadden96516932009-10-28 17:39:02 -0700214 * Add a breakpoint at a specific address. If the address is already
215 * present in the table, this just increments the count.
216 *
217 * For a new entry, this will extract and preserve the current opcode from
218 * the instruction stream, and replace it with a breakpoint opcode.
219 *
220 * The BreakpointSet's lock must be acquired before calling here.
221 *
222 * Returns "true" on success.
223 */
224static bool dvmBreakpointSetAdd(BreakpointSet* pSet, Method* method,
225 unsigned int instrOffset)
226{
227 const int kBreakpointGrowth = 10;
228 const u2* addr = method->insns + instrOffset;
229 int idx = dvmBreakpointSetFind(pSet, addr);
230 Breakpoint* pBreak;
231
232 if (idx < 0) {
233 if (pSet->count == pSet->alloc) {
234 int newSize = pSet->alloc + kBreakpointGrowth;
235 Breakpoint* newVec;
236
237 LOGV("+++ increasing breakpoint set size to %d\n", newSize);
238
239 /* pSet->breakpoints will be NULL on first entry */
240 newVec = realloc(pSet->breakpoints, newSize * sizeof(Breakpoint));
241 if (newVec == NULL)
242 return false;
243
244 pSet->breakpoints = newVec;
245 pSet->alloc = newSize;
246 }
247
248 pBreak = &pSet->breakpoints[pSet->count++];
Andy McFaddend22748a2010-04-22 17:08:11 -0700249 pBreak->method = method;
Andy McFadden96516932009-10-28 17:39:02 -0700250 pBreak->addr = (u2*)addr;
251 pBreak->originalOpCode = *(u1*)addr;
252 pBreak->setCount = 1;
253
254 /*
255 * Change the opcode. We must ensure that the BreakpointSet
256 * updates happen before we change the opcode.
Andy McFaddend22748a2010-04-22 17:08:11 -0700257 *
258 * If the method has not been verified, we do NOT insert the
259 * breakpoint yet, since that will screw up the verifier. The
260 * debugger is allowed to insert breakpoints in unverified code,
261 * but since we don't execute unverified code we don't need to
262 * alter the bytecode yet.
263 *
Andy McFaddenc7a12b22010-04-30 10:08:55 -0700264 * The class init code will "flush" all pending opcode writes
265 * before verification completes.
Andy McFadden96516932009-10-28 17:39:02 -0700266 */
Andy McFadden96516932009-10-28 17:39:02 -0700267 assert(*(u1*)addr != OP_BREAKPOINT);
Andy McFaddend22748a2010-04-22 17:08:11 -0700268 if (dvmIsClassVerified(method->clazz)) {
269 LOGV("Class %s verified, adding breakpoint at %p\n",
270 method->clazz->descriptor, addr);
Andy McFaddenda9dc842010-05-03 16:11:20 -0700271 if (instructionIsMagicNop(addr)) {
272 LOGV("Refusing to set breakpoint on %04x at %s.%s + 0x%x\n",
273 *addr, method->clazz->descriptor, method->name,
274 instrOffset);
275 } else {
276 MEM_BARRIER();
277 dvmDexChangeDex1(method->clazz->pDvmDex, (u1*)addr,
278 OP_BREAKPOINT);
279 }
Andy McFaddend22748a2010-04-22 17:08:11 -0700280 } else {
281 LOGV("Class %s NOT verified, deferring breakpoint at %p\n",
282 method->clazz->descriptor, addr);
283 }
Andy McFadden96516932009-10-28 17:39:02 -0700284 } else {
Andy McFaddenc7a12b22010-04-30 10:08:55 -0700285 /*
286 * Breakpoint already exists, just increase the count.
287 */
Andy McFadden96516932009-10-28 17:39:02 -0700288 pBreak = &pSet->breakpoints[idx];
289 pBreak->setCount++;
Andy McFadden96516932009-10-28 17:39:02 -0700290 }
291
292 return true;
293}
294
295/*
296 * Remove one instance of the specified breakpoint. When the count
297 * reaches zero, the entry is removed from the table, and the original
298 * opcode is restored.
299 *
300 * The BreakpointSet's lock must be acquired before calling here.
301 */
302static void dvmBreakpointSetRemove(BreakpointSet* pSet, Method* method,
303 unsigned int instrOffset)
304{
305 const u2* addr = method->insns + instrOffset;
306 int idx = dvmBreakpointSetFind(pSet, addr);
307
308 if (idx < 0) {
309 /* breakpoint not found in set -- unexpected */
310 if (*(u1*)addr == OP_BREAKPOINT) {
Andy McFaddenda9dc842010-05-03 16:11:20 -0700311 LOGE("Unable to restore breakpoint opcode (%s.%s +0x%x)\n",
Andy McFadden96516932009-10-28 17:39:02 -0700312 method->clazz->descriptor, method->name, instrOffset);
313 dvmAbort();
314 } else {
Andy McFaddenda9dc842010-05-03 16:11:20 -0700315 LOGW("Breakpoint was already restored? (%s.%s +0x%x)\n",
Andy McFadden96516932009-10-28 17:39:02 -0700316 method->clazz->descriptor, method->name, instrOffset);
317 }
318 } else {
319 Breakpoint* pBreak = &pSet->breakpoints[idx];
320 if (pBreak->setCount == 1) {
321 /*
322 * Must restore opcode before removing set entry.
Andy McFaddend22748a2010-04-22 17:08:11 -0700323 *
324 * If the breakpoint was never flushed, we could be ovewriting
325 * a value with the same value. Not a problem, though we
326 * could end up causing a copy-on-write here when we didn't
327 * need to. (Not worth worrying about.)
Andy McFadden96516932009-10-28 17:39:02 -0700328 */
329 dvmDexChangeDex1(method->clazz->pDvmDex, (u1*)addr,
330 pBreak->originalOpCode);
331 MEM_BARRIER();
332
333 if (idx != pSet->count-1) {
334 /* shift down */
335 memmove(&pSet->breakpoints[idx], &pSet->breakpoints[idx+1],
336 (pSet->count-1 - idx) * sizeof(pSet->breakpoints[0]));
337 }
338 pSet->count--;
339 pSet->breakpoints[pSet->count].addr = (u2*) 0xdecadead; // debug
340 } else {
341 pBreak->setCount--;
342 assert(pBreak->setCount > 0);
343 }
344 }
345}
346
347/*
Andy McFaddend22748a2010-04-22 17:08:11 -0700348 * Flush any breakpoints associated with methods in "clazz". We want to
349 * change the opcode, which might not have happened when the breakpoint
350 * was initially set because the class was in the process of being
351 * verified.
Andy McFadden96516932009-10-28 17:39:02 -0700352 *
353 * The BreakpointSet's lock must be acquired before calling here.
354 */
Andy McFaddend22748a2010-04-22 17:08:11 -0700355static void dvmBreakpointSetFlush(BreakpointSet* pSet, ClassObject* clazz)
Andy McFadden96516932009-10-28 17:39:02 -0700356{
Andy McFadden96516932009-10-28 17:39:02 -0700357 int i;
358 for (i = 0; i < pSet->count; i++) {
359 Breakpoint* pBreak = &pSet->breakpoints[i];
Andy McFaddend22748a2010-04-22 17:08:11 -0700360 if (pBreak->method->clazz == clazz) {
361 /*
362 * The breakpoint is associated with a method in this class.
363 * It might already be there or it might not; either way,
364 * flush it out.
365 */
366 LOGV("Flushing breakpoint at %p for %s\n",
367 pBreak->addr, clazz->descriptor);
Andy McFaddenda9dc842010-05-03 16:11:20 -0700368 if (instructionIsMagicNop(pBreak->addr)) {
369 const Method* method = pBreak->method;
370 LOGV("Refusing to flush breakpoint on %04x at %s.%s + 0x%x\n",
371 *pBreak->addr, method->clazz->descriptor,
372 method->name, pBreak->addr - method->insns);
373 } else {
374 dvmDexChangeDex1(clazz->pDvmDex, (u1*)pBreak->addr,
375 OP_BREAKPOINT);
376 }
Andy McFadden96516932009-10-28 17:39:02 -0700377 }
378 }
379}
Andy McFadden96516932009-10-28 17:39:02 -0700380#endif /*WITH_DEBUGGER*/
381
382
383/*
384 * Do any debugger-attach-time initialization.
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700385 */
386void dvmInitBreakpoints(void)
387{
388#ifdef WITH_DEBUGGER
Andy McFadden96516932009-10-28 17:39:02 -0700389 /* quick sanity check */
390 BreakpointSet* pSet = gDvm.breakpointSet;
391 dvmBreakpointSetLock(pSet);
392 if (dvmBreakpointSetCount(pSet) != 0) {
393 LOGW("WARNING: %d leftover breakpoints\n", dvmBreakpointSetCount(pSet));
394 /* generally not good, but we can keep going */
395 }
396 dvmBreakpointSetUnlock(pSet);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700397#else
398 assert(false);
399#endif
400}
401
402/*
403 * Add an address to the list, putting it in the first non-empty slot.
404 *
405 * Sometimes the debugger likes to add two entries for one breakpoint.
406 * We add two entries here, so that we get the right behavior when it's
407 * removed twice.
408 *
409 * This will only be run from the JDWP thread, and it will happen while
410 * we are updating the event list, which is synchronized. We're guaranteed
411 * to be the only one adding entries, and the lock ensures that nobody
412 * will be trying to remove them while we're in here.
413 *
414 * "addr" is the absolute address of the breakpoint bytecode.
415 */
Andy McFadden96516932009-10-28 17:39:02 -0700416void dvmAddBreakAddr(Method* method, unsigned int instrOffset)
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700417{
418#ifdef WITH_DEBUGGER
Andy McFadden96516932009-10-28 17:39:02 -0700419 BreakpointSet* pSet = gDvm.breakpointSet;
420 dvmBreakpointSetLock(pSet);
421 dvmBreakpointSetAdd(pSet, method, instrOffset);
422 dvmBreakpointSetUnlock(pSet);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700423#else
424 assert(false);
425#endif
426}
427
428/*
429 * Remove an address from the list by setting the entry to NULL.
430 *
431 * This can be called from the JDWP thread (because the debugger has
432 * cancelled the breakpoint) or from an event thread (because it's a
433 * single-shot breakpoint, e.g. "run to line"). We only get here as
434 * the result of removing an entry from the event list, which is
435 * synchronized, so it should not be possible for two threads to be
436 * updating breakpoints at the same time.
437 */
Andy McFadden96516932009-10-28 17:39:02 -0700438void dvmClearBreakAddr(Method* method, unsigned int instrOffset)
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700439{
440#ifdef WITH_DEBUGGER
Andy McFadden96516932009-10-28 17:39:02 -0700441 BreakpointSet* pSet = gDvm.breakpointSet;
442 dvmBreakpointSetLock(pSet);
443 dvmBreakpointSetRemove(pSet, method, instrOffset);
444 dvmBreakpointSetUnlock(pSet);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700445
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700446#else
447 assert(false);
448#endif
449}
450
Andy McFadden96516932009-10-28 17:39:02 -0700451#ifdef WITH_DEBUGGER
452/*
453 * Get the original opcode from under a breakpoint.
454 */
455u1 dvmGetOriginalOpCode(const u2* addr)
456{
457 BreakpointSet* pSet = gDvm.breakpointSet;
458 u1 orig = 0;
459
460 dvmBreakpointSetLock(pSet);
461 if (!dvmBreakpointSetOriginalOpCode(pSet, addr, &orig)) {
462 orig = *(u1*)addr;
463 if (orig == OP_BREAKPOINT) {
464 LOGE("GLITCH: can't find breakpoint, opcode is still set\n");
465 dvmAbort();
466 }
467 }
468 dvmBreakpointSetUnlock(pSet);
469
470 return orig;
471}
472
473/*
Andy McFaddend22748a2010-04-22 17:08:11 -0700474 * Flush any breakpoints associated with methods in "clazz".
Andy McFadden96516932009-10-28 17:39:02 -0700475 *
Andy McFaddend22748a2010-04-22 17:08:11 -0700476 * We don't want to modify the bytecode of a method before the verifier
477 * gets a chance to look at it, so we postpone opcode replacement until
478 * after verification completes.
Andy McFadden96516932009-10-28 17:39:02 -0700479 */
Andy McFaddend22748a2010-04-22 17:08:11 -0700480void dvmFlushBreakpoints(ClassObject* clazz)
Andy McFadden96516932009-10-28 17:39:02 -0700481{
482 BreakpointSet* pSet = gDvm.breakpointSet;
483
Andy McFaddend22748a2010-04-22 17:08:11 -0700484 if (pSet == NULL)
485 return;
486
487 assert(dvmIsClassVerified(clazz));
Andy McFadden96516932009-10-28 17:39:02 -0700488 dvmBreakpointSetLock(pSet);
Andy McFaddend22748a2010-04-22 17:08:11 -0700489 dvmBreakpointSetFlush(pSet, clazz);
Andy McFadden96516932009-10-28 17:39:02 -0700490 dvmBreakpointSetUnlock(pSet);
491}
492#endif
493
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700494/*
495 * Add a single step event. Currently this is a global item.
496 *
497 * We set up some initial values based on the thread's current state. This
498 * won't work well if the thread is running, so it's up to the caller to
499 * verify that it's suspended.
500 *
501 * This is only called from the JDWP thread.
502 */
503bool dvmAddSingleStep(Thread* thread, int size, int depth)
504{
505#ifdef WITH_DEBUGGER
506 StepControl* pCtrl = &gDvm.stepControl;
507
508 if (pCtrl->active && thread != pCtrl->thread) {
509 LOGW("WARNING: single-step active for %p; adding %p\n",
510 pCtrl->thread, thread);
511
512 /*
513 * Keep going, overwriting previous. This can happen if you
514 * suspend a thread in Object.wait, hit the single-step key, then
515 * switch to another thread and do the same thing again.
516 * The first thread's step is still pending.
517 *
518 * TODO: consider making single-step per-thread. Adds to the
519 * overhead, but could be useful in rare situations.
520 */
521 }
522
523 pCtrl->size = size;
524 pCtrl->depth = depth;
525 pCtrl->thread = thread;
526
527 /*
528 * We may be stepping into or over method calls, or running until we
529 * return from the current method. To make this work we need to track
530 * the current line, current method, and current stack depth. We need
531 * to be checking these after most instructions, notably those that
532 * call methods, return from methods, or are on a different line from the
533 * previous instruction.
534 *
535 * We have to start with a snapshot of the current state. If we're in
536 * an interpreted method, everything we need is in the current frame. If
537 * we're in a native method, possibly with some extra JNI frames pushed
538 * on by PushLocalFrame, we want to use the topmost native method.
539 */
540 const StackSaveArea* saveArea;
541 void* fp;
542 void* prevFp = NULL;
Ben Cheng38329f52009-07-07 14:19:20 -0700543
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700544 for (fp = thread->curFrame; fp != NULL; fp = saveArea->prevFrame) {
545 const Method* method;
546
547 saveArea = SAVEAREA_FROM_FP(fp);
548 method = saveArea->method;
549
550 if (!dvmIsBreakFrame(fp) && !dvmIsNativeMethod(method))
551 break;
552 prevFp = fp;
553 }
554 if (fp == NULL) {
555 LOGW("Unexpected: step req in native-only threadid=%d\n",
556 thread->threadId);
557 return false;
558 }
559 if (prevFp != NULL) {
560 /*
561 * First interpreted frame wasn't the one at the bottom. Break
562 * frames are only inserted when calling from native->interp, so we
563 * don't need to worry about one being here.
564 */
565 LOGV("##### init step while in native method\n");
566 fp = prevFp;
567 assert(!dvmIsBreakFrame(fp));
568 assert(dvmIsNativeMethod(SAVEAREA_FROM_FP(fp)->method));
569 saveArea = SAVEAREA_FROM_FP(fp);
570 }
571
572 /*
573 * Pull the goodies out. "xtra.currentPc" should be accurate since
574 * we update it on every instruction while the debugger is connected.
575 */
576 pCtrl->method = saveArea->method;
577 // Clear out any old address set
578 if (pCtrl->pAddressSet != NULL) {
579 // (discard const)
580 free((void *)pCtrl->pAddressSet);
581 pCtrl->pAddressSet = NULL;
582 }
583 if (dvmIsNativeMethod(pCtrl->method)) {
584 pCtrl->line = -1;
585 } else {
586 pCtrl->line = dvmLineNumFromPC(saveArea->method,
587 saveArea->xtra.currentPc - saveArea->method->insns);
Ben Cheng38329f52009-07-07 14:19:20 -0700588 pCtrl->pAddressSet
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700589 = dvmAddressSetForLine(saveArea->method, pCtrl->line);
590 }
591 pCtrl->frameDepth = dvmComputeVagueFrameDepth(thread, thread->curFrame);
592 pCtrl->active = true;
593
594 LOGV("##### step init: thread=%p meth=%p '%s' line=%d frameDepth=%d depth=%s size=%s\n",
595 pCtrl->thread, pCtrl->method, pCtrl->method->name,
596 pCtrl->line, pCtrl->frameDepth,
597 dvmJdwpStepDepthStr(pCtrl->depth),
598 dvmJdwpStepSizeStr(pCtrl->size));
599
600 return true;
601#else
602 assert(false);
603 return false;
604#endif
605}
606
607/*
608 * Disable a single step event.
609 */
610void dvmClearSingleStep(Thread* thread)
611{
612#ifdef WITH_DEBUGGER
613 UNUSED_PARAMETER(thread);
614
615 gDvm.stepControl.active = false;
616#else
617 assert(false);
618#endif
619}
620
621
622/*
623 * Recover the "this" pointer from the current interpreted method. "this"
624 * is always in "in0" for non-static methods.
625 *
626 * The "ins" start at (#of registers - #of ins). Note in0 != v0.
627 *
628 * This works because "dx" guarantees that it will work. It's probably
629 * fairly common to have a virtual method that doesn't use its "this"
630 * pointer, in which case we're potentially wasting a register. However,
631 * the debugger doesn't treat "this" as just another argument. For
632 * example, events (such as breakpoints) can be enabled for specific
633 * values of "this". There is also a separate StackFrame.ThisObject call
634 * in JDWP that is expected to work for any non-native non-static method.
635 *
636 * Because we need it when setting up debugger event filters, we want to
637 * be able to do this quickly.
638 */
639Object* dvmGetThisPtr(const Method* method, const u4* fp)
640{
641 if (dvmIsStaticMethod(method))
642 return NULL;
643 return (Object*)fp[method->registersSize - method->insSize];
644}
645
646
647#if defined(WITH_TRACKREF_CHECKS)
648/*
649 * Verify that all internally-tracked references have been released. If
650 * they haven't, print them and abort the VM.
651 *
652 * "debugTrackedRefStart" indicates how many refs were on the list when
653 * we were first invoked.
654 */
655void dvmInterpCheckTrackedRefs(Thread* self, const Method* method,
656 int debugTrackedRefStart)
657{
658 if (dvmReferenceTableEntries(&self->internalLocalRefTable)
659 != (size_t) debugTrackedRefStart)
660 {
661 char* desc;
662 Object** top;
663 int count;
664
665 count = dvmReferenceTableEntries(&self->internalLocalRefTable);
666
667 LOGE("TRACK: unreleased internal reference (prev=%d total=%d)\n",
668 debugTrackedRefStart, count);
669 desc = dexProtoCopyMethodDescriptor(&method->prototype);
670 LOGE(" current method is %s.%s %s\n", method->clazz->descriptor,
671 method->name, desc);
672 free(desc);
673 top = self->internalLocalRefTable.table + debugTrackedRefStart;
674 while (top < self->internalLocalRefTable.nextEntry) {
675 LOGE(" %p (%s)\n",
676 *top,
677 ((*top)->clazz != NULL) ? (*top)->clazz->descriptor : "");
678 top++;
679 }
680 dvmDumpThread(self, false);
681
682 dvmAbort();
683 }
684 //LOGI("TRACK OK\n");
685}
686#endif
687
688
689#ifdef LOG_INSTR
690/*
691 * Dump the v-registers. Sent to the ILOG log tag.
692 */
693void dvmDumpRegs(const Method* method, const u4* framePtr, bool inOnly)
694{
695 int i, localCount;
696
697 localCount = method->registersSize - method->insSize;
698
699 LOG(LOG_VERBOSE, LOG_TAG"i", "Registers (fp=%p):\n", framePtr);
700 for (i = method->registersSize-1; i >= 0; i--) {
701 if (i >= localCount) {
702 LOG(LOG_VERBOSE, LOG_TAG"i", " v%-2d in%-2d : 0x%08x\n",
703 i, i-localCount, framePtr[i]);
704 } else {
705 if (inOnly) {
706 LOG(LOG_VERBOSE, LOG_TAG"i", " [...]\n");
707 break;
708 }
709 const char* name = "";
710 int j;
711#if 0 // "locals" structure has changed -- need to rewrite this
712 DexFile* pDexFile = method->clazz->pDexFile;
713 const DexCode* pDexCode = dvmGetMethodCode(method);
714 int localsSize = dexGetLocalsSize(pDexFile, pDexCode);
715 const DexLocal* locals = dvmDexGetLocals(pDexFile, pDexCode);
716 for (j = 0; j < localsSize, j++) {
717 if (locals[j].registerNum == (u4) i) {
718 name = dvmDexStringStr(locals[j].pName);
719 break;
720 }
721 }
722#endif
723 LOG(LOG_VERBOSE, LOG_TAG"i", " v%-2d : 0x%08x %s\n",
724 i, framePtr[i], name);
725 }
726 }
727}
728#endif
729
730
731/*
732 * ===========================================================================
733 * Entry point and general support functions
734 * ===========================================================================
735 */
736
Ben Cheng38329f52009-07-07 14:19:20 -0700737/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700738 * Construct an s4 from two consecutive half-words of switch data.
739 * This needs to check endianness because the DEX optimizer only swaps
740 * half-words in instruction stream.
741 *
742 * "switchData" must be 32-bit aligned.
743 */
744#if __BYTE_ORDER == __LITTLE_ENDIAN
745static inline s4 s4FromSwitchData(const void* switchData) {
746 return *(s4*) switchData;
747}
748#else
749static inline s4 s4FromSwitchData(const void* switchData) {
750 u2* data = switchData;
751 return data[0] | (((s4) data[1]) << 16);
Jay Freeman (saurik)ffa5c292008-11-16 13:51:51 +0000752}
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700753#endif
754
755/*
756 * Find the matching case. Returns the offset to the handler instructions.
757 *
758 * Returns 3 if we don't find a match (it's the size of the packed-switch
759 * instruction).
760 */
761s4 dvmInterpHandlePackedSwitch(const u2* switchData, s4 testVal)
762{
763 const int kInstrLen = 3;
764 u2 size;
765 s4 firstKey;
766 const s4* entries;
767
768 /*
769 * Packed switch data format:
770 * ushort ident = 0x0100 magic value
771 * ushort size number of entries in the table
772 * int first_key first (and lowest) switch case value
773 * int targets[size] branch targets, relative to switch opcode
774 *
775 * Total size is (4+size*2) 16-bit code units.
776 */
777 if (*switchData++ != kPackedSwitchSignature) {
778 /* should have been caught by verifier */
779 dvmThrowException("Ljava/lang/InternalError;",
780 "bad packed switch magic");
781 return kInstrLen;
782 }
783
784 size = *switchData++;
785 assert(size > 0);
786
787 firstKey = *switchData++;
788 firstKey |= (*switchData++) << 16;
789
790 if (testVal < firstKey || testVal >= firstKey + size) {
791 LOGVV("Value %d not found in switch (%d-%d)\n",
792 testVal, firstKey, firstKey+size-1);
793 return kInstrLen;
794 }
795
796 /* The entries are guaranteed to be aligned on a 32-bit boundary;
797 * we can treat them as a native int array.
798 */
799 entries = (const s4*) switchData;
800 assert(((u4)entries & 0x3) == 0);
801
802 assert(testVal - firstKey >= 0 && testVal - firstKey < size);
803 LOGVV("Value %d found in slot %d (goto 0x%02x)\n",
804 testVal, testVal - firstKey,
805 s4FromSwitchData(&entries[testVal - firstKey]));
806 return s4FromSwitchData(&entries[testVal - firstKey]);
807}
808
809/*
810 * Find the matching case. Returns the offset to the handler instructions.
811 *
812 * Returns 3 if we don't find a match (it's the size of the sparse-switch
813 * instruction).
814 */
815s4 dvmInterpHandleSparseSwitch(const u2* switchData, s4 testVal)
816{
817 const int kInstrLen = 3;
818 u2 ident, size;
819 const s4* keys;
820 const s4* entries;
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700821
822 /*
823 * Sparse switch data format:
824 * ushort ident = 0x0200 magic value
825 * ushort size number of entries in the table; > 0
826 * int keys[size] keys, sorted low-to-high; 32-bit aligned
827 * int targets[size] branch targets, relative to switch opcode
828 *
829 * Total size is (2+size*4) 16-bit code units.
830 */
831
832 if (*switchData++ != kSparseSwitchSignature) {
833 /* should have been caught by verifier */
834 dvmThrowException("Ljava/lang/InternalError;",
835 "bad sparse switch magic");
836 return kInstrLen;
837 }
838
839 size = *switchData++;
840 assert(size > 0);
Ben Cheng38329f52009-07-07 14:19:20 -0700841
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700842 /* The keys are guaranteed to be aligned on a 32-bit boundary;
843 * we can treat them as a native int array.
844 */
845 keys = (const s4*) switchData;
846 assert(((u4)keys & 0x3) == 0);
847
848 /* The entries are guaranteed to be aligned on a 32-bit boundary;
849 * we can treat them as a native int array.
850 */
851 entries = keys + size;
852 assert(((u4)entries & 0x3) == 0);
853
854 /*
Andy McFadden62f19152009-10-21 16:59:31 -0700855 * Binary-search through the array of keys, which are guaranteed to
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700856 * be sorted low-to-high.
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700857 */
Andy McFadden62f19152009-10-21 16:59:31 -0700858 int lo = 0;
859 int hi = size - 1;
860 while (lo <= hi) {
861 int mid = (lo + hi) >> 1;
862
863 s4 foundVal = s4FromSwitchData(&keys[mid]);
864 if (testVal < foundVal) {
865 hi = mid - 1;
866 } else if (testVal > foundVal) {
867 lo = mid + 1;
868 } else {
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700869 LOGVV("Value %d found in entry %d (goto 0x%02x)\n",
Andy McFadden62f19152009-10-21 16:59:31 -0700870 testVal, mid, s4FromSwitchData(&entries[mid]));
871 return s4FromSwitchData(&entries[mid]);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700872 }
873 }
874
875 LOGVV("Value %d not found in switch\n", testVal);
876 return kInstrLen;
877}
878
879/*
Andy McFadden6f214502009-06-30 16:14:30 -0700880 * Copy data for a fill-array-data instruction. On a little-endian machine
881 * we can just do a memcpy(), on a big-endian system we have work to do.
882 *
883 * The trick here is that dexopt has byte-swapped each code unit, which is
884 * exactly what we want for short/char data. For byte data we need to undo
885 * the swap, and for 4- or 8-byte values we need to swap pieces within
886 * each word.
887 */
888static void copySwappedArrayData(void* dest, const u2* src, u4 size, u2 width)
889{
890#if __BYTE_ORDER == __LITTLE_ENDIAN
891 memcpy(dest, src, size*width);
892#else
893 int i;
894
895 switch (width) {
896 case 1:
897 /* un-swap pairs of bytes as we go */
898 for (i = (size-1) & ~1; i >= 0; i -= 2) {
899 ((u1*)dest)[i] = ((u1*)src)[i+1];
900 ((u1*)dest)[i+1] = ((u1*)src)[i];
901 }
902 /*
903 * "src" is padded to end on a two-byte boundary, but we don't want to
904 * assume "dest" is, so we handle odd length specially.
905 */
906 if ((size & 1) != 0) {
907 ((u1*)dest)[size-1] = ((u1*)src)[size];
908 }
909 break;
910 case 2:
911 /* already swapped correctly */
912 memcpy(dest, src, size*width);
913 break;
914 case 4:
915 /* swap word halves */
916 for (i = 0; i < (int) size; i++) {
917 ((u4*)dest)[i] = (src[(i << 1) + 1] << 16) | src[i << 1];
918 }
919 break;
920 case 8:
921 /* swap word halves and words */
922 for (i = 0; i < (int) (size << 1); i += 2) {
923 ((int*)dest)[i] = (src[(i << 1) + 3] << 16) | src[(i << 1) + 2];
924 ((int*)dest)[i+1] = (src[(i << 1) + 1] << 16) | src[i << 1];
925 }
926 break;
927 default:
928 LOGE("Unexpected width %d in copySwappedArrayData\n", width);
929 dvmAbort();
930 break;
931 }
932#endif
933}
934
935/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700936 * Fill the array with predefined constant values.
937 *
938 * Returns true if job is completed, otherwise false to indicate that
939 * an exception has been thrown.
940 */
The Android Open Source Project89c1feb2008-12-17 18:03:55 -0800941bool dvmInterpHandleFillArrayData(ArrayObject* arrayObj, const u2* arrayData)
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700942{
943 u2 width;
944 u4 size;
945
The Android Open Source Project89c1feb2008-12-17 18:03:55 -0800946 if (arrayObj == NULL) {
947 dvmThrowException("Ljava/lang/NullPointerException;", NULL);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700948 return false;
949 }
Barry Hayes7dc96602010-02-24 09:19:07 -0800950 assert (!IS_CLASS_FLAG_SET(((Object *)arrayObj)->clazz,
951 CLASS_ISOBJECTARRAY));
The Android Open Source Project89c1feb2008-12-17 18:03:55 -0800952
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700953 /*
954 * Array data table format:
955 * ushort ident = 0x0300 magic value
956 * ushort width width of each element in the table
957 * uint size number of elements in the table
958 * ubyte data[size*width] table of data values (may contain a single-byte
959 * padding at the end)
960 *
961 * Total size is 4+(width * size + 1)/2 16-bit code units.
962 */
963 if (arrayData[0] != kArrayDataSignature) {
964 dvmThrowException("Ljava/lang/InternalError;", "bad array data magic");
965 return false;
966 }
967
968 width = arrayData[1];
969 size = arrayData[2] | (((u4)arrayData[3]) << 16);
970
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800971 if (size > arrayObj->length) {
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700972 dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", NULL);
973 return false;
974 }
Andy McFadden6f214502009-06-30 16:14:30 -0700975 copySwappedArrayData(arrayObj->contents, &arrayData[4], size, width);
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -0700976 return true;
977}
978
979/*
980 * Find the concrete method that corresponds to "methodIdx". The code in
981 * "method" is executing invoke-method with "thisClass" as its first argument.
982 *
983 * Returns NULL with an exception raised on failure.
984 */
985Method* dvmInterpFindInterfaceMethod(ClassObject* thisClass, u4 methodIdx,
986 const Method* method, DvmDex* methodClassDex)
987{
988 Method* absMethod;
989 Method* methodToCall;
990 int i, vtableIndex;
991
992 /*
993 * Resolve the method. This gives us the abstract method from the
994 * interface class declaration.
995 */
996 absMethod = dvmDexGetResolvedMethod(methodClassDex, methodIdx);
997 if (absMethod == NULL) {
998 absMethod = dvmResolveInterfaceMethod(method->clazz, methodIdx);
999 if (absMethod == NULL) {
1000 LOGV("+ unknown method\n");
1001 return NULL;
1002 }
1003 }
1004
1005 /* make sure absMethod->methodIndex means what we think it means */
1006 assert(dvmIsAbstractMethod(absMethod));
1007
1008 /*
1009 * Run through the "this" object's iftable. Find the entry for
1010 * absMethod's class, then use absMethod->methodIndex to find
1011 * the method's entry. The value there is the offset into our
1012 * vtable of the actual method to execute.
1013 *
1014 * The verifier does not guarantee that objects stored into
1015 * interface references actually implement the interface, so this
1016 * check cannot be eliminated.
1017 */
1018 for (i = 0; i < thisClass->iftableCount; i++) {
1019 if (thisClass->iftable[i].clazz == absMethod->clazz)
1020 break;
1021 }
1022 if (i == thisClass->iftableCount) {
1023 /* impossible in verified DEX, need to check for it in unverified */
1024 dvmThrowException("Ljava/lang/IncompatibleClassChangeError;",
1025 "interface not implemented");
1026 return NULL;
1027 }
1028
1029 assert(absMethod->methodIndex <
1030 thisClass->iftable[i].clazz->virtualMethodCount);
1031
1032 vtableIndex =
1033 thisClass->iftable[i].methodIndexArray[absMethod->methodIndex];
1034 assert(vtableIndex >= 0 && vtableIndex < thisClass->vtableCount);
1035 methodToCall = thisClass->vtable[vtableIndex];
1036
1037#if 0
1038 /* this can happen when there's a stale class file */
1039 if (dvmIsAbstractMethod(methodToCall)) {
1040 dvmThrowException("Ljava/lang/AbstractMethodError;",
1041 "interface method not implemented");
1042 return NULL;
1043 }
1044#else
1045 assert(!dvmIsAbstractMethod(methodToCall) ||
1046 methodToCall->nativeFunc != NULL);
1047#endif
1048
1049 LOGVV("+++ interface=%s.%s concrete=%s.%s\n",
1050 absMethod->clazz->descriptor, absMethod->name,
1051 methodToCall->clazz->descriptor, methodToCall->name);
1052 assert(methodToCall != NULL);
1053
1054 return methodToCall;
1055}
1056
1057
Andy McFaddenb51ea112009-05-08 16:50:17 -07001058
1059/*
1060 * Helpers for dvmThrowVerificationError().
1061 *
1062 * Each returns a newly-allocated string.
1063 */
1064#define kThrowShow_accessFromClass 1
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001065static char* classNameFromIndex(const Method* method, int ref,
1066 VerifyErrorRefType refType, int flags)
Andy McFaddenb51ea112009-05-08 16:50:17 -07001067{
1068 static const int kBufLen = 256;
1069 const DvmDex* pDvmDex = method->clazz->pDvmDex;
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001070
1071 if (refType == VERIFY_ERROR_REF_FIELD) {
1072 /* get class ID from field ID */
1073 const DexFieldId* pFieldId = dexGetFieldId(pDvmDex->pDexFile, ref);
1074 ref = pFieldId->classIdx;
1075 } else if (refType == VERIFY_ERROR_REF_METHOD) {
1076 /* get class ID from method ID */
1077 const DexMethodId* pMethodId = dexGetMethodId(pDvmDex->pDexFile, ref);
1078 ref = pMethodId->classIdx;
1079 }
1080
Andy McFaddenb51ea112009-05-08 16:50:17 -07001081 const char* className = dexStringByTypeIdx(pDvmDex->pDexFile, ref);
1082 char* dotClassName = dvmDescriptorToDot(className);
1083 if (flags == 0)
1084 return dotClassName;
1085
1086 char* result = (char*) malloc(kBufLen);
1087
1088 if ((flags & kThrowShow_accessFromClass) != 0) {
1089 char* dotFromName = dvmDescriptorToDot(method->clazz->descriptor);
1090 snprintf(result, kBufLen, "tried to access class %s from class %s",
1091 dotClassName, dotFromName);
1092 free(dotFromName);
1093 } else {
1094 assert(false); // should've been caught above
1095 result[0] = '\0';
1096 }
1097
1098 free(dotClassName);
1099 return result;
1100}
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001101static char* fieldNameFromIndex(const Method* method, int ref,
1102 VerifyErrorRefType refType, int flags)
Andy McFaddenb51ea112009-05-08 16:50:17 -07001103{
1104 static const int kBufLen = 256;
1105 const DvmDex* pDvmDex = method->clazz->pDvmDex;
1106 const DexFieldId* pFieldId;
1107 const char* className;
1108 const char* fieldName;
1109
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001110 if (refType != VERIFY_ERROR_REF_FIELD) {
1111 LOGW("Expected ref type %d, got %d\n", VERIFY_ERROR_REF_FIELD, refType);
1112 return NULL; /* no message */
1113 }
1114
Andy McFaddenb51ea112009-05-08 16:50:17 -07001115 pFieldId = dexGetFieldId(pDvmDex->pDexFile, ref);
1116 className = dexStringByTypeIdx(pDvmDex->pDexFile, pFieldId->classIdx);
1117 fieldName = dexStringById(pDvmDex->pDexFile, pFieldId->nameIdx);
1118
1119 char* dotName = dvmDescriptorToDot(className);
1120 char* result = (char*) malloc(kBufLen);
1121
1122 if ((flags & kThrowShow_accessFromClass) != 0) {
1123 char* dotFromName = dvmDescriptorToDot(method->clazz->descriptor);
1124 snprintf(result, kBufLen, "tried to access field %s.%s from class %s",
1125 dotName, fieldName, dotFromName);
1126 free(dotFromName);
1127 } else {
1128 snprintf(result, kBufLen, "%s.%s", dotName, fieldName);
1129 }
1130
1131 free(dotName);
1132 return result;
1133}
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001134static char* methodNameFromIndex(const Method* method, int ref,
1135 VerifyErrorRefType refType, int flags)
Andy McFaddenb51ea112009-05-08 16:50:17 -07001136{
1137 static const int kBufLen = 384;
1138 const DvmDex* pDvmDex = method->clazz->pDvmDex;
1139 const DexMethodId* pMethodId;
1140 const char* className;
1141 const char* methodName;
1142
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001143 if (refType != VERIFY_ERROR_REF_METHOD) {
1144 LOGW("Expected ref type %d, got %d\n", VERIFY_ERROR_REF_METHOD,refType);
1145 return NULL; /* no message */
1146 }
1147
Andy McFaddenb51ea112009-05-08 16:50:17 -07001148 pMethodId = dexGetMethodId(pDvmDex->pDexFile, ref);
1149 className = dexStringByTypeIdx(pDvmDex->pDexFile, pMethodId->classIdx);
1150 methodName = dexStringById(pDvmDex->pDexFile, pMethodId->nameIdx);
1151
1152 char* dotName = dvmDescriptorToDot(className);
1153 char* result = (char*) malloc(kBufLen);
1154
1155 if ((flags & kThrowShow_accessFromClass) != 0) {
1156 char* dotFromName = dvmDescriptorToDot(method->clazz->descriptor);
1157 char* desc = dexProtoCopyMethodDescriptor(&method->prototype);
1158 snprintf(result, kBufLen,
1159 "tried to access method %s.%s:%s from class %s",
1160 dotName, methodName, desc, dotFromName);
1161 free(dotFromName);
1162 free(desc);
1163 } else {
1164 snprintf(result, kBufLen, "%s.%s", dotName, methodName);
1165 }
1166
1167 free(dotName);
1168 return result;
1169}
1170
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001171/*
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001172 * Throw an exception for a problem identified by the verifier.
1173 *
1174 * This is used by the invoke-verification-error instruction. It always
1175 * throws an exception.
1176 *
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001177 * "kind" indicates the kind of failure encountered by the verifier. It
1178 * has two parts, an error code and an indication of the reference type.
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001179 */
Andy McFaddenb51ea112009-05-08 16:50:17 -07001180void dvmThrowVerificationError(const Method* method, int kind, int ref)
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001181{
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001182 const int typeMask = 0xff << kVerifyErrorRefTypeShift;
1183 VerifyError errorKind = kind & ~typeMask;
1184 VerifyErrorRefType refType = kind >> kVerifyErrorRefTypeShift;
Andy McFaddenb51ea112009-05-08 16:50:17 -07001185 const char* exceptionName = "Ljava/lang/VerifyError;";
1186 char* msg = NULL;
1187
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001188 switch ((VerifyError) errorKind) {
Andy McFaddenb51ea112009-05-08 16:50:17 -07001189 case VERIFY_ERROR_NO_CLASS:
1190 exceptionName = "Ljava/lang/NoClassDefFoundError;";
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001191 msg = classNameFromIndex(method, ref, refType, 0);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001192 break;
1193 case VERIFY_ERROR_NO_FIELD:
1194 exceptionName = "Ljava/lang/NoSuchFieldError;";
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001195 msg = fieldNameFromIndex(method, ref, refType, 0);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001196 break;
1197 case VERIFY_ERROR_NO_METHOD:
1198 exceptionName = "Ljava/lang/NoSuchMethodError;";
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001199 msg = methodNameFromIndex(method, ref, refType, 0);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001200 break;
1201 case VERIFY_ERROR_ACCESS_CLASS:
1202 exceptionName = "Ljava/lang/IllegalAccessError;";
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001203 msg = classNameFromIndex(method, ref, refType,
1204 kThrowShow_accessFromClass);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001205 break;
1206 case VERIFY_ERROR_ACCESS_FIELD:
1207 exceptionName = "Ljava/lang/IllegalAccessError;";
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001208 msg = fieldNameFromIndex(method, ref, refType,
1209 kThrowShow_accessFromClass);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001210 break;
1211 case VERIFY_ERROR_ACCESS_METHOD:
1212 exceptionName = "Ljava/lang/IllegalAccessError;";
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001213 msg = methodNameFromIndex(method, ref, refType,
1214 kThrowShow_accessFromClass);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001215 break;
1216 case VERIFY_ERROR_CLASS_CHANGE:
1217 exceptionName = "Ljava/lang/IncompatibleClassChangeError;";
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001218 msg = classNameFromIndex(method, ref, refType, 0);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001219 break;
1220 case VERIFY_ERROR_INSTANTIATION:
1221 exceptionName = "Ljava/lang/InstantiationError;";
Andy McFaddenaf0e8382009-08-28 10:38:37 -07001222 msg = classNameFromIndex(method, ref, refType, 0);
Andy McFaddenb51ea112009-05-08 16:50:17 -07001223 break;
1224
1225 case VERIFY_ERROR_GENERIC:
1226 /* generic VerifyError; use default exception, no message */
1227 break;
1228 case VERIFY_ERROR_NONE:
1229 /* should never happen; use default exception */
1230 assert(false);
1231 msg = strdup("weird - no error specified");
1232 break;
1233
1234 /* no default clause -- want warning if enum updated */
1235 }
1236
1237 dvmThrowException(exceptionName, msg);
1238 free(msg);
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001239}
1240
Andy McFadden3a1aedb2009-05-07 13:30:23 -07001241/*
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001242 * Main interpreter loop entry point. Select "standard" or "debug"
1243 * interpreter and switch between them as required.
1244 *
1245 * This begins executing code at the start of "method". On exit, "pResult"
1246 * holds the return value of the method (or, if "method" returns NULL, it
1247 * holds an undefined value).
1248 *
1249 * The interpreted stack frame, which holds the method arguments, has
1250 * already been set up.
1251 */
1252void dvmInterpret(Thread* self, const Method* method, JValue* pResult)
1253{
1254 InterpState interpState;
1255 bool change;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001256#if defined(WITH_JIT)
Bill Buzbee342806d2009-12-08 12:37:13 -08001257 /* Target-specific save/restore */
1258 extern void dvmJitCalleeSave(double *saveArea);
1259 extern void dvmJitCalleeRestore(double *saveArea);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001260 /* Interpreter entry points from compiled code */
1261 extern void dvmJitToInterpNormal();
1262 extern void dvmJitToInterpNoChain();
1263 extern void dvmJitToInterpPunt();
1264 extern void dvmJitToInterpSingleStep();
Ben Cheng40094c12010-02-24 20:58:44 -08001265 extern void dvmJitToInterpTraceSelectNoChain();
1266 extern void dvmJitToInterpTraceSelect();
Ben Cheng38329f52009-07-07 14:19:20 -07001267 extern void dvmJitToPatchPredictedChain();
Jeff Hao97319a82009-08-12 16:57:15 -07001268#if defined(WITH_SELF_VERIFICATION)
Ben Cheng40094c12010-02-24 20:58:44 -08001269 extern void dvmJitToInterpBackwardBranch();
Jeff Hao97319a82009-08-12 16:57:15 -07001270#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001271
Ben Cheng38329f52009-07-07 14:19:20 -07001272 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07001273 * Reserve a static entity here to quickly setup runtime contents as
1274 * gcc will issue block copy instructions.
1275 */
1276 static struct JitToInterpEntries jitToInterpEntries = {
1277 dvmJitToInterpNormal,
1278 dvmJitToInterpNoChain,
1279 dvmJitToInterpPunt,
1280 dvmJitToInterpSingleStep,
Ben Cheng40094c12010-02-24 20:58:44 -08001281 dvmJitToInterpTraceSelectNoChain,
1282 dvmJitToInterpTraceSelect,
Ben Cheng38329f52009-07-07 14:19:20 -07001283 dvmJitToPatchPredictedChain,
Jeff Hao97319a82009-08-12 16:57:15 -07001284#if defined(WITH_SELF_VERIFICATION)
Ben Cheng40094c12010-02-24 20:58:44 -08001285 dvmJitToInterpBackwardBranch,
Jeff Hao97319a82009-08-12 16:57:15 -07001286#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001287 };
Ben Cheng7a0bcd02010-01-22 16:45:45 -08001288
1289 assert(self->inJitCodeCache == NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001290#endif
1291
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001292
1293#if defined(WITH_TRACKREF_CHECKS)
1294 interpState.debugTrackedRefStart =
1295 dvmReferenceTableEntries(&self->internalLocalRefTable);
1296#endif
1297#if defined(WITH_PROFILER) || defined(WITH_DEBUGGER)
1298 interpState.debugIsMethodEntry = true;
1299#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001300#if defined(WITH_JIT)
Bill Buzbee342806d2009-12-08 12:37:13 -08001301 dvmJitCalleeSave(interpState.calleeSave);
Ben Chenga4973592010-03-31 11:59:18 -07001302 /* Initialize the state to kJitNot */
1303 interpState.jitState = kJitNot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001304
1305 /* Setup the Jit-to-interpreter entry points */
1306 interpState.jitToInterpEntries = jitToInterpEntries;
Bill Buzbee48f18242009-06-19 16:02:27 -07001307
1308 /*
1309 * Initialize the threshold filter [don't bother to zero out the
1310 * actual table. We're looking for matches, and an occasional
1311 * false positive is acceptible.
1312 */
1313 interpState.lastThreshFilter = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001314#endif
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001315
1316 /*
1317 * Initialize working state.
1318 *
1319 * No need to initialize "retval".
1320 */
1321 interpState.method = method;
1322 interpState.fp = (u4*) self->curFrame;
1323 interpState.pc = method->insns;
1324 interpState.entryPoint = kInterpEntryInstr;
1325
1326 if (dvmDebuggerOrProfilerActive())
1327 interpState.nextMode = INTERP_DBG;
1328 else
1329 interpState.nextMode = INTERP_STD;
1330
1331 assert(!dvmIsNativeMethod(method));
1332
1333 /*
1334 * Make sure the class is ready to go. Shouldn't be possible to get
1335 * here otherwise.
1336 */
1337 if (method->clazz->status < CLASS_INITIALIZING ||
1338 method->clazz->status == CLASS_ERROR)
1339 {
1340 LOGE("ERROR: tried to execute code in unprepared class '%s' (%d)\n",
1341 method->clazz->descriptor, method->clazz->status);
1342 dvmDumpThread(self, false);
1343 dvmAbort();
1344 }
1345
1346 typedef bool (*Interpreter)(Thread*, InterpState*);
1347 Interpreter stdInterp;
1348 if (gDvm.executionMode == kExecutionModeInterpFast)
1349 stdInterp = dvmMterpStd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001350#if defined(WITH_JIT)
1351 else if (gDvm.executionMode == kExecutionModeJit)
1352/* If profiling overhead can be kept low enough, we can use a profiling
1353 * mterp fast for both Jit and "fast" modes. If overhead is too high,
1354 * create a specialized profiling interpreter.
1355 */
1356 stdInterp = dvmMterpStd;
1357#endif
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001358 else
1359 stdInterp = dvmInterpretStd;
1360
1361 change = true;
1362 while (change) {
1363 switch (interpState.nextMode) {
1364 case INTERP_STD:
1365 LOGVV("threadid=%d: interp STD\n", self->threadId);
1366 change = (*stdInterp)(self, &interpState);
1367 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001368#if defined(WITH_PROFILER) || defined(WITH_DEBUGGER) || defined(WITH_JIT)
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001369 case INTERP_DBG:
1370 LOGVV("threadid=%d: interp DBG\n", self->threadId);
1371 change = dvmInterpretDbg(self, &interpState);
1372 break;
1373#endif
1374 default:
1375 dvmAbort();
1376 }
1377 }
1378
1379 *pResult = interpState.retval;
Bill Buzbee342806d2009-12-08 12:37:13 -08001380#if defined(WITH_JIT)
1381 dvmJitCalleeRestore(interpState.calleeSave);
1382#endif
The Android Open Source Project2ad60cf2008-10-21 07:00:00 -07001383}