blob: 2443cc6ba9e41104accd0a1a0e3edf81042d343f [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07004
Elliott Hughesffe67362011-07-17 12:09:27 -07005#include <cstdio>
6#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -07007#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -07009
Elliott Hughes18c07532011-08-18 15:50:51 -070010#include "JniConstants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "class_linker.h"
12#include "heap.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070013#include "jni_internal.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070014#include "scoped_ptr.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070015#include "signal_catcher.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "thread.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070017
Carl Shapiro1fb86202011-06-27 17:43:13 -070018namespace art {
19
Carl Shapiro2ed144c2011-07-26 16:52:08 -070020Runtime* Runtime::instance_ = NULL;
21
Carl Shapiro61e019d2011-07-14 16:53:09 -070022Runtime::~Runtime() {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070023 // TODO: use smart pointers instead. (we'll need the pimpl idiom.)
Carl Shapiro61e019d2011-07-14 16:53:09 -070024 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070025 Heap::Destroy();
Elliott Hughese27955c2011-08-26 15:21:24 -070026 delete signal_catcher_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070027 delete thread_list_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070028 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070029 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070030 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070031 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070032 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070033}
34
Elliott Hughesffe67362011-07-17 12:09:27 -070035void Runtime::Abort(const char* file, int line) {
36 // Get any pending output out of the way.
37 fflush(NULL);
38
39 // Many people have difficulty distinguish aborts from crashes,
40 // so be explicit.
41 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
42
Elliott Hughesffe67362011-07-17 12:09:27 -070043 // Perform any platform-specific pre-abort actions.
44 PlatformAbort(file, line);
45
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070046 // use abort hook if we have one
47 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
48 Runtime::Current()->abort_();
49 // notreached
50 }
51
Elliott Hughesffe67362011-07-17 12:09:27 -070052 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070053 // receive SIGABRT. debuggerd dumps the stack trace of the main
54 // thread, whether or not that was the thread that failed. By
55 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070056 // fault in the current thread, and get a useful log from debuggerd.
57 // We can also trivially tell the difference between a VM crash and
58 // a deliberate abort by looking at the fault address.
59 *reinterpret_cast<char*>(0xdeadd00d) = 38;
60 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070061 // notreached
62}
63
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070064// Splits a C string using the given delimiter characters into a vector of
65// strings. Empty strings will be omitted.
66void Split(const char* str, const char* delim, std::vector<std::string>& vec) {
67 DCHECK(str != NULL);
68 DCHECK(delim != NULL);
69 scoped_ptr_malloc<char> tmp(strdup(str));
Carl Shapirofc322c72011-07-27 00:20:01 -070070 char* full = tmp.get();
71 char* p = full;
Carl Shapirod3c15752011-07-27 16:27:22 -070072 while (p != NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070073 p = strpbrk(full, delim);
Carl Shapirofc322c72011-07-27 00:20:01 -070074 if (p != NULL) {
75 p[0] = '\0';
76 }
77 if (full[0] != '\0') {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070078 vec.push_back(std::string(full));
Carl Shapirofc322c72011-07-27 00:20:01 -070079 }
Carl Shapirod3c15752011-07-27 16:27:22 -070080 if (p != NULL) {
Carl Shapirofc322c72011-07-27 00:20:01 -070081 full = p + 1;
82 }
83 }
84}
85
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070086// Splits a colon delimited list of pathname elements into a vector of
87// strings. Empty strings will be omitted.
88void ParseClassPath(const char* class_path, std::vector<std::string>& vec) {
89 Split(class_path, ":", vec);
90}
Brian Carlstrom8a436592011-08-15 21:27:23 -070091
92// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
93// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
94// [gG] gigabytes.
95//
96// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070097// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
98// of 1024.
99//
100// The spec says the -Xmx and -Xms options must be multiples of 1024. It
101// doesn't say anything about -Xss.
102//
103// Returns 0 (a useless size) if "s" is malformed or specifies a low or
104// non-evenly-divisible value.
105//
106size_t ParseMemoryOption(const char *s, size_t div) {
107 // strtoul accepts a leading [+-], which we don't want,
108 // so make sure our string starts with a decimal digit.
109 if (isdigit(*s)) {
110 const char *s2;
111 size_t val = strtoul(s, (char **)&s2, 10);
112 if (s2 != s) {
113 // s2 should be pointing just after the number.
114 // If this is the end of the string, the user
115 // has specified a number of bytes. Otherwise,
116 // there should be exactly one more character
117 // that specifies a multiplier.
118 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700119 // The remainder of the string is either a single multiplier
120 // character, or nothing to indicate that the value is in
121 // bytes.
122 char c = *s2++;
123 if (*s2 == '\0') {
124 size_t mul;
125 if (c == '\0') {
126 mul = 1;
127 } else if (c == 'k' || c == 'K') {
128 mul = KB;
129 } else if (c == 'm' || c == 'M') {
130 mul = MB;
131 } else if (c == 'g' || c == 'G') {
132 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700133 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700134 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700135 return 0;
136 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700137
138 if (val <= std::numeric_limits<size_t>::max() / mul) {
139 val *= mul;
140 } else {
141 // Clamp to a multiple of 1024.
142 val = std::numeric_limits<size_t>::max() & ~(1024-1);
143 }
144 } else {
145 // There's more than one character after the numeric part.
146 return 0;
147 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700148 }
149 // The man page says that a -Xm value must be a multiple of 1024.
150 if (val % div == 0) {
151 return val;
152 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700153 }
154 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700155 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700156}
157
Elliott Hughes0af55432011-08-17 18:37:28 -0700158void LoadJniLibrary(JavaVMExt* vm, const char* name) {
159 // TODO: OS_SHARED_LIB_FORMAT_STR
160 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700161 std::string reason;
162 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700163 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
164 << reason;
165 }
166}
167
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700168DexFile* Open(const std::string& filename) {
169 if (filename.size() < 4) {
170 LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'";
171 return NULL;
172 }
173 std::string suffix(filename.substr(filename.size() - 4));
174 if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") {
175 return DexFile::OpenZip(filename);
176 } else {
177 return DexFile::OpenFile(filename);
178 }
179}
180
Brian Carlstrom8a436592011-08-15 21:27:23 -0700181void CreateBootClassPath(const char* boot_class_path_cstr,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700182 std::vector<const DexFile*>& boot_class_path_vector) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700183 CHECK(boot_class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700184 std::vector<std::string> parsed;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700185 ParseClassPath(boot_class_path_cstr, parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700186 for (size_t i = 0; i < parsed.size(); ++i) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700187 DexFile* dex_file = Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700188 if (dex_file != NULL) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700189 boot_class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700190 }
191 }
192}
193
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700194Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
195 scoped_ptr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700196 const char* boot_class_path = getenv("BOOTCLASSPATH");
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700197 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700198#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700199 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700200 parsed->check_jni_ = false;
201#else
202 // ...but on by default in debug builds.
203 parsed->check_jni_ = true;
204#endif
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700205 parsed->heap_initial_size_ = Heap::kInitialSize;
206 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700207 parsed->stack_size_ = Thread::kDefaultStackSize;
208
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700209 parsed->hook_vfprintf_ = vfprintf;
210 parsed->hook_exit_ = exit;
211 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700212
213 for (size_t i = 0; i < options.size(); ++i) {
214 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700215 if (option.starts_with("-Xbootclasspath:")) {
216 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
217 } else if (option == "bootclasspath") {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700218 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700219 const std::vector<const DexFile*>* v
220 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700221 if (v == NULL) {
222 if (ignore_unrecognized) {
223 continue;
224 }
225 // TODO: usage
226 LOG(FATAL) << "Could not parse " << option;
227 return NULL;
228 }
229 parsed->boot_class_path_ = *v;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700230 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700231 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700232 } else if (option.starts_with("-Xcheck:jni")) {
233 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700234 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700235 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
236 if (size == 0) {
237 if (ignore_unrecognized) {
238 continue;
239 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700240 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700241 LOG(FATAL) << "Could not parse " << option;
242 return NULL;
243 }
244 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700245 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700246 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
247 if (size == 0) {
248 if (ignore_unrecognized) {
249 continue;
250 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700251 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700252 LOG(FATAL) << "Could not parse " << option;
253 return NULL;
254 }
255 parsed->heap_maximum_size_ = size;
256 } else if (option.starts_with("-Xss")) {
257 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
258 if (size == 0) {
259 if (ignore_unrecognized) {
260 continue;
261 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700262 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700263 LOG(FATAL) << "Could not parse " << option;
264 return NULL;
265 }
266 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700267 } else if (option.starts_with("-D")) {
268 parsed->properties_.push_back(option.substr(strlen("-D")).data());
269 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700270 std::vector<std::string> verbose_options;
271 Split(option.substr(strlen("-verbose:")).data(), ",", verbose_options);
272 for (size_t i = 0; i < verbose_options.size(); ++i) {
273 parsed->verbose_.insert(verbose_options[i]);
274 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700275 } else if (option == "vfprintf") {
276 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
277 } else if (option == "exit") {
278 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
279 } else if (option == "abort") {
280 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700281 } else {
282 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700283 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700284 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700285 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700286 }
287 }
288 }
289
290 if (boot_class_path == NULL) {
291 boot_class_path = "";
292 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700293 if (parsed->boot_class_path_.size() == 0) {
294 CreateBootClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700295 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700296 return parsed.release();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700297}
298
Brian Carlstrom8a436592011-08-15 21:27:23 -0700299Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) {
300 Runtime::Options options;
301 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
302 return Runtime::Create(options, false);
303}
304
305Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700306 // TODO: acquire a static mutex on Runtime to avoid racing.
307 if (Runtime::instance_ != NULL) {
308 return NULL;
309 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700310 scoped_ptr<Runtime> runtime(new Runtime());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700311 bool success = runtime->Init(options, ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700312 if (!success) {
313 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700314 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700315 instance_ = runtime.release();
316
317 // Most JNI libraries can just use System.loadLibrary, but you can't
318 // if you're the library that implements System.loadLibrary!
Elliott Hughes18c07532011-08-18 15:50:51 -0700319 Thread* self = Thread::Current();
320 Thread::State old_state = self->GetState();
321 self->SetState(Thread::kNative);
322 JniConstants::init(self->GetJniEnv());
Elliott Hughes0af55432011-08-17 18:37:28 -0700323 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes18c07532011-08-18 15:50:51 -0700324 self->SetState(old_state);
Elliott Hughes0af55432011-08-17 18:37:28 -0700325
Elliott Hughese27955c2011-08-26 15:21:24 -0700326 instance_->signal_catcher_ = new SignalCatcher;
327
Elliott Hughes0af55432011-08-17 18:37:28 -0700328 return instance_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700329}
330
Elliott Hughes0af55432011-08-17 18:37:28 -0700331bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700332 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700333
Elliott Hughes0af55432011-08-17 18:37:28 -0700334 scoped_ptr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
335 if (options == NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700336 return false;
337 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700338 vfprintf_ = options->hook_vfprintf_;
339 exit_ = options->hook_exit_;
340 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700341
Brian Carlstromb765be02011-08-17 23:54:10 -0700342 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700343 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700344
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700345 if (!Heap::Init(options->heap_initial_size_,
346 options->heap_maximum_size_,
347 options->boot_image_)) {
348 return false;
349 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700350
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700351 BlockSignals();
352
Elliott Hughes0af55432011-08-17 18:37:28 -0700353 bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end();
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700354 java_vm_ = new JavaVMExt(this, options->check_jni_, verbose_jni);
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700355
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700356 if (!Thread::Startup()) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700357 return false;
358 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700359 Thread* current_thread = Thread::Attach(this);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700360 thread_list_->Register(current_thread);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700361
Brian Carlstroma663ea52011-08-19 23:33:41 -0700362 class_linker_ = ClassLinker::Create(options->boot_class_path_, Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700363
Carl Shapiro1fb86202011-06-27 17:43:13 -0700364 return true;
365}
366
Elliott Hughese27955c2011-08-26 15:21:24 -0700367void Runtime::DumpStatistics(std::ostream& os) {
368 // TODO: dump other runtime statistics?
369 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
370 os << "Intern table size: " << class_linker_->GetInternTable().Size() << "\n";
371 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
372 // gDvm.numDeclaredMethods,
373 // gDvm.numDeclaredInstFields,
374 // gDvm.numDeclaredStaticFields,
375 // gDvm.pBootLoaderAlloc->curOffset);
376 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
377}
378
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700379void Runtime::BlockSignals() {
380 sigset_t sigset;
381 if (sigemptyset(&sigset) == -1) {
382 PLOG(FATAL) << "sigemptyset failed";
383 }
384 if (sigaddset(&sigset, SIGPIPE) == -1) {
385 PLOG(ERROR) << "sigaddset SIGPIPE failed";
386 }
387 // SIGQUIT is used to dump the runtime's state (including stack traces).
388 if (sigaddset(&sigset, SIGQUIT) == -1) {
389 PLOG(ERROR) << "sigaddset SIGQUIT failed";
390 }
391 // SIGUSR1 is used to initiate a heap dump.
392 if (sigaddset(&sigset, SIGUSR1) == -1) {
393 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
394 }
395 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
396}
397
Elliott Hughes75770752011-08-24 17:52:38 -0700398bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) {
399 if (as_daemon) {
400 UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads";
401 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700402 return Thread::Attach(instance_) != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700403}
404
Ian Rogersb033c752011-07-20 12:22:35 -0700405bool Runtime::DetachCurrentThread() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700406 Thread* self = Thread::Current();
407 thread_list_->Unregister(self);
408 delete self;
Ian Rogersb033c752011-07-20 12:22:35 -0700409 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700410}
411
412} // namespace art