debugInfo.cpp revision 3602:da91efe96a93
1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "code/debugInfo.hpp"
27#include "code/debugInfoRec.hpp"
28#include "code/nmethod.hpp"
29#include "runtime/handles.inline.hpp"
30
31// Comstructors
32
33DebugInfoWriteStream::DebugInfoWriteStream(DebugInformationRecorder* recorder, int initial_size)
34: CompressedWriteStream(initial_size) {
35  _recorder = recorder;
36}
37
38// Serializing oops
39
40void DebugInfoWriteStream::write_handle(jobject h) {
41  write_int(recorder()->oop_recorder()->find_index(h));
42}
43
44void DebugInfoWriteStream::write_metadata(Metadata* h) {
45  write_int(recorder()->oop_recorder()->find_index(h));
46}
47
48ScopeValue* DebugInfoReadStream::read_object_value() {
49  int id = read_int();
50#ifdef ASSERT
51  assert(_obj_pool != NULL, "object pool does not exist");
52  for (int i = _obj_pool->length() - 1; i >= 0; i--) {
53    assert(((ObjectValue*) _obj_pool->at(i))->id() != id, "should not be read twice");
54  }
55#endif
56  ObjectValue* result = new ObjectValue(id);
57  // Cache the object since an object field could reference it.
58  _obj_pool->push(result);
59  result->read_object(this);
60  return result;
61}
62
63ScopeValue* DebugInfoReadStream::get_cached_object() {
64  int id = read_int();
65  assert(_obj_pool != NULL, "object pool does not exist");
66  for (int i = _obj_pool->length() - 1; i >= 0; i--) {
67    ObjectValue* ov = (ObjectValue*) _obj_pool->at(i);
68    if (ov->id() == id) {
69      return ov;
70    }
71  }
72  ShouldNotReachHere();
73  return NULL;
74}
75
76// Serializing scope values
77
78enum { LOCATION_CODE = 0, CONSTANT_INT_CODE = 1,  CONSTANT_OOP_CODE = 2,
79                          CONSTANT_LONG_CODE = 3, CONSTANT_DOUBLE_CODE = 4,
80                          OBJECT_CODE = 5,        OBJECT_ID_CODE = 6 };
81
82ScopeValue* ScopeValue::read_from(DebugInfoReadStream* stream) {
83  ScopeValue* result = NULL;
84  switch(stream->read_int()) {
85   case LOCATION_CODE:        result = new LocationValue(stream);        break;
86   case CONSTANT_INT_CODE:    result = new ConstantIntValue(stream);     break;
87   case CONSTANT_OOP_CODE:    result = new ConstantOopReadValue(stream); break;
88   case CONSTANT_LONG_CODE:   result = new ConstantLongValue(stream);    break;
89   case CONSTANT_DOUBLE_CODE: result = new ConstantDoubleValue(stream);  break;
90   case OBJECT_CODE:          result = stream->read_object_value();      break;
91   case OBJECT_ID_CODE:       result = stream->get_cached_object();      break;
92   default: ShouldNotReachHere();
93  }
94  return result;
95}
96
97// LocationValue
98
99LocationValue::LocationValue(DebugInfoReadStream* stream) {
100  _location = Location(stream);
101}
102
103void LocationValue::write_on(DebugInfoWriteStream* stream) {
104  stream->write_int(LOCATION_CODE);
105  location().write_on(stream);
106}
107
108void LocationValue::print_on(outputStream* st) const {
109  location().print_on(st);
110}
111
112// ObjectValue
113
114void ObjectValue::read_object(DebugInfoReadStream* stream) {
115  _klass = read_from(stream);
116  assert(_klass->is_constant_oop(), "should be constant java mirror oop");
117  int length = stream->read_int();
118  for (int i = 0; i < length; i++) {
119    ScopeValue* val = read_from(stream);
120    _field_values.append(val);
121  }
122}
123
124void ObjectValue::write_on(DebugInfoWriteStream* stream) {
125  if (_visited) {
126    stream->write_int(OBJECT_ID_CODE);
127    stream->write_int(_id);
128  } else {
129    _visited = true;
130    stream->write_int(OBJECT_CODE);
131    stream->write_int(_id);
132    _klass->write_on(stream);
133    int length = _field_values.length();
134    stream->write_int(length);
135    for (int i = 0; i < length; i++) {
136      _field_values.at(i)->write_on(stream);
137    }
138  }
139}
140
141void ObjectValue::print_on(outputStream* st) const {
142  st->print("obj[%d]", _id);
143}
144
145void ObjectValue::print_fields_on(outputStream* st) const {
146#ifndef PRODUCT
147  if (_field_values.length() > 0) {
148    _field_values.at(0)->print_on(st);
149  }
150  for (int i = 1; i < _field_values.length(); i++) {
151    st->print(", ");
152    _field_values.at(i)->print_on(st);
153  }
154#endif
155}
156
157// ConstantIntValue
158
159ConstantIntValue::ConstantIntValue(DebugInfoReadStream* stream) {
160  _value = stream->read_signed_int();
161}
162
163void ConstantIntValue::write_on(DebugInfoWriteStream* stream) {
164  stream->write_int(CONSTANT_INT_CODE);
165  stream->write_signed_int(value());
166}
167
168void ConstantIntValue::print_on(outputStream* st) const {
169  st->print("%d", value());
170}
171
172// ConstantLongValue
173
174ConstantLongValue::ConstantLongValue(DebugInfoReadStream* stream) {
175  _value = stream->read_long();
176}
177
178void ConstantLongValue::write_on(DebugInfoWriteStream* stream) {
179  stream->write_int(CONSTANT_LONG_CODE);
180  stream->write_long(value());
181}
182
183void ConstantLongValue::print_on(outputStream* st) const {
184  st->print(INT64_FORMAT, value());
185}
186
187// ConstantDoubleValue
188
189ConstantDoubleValue::ConstantDoubleValue(DebugInfoReadStream* stream) {
190  _value = stream->read_double();
191}
192
193void ConstantDoubleValue::write_on(DebugInfoWriteStream* stream) {
194  stream->write_int(CONSTANT_DOUBLE_CODE);
195  stream->write_double(value());
196}
197
198void ConstantDoubleValue::print_on(outputStream* st) const {
199  st->print("%f", value());
200}
201
202// ConstantOopWriteValue
203
204void ConstantOopWriteValue::write_on(DebugInfoWriteStream* stream) {
205  assert(JNIHandles::resolve(value()) == NULL ||
206         Universe::heap()->is_in_reserved(JNIHandles::resolve(value())),
207         "Should be in heap");
208  stream->write_int(CONSTANT_OOP_CODE);
209  stream->write_handle(value());
210}
211
212void ConstantOopWriteValue::print_on(outputStream* st) const {
213  JNIHandles::resolve(value())->print_value_on(st);
214}
215
216
217// ConstantOopReadValue
218
219ConstantOopReadValue::ConstantOopReadValue(DebugInfoReadStream* stream) {
220  _value = Handle(stream->read_oop());
221  assert(_value() == NULL ||
222         Universe::heap()->is_in_reserved(_value()), "Should be in heap");
223}
224
225void ConstantOopReadValue::write_on(DebugInfoWriteStream* stream) {
226  ShouldNotReachHere();
227}
228
229void ConstantOopReadValue::print_on(outputStream* st) const {
230  value()()->print_value_on(st);
231}
232
233
234// MonitorValue
235
236MonitorValue::MonitorValue(ScopeValue* owner, Location basic_lock, bool eliminated) {
237  _owner       = owner;
238  _basic_lock  = basic_lock;
239  _eliminated  = eliminated;
240}
241
242MonitorValue::MonitorValue(DebugInfoReadStream* stream) {
243  _basic_lock  = Location(stream);
244  _owner       = ScopeValue::read_from(stream);
245  _eliminated  = (stream->read_bool() != 0);
246}
247
248void MonitorValue::write_on(DebugInfoWriteStream* stream) {
249  _basic_lock.write_on(stream);
250  _owner->write_on(stream);
251  stream->write_bool(_eliminated);
252}
253
254#ifndef PRODUCT
255void MonitorValue::print_on(outputStream* st) const {
256  st->print("monitor{");
257  owner()->print_on(st);
258  st->print(",");
259  basic_lock().print_on(st);
260  st->print("}");
261  if (_eliminated) {
262    st->print(" (eliminated)");
263  }
264}
265#endif
266