gdb-tests.cpp (2699B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 #include "gdb-tests.h" 10 #include "jsapi.h" 11 #include "jsfriendapi.h" 12 #include "js/Context.h" 13 #include "js/GlobalObject.h" 14 #include "js/Initialization.h" 15 #include "js/RealmOptions.h" 16 #include "js/Warnings.h" // JS::SetWarningReporter 17 18 using namespace JS; 19 20 /* The class of the global object. */ 21 static const JSClass global_class = { 22 "global", 23 JSCLASS_GLOBAL_FLAGS, 24 &DefaultGlobalClassOps, 25 }; 26 27 static volatile int dontOptimizeMeAway = 0; 28 29 void usePointer(const void* ptr) { dontOptimizeMeAway = 1; } 30 31 template <typename T> 32 static inline T* checkPtr(T* ptr) { 33 if (!ptr) { 34 abort(); 35 } 36 return ptr; 37 } 38 39 static void checkBool(bool success) { 40 if (!success) { 41 abort(); 42 } 43 } 44 45 /* The warning reporter callback. */ 46 void reportWarning(JSContext* cx, JSErrorReport* report) { 47 fprintf(stderr, "%s:%u: %s\n", 48 report->filename ? report->filename.c_str() : "<no filename>", 49 (unsigned int)report->lineno, report->message().c_str()); 50 } 51 52 // prologue.py sets a breakpoint on this function; test functions can call it 53 // to easily return control to GDB where desired. 54 void breakpoint() { 55 // If we leave this function empty, the linker will unify it with other 56 // empty functions throughout SpiderMonkey. If we then set a GDB 57 // breakpoint on it, that breakpoint will hit at all sorts of random 58 // times. So make it perform a distinctive side effect. 59 fprintf(stderr, "Called " __FILE__ ":breakpoint\n"); 60 } 61 62 GDBFragment* GDBFragment::allFragments = nullptr; 63 64 int main(int argc, const char** argv) { 65 if (!JS_Init()) return 1; 66 JSContext* cx = checkPtr(JS_NewContext(1024 * 1024)); 67 68 JS_SetGCParameter(cx, JSGC_MAX_BYTES, 0xffffffff); 69 70 checkBool(JS::InitSelfHostedCode(cx)); 71 JS::SetWarningReporter(cx, reportWarning); 72 73 /* Create the global object. */ 74 JS::RealmOptions options; 75 RootedObject global( 76 cx, checkPtr(JS_NewGlobalObject(cx, &global_class, nullptr, 77 JS::FireOnNewGlobalHook, options))); 78 JSAutoRealm ar(cx, global); 79 80 argv++; 81 while (*argv) { 82 const char* name = *argv++; 83 GDBFragment* fragment; 84 for (fragment = GDBFragment::allFragments; fragment; 85 fragment = fragment->next) { 86 if (strcmp(fragment->name(), name) == 0) { 87 fragment->run(cx, argv); 88 break; 89 } 90 } 91 if (!fragment) { 92 fprintf(stderr, "Unrecognized fragment name: %s\n", name); 93 exit(1); 94 } 95 } 96 97 return 0; 98 }