Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / nonnacl_util / sel_ldr_launcher.cc
blob6c538adc594652fe28955c012c700c8780071ad6
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "native_client/nonnacl_util/sel_ldr_launcher.h"
35 #include <stdio.h>
36 #include <sys/types.h>
38 #include "native_client/include/nacl_elf.h"
40 namespace nacl {
42 SelLdrLauncher::SelLdrLauncher() :
43 child_(kInvalidHandle),
44 channel_(kInvalidHandle),
45 argc_(-1),
46 argv_(NULL),
47 is_sel_ldr_(true) {
50 void SelLdrLauncher::BuildArgv(const char* sel_ldr_pathname,
51 const char* application_name,
52 int imc_fd,
53 Handle imc_channel_handle,
54 int sel_ldr_argc,
55 const char* sel_ldr_argv[],
56 int application_argc,
57 const char* application_argv[]) {
58 #if NACL_WINDOWS
59 _snprintf_s(channel_buf_, sizeof(channel_buf_), sizeof(channel_buf_),
60 "%d:%u", imc_fd, imc_channel_handle);
61 _snprintf_s(channel_number_, sizeof(channel_number_), sizeof(channel_number_),
62 "%d", imc_fd);
63 #else
64 snprintf(channel_buf_,
65 sizeof(channel_buf_),
66 "%d:%d",
67 imc_fd,
68 imc_channel_handle);
69 snprintf(channel_number_, sizeof(channel_number_), "%d", imc_fd);
70 #endif
71 // Fixed args are:
72 // .../sel_ldr -f <application_name> -i <NaCl fd>:<imcchannel#>
73 const char* kFixedArgs[] = {
74 const_cast<char*>(sel_ldr_pathname),
75 "-f",
76 const_cast<char*>(application_name),
77 "-i",
78 channel_buf_
80 const int kFixedArgc = sizeof(kFixedArgs) / sizeof(kFixedArgs[0]);
82 argv_ = new char const*[kFixedArgc + sel_ldr_argc
83 + (application_argc + 1) + 1];
84 // We use pre-increment everywhere, so we need to initialize to -1.
85 argc_ = -1;
87 // Copy the fixed arguments to argv_.
88 for (int i = 0; i < kFixedArgc; ++i) {
89 argv_[++argc_] = kFixedArgs[i];
91 // Copy the specified additional arguments (e.g., "-d" or "-P") to sel_ldr
92 for (int i = 0; i < sel_ldr_argc; ++i) {
93 argv_[++argc_] = sel_ldr_argv[i];
95 // Copy the specified arguments to the application
96 if (application_argc > 0) {
97 argv_[++argc_] = "--";
98 for (int i = 0; i < application_argc; ++i) {
99 if (strncmp("$CHAN", application_argv[i], 6) == 0) {
100 argv_[++argc_] = channel_number_;
101 } else {
102 argv_[++argc_] = application_argv[i];
106 // NULL terminate the argument list.
107 argv_[++argc_] = NULL;
110 } // namespace nacl