2 * Copyright (c) 1996-2009 Barton P. Miller
4 * We provide the Paradyn Parallel Performance Tools (below
5 * described as "Paradyn") on an AS IS basis, and do not warrant its
6 * validity or performance. We reserve the right to update, modify,
7 * or discontinue this software at any time. We shall have no
8 * obligation to supply such updates or modifications or any other
9 * form of support to you.
11 * By your use of Paradyn, you understand and agree that we (or any
12 * other person or entity with proprietary rights in Paradyn) are
13 * under no obligation to provide either maintenance services,
14 * update services, notices of latent defects, or correction of
15 * defects for Paradyn.
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "common/h/headers.h"
33 #include "common/src/addrtranslate-sysv.h"
34 #include "common/h/freebsdKludges.h"
38 #include <sys/types.h>
39 #include <sys/ptrace.h>
44 using namespace Dyninst;
46 class ProcessReaderPtrace : public ProcessReader {
50 ProcessReaderPtrace(int pid_);
52 virtual bool ReadMem(Address inTraced, void *inSelf, unsigned amount);
53 virtual bool GetReg(MachRegister /*reg*/, MachRegisterVal &/*val*/) {
59 virtual ~ProcessReaderPtrace();
62 bool ProcessReaderPtrace::start() {
63 if( 0 != ptrace(PT_ATTACH, pid, (caddr_t)1, 0) ) {
64 translate_printf("[%s:%u] - Failed to attach to process %u: %s\n",
65 __FILE__, __LINE__, pid, strerror(errno));
71 int result = (long) waitpid(pid, &status, 0);
72 if (result == -1 && errno == EINTR) {
74 } else if (result == -1 || WIFEXITED(status) || WIFSIGNALED(status)) {
75 translate_printf("[%s:%u] - Failed to stop process %u: %s\n",
76 __FILE__, __LINE__, pid, strerror(errno));
79 } else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGSTOP
80 || WSTOPSIG(status) == SIGTRAP) ) {
82 } else if (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) {
83 if( 0 != ptrace(PT_CONTINUE, pid, (caddr_t)1, WSTOPSIG(status)) ) {
84 translate_printf("[%s:%u] - Failed to continue process %u: %s\n"
85 __FILE__, __LINE__, pid, strerror(errno));
90 translate_printf("[%s:%u] - Unknown error encountered for process %u: %s\n",
91 __FILE__, __LINE__, pid, strerror(errno));
100 bool ProcessReaderPtrace::done()
102 return ( 0 != ptrace(PT_DETACH, pid, (caddr_t)1, 0) ) ? true : false;
105 ProcessReaderPtrace::ProcessReaderPtrace(int pid_) :
110 ProcessReaderPtrace::~ProcessReaderPtrace()
114 bool ProcessReaderPtrace::ReadMem(Address inTraced, void *inSelf, unsigned amount) {
115 bool result = PtraceBulkRead(inTraced, amount, inSelf, pid);
117 translate_printf("[%s:%u] - Failed to read memory from process: %s\n",
118 __FILE__, __LINE__, strerror(errno));
124 /* Complete the implementation of the AddressTranslateSysV class */
126 ProcessReader *AddressTranslateSysV::createDefaultDebugger(int pid)
128 return new ProcessReaderPtrace(pid);
132 bool AddressTranslateSysV::setInterpreter() {
133 if( interpreter ) return true;
135 string l_exec = getExecName();
136 if( l_exec.empty() ) {
140 FCNode *exe = files.getNode(l_exec, symfactory);
142 translate_printf("[%s:%u] - Failed to get FCNode for %s\n",
143 __FILE__, __LINE__, l_exec.c_str());
147 if( exe->getInterpreter().empty() ) {
148 translate_printf("[%s:%u] - No interpreter found\n",
153 interpreter = files.getNode(exe->getInterpreter(), symfactory);
154 if( interpreter ) interpreter->markInterpreter();
156 translate_printf("[%s:%u] - Failed to set interpreter for %s\n",
157 __FILE__, __LINE__, l_exec.c_str());
164 bool AddressTranslateSysV::setAddressSize() {
165 if (address_size) return true;
167 if( (address_size = sysctl_computeAddrWidth(pid)) == -1 ) {
175 bool AddressTranslateSysV::setInterpreterBase() {
176 if( set_interp_base ) return true;
178 string l_exec = getExecName();
179 if( l_exec.empty() ) {
183 FCNode *exe = files.getNode(l_exec, symfactory);
185 translate_printf("[%s:%u] - Failed to get FCNode for %s\n",
186 __FILE__, __LINE__, l_exec.c_str());
191 map_entries *maps = getVMMaps(pid, maps_size);
193 translate_printf("[%s:%u] - Failed to get VM maps\n",
198 string interp_name = exe->getInterpreter();
199 for(unsigned i = 0; i < maps_size; ++i) {
200 if( string(maps[i].path) == interp_name ) {
201 interpreter_base = maps[i].start;
202 set_interp_base = true;
208 if( !set_interp_base ) {
209 translate_printf("[%s:%u] - Failed to get locate interpreter in memory map\n",
217 const string& AddressTranslateSysV::getExecName() {
218 if( exec_name.empty() ) {
219 char *pathname = sysctl_getExecPathname(pid);
220 if( NULL != pathname ) {
221 exec_name = std::string(pathname);
229 LoadedLib *AddressTranslateSysV::getAOut()
231 // TODO: shouldn't this just return exec if it's set?
232 LoadedLib *ll = new LoadedLib(getExecName(), 0);
233 ll->setFactory(symfactory);