2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include <TeamDebugger.h>
18 #include <libroot_private.h>
22 BTeamDebugger::BTeamDebugger()
29 BTeamDebugger::~BTeamDebugger()
36 BTeamDebugger::Install(team_id team
)
40 // create a debugger port
41 char name
[B_OS_NAME_LENGTH
];
42 snprintf(name
, sizeof(name
), "debugger for team %" B_PRId32
, team
);
43 fDebuggerPort
= create_port(100, name
);
44 if (fDebuggerPort
< 0)
47 port_id nubPort
= install_team_debugger(team
, fDebuggerPort
);
49 delete_port(fDebuggerPort
);
54 status_t error
= BDebugContext::Init(team
, nubPort
);
56 remove_team_debugger(team
);
57 delete_port(fDebuggerPort
);
67 BTeamDebugger::Uninstall()
72 remove_team_debugger(Team());
74 delete_port(fDebuggerPort
);
76 BDebugContext::Uninit();
85 BTeamDebugger::LoadProgram(const char* const* args
, int32 argCount
,
89 thread_id thread
= _LoadProgram(args
, argCount
, traceLoading
);
93 // install the debugger
94 status_t error
= Install(thread
);
105 BTeamDebugger::ReadDebugMessage(int32
& _messageCode
,
106 debug_debugger_message_data
& messageBuffer
)
108 ssize_t bytesRead
= read_port(fDebuggerPort
, &_messageCode
, &messageBuffer
,
109 sizeof(messageBuffer
));
119 BTeamDebugger::_LoadProgram(const char* const* args
, int32 argCount
,
122 // clone the argument vector so that we can change it
123 const char** mutableArgs
= new const char*[argCount
];
124 for (int i
= 0; i
< argCount
; i
++)
125 mutableArgs
[i
] = args
[i
];
127 // resolve the program path
129 status_t error
= _FindProgram(args
[0], programPath
);
131 delete[] mutableArgs
;
134 mutableArgs
[0] = programPath
.Path();
136 // count environment variables
138 while (environ
[envCount
] != NULL
)
141 // flatten the program args and environment
142 char** flatArgs
= NULL
;
144 error
= __flatten_process_args(mutableArgs
, argCount
, environ
, &envCount
,
145 mutableArgs
[0], &flatArgs
, &flatArgsSize
);
150 thread
= _kern_load_image(flatArgs
, flatArgsSize
, argCount
, envCount
,
151 B_NORMAL_PRIORITY
, (traceLoading
? 0 : B_WAIT_TILL_LOADED
), -1, 0);
157 delete[] mutableArgs
;
164 BTeamDebugger::_FindProgram(const char* programName
, BPath
& resolvedPath
)
166 // If the program name is absolute, then there's nothing to do.
167 // If the program name consists of more than one path element, then we
168 // consider it a relative path and don't search in PATH either.
169 if (*programName
== '/' || strchr(programName
, '/'))
170 return resolvedPath
.SetTo(programName
);
172 // get the PATH environment variable
173 const char* paths
= getenv("PATH");
175 return B_ENTRY_NOT_FOUND
;
177 // iterate through the paths
179 const char* pathEnd
= strchr(paths
, ':');
180 int pathLen
= (pathEnd
? pathEnd
- paths
: strlen(paths
));
182 // We skip empty paths.
184 // get the program path
185 BString
directory(paths
, pathLen
);
186 if (directory
.Length() == 0)
190 status_t error
= path
.SetTo(directory
, programName
);
194 // stat() the path to be sure, there is a file
196 if (stat(path
.Path(), &st
) == 0 && S_ISREG(st
.st_mode
)) {
202 paths
= (pathEnd
? pathEnd
+ 1 : NULL
);
206 return B_ENTRY_NOT_FOUND
;