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