00001
00020 #include "mfcpch.h"
00021 #include <stdarg.h>
00022 #include "debugwin.h"
00023
00026 DLLSYM INT_VAR (debug_lines, 256, "Number of lines in debug window");
00029 #ifndef GRAPHICS_DISABLED
00030
00031 #ifdef __MAC__
00032 #include <ltextedit.h>
00033 #include <lwindow.h>
00034
00035
00036 #define scrl_SCROLLER 101
00037 #define text_FLOWED 100
00038
00039 static LCommander *pCommander = NULL;
00040 #endif
00041
00042
00043 #if defined(__MSW32__) && !defined(_CONSOLE)
00044
00045 #include <io.h>
00046 #define ID_DEBUG_MSG 32779
00047
00059 DEBUG_WIN::DEBUG_WIN(
00060 const char *title,
00061 INT32 xpos,
00062 INT32 ypos,
00063 INT32 xsize,
00064 INT32 ysize,
00065 INT32 buflines
00066 ) {
00067 char cmd[1024];
00068 int parm;
00069 STARTUPINFO start_info;
00070 PROCESS_INFORMATION proc_info;
00071 SECURITY_ATTRIBUTES security;
00072
00073 handle = NULL;
00074 shm_hand = NULL;
00075 shm_mem = NULL;
00076 msg_end = NULL;
00077 dbg_process = NULL;
00078 dbg_thread = NULL;
00079 security.nLength = sizeof (security);
00080 security.lpSecurityDescriptor = NULL;
00081 security.bInheritHandle = TRUE;
00082
00083 shm_hand = CreateFileMapping ((HANDLE) 0xffffffff,
00084 &security, PAGE_READWRITE, 0, 4096, NULL);
00085 if (shm_hand == NULL)
00086 return;
00087 shm_mem = (char *) MapViewOfFile (shm_hand, FILE_MAP_WRITE, 0, 0, 0);
00088 if (shm_mem == NULL)
00089 return;
00090 shm_mem[5] = 0;
00091 sprintf (cmd, "scrolwin.exe %d %d", buflines, shm_hand);
00092 GetStartupInfo(&start_info);
00093 if (!CreateProcess (NULL, cmd, NULL, NULL, TRUE,
00094 CREATE_NO_WINDOW | DETACHED_PROCESS | CREATE_SUSPENDED,
00095 NULL, NULL, &start_info, &proc_info))
00096 return;
00097
00098
00099 dbg_process = proc_info.hProcess;
00100 dbg_thread = proc_info.hThread;
00101 if (ResumeThread (dbg_thread) != 1)
00102 return;
00103 do
00104 Sleep (100);
00105 while (shm_mem[5] == 0);
00106 parm = ((((UINT8) shm_mem[4] << 8) + (UINT8) shm_mem[3] << 8)
00107 + (UINT8) shm_mem[2] << 8) + (UINT8) shm_mem[1];
00108 handle = (HWND) parm;
00109 if (handle != NULL) {
00110
00111 ::SetWindowText (handle, title);
00112 ::MoveWindow (handle, xpos, ypos, xsize, ysize, TRUE);
00113 ::ShowWindow (handle, SW_SHOW);
00114 }
00115 }
00116
00117
00118
00122 DEBUG_WIN::~DEBUG_WIN (
00123 ) {
00124 if (IsWindow (handle))
00125 ::SendMessage (handle, WM_COMMAND, IDOK, 0);
00126 if (shm_mem != NULL)
00127 UnmapViewOfFile(shm_mem);
00128 if (shm_hand != NULL)
00129 CloseHandle(shm_hand);
00130 if (dbg_thread != NULL)
00131 CloseHandle(dbg_thread);
00132 if (dbg_process == NULL)
00133 CloseHandle(dbg_process);
00134
00135 }
00136
00137
00138
00145 void
00146 DEBUG_WIN::dprintf (
00147 const char *format, ...
00148 ) {
00149 va_list args;
00150 char *msg_start;
00151
00152 if (!IsWindow (handle))
00153 return;
00154 if (msg_end == NULL)
00155 msg_end = shm_mem + 1;
00156 va_start(args, format);
00157
00158 vsprintf(msg_end, format, args);
00159 va_end(args);
00160 if (*msg_end == '\0')
00161 return;
00162 msg_start = shm_mem + 1;
00163 do {
00164
00165 msg_end = strchr (msg_start, '\n');
00166 if (msg_end == NULL) {
00167 if (msg_start != shm_mem + 1)
00168
00169 strcpy (shm_mem + 1, msg_start);
00170
00171 msg_end = shm_mem + 1 + strlen (shm_mem + 1);
00172 return;
00173 }
00174 *msg_end = '\0';
00175 while (IsWindow (handle) && shm_mem[0])
00176 Sleep (500);
00177 if (IsWindow (handle)) {
00178
00179 ::SendMessage (handle, WM_COMMAND, ID_DEBUG_MSG, (DWORD) (msg_start - shm_mem));
00180 }
00181 msg_start = msg_end + 1;
00182 }
00183 while (*msg_start != '\0');
00184 msg_end = shm_mem + 1;
00185 }
00186
00187
00188
00194 void DEBUG_WIN::await_destruction() {
00195 WaitForSingleObject (dbg_process, (unsigned long) -1);
00196 }
00197 #endif //NT Implmentation
00198
00199
00200 #if defined(__UNIX__) || defined(_CONSOLE)
00201 #ifdef __UNIX__
00202 #include <unistd.h>
00203 #include <signal.h>
00204 #endif
00205
00206
00207
00213 DEBUG_WIN::DEBUG_WIN(
00214 const char *title,
00215 INT32 xpos,
00216 INT32 ypos,
00217 INT32 xsize,
00218 INT32 ysize,
00219 INT32 buflines
00220 ) {
00221 #ifdef __UNIX__
00222 INT32 length;
00223 char command[MAX_PATH];
00224 pid_t pid;
00225 char host[MAX_PATH];
00226 BOOL8 remote;
00227
00228
00229 remote = FALSE;
00230 if (remote)
00231
00232 length = sprintf (command, "remsh %s 'DISPLAY=%s;export DISPLAY;",
00233 host, getenv ("DISPLAY"));
00234 else
00235 length = 0;
00236 length += sprintf (command + length, "trap \"\" 1 2 3 13 15\n");
00237 length +=
00238 sprintf (command + length,
00239 "/usr/bin/X11/xterm -sb -sl " INT32FORMAT " -geometry "
00240 INT32FORMAT "x" INT32FORMAT "", buflines, xsize / 8, ysize / 16);
00241 if (xpos >= 0)
00242 command[length++] = '+';
00243 length += sprintf (command + length, INT32FORMAT, xpos);
00244 if (ypos >= 0)
00245 command[length++] = '+';
00246 length +=
00247 sprintf (command + length,
00248 INT32FORMAT " -title \"%s\" -n \"%s\" -e /bin/sh -c ", ypos,
00249 title, title);
00250 pid = getpid ();
00251 length +=
00252 sprintf (command + length,
00253 "\"stty opost; tty >/tmp/debug%d; while [ -s /tmp/debug%d ]\ndo\nsleep 1\ndone\" &\n",
00254 pid, pid);
00255 length +=
00256 sprintf (command + length, "trap \"rm -f /tmp/debug%d; kill -9 $!\" 0\n",
00257 pid);
00258 length += sprintf (command + length, "trap \"exit\" 1 2 3 13 15\n");
00259 length +=
00260 sprintf (command + length,
00261 "while [ ! -s /tmp/debug%d ]\ndo\nsleep 1\ndone\n", pid);
00262 length += sprintf (command + length, "trap \"\" 1 2 3 13 15\n");
00263 length += sprintf (command + length, "ofile=`cat /tmp/debug%d`\n", pid);
00264 length +=
00265 sprintf (command + length, "cat -u - >$ofile; rm /tmp/debug%d\n", pid);
00266 if (remote) {
00267 command[length++] = '\'';
00268 command[length] = '\0';
00269 }
00270 fp = popen (command, "w");
00271 if (fp != NULL) {
00272
00273 if (setvbuf (fp, NULL, _IONBF, BUFSIZ)) {
00274 pclose(fp);
00275 fp = NULL;
00276 }
00277 }
00278 #endif
00279 }
00280
00281
00282
00286 DEBUG_WIN::~DEBUG_WIN (
00287 ) {
00288 #ifdef __UNIX__
00289 pclose(fp);
00290 #endif
00291 }
00292
00293
00294
00301 void
00302 DEBUG_WIN::dprintf (
00303 const char *format, ...
00304 ) {
00305 va_list args;
00306
00307 va_start(args, format);
00308 #ifdef __UNIX__
00309 vfprintf(fp, format, args);
00310 #else
00311
00312 vfprintf(stderr, format, args);
00313 #endif
00314 va_end(args);
00315 }
00316
00317
00318
00322 void DEBUG_WIN::await_destruction() {
00323 #ifdef __UNIX__
00324 signal(SIGPIPE, SIG_IGN);
00325 while (!ferror (fp)) {
00326 sleep (1);
00327 fputc (0, fp);
00328 }
00329 #endif
00330 }
00331 #endif //UNIX Implmentation
00332
00333 #ifdef __MAC__ //NT implementation
00334 #include <stdio.h>
00335
00336 #include <lwindow.h>
00337 #include "ipcbase.h"
00338
00339
00347 void DEBUG_WIN::SetCommander(LCommander *pNew) {
00348 pCommander = pNew;
00349 }
00350
00351
00352
00358 DEBUG_WIN::DEBUG_WIN(
00359 const char *title,
00360 INT32 xpos,
00361 INT32 ypos,
00362 INT32 xsize,
00363 INT32 ysize,
00364 INT32 buflines
00365 ) {
00366 INT32 length;
00367
00368
00369
00370
00371
00372
00373
00374
00375 }
00376
00377
00378
00382 DEBUG_WIN::~DEBUG_WIN (
00383 ) {
00384 }
00385
00386
00387
00394 void
00395 DEBUG_WIN::dprintf (
00396 const char *format, ...
00397 ) {
00398 #if 0
00399 LTextEdit *pTextEdit;
00400 va_list args;
00401 static char msg[1024];
00402
00403 INT32 i;
00404 INT32 OriginalLength;
00405 INT32 NewLength;
00406 TEHandle hTextEdit;
00407 char *pTempBuffer;
00408 CharsHandle hChar;
00409 char *pOriginalText;
00410 INT32 StringLength;
00411
00412 pTextEdit = (LTextEdit *) pWindow->FindPaneByID (text_FLOWED);
00413 if (pTextEdit == NULL)
00414 DebugStr ("\pwhoops");
00415
00416
00417
00418 va_start(args, format);
00419 vsprintf(msg, format, args);
00420 va_end(args);
00421
00422 StringLength = strlen (msg);
00423
00424
00425
00426 hTextEdit = pTextEdit->GetMacTEH ();
00427 if (hTextEdit == NULL)
00428 DebugStr ("\pDEBUG_WIN,WriteCharsToConsole()");
00429
00430
00431
00432 hChar = TEGetText (hTextEdit);
00433 if (hChar == NULL)
00434 DebugStr ("\pDEBUG_WIN,WriteCharsToConsole()");
00435
00436 pOriginalText = *hChar;
00437
00438
00439 OriginalLength = (*hTextEdit)->teLength;
00440
00441
00442
00443 NewLength = OriginalLength + StringLength;
00444
00445 pTempBuffer = NewPtr (NewLength);
00446 if (pTempBuffer == NULL)
00447 DebugStr ("\pDEBUG_WIN,WriteCharsToConsole()");
00448
00449
00450
00451 for (i = 0; i < OriginalLength; i++)
00452 pTempBuffer[i] = pOriginalText[i];
00453
00454
00455
00456 for (i = 0; i < StringLength; i++) {
00457 if (msg[i] == '\n')
00458 pTempBuffer[i + OriginalLength] = '\r';
00459 else
00460 pTempBuffer[i + OriginalLength] = msg[i];
00461 }
00462
00463
00464
00465 TESetText(pTempBuffer, NewLength, hTextEdit);
00466
00467
00468
00469 DisposePtr(pTempBuffer);
00470 #endif
00471 }
00472 #endif //Mac Implmentation
00473
00474 #else // Non graphical debugger
00475
00476 DEBUG_WIN::DEBUG_WIN( const char*, INT32, INT32, INT32, INT32, INT32 ) {
00477 }
00478
00479 DEBUG_WIN::~DEBUG_WIN () {
00480 }
00481
00482 void DEBUG_WIN::dprintf (const char *format, ...) {
00483 va_list ap;
00484 va_start(ap, format);
00485 vfprintf(stderr, format, ap);
00486 va_end(ap);
00487 }
00488
00489 void await_destruction() {
00490 }
00491
00492
00493 #endif
00494