Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / methodjit / Logging.cpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=4 sw=4 et tw=99:
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
18  * May 28, 2008.
19  *
20  * The Initial Developer of the Original Code is
21  *   Brendan Eich <brendan@mozilla.org>
22  *
23  * Contributor(s):
24  *   David Anderson <danderson@mozilla.com>
25  *   Julian Seward <jseward@acm.org>
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either of the GNU General Public License Version 2 or later (the "GPL"),
29  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <string.h>
45 #include "jsutil.h"
46 #include "MethodJIT.h"
47 #include "Logging.h"
48
49 #if defined(JS_METHODJIT_SPEW)
50
51 static bool LoggingChecked = false;
52 static uint32 LoggingBits = 0;
53
54 static const char *ChannelNames[] =
55 {
56 #define _(name) #name,
57     JSPEW_CHAN_MAP(_)
58 #undef  _
59 };
60
61 void
62 js::JMCheckLogging()
63 {
64     /* Not MT safe; races on Logging{Checked,Bits}. */
65     if (LoggingChecked)
66         return;
67     LoggingChecked = true;
68     const char *env = getenv("JMFLAGS");
69     if (!env)
70         return;
71     if (strstr(env, "help")) {
72         fflush(NULL);
73         printf(
74             "\n"
75             "usage: JMFLAGS=option,option,option,... where options can be:\n"
76             "\n"
77             "  help          show this message\n"
78             "  abort/aborts  ???\n"
79             "  scripts       ???\n"
80             "  profile       ???\n"
81 #ifdef DEBUG
82             "  jsops         JS opcodes\n"
83 #endif
84             "  insns         JS opcodes and generated insns\n"
85             "  vmframe       VMFrame contents\n"
86             "  pics          PIC patching activity\n"
87             "  slowcalls     Calls to slow path functions\n"
88             "  full          everything\n"
89             "  notrace       disable trace hints\n"
90             "\n"
91         );
92         exit(0);
93         /*NOTREACHED*/
94     }
95     if (strstr(env, "abort") || strstr(env, "aborts"))
96         LoggingBits |= (1 << uint32(JSpew_Abort));
97     if (strstr(env, "scripts"))
98         LoggingBits |= (1 << uint32(JSpew_Scripts));
99     if (strstr(env, "profile"))
100         LoggingBits |= (1 << uint32(JSpew_Prof));
101 #ifdef DEBUG
102     if (strstr(env, "jsops"))
103         LoggingBits |= (1 << uint32(JSpew_JSOps));
104 #endif
105     if (strstr(env, "insns"))
106         LoggingBits |= (1 << uint32(JSpew_Insns) | (1 << uint32(JSpew_JSOps)));
107     if (strstr(env, "vmframe"))
108         LoggingBits |= (1 << uint32(JSpew_VMFrame));
109     if (strstr(env, "pics"))
110         LoggingBits |= (1 << uint32(JSpew_PICs));
111     if (strstr(env, "slowcalls"))
112         LoggingBits |= (1 << uint32(JSpew_SlowCalls));
113     if (strstr(env, "full"))
114         LoggingBits |= 0xFFFFFFFF;
115 }
116
117 bool
118 js::IsJaegerSpewChannelActive(JaegerSpewChannel channel)
119 {
120     JS_ASSERT(LoggingChecked);
121     return !!(LoggingBits & (1 << uint32(channel)));
122 }
123
124 void
125 js::JaegerSpew(JaegerSpewChannel channel, const char *fmt, ...)
126 {
127     JS_ASSERT(LoggingChecked);
128
129     if (!(LoggingBits & (1 << uint32(channel))))
130         return;
131
132     fprintf(stderr, "[jaeger] %-7s  ", ChannelNames[channel]);
133
134     va_list ap;
135     va_start(ap, fmt);
136     vfprintf(stderr, fmt, ap);
137     va_end(ap);
138
139     /* fprintf(stdout, "\n"); */
140 }
141
142 #endif
143