1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * JavaScript Debugging support - Hook support
45 jsd_InterruptHandler(JSContext
*cx
, JSScript
*script
, jsbytecode
*pc
, jsval
*rval
,
49 JSDContext
* jsdc
= (JSDContext
*) closure
;
50 JSD_ExecutionHookProc hook
;
53 if( ! jsdc
|| ! jsdc
->inited
)
54 return JSTRAP_CONTINUE
;
56 if( JSD_IS_DANGEROUS_THREAD(jsdc
) )
57 return JSTRAP_CONTINUE
;
59 /* local in case jsdc->interruptHook gets cleared on another thread */
61 hook
= jsdc
->interruptHook
;
62 hookData
= jsdc
->interruptHookData
;
66 return JSTRAP_CONTINUE
;
68 JSD_LOCK_SCRIPTS(jsdc
);
69 jsdscript
= jsd_FindOrCreateJSDScript(jsdc
, cx
, script
, NULL
);
70 JSD_UNLOCK_SCRIPTS(jsdc
);
72 return JSTRAP_CONTINUE
;
75 if( ! jsdlw_UserCodeAtPC(jsdc
, jsdscript
, (jsuword
)pc
) )
76 return JSTRAP_CONTINUE
;
79 return jsd_CallExecutionHook(jsdc
, cx
, JSD_HOOK_INTERRUPTED
,
80 hook
, hookData
, rval
);
84 jsd_DebuggerHandler(JSContext
*cx
, JSScript
*script
, jsbytecode
*pc
,
85 jsval
*rval
, void *closure
)
88 JSDContext
* jsdc
= (JSDContext
*) closure
;
89 JSD_ExecutionHookProc hook
;
92 if( ! jsdc
|| ! jsdc
->inited
)
93 return JSTRAP_CONTINUE
;
95 if( JSD_IS_DANGEROUS_THREAD(jsdc
) )
96 return JSTRAP_CONTINUE
;
98 /* local in case jsdc->debuggerHook gets cleared on another thread */
100 hook
= jsdc
->debuggerHook
;
101 hookData
= jsdc
->debuggerHookData
;
104 return JSTRAP_CONTINUE
;
106 JSD_LOCK_SCRIPTS(jsdc
);
107 jsdscript
= jsd_FindOrCreateJSDScript(jsdc
, cx
, script
, NULL
);
108 JSD_UNLOCK_SCRIPTS(jsdc
);
110 return JSTRAP_CONTINUE
;
112 return jsd_CallExecutionHook(jsdc
, cx
, JSD_HOOK_DEBUGGER_KEYWORD
,
113 hook
, hookData
, rval
);
118 jsd_ThrowHandler(JSContext
*cx
, JSScript
*script
, jsbytecode
*pc
,
119 jsval
*rval
, void *closure
)
121 JSDScript
* jsdscript
;
122 JSDContext
* jsdc
= (JSDContext
*) closure
;
123 JSD_ExecutionHookProc hook
;
126 if( ! jsdc
|| ! jsdc
->inited
)
127 return JSD_HOOK_RETURN_CONTINUE_THROW
;
129 if( JSD_IS_DANGEROUS_THREAD(jsdc
) )
130 return JSD_HOOK_RETURN_CONTINUE_THROW
;
132 /* local in case jsdc->throwHook gets cleared on another thread */
134 hook
= jsdc
->throwHook
;
135 hookData
= jsdc
->throwHookData
;
138 return JSD_HOOK_RETURN_CONTINUE_THROW
;
140 JSD_LOCK_SCRIPTS(jsdc
);
141 jsdscript
= jsd_FindOrCreateJSDScript(jsdc
, cx
, script
, NULL
);
142 JSD_UNLOCK_SCRIPTS(jsdc
);
144 return JSD_HOOK_RETURN_CONTINUE_THROW
;
146 JS_GetPendingException(cx
, rval
);
148 return jsd_CallExecutionHook(jsdc
, cx
, JSD_HOOK_THROW
,
149 hook
, hookData
, rval
);
153 jsd_CallExecutionHook(JSDContext
* jsdc
,
156 JSD_ExecutionHookProc hook
,
160 uintN hookanswer
= JSD_HOOK_THROW
== type
?
161 JSD_HOOK_RETURN_CONTINUE_THROW
:
162 JSD_HOOK_RETURN_CONTINUE
;
163 JSDThreadState
* jsdthreadstate
;
165 if(hook
&& NULL
!= (jsdthreadstate
= jsd_NewThreadState(jsdc
,cx
)))
167 if ((type
!= JSD_HOOK_THROW
&& type
!= JSD_HOOK_INTERRUPTED
) ||
168 jsdc
->flags
& JSD_MASK_TOP_FRAME_ONLY
||
169 !(jsdthreadstate
->flags
& TS_HAS_DISABLED_FRAME
))
172 * if it's not a throw and it's not an interrupt,
173 * or we're only masking the top frame,
174 * or there are no disabled frames in this stack,
177 hookanswer
= hook(jsdc
, jsdthreadstate
, type
, hookData
, rval
);
178 jsd_DestroyThreadState(jsdc
, jsdthreadstate
);
184 case JSD_HOOK_RETURN_ABORT
:
185 case JSD_HOOK_RETURN_HOOK_ERROR
:
187 case JSD_HOOK_RETURN_RET_WITH_VAL
:
188 return JSTRAP_RETURN
;
189 case JSD_HOOK_RETURN_THROW_WITH_VAL
:
191 case JSD_HOOK_RETURN_CONTINUE
:
193 case JSD_HOOK_RETURN_CONTINUE_THROW
:
194 /* only makes sense for jsd_ThrowHandler (which init'd rval) */
195 JS_ASSERT(JSD_HOOK_THROW
== type
);
201 return JSTRAP_CONTINUE
;
205 jsd_CallCallHook (JSDContext
* jsdc
,
208 JSD_CallHookProc hook
,
212 JSDThreadState
* jsdthreadstate
;
214 hookanswer
= JS_FALSE
;
215 if(hook
&& NULL
!= (jsdthreadstate
= jsd_NewThreadState(jsdc
, cx
)))
217 hookanswer
= hook(jsdc
, jsdthreadstate
, type
, hookData
);
218 jsd_DestroyThreadState(jsdc
, jsdthreadstate
);
225 jsd_SetInterruptHook(JSDContext
* jsdc
,
226 JSD_ExecutionHookProc hook
,
230 jsdc
->interruptHookData
= callerdata
;
231 jsdc
->interruptHook
= hook
;
232 JS_SetInterrupt(jsdc
->jsrt
, jsd_InterruptHandler
, (void*) jsdc
);
239 jsd_ClearInterruptHook(JSDContext
* jsdc
)
242 JS_ClearInterrupt(jsdc
->jsrt
, NULL
, NULL
);
243 jsdc
->interruptHook
= NULL
;
250 jsd_SetDebugBreakHook(JSDContext
* jsdc
,
251 JSD_ExecutionHookProc hook
,
255 jsdc
->debugBreakHookData
= callerdata
;
256 jsdc
->debugBreakHook
= hook
;
263 jsd_ClearDebugBreakHook(JSDContext
* jsdc
)
266 jsdc
->debugBreakHook
= NULL
;
273 jsd_SetDebuggerHook(JSDContext
* jsdc
,
274 JSD_ExecutionHookProc hook
,
278 jsdc
->debuggerHookData
= callerdata
;
279 jsdc
->debuggerHook
= hook
;
286 jsd_ClearDebuggerHook(JSDContext
* jsdc
)
289 jsdc
->debuggerHook
= NULL
;
296 jsd_SetThrowHook(JSDContext
* jsdc
,
297 JSD_ExecutionHookProc hook
,
301 jsdc
->throwHookData
= callerdata
;
302 jsdc
->throwHook
= hook
;
309 jsd_ClearThrowHook(JSDContext
* jsdc
)
312 jsdc
->throwHook
= NULL
;
319 jsd_SetFunctionHook(JSDContext
* jsdc
,
320 JSD_CallHookProc hook
,
324 jsdc
->functionHookData
= callerdata
;
325 jsdc
->functionHook
= hook
;
332 jsd_ClearFunctionHook(JSDContext
* jsdc
)
335 jsdc
->functionHook
= NULL
;
342 jsd_SetTopLevelHook(JSDContext
* jsdc
,
343 JSD_CallHookProc hook
,
347 jsdc
->toplevelHookData
= callerdata
;
348 jsdc
->toplevelHook
= hook
;
355 jsd_ClearTopLevelHook(JSDContext
* jsdc
)
358 jsdc
->toplevelHook
= NULL
;