Branch data Line data Source code
1 : : // $Id: Obj.cc 6752 2009-06-14 04:24:52Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #include "config.h"
6 : :
7 : : #include <stdlib.h>
8 : :
9 : : #include "Obj.h"
10 : : #include "Serializer.h"
11 : : #include "File.h"
12 : :
13 : 6 : Location no_location("<no location>", 0, 0, 0, 0);
14 : 6 : Location start_location("<start uninitialized>", 0, 0, 0, 0);
15 : 6 : Location end_location("<end uninitialized>", 0, 0, 0, 0);
16 : :
17 : 32 : bool Location::Serialize(SerialInfo* info) const
18 : : {
19 : 32 : return SerialObj::Serialize(info);
20 : : }
21 : :
22 : 0 : Location* Location::Unserialize(UnserialInfo* info)
23 : : {
24 : 0 : return (Location*) SerialObj::Unserialize(info, SER_LOCATION);
25 : : }
26 : :
27 : 99 : IMPLEMENT_SERIAL(Location, SER_LOCATION);
28 : :
29 : 32 : bool Location::DoSerialize(SerialInfo* info) const
30 : : {
31 [ + - ][ - + ]: 32 : DO_SERIALIZE(SER_LOCATION, SerialObj);
32 : 32 : info->s->WriteOpenTag("Location");
33 : 32 : SERIALIZE(filename);
34 : 32 : SERIALIZE(first_line);
35 : 32 : SERIALIZE(last_line);
36 : 32 : SERIALIZE(first_column);
37 : 32 : SERIALIZE(last_column);
38 : 32 : info->s->WriteCloseTag("Location");
39 : 32 : return true;
40 : : }
41 : :
42 : 0 : bool Location::DoUnserialize(UnserialInfo* info)
43 : : {
44 [ # # ]: 0 : DO_UNSERIALIZE(SerialObj);
45 : :
46 : 0 : delete_data = true;
47 : :
48 : : return UNSERIALIZE_STR(&filename, 0)
49 : : && UNSERIALIZE(&first_line)
50 : : && UNSERIALIZE(&last_line)
51 : : && UNSERIALIZE(&first_column)
52 [ # # ][ # # ]: 0 : && UNSERIALIZE(&last_column);
[ # # ][ # # ]
[ # # ]
53 : : }
54 : :
55 : 0 : bool Location::operator==(const Location& l) const
56 : : {
57 [ # # ][ # # ]: 0 : if ( filename == l.filename ||
[ # # ][ # # ]
[ # # ]
58 : : (filename && l.filename && streq(filename, l.filename)) )
59 [ # # ][ # # ]: 0 : return first_line == l.first_line && last_line == l.last_line;
60 : : else
61 : 0 : return false;
62 : : }
63 : :
64 : : int BroObj::suppress_runtime = 0;
65 : :
66 : 1111037 : BroObj::~BroObj()
67 : : {
68 [ # # ][ # # ]: 1111037 : delete location;
[ + + ]
69 [ # # ][ # # ]: 1111037 : }
[ - + ]
70 : :
71 : 0 : void BroObj::Warn(const char* msg, const BroObj* obj2, int pinpoint_only) const
72 : : {
73 : 0 : DoMsg("warning,", msg, obj2, pinpoint_only);
74 : 0 : ++nwarn;
75 : 0 : }
76 : :
77 : 0 : void BroObj::Error(const char* msg, const BroObj* obj2, int pinpoint_only) const
78 : : {
79 : 0 : DoMsg("error,", msg, obj2, pinpoint_only);
80 : 0 : ++nerr;
81 : 0 : }
82 : :
83 : 0 : void BroObj::RunTime(const char* msg, const BroObj* obj2, int pinpoint_only) const
84 : : {
85 [ # # ]: 0 : if ( ! suppress_runtime )
86 : : {
87 : 0 : DoMsg("run-time error,", msg, obj2, pinpoint_only);
88 : 0 : ++nruntime;
89 : : }
90 : 0 : }
91 : :
92 : 0 : void BroObj::BadTag(const char* msg, const char* t1, const char* t2) const
93 : : {
94 : : char out[512];
95 : :
96 [ # # ]: 0 : if ( t2 )
97 : 0 : snprintf(out, sizeof(out), "%s (%s/%s)", msg, t1, t2);
98 [ # # ]: 0 : else if ( t1 )
99 : 0 : snprintf(out, sizeof(out), "%s (%s)", msg, t1);
100 : : else
101 : 0 : snprintf(out, sizeof(out), "%s", msg);
102 : :
103 : 0 : DoMsg("bad tag in", out);
104 : 0 : Fatal();
105 : 0 : }
106 : :
107 : 0 : void BroObj::Internal(const char* msg) const
108 : : {
109 : 0 : DoMsg("internal error:", msg);
110 : 0 : Fatal();
111 : 0 : }
112 : :
113 : 0 : void BroObj::InternalWarning(const char* msg) const
114 : : {
115 : 0 : DoMsg("internal warning:", msg);
116 : 0 : }
117 : :
118 : 0 : void BroObj::AddLocation(ODesc* d) const
119 : : {
120 [ # # ]: 0 : if ( ! location )
121 : : {
122 : 0 : d->Add("<no location>");
123 : 0 : return;
124 : : }
125 : :
126 [ # # ]: 0 : if ( location->filename )
127 : : {
128 : 0 : d->Add(location->filename);
129 : :
130 [ # # ]: 0 : if ( location->first_line == 0 )
131 : 0 : return;
132 : :
133 : 0 : d->AddSP(",");
134 : : }
135 : :
136 [ # # ]: 0 : if ( location->last_line != location->first_line )
137 : : {
138 : 0 : d->Add("lines ");
139 : 0 : d->Add(location->first_line);
140 : 0 : d->Add("-");
141 : 0 : d->Add(location->last_line);
142 : : }
143 : : else
144 : : {
145 : 0 : d->Add("line ");
146 : 0 : d->Add(location->first_line);
147 : : }
148 : : }
149 : :
150 : 298305 : bool BroObj::SetLocationInfo(const Location* start, const Location* end)
151 : : {
152 [ + - ][ - + ]: 298305 : if ( ! start || ! end )
153 : 0 : return false;
154 : :
155 [ + - ][ + + ]: 298305 : if ( end->filename && ! streq(start->filename, end->filename) )
[ + + ]
156 : 174 : return false;
157 : :
158 [ + + ][ + - ]: 298131 : if ( location && (start == &no_location || end == &no_location) )
[ - + ]
159 : : // We already have a better location, so don't use this one.
160 : 0 : return true;
161 : :
162 [ + + ]: 298131 : delete location;
163 : :
164 : : location = new Location(start->filename,
165 : : start->first_line, end->last_line,
166 : 298131 : start->first_column, end->last_column);
167 : :
168 : 298305 : return true;
169 : : }
170 : :
171 : 5540 : void BroObj::UpdateLocationEndInfo(const Location& end)
172 : : {
173 [ - + ]: 5540 : if ( ! location )
174 : 0 : SetLocationInfo(&end, &end);
175 : :
176 : 5540 : location->last_line = end.last_line;
177 : 5540 : location->last_column = end.last_column;
178 : 5540 : }
179 : :
180 : : void BroObj::DoMsg(const char s1[], const char s2[], const BroObj* obj2,
181 : 0 : int pinpoint_only) const
182 : : {
183 : 0 : ODesc d;
184 : 0 : d.SetShort();
185 : :
186 : 0 : PinPoint(&d, obj2, pinpoint_only);
187 : 0 : d.SP();
188 : 0 : d.Add(s1);
189 : 0 : d.SP();
190 : 0 : d.Add(s2);
191 : 0 : fprintf(stderr, "%s\n", d.Description());
192 : 0 : }
193 : :
194 : 0 : void BroObj::PinPoint(ODesc* d, const BroObj* obj2, int pinpoint_only) const
195 : : {
196 [ # # ]: 0 : if ( network_time > 0.0 )
197 : : {
198 : : char time[256];
199 : 0 : safe_snprintf(time, sizeof(time), "%.6f", network_time);
200 : 0 : d->Add(time);
201 : 0 : d->SP();
202 : : }
203 : :
204 : 0 : AddLocation(d);
205 [ # # # # ]: 0 : if ( obj2 && obj2->GetLocationInfo() != &no_location &&
[ # # ][ # # ]
206 : : *obj2->GetLocationInfo() != *GetLocationInfo() )
207 : : {
208 : 0 : d->Add(" and ");
209 : 0 : obj2->AddLocation(d);
210 : 0 : d->Add("\n ");
211 : : }
212 : :
213 : 0 : d->Add(" (");
214 : 0 : Describe(d);
215 [ # # # # ]: 0 : if ( obj2 && ! pinpoint_only )
216 : : {
217 : 0 : d->Add(" and ");
218 : 0 : obj2->Describe(d);
219 : : }
220 : :
221 : 0 : d->Add("):");
222 : 0 : }
223 : :
224 : 0 : void BroObj::Fatal() const
225 : : {
226 : : #ifdef DEBUG_BRO
227 : : internal_error("BroObj::Fatal()");
228 : : #endif
229 : 0 : exit(1);
230 : : }
231 : :
232 : 33 : bool BroObj::DoSerialize(SerialInfo* info) const
233 : : {
234 [ - + ][ - + ]: 33 : DO_SERIALIZE(SER_BRO_OBJ, SerialObj);
235 : :
236 : 33 : info->s->WriteOpenTag("Object");
237 : :
238 [ + - ]: 33 : Location* loc = info->include_locations ? location : 0;
239 [ + + ][ + - ]: 33 : SERIALIZE_OPTIONAL(loc);
[ - + ][ - + ]
[ - + ][ - + ]
240 : 33 : info->s->WriteCloseTag("Object");
241 : :
242 : 33 : return true;
243 : : }
244 : :
245 : 0 : bool BroObj::DoUnserialize(UnserialInfo* info)
246 : : {
247 [ # # ]: 0 : DO_UNSERIALIZE(SerialObj);
248 : :
249 [ # # ][ # # ]: 0 : UNSERIALIZE_OPTIONAL(location, Location::Unserialize(info));
[ # # ]
250 : 0 : return true;
251 : : }
252 : :
253 : 0 : void print(const BroObj* obj)
254 : : {
255 [ # # ][ # # ]: 0 : static BroFile fstderr(stderr);
[ # # ]
256 : 0 : ODesc d(DESC_READABLE, &fstderr);
257 : 0 : obj->Describe(&d);
258 : 0 : d.Add("\n");
259 : 0 : }
260 : :
261 : 0 : void bad_ref(int type)
262 : : {
263 : 0 : internal_error("bad reference count [%d]", type);
264 : : abort();
265 : : }
266 : :
267 : 3 : void bro_obj_delete_func(void* v)
268 : : {
269 : 3 : Unref((BroObj*) v);
270 [ + - ][ + - ]: 9 : }
|