* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / contrib / vmlog / vmlog_jamvm.c
1 /* vmlog - high-speed logging for free VMs                  */
2 /* Copyright (C) 2006 Edwin Steiner <edwin.steiner@gmx.net> */
3
4 /* This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /* vmlog_jamvm.c - code to be #included in jamvm */
20
21 #include <vmlog_jamvm.h>
22 #include <vmlog.h>
23 #include <assert.h>
24
25 /*** global variables ************************************************/
26
27 static vmlog_log *vmlog_global_log = NULL;
28 static VMLock vmlog_global_lock;
29
30 /*** locking *********************************************************/
31
32 #define VMLOG_LOCK(vml)    lockVMLock(vmlog_global_lock,threadSelf())
33 #define VMLOG_UNLOCK(vml)  unlockVMLock(vmlog_global_lock,threadSelf())
34
35 /*** include the vmlog code ******************************************/
36
37 #include "vmlog.c"
38
39 /*** internal functions **********************************************/
40
41 void vmlog_jamvm_init(int *pargc,char **argv)
42 {
43         vmlog_options *opts;
44
45         opts = vmlog_opt_parse_cmd_line(pargc,argv);
46         
47         if (!opts->prefix)
48                 return;
49
50         vmlog_global_log = vmlog_log_new(opts->prefix,1);
51
52         if (opts->ignoreprefix) {
53                 vmlog_log_load_ignorelist(vmlog_global_log,
54                                 opts->ignoreprefix);
55         }
56
57         if (opts->stringprefix) {
58                 vmlog_load_stringhash(vmlog_global_log,
59                                 opts->stringprefix);
60         }
61
62         vmlog_opt_free(opts);
63 }
64
65 static void vmlog_jamvm_do_log(vmlog_log_function fun,
66                                Object *thread,MethodBlock *mb)
67 {
68         char *name;
69         int namelen;
70         ClassBlock *cb;
71         
72         assert(mb);
73
74         if (!vmlog_global_log)
75                 return;
76
77         cb = CLASS_CB(mb->class);
78                 
79         name = vmlog_concat4len(
80                 cb->name,strlen(cb->name),
81                 ".",1,
82                 mb->name,strlen(mb->name),
83                 mb->type,strlen(mb->type),
84                 &namelen);
85
86         fun(vmlog_global_log,thread,name,namelen);
87
88         VMLOG_FREE_ARRAY(char,namelen+1,name);
89 }
90
91 /*** functions callable from jamvm ***********************************/
92
93 void vmlog_jamvm_enter_method(Object *thread,MethodBlock *mb)
94 {
95         vmlog_jamvm_do_log(vmlog_log_enter,thread,mb);
96 }
97
98 void vmlog_jamvm_leave_method(Object *thread,MethodBlock *mb)
99 {
100         vmlog_jamvm_do_log(vmlog_log_leave,thread,mb);
101 }
102
103 void vmlog_jamvm_unwnd_method(Object *thread,MethodBlock *mb)
104 {
105         vmlog_jamvm_do_log(vmlog_log_unwnd,thread,mb);
106 }
107
108 void vmlog_jamvm_throw(Object *thread,Object *exp)
109 {
110         ClassBlock *cb;
111         
112         assert(exp);
113
114         if (!vmlog_global_log)
115                 return;
116
117         cb = CLASS_CB(exp->class);
118         assert(cb);
119
120         vmlog_log_throw(vmlog_global_log,thread,cb->name,strlen(cb->name));
121 }
122
123 void vmlog_jamvm_catch(Object *thread,Object *exp)
124 {
125         ClassBlock *cb;
126         
127         assert(exp);
128
129         if (!vmlog_global_log)
130                 return;
131
132         cb = CLASS_CB(exp->class);
133         assert(cb);
134
135         vmlog_log_catch(vmlog_global_log,thread,cb->name,strlen(cb->name));
136 }
137
138 /* vim: noet ts=8 sw=8
139  */
140