forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsdbgapi.cpp
2715 lines (2331 loc) · 75.8 KB
/
jsdbgapi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sw=4 et tw=99:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* JS debugging API.
*/
#include <string.h>
#include "jsprvtd.h"
#include "jstypes.h"
#include "jsstdint.h"
#include "jsutil.h"
#include "jsclist.h"
#include "jshashtable.h"
#include "jsapi.h"
#include "jscntxt.h"
#include "jsversion.h"
#include "jsdbgapi.h"
#include "jsemit.h"
#include "jsfun.h"
#include "jsgc.h"
#include "jsgcmark.h"
#include "jsinterp.h"
#include "jslock.h"
#include "jsobj.h"
#include "jsopcode.h"
#include "jsparse.h"
#include "jsscope.h"
#include "jsscript.h"
#include "jsstaticcheck.h"
#include "jsstr.h"
#include "jswrapper.h"
#include "jsatominlines.h"
#include "jsdbgapiinlines.h"
#include "jsobjinlines.h"
#include "jsinterpinlines.h"
#include "jsscopeinlines.h"
#include "jsscriptinlines.h"
#include "vm/Stack-inl.h"
#include "jsautooplen.h"
#include "methodjit/MethodJIT.h"
#include "methodjit/Retcon.h"
using namespace js;
using namespace js::gc;
typedef struct JSTrap {
JSCList links;
JSScript *script;
jsbytecode *pc;
JSOp op;
JSTrapHandler handler;
jsval closure;
} JSTrap;
#define DBG_LOCK(rt) JS_ACQUIRE_LOCK((rt)->debuggerLock)
#define DBG_UNLOCK(rt) JS_RELEASE_LOCK((rt)->debuggerLock)
#define DBG_LOCK_EVAL(rt,expr) (DBG_LOCK(rt), (expr), DBG_UNLOCK(rt))
JS_PUBLIC_API(JSBool)
JS_GetDebugMode(JSContext *cx)
{
return cx->compartment->debugMode;
}
JS_PUBLIC_API(JSBool)
JS_SetDebugMode(JSContext *cx, JSBool debug)
{
return JS_SetDebugModeForCompartment(cx, cx->compartment, debug);
}
JS_PUBLIC_API(void)
JS_SetRuntimeDebugMode(JSRuntime *rt, JSBool debug)
{
rt->debugMode = debug;
}
namespace js {
void
ScriptDebugPrologue(JSContext *cx, StackFrame *fp)
{
if (fp->isFramePushedByExecute()) {
if (JSInterpreterHook hook = cx->debugHooks->executeHook)
fp->setHookData(hook(cx, Jsvalify(fp), true, 0, cx->debugHooks->executeHookData));
} else {
if (JSInterpreterHook hook = cx->debugHooks->callHook)
fp->setHookData(hook(cx, Jsvalify(fp), true, 0, cx->debugHooks->callHookData));
}
Probes::enterJSFun(cx, fp->maybeFun(), fp->script());
}
bool
ScriptDebugEpilogue(JSContext *cx, StackFrame *fp, bool okArg)
{
JSBool ok = okArg;
Probes::exitJSFun(cx, fp->maybeFun(), fp->script());
if (void *hookData = fp->maybeHookData()) {
if (fp->isFramePushedByExecute()) {
if (JSInterpreterHook hook = cx->debugHooks->executeHook)
hook(cx, Jsvalify(fp), false, &ok, hookData);
} else {
if (JSInterpreterHook hook = cx->debugHooks->callHook)
hook(cx, Jsvalify(fp), false, &ok, hookData);
}
}
return ok;
}
} /* namespace js */
#ifdef DEBUG
static bool
CompartmentHasLiveScripts(JSCompartment *comp)
{
#if defined(JS_METHODJIT) && defined(JS_THREADSAFE)
jsword currentThreadId = reinterpret_cast<jsword>(js_CurrentThreadId());
#endif
// Unsynchronized context iteration is technically a race; but this is only
// for debug asserts where such a race would be rare
JSContext *iter = NULL;
JSContext *icx;
while ((icx = JS_ContextIterator(comp->rt, &iter))) {
#if defined(JS_METHODJIT) && defined(JS_THREADSAFE)
if (JS_GetContextThread(icx) != currentThreadId)
continue;
#endif
for (AllFramesIter i(icx); !i.done(); ++i) {
JSScript *script = i.fp()->maybeScript();
if (script && script->compartment == comp)
return JS_TRUE;
}
}
return JS_FALSE;
}
#endif
JS_FRIEND_API(JSBool)
JS_SetDebugModeForCompartment(JSContext *cx, JSCompartment *comp, JSBool debug)
{
if (comp->debugMode == !!debug)
return JS_TRUE;
// This should only be called when no scripts are live. It would even be
// incorrect to discard just the non-live scripts' JITScripts because they
// might share ICs with live scripts (bug 632343).
JS_ASSERT(!CompartmentHasLiveScripts(comp));
// All scripts compiled from this point on should be in the requested debugMode.
comp->debugMode = !!debug;
// Discard JIT code for any scripts that change debugMode. This function
// assumes that 'comp' is in the same thread as 'cx'.
#ifdef JS_METHODJIT
JS::AutoEnterScriptCompartment ac;
for (JSScript *script = (JSScript *)comp->scripts.next;
&script->links != &comp->scripts;
script = (JSScript *)script->links.next)
{
if (!script->debugMode == !debug)
continue;
/*
* If compartment entry fails, debug mode is left partially on, leading
* to a small performance overhead but no loss of correctness. We set
* the debug flags to false so that the caller will not later attempt
* to use debugging features.
*/
if (!ac.entered() && !ac.enter(cx, script)) {
comp->debugMode = JS_FALSE;
return JS_FALSE;
}
mjit::ReleaseScriptCode(cx, script);
script->debugMode = !!debug;
}
#endif
return JS_TRUE;
}
JS_FRIEND_API(JSBool)
js_SetSingleStepMode(JSContext *cx, JSScript *script, JSBool singleStep)
{
assertSameCompartment(cx, script);
#ifdef JS_METHODJIT
if (!script->singleStepMode == !singleStep)
return JS_TRUE;
#endif
JS_ASSERT_IF(singleStep, cx->compartment->debugMode);
#ifdef JS_METHODJIT
/* request the next recompile to inject single step interrupts */
script->singleStepMode = !!singleStep;
js::mjit::JITScript *jit = script->jitNormal ? script->jitNormal : script->jitCtor;
if (jit && script->singleStepMode != jit->singleStepMode) {
js::mjit::Recompiler recompiler(cx, script);
if (!recompiler.recompile()) {
script->singleStepMode = !singleStep;
return JS_FALSE;
}
}
#endif
return JS_TRUE;
}
static JSBool
CheckDebugMode(JSContext *cx)
{
JSBool debugMode = JS_GetDebugMode(cx);
/*
* :TODO:
* This probably should be an assertion, since it's indicative of a severe
* API misuse.
*/
if (!debugMode) {
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage,
NULL, JSMSG_NEED_DEBUG_MODE);
}
return debugMode;
}
JS_PUBLIC_API(JSBool)
JS_SetSingleStepMode(JSContext *cx, JSScript *script, JSBool singleStep)
{
assertSameCompartment(cx, script);
if (!CheckDebugMode(cx))
return JS_FALSE;
return js_SetSingleStepMode(cx, script, singleStep);
}
/*
* NB: FindTrap must be called with rt->debuggerLock acquired.
*/
static JSTrap *
FindTrap(JSRuntime *rt, JSScript *script, jsbytecode *pc)
{
JSTrap *trap;
for (trap = (JSTrap *)rt->trapList.next;
&trap->links != &rt->trapList;
trap = (JSTrap *)trap->links.next) {
if (trap->script == script && trap->pc == pc)
return trap;
}
return NULL;
}
jsbytecode *
js_UntrapScriptCode(JSContext *cx, JSScript *script)
{
jsbytecode *code;
JSRuntime *rt;
JSTrap *trap;
code = script->code;
rt = cx->runtime;
DBG_LOCK(rt);
for (trap = (JSTrap *)rt->trapList.next;
&trap->links !=
&rt->trapList;
trap = (JSTrap *)trap->links.next) {
if (trap->script == script &&
(size_t)(trap->pc - script->code) < script->length) {
if (code == script->code) {
jssrcnote *sn, *notes;
size_t nbytes;
nbytes = script->length * sizeof(jsbytecode);
notes = script->notes();
for (sn = notes; !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn))
continue;
nbytes += (sn - notes + 1) * sizeof *sn;
code = (jsbytecode *) cx->malloc_(nbytes);
if (!code)
break;
memcpy(code, script->code, nbytes);
GetGSNCache(cx)->purge();
}
code[trap->pc - script->code] = trap->op;
}
}
DBG_UNLOCK(rt);
return code;
}
JS_PUBLIC_API(JSBool)
JS_SetTrap(JSContext *cx, JSScript *script, jsbytecode *pc,
JSTrapHandler handler, jsval closure)
{
JSTrap *junk, *trap, *twin;
JSRuntime *rt;
uint32 sample;
if (!CheckDebugMode(cx))
return JS_FALSE;
JS_ASSERT((JSOp) *pc != JSOP_TRAP);
junk = NULL;
rt = cx->runtime;
DBG_LOCK(rt);
trap = FindTrap(rt, script, pc);
if (trap) {
JS_ASSERT(trap->script == script && trap->pc == pc);
JS_ASSERT(*pc == JSOP_TRAP);
} else {
sample = rt->debuggerMutations;
DBG_UNLOCK(rt);
trap = (JSTrap *) cx->malloc_(sizeof *trap);
if (!trap)
return JS_FALSE;
trap->closure = JSVAL_NULL;
DBG_LOCK(rt);
twin = (rt->debuggerMutations != sample)
? FindTrap(rt, script, pc)
: NULL;
if (twin) {
junk = trap;
trap = twin;
} else {
JS_APPEND_LINK(&trap->links, &rt->trapList);
++rt->debuggerMutations;
trap->script = script;
trap->pc = pc;
trap->op = (JSOp)*pc;
*pc = JSOP_TRAP;
}
}
trap->handler = handler;
trap->closure = closure;
DBG_UNLOCK(rt);
if (junk)
cx->free_(junk);
#ifdef JS_METHODJIT
if (script->hasJITCode()) {
js::mjit::Recompiler recompiler(cx, script);
if (!recompiler.recompile())
return JS_FALSE;
}
#endif
return JS_TRUE;
}
JS_PUBLIC_API(JSOp)
JS_GetTrapOpcode(JSContext *cx, JSScript *script, jsbytecode *pc)
{
JSRuntime *rt;
JSTrap *trap;
JSOp op;
rt = cx->runtime;
DBG_LOCK(rt);
trap = FindTrap(rt, script, pc);
op = trap ? trap->op : (JSOp) *pc;
DBG_UNLOCK(rt);
return op;
}
static void
DestroyTrapAndUnlock(JSContext *cx, JSTrap *trap)
{
++cx->runtime->debuggerMutations;
JS_REMOVE_LINK(&trap->links);
*trap->pc = (jsbytecode)trap->op;
DBG_UNLOCK(cx->runtime);
cx->free_(trap);
}
JS_PUBLIC_API(void)
JS_ClearTrap(JSContext *cx, JSScript *script, jsbytecode *pc,
JSTrapHandler *handlerp, jsval *closurep)
{
JSTrap *trap;
DBG_LOCK(cx->runtime);
trap = FindTrap(cx->runtime, script, pc);
if (handlerp)
*handlerp = trap ? trap->handler : NULL;
if (closurep)
*closurep = trap ? trap->closure : JSVAL_NULL;
if (trap)
DestroyTrapAndUnlock(cx, trap);
else
DBG_UNLOCK(cx->runtime);
#ifdef JS_METHODJIT
if (script->hasJITCode()) {
mjit::Recompiler recompiler(cx, script);
recompiler.recompile();
}
#endif
}
JS_PUBLIC_API(void)
JS_ClearScriptTraps(JSContext *cx, JSScript *script)
{
JSRuntime *rt;
JSTrap *trap, *next;
uint32 sample;
rt = cx->runtime;
DBG_LOCK(rt);
for (trap = (JSTrap *)rt->trapList.next;
&trap->links != &rt->trapList;
trap = next) {
next = (JSTrap *)trap->links.next;
if (trap->script == script) {
sample = rt->debuggerMutations;
DestroyTrapAndUnlock(cx, trap);
DBG_LOCK(rt);
if (rt->debuggerMutations != sample + 1)
next = (JSTrap *)rt->trapList.next;
}
}
DBG_UNLOCK(rt);
}
JS_PUBLIC_API(void)
JS_ClearAllTraps(JSContext *cx)
{
JSRuntime *rt;
JSTrap *trap, *next;
uint32 sample;
rt = cx->runtime;
DBG_LOCK(rt);
for (trap = (JSTrap *)rt->trapList.next;
&trap->links != &rt->trapList;
trap = next) {
next = (JSTrap *)trap->links.next;
sample = rt->debuggerMutations;
DestroyTrapAndUnlock(cx, trap);
DBG_LOCK(rt);
if (rt->debuggerMutations != sample + 1)
next = (JSTrap *)rt->trapList.next;
}
DBG_UNLOCK(rt);
}
/*
* NB: js_MarkTraps does not acquire cx->runtime->debuggerLock, since the
* debugger should never be racing with the GC (i.e., the debugger must
* respect the request model).
*/
void
js_MarkTraps(JSTracer *trc)
{
JSRuntime *rt = trc->context->runtime;
for (JSTrap *trap = (JSTrap *) rt->trapList.next;
&trap->links != &rt->trapList;
trap = (JSTrap *) trap->links.next) {
MarkValue(trc, Valueify(trap->closure), "trap->closure");
}
}
JS_PUBLIC_API(JSTrapStatus)
JS_HandleTrap(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval)
{
JSTrap *trap;
jsint op;
JSTrapStatus status;
assertSameCompartment(cx, script);
DBG_LOCK(cx->runtime);
trap = FindTrap(cx->runtime, script, pc);
JS_ASSERT(!trap || trap->handler);
if (!trap) {
op = (JSOp) *pc;
DBG_UNLOCK(cx->runtime);
/* Defend against "pc for wrong script" API usage error. */
JS_ASSERT(op != JSOP_TRAP);
#ifdef JS_THREADSAFE
/* If the API was abused, we must fail for want of the real op. */
if (op == JSOP_TRAP)
return JSTRAP_ERROR;
/* Assume a race with a debugger thread and try to carry on. */
*rval = INT_TO_JSVAL(op);
return JSTRAP_CONTINUE;
#else
/* Always fail if single-threaded (must be an API usage error). */
return JSTRAP_ERROR;
#endif
}
DBG_UNLOCK(cx->runtime);
/*
* It's important that we not use 'trap->' after calling the callback --
* the callback might remove the trap!
*/
op = (jsint)trap->op;
status = trap->handler(cx, script, pc, rval, trap->closure);
if (status == JSTRAP_CONTINUE) {
/* By convention, return the true op to the interpreter in rval. */
*rval = INT_TO_JSVAL(op);
}
return status;
}
#ifdef JS_TRACER
static void
JITInhibitingHookChange(JSRuntime *rt, bool wasInhibited)
{
if (wasInhibited) {
if (!rt->debuggerInhibitsJIT()) {
for (JSCList *cl = rt->contextList.next; cl != &rt->contextList; cl = cl->next)
js_ContextFromLinkField(cl)->updateJITEnabled();
}
} else if (rt->debuggerInhibitsJIT()) {
for (JSCList *cl = rt->contextList.next; cl != &rt->contextList; cl = cl->next)
js_ContextFromLinkField(cl)->traceJitEnabled = false;
}
}
#endif
JS_PUBLIC_API(JSBool)
JS_SetInterrupt(JSRuntime *rt, JSInterruptHook hook, void *closure)
{
#ifdef JS_TRACER
{
AutoLockGC lock(rt);
bool wasInhibited = rt->debuggerInhibitsJIT();
#endif
rt->globalDebugHooks.interruptHook = hook;
rt->globalDebugHooks.interruptHookData = closure;
#ifdef JS_TRACER
JITInhibitingHookChange(rt, wasInhibited);
}
#endif
return JS_TRUE;
}
JS_PUBLIC_API(JSBool)
JS_ClearInterrupt(JSRuntime *rt, JSInterruptHook *hoop, void **closurep)
{
#ifdef JS_TRACER
AutoLockGC lock(rt);
bool wasInhibited = rt->debuggerInhibitsJIT();
#endif
if (hoop)
*hoop = rt->globalDebugHooks.interruptHook;
if (closurep)
*closurep = rt->globalDebugHooks.interruptHookData;
rt->globalDebugHooks.interruptHook = 0;
rt->globalDebugHooks.interruptHookData = 0;
#ifdef JS_TRACER
JITInhibitingHookChange(rt, wasInhibited);
#endif
return JS_TRUE;
}
/************************************************************************/
struct JSWatchPoint {
JSCList links;
JSObject *object; /* weak link, see js_SweepWatchPoints */
const Shape *shape;
StrictPropertyOp setter;
JSWatchPointHandler handler;
JSObject *closure;
uintN flags;
};
#define JSWP_LIVE 0x1 /* live because set and not cleared */
#define JSWP_HELD 0x2 /* held while running handler/setter */
/*
* NB: DropWatchPointAndUnlock releases cx->runtime->debuggerLock in all cases.
* The sweeping parameter is true if the watchpoint and its object are about to
* be finalized, in which case we don't need to changeProperty.
*/
static JSBool
DropWatchPointAndUnlock(JSContext *cx, JSWatchPoint *wp, uintN flag, bool sweeping)
{
bool ok = true;
JSRuntime *rt = cx->runtime;
wp->flags &= ~flag;
if (wp->flags != 0) {
DBG_UNLOCK(rt);
return ok;
}
/* Remove wp from the list, then restore wp->shape->setter from wp. */
++rt->debuggerMutations;
JS_REMOVE_LINK(&wp->links);
DBG_UNLOCK(rt);
/*
* If the property isn't found on wp->object, then someone else must have deleted it,
* and we don't need to change the property attributes.
*/
if (!sweeping) {
const Shape *shape = wp->shape;
const Shape *wprop = wp->object->nativeLookup(shape->propid);
if (wprop &&
wprop->hasSetterValue() == shape->hasSetterValue() &&
IsWatchedProperty(cx, wprop)) {
shape = wp->object->changeProperty(cx, wprop, 0, wprop->attributes(),
wprop->getter(), wp->setter);
if (!shape)
ok = false;
}
}
cx->free_(wp);
return ok;
}
/*
* NB: js_TraceWatchPoints does not acquire cx->runtime->debuggerLock, since
* the debugger should never be racing with the GC (i.e., the debugger must
* respect the request model). If any unmarked objects were marked, this
* function returns true and the GC will iteratively call this function again
* until no more unmarked heap objects are found. This is necessary because
* watch points have a weak pointer semantics.
*/
JSBool
js_TraceWatchPoints(JSTracer *trc)
{
JSRuntime *rt;
JSWatchPoint *wp;
rt = trc->context->runtime;
bool modified = false;
for (wp = (JSWatchPoint *)rt->watchPointList.next;
&wp->links != &rt->watchPointList;
wp = (JSWatchPoint *)wp->links.next) {
if (wp->object->isMarked()) {
if (!wp->shape->isMarked()) {
modified = true;
MarkShape(trc, wp->shape, "shape");
}
if (wp->shape->hasSetterValue() && wp->setter) {
if (!CastAsObject(wp->setter)->isMarked()) {
modified = true;
MarkObject(trc, *CastAsObject(wp->setter), "wp->setter");
}
}
if (!wp->closure->isMarked()) {
modified = true;
MarkObject(trc, *wp->closure, "wp->closure");
}
}
}
return modified;
}
void
js_SweepWatchPoints(JSContext *cx)
{
JSRuntime *rt;
JSWatchPoint *wp, *next;
uint32 sample;
rt = cx->runtime;
DBG_LOCK(rt);
for (wp = (JSWatchPoint *)rt->watchPointList.next;
&wp->links != &rt->watchPointList;
wp = next) {
next = (JSWatchPoint *)wp->links.next;
if (IsAboutToBeFinalized(cx, wp->object)) {
sample = rt->debuggerMutations;
/* Ignore failures. */
DropWatchPointAndUnlock(cx, wp, JSWP_LIVE, true);
DBG_LOCK(rt);
if (rt->debuggerMutations != sample + 1)
next = (JSWatchPoint *)rt->watchPointList.next;
}
}
DBG_UNLOCK(rt);
}
/*
* NB: LockedFindWatchPoint must be called with rt->debuggerLock acquired.
*/
static JSWatchPoint *
LockedFindWatchPoint(JSRuntime *rt, JSObject *obj, jsid propid)
{
JSWatchPoint *wp;
for (wp = (JSWatchPoint *)rt->watchPointList.next;
&wp->links != &rt->watchPointList;
wp = (JSWatchPoint *)wp->links.next) {
if (wp->object == obj && wp->shape->propid == propid)
return wp;
}
return NULL;
}
static JSWatchPoint *
FindWatchPoint(JSRuntime *rt, JSObject *obj, jsid id)
{
JSWatchPoint *wp;
DBG_LOCK(rt);
wp = LockedFindWatchPoint(rt, obj, id);
DBG_UNLOCK(rt);
return wp;
}
JSBool
js_watch_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
{
assertSameCompartment(cx, obj);
JSRuntime *rt = cx->runtime;
DBG_LOCK(rt);
for (JSWatchPoint *wp = (JSWatchPoint *)rt->watchPointList.next;
&wp->links != &rt->watchPointList;
wp = (JSWatchPoint *)wp->links.next) {
const Shape *shape = wp->shape;
if (wp->object == obj && SHAPE_USERID(shape) == id && !(wp->flags & JSWP_HELD)) {
bool ok;
Value old;
uint32 slot;
const Shape *needMethodSlotWrite = NULL;
wp->flags |= JSWP_HELD;
DBG_UNLOCK(rt);
jsid propid = shape->propid;
shape = obj->nativeLookup(propid);
if (!shape) {
/*
* This happens if the watched property has been deleted, but a
* prototype has a watched accessor property with the same
* name. See bug 636697.
*/
ok = true;
goto out;
}
JS_ASSERT(IsWatchedProperty(cx, shape));
/* Determine the property's old value. */
slot = shape->slot;
old = obj->containsSlot(slot) ? obj->nativeGetSlot(slot) : UndefinedValue();
if (shape->isMethod()) {
/*
* We get here in two cases: (1) the existing watched property
* is a method; or (2) the watched property was deleted and is
* now in the middle of being re-added via JSOP_SETMETHOD. In
* both cases we must trip the method read barrier in order to
* avoid passing an uncloned function object to the handler.
*
* Case 2 is especially hairy. js_watch_set, uniquely, gets
* called in the middle of creating a method property, after
* shape is in obj but before the slot has been set. So in this
* case we must finish initializing the half-finished method
* property before triggering the method read barrier.
*
* Bonus weirdness: because this changes obj's shape,
* js_NativeSet (which is our caller) will not write to the
* slot, as it will appear the property was deleted and a new
* property added. We must write the slot ourselves -- however
* we must do it after calling the watchpoint handler. So set
* needMethodSlotWrite here and use it to write to the slot
* below, if the handler does not tinker with the property
* further.
*/
JS_ASSERT(!wp->setter);
Value method = ObjectValue(shape->methodObject());
if (old.isUndefined())
obj->nativeSetSlot(slot, method);
ok = obj->methodReadBarrier(cx, *shape, &method);
if (!ok)
goto out;
wp->shape = shape = needMethodSlotWrite = obj->nativeLookup(propid);
JS_ASSERT(shape->isDataDescriptor());
JS_ASSERT(!shape->isMethod());
if (old.isUndefined())
obj->nativeSetSlot(shape->slot, old);
else
old = method;
}
{
Maybe<AutoShapeRooter> tvr;
if (needMethodSlotWrite)
tvr.construct(cx, needMethodSlotWrite);
/*
* Call the handler. This invalidates shape, so re-lookup the shape.
* NB: wp is held, so we can safely dereference it still.
*/
ok = wp->handler(cx, obj, propid, Jsvalify(old), Jsvalify(vp), wp->closure);
if (!ok)
goto out;
shape = obj->nativeLookup(propid);
if (!shape) {
ok = true;
} else if (wp->setter) {
/*
* Pass the output of the handler to the setter. Security wrappers
* prevent any funny business between watchpoints and setters.
*/
ok = shape->hasSetterValue()
? ExternalInvoke(cx, ObjectValue(*obj),
ObjectValue(*CastAsObject(wp->setter)),
1, vp, vp)
: CallJSPropertyOpSetter(cx, wp->setter, obj, SHAPE_USERID(shape),
strict, vp);
} else if (shape == needMethodSlotWrite) {
/* See comment above about needMethodSlotWrite. */
obj->nativeSetSlot(shape->slot, *vp);
ok = true;
} else {
/*
* A property with the default setter might be either a method
* or an ordinary function-valued data property subject to the
* method write barrier.
*
* It is not the setter's job to call methodWriteBarrier,
* but js_watch_set must do so, because the caller will be
* fooled into not doing it: shape does *not* have the
* default setter and therefore seems not to be a method.
*/
ok = obj->methodWriteBarrier(cx, *shape, *vp) != NULL;
}
}
out:
DBG_LOCK(rt);
return DropWatchPointAndUnlock(cx, wp, JSWP_HELD, false) && ok;
}
}
DBG_UNLOCK(rt);
return true;
}
static JSBool
js_watch_set_wrapper(JSContext *cx, uintN argc, Value *vp)
{
JSObject *obj = ToObject(cx, &vp[1]);
if (!obj)
return false;
JSObject &funobj = JS_CALLEE(cx, vp).toObject();
JSFunction *wrapper = funobj.getFunctionPrivate();
jsid userid = ATOM_TO_JSID(wrapper->atom);
JS_SET_RVAL(cx, vp, argc ? JS_ARGV(cx, vp)[0] : UndefinedValue());
/*
* The strictness we pass here doesn't matter, since we know that it's
* a JS setter, which can't depend on the assigning code's strictness.
*/
return js_watch_set(cx, obj, userid, false, vp);
}
namespace js {
bool
IsWatchedProperty(JSContext *cx, const Shape *shape)
{
if (shape->hasSetterValue()) {
JSObject *funobj = shape->setterObject();
if (!funobj || !funobj->isFunction())
return false;
JSFunction *fun = funobj->getFunctionPrivate();
return fun->maybeNative() == js_watch_set_wrapper;
}
return shape->setterOp() == js_watch_set;
}
}
/*
* Return an appropriate setter to substitute for |setter| on a property
* with attributes |attrs|, to implement a watchpoint on the property named
* |id|.
*/
static StrictPropertyOp
WrapWatchedSetter(JSContext *cx, jsid id, uintN attrs, StrictPropertyOp setter)
{
JSAtom *atom;
JSFunction *wrapper;
/* Wrap a C++ setter simply by returning our own C++ setter. */
if (!(attrs & JSPROP_SETTER))
return &js_watch_set; /* & to silence schoolmarmish MSVC */
/*
* Wrap a JSObject * setter by constructing our own JSFunction * that saves the
* property id as the function name, and calls js_watch_set.
*/
if (JSID_IS_ATOM(id)) {
atom = JSID_TO_ATOM(id);
} else if (JSID_IS_INT(id)) {
if (!js_ValueToStringId(cx, IdToValue(id), &id))
return NULL;
atom = JSID_TO_ATOM(id);
} else {
atom = NULL;
}
wrapper = js_NewFunction(cx, NULL, js_watch_set_wrapper, 1, 0,
setter ? CastAsObject(setter)->getParent() : NULL, atom);
if (!wrapper)
return NULL;
return CastAsStrictPropertyOp(FUN_OBJECT(wrapper));
}
static const Shape *
UpdateWatchpointShape(JSContext *cx, JSWatchPoint *wp, const Shape *newShape)
{
JS_ASSERT_IF(wp->shape, wp->shape->propid == newShape->propid);
JS_ASSERT(!IsWatchedProperty(cx, newShape));
/* Create a watching setter we can substitute for the new shape's setter. */
StrictPropertyOp watchingSetter =
WrapWatchedSetter(cx, newShape->propid, newShape->attributes(), newShape->setter());
if (!watchingSetter)
return NULL;
/*
* Save the shape's setter; we don't know whether js_ChangeNativePropertyAttrs will
* return a new shape, or mutate this one.
*/
StrictPropertyOp originalSetter = newShape->setter();
/*
* Drop the watching setter into the object, in place of newShape. Note that a single
* watchpoint-wrapped shape may correspond to more than one non-watchpoint shape: we
* wrap all (JSPropertyOp, not JSObject *) setters with js_watch_set, so shapes that
* differ only in their setter may all get wrapped to the same shape.
*/
const Shape *watchingShape =
js_ChangeNativePropertyAttrs(cx, wp->object, newShape, 0, newShape->attributes(),
newShape->getter(), watchingSetter);
if (!watchingShape)
return NULL;
/* Update the watchpoint with the new shape and its original setter. */
wp->setter = originalSetter;
wp->shape = watchingShape;