Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: testing/android/native_test_launcher.cc

Issue 10310046: Detect crashes while running native tests in APK. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« build/android/test_result.py ('K') | « build/android/test_result.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This class sets up the environment for running the native tests inside an
6 // android application. It outputs (to logcat) markers identifying the
7 // START/END/CRASH of the test suite, FAILURE/SUCCESS of individual tests etc.
8 // These markers are read by the test runner script to generate test results.
9 // It injects an event listener in gtest to detect various test stages and
10 // installs signal handlers to detect crashes.
11
12 #include <android/log.h>
13 #include <signal.h>
5 #include <stdio.h> 14 #include <stdio.h>
6 15
7 #include "base/android/jni_android.h" 16 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h" 17 #include "base/android/jni_string.h"
9 #include "base/android/path_utils.h" 18 #include "base/android/path_utils.h"
10 #include "base/android/scoped_java_ref.h" 19 #include "base/android/scoped_java_ref.h"
11 #include "base/at_exit.h" 20 #include "base/at_exit.h"
12 #include "base/command_line.h" 21 #include "base/command_line.h"
13 #include "base/file_path.h" 22 #include "base/file_path.h"
14 #include "base/file_util.h" 23 #include "base/file_util.h"
15 #include "base/logging.h" 24 #include "base/logging.h"
16 #include "base/stringprintf.h" 25 #include "base/stringprintf.h"
17 #include "base/string_tokenizer.h" 26 #include "base/string_tokenizer.h"
18 #include "base/string_util.h" 27 #include "base/string_util.h"
19 #include "base/test/test_suite.h" 28 #include "base/test/test_suite.h"
20 #include "testing/android/jni/chrome_native_test_activity_jni.h" 29 #include "testing/android/jni/chrome_native_test_activity_jni.h"
21 #include "gtest/gtest.h" 30 #include "gtest/gtest.h"
22 31
23 // GTest's main function. 32 // GTest's main function.
24 extern int main(int argc, char** argv); 33 extern int main(int argc, char** argv);
25 34
26 namespace { 35 namespace {
27 36
37 // The list of signals which are considered to be crashes.
38 static const int kExceptionSignals[] = {
John Grabowski 2012/05/08 12:54:10 'static' redundant (see below)
nilesh 2012/05/08 22:18:44 Done.
39 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1
40 };
41
42 static struct sigaction old_sa[NSIG];
John Grabowski 2012/05/08 12:54:10 is inside an anonymous namespace so no need to als
nilesh 2012/05/08 22:18:44 Done.
43
44 // This function runs in a compromised context. It should not allocate memory.
45 void SignalHandler(int sig, siginfo_t *info, void *reserved)
46 {
47 // Output the crash marker.
48 __android_log_write(ANDROID_LOG_ERROR, "chromium", "[ CRASHED ]");
49 old_sa[sig].sa_sigaction(sig, info, reserved);
50 }
51
52 void InstallHandlers() {
53 struct sigaction sa;
54 memset(&sa, 0, sizeof(sa));
55
56 sa.sa_sigaction = SignalHandler;
57 sa.sa_flags = SA_SIGINFO;
58
59 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) {
60 sigaction(kExceptionSignals[i], &sa, &old_sa[kExceptionSignals[i]]);
61 }
62 }
63
28 void ParseArgsFromString(const std::string& command_line, 64 void ParseArgsFromString(const std::string& command_line,
29 std::vector<std::string>* args) { 65 std::vector<std::string>* args) {
30 StringTokenizer tokenizer(command_line, kWhitespaceASCII); 66 StringTokenizer tokenizer(command_line, kWhitespaceASCII);
31 tokenizer.set_quote_chars("\""); 67 tokenizer.set_quote_chars("\"");
32 while (tokenizer.GetNext()) { 68 while (tokenizer.GetNext()) {
33 std::string token; 69 std::string token;
34 RemoveChars(tokenizer.token(), "\"", &token); 70 RemoveChars(tokenizer.token(), "\"", &token);
35 args->push_back(token); 71 args->push_back(token);
36 } 72 }
37 } 73 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 // Set the application context in base. 218 // Set the application context in base.
183 base::android::ScopedJavaLocalRef<jobject> scoped_context( 219 base::android::ScopedJavaLocalRef<jobject> scoped_context(
184 env, env->NewLocalRef(app_context)); 220 env, env->NewLocalRef(app_context));
185 base::android::InitApplicationContext(scoped_context); 221 base::android::InitApplicationContext(scoped_context);
186 222
187 main(argc, &argv[0]); 223 main(argc, &argv[0]);
188 } 224 }
189 225
190 // This is called by the VM when the shared library is first loaded. 226 // This is called by the VM when the shared library is first loaded.
191 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { 227 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
228
229 // Install signal handlers to detect crashes.
230 InstallHandlers();
231
192 base::android::InitVM(vm); 232 base::android::InitVM(vm);
193 JNIEnv* env = base::android::AttachCurrentThread(); 233 JNIEnv* env = base::android::AttachCurrentThread();
194 if (!RegisterNativesImpl(env)) { 234 if (!RegisterNativesImpl(env)) {
195 return -1; 235 return -1;
196 } 236 }
197 LibraryLoadedOnMainThread(env); 237 LibraryLoadedOnMainThread(env);
238
198 return JNI_VERSION_1_4; 239 return JNI_VERSION_1_4;
199 } 240 }
OLDNEW
« build/android/test_result.py ('K') | « build/android/test_result.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698