[PATCH 06/12] [WineDbg]: rewrite auto mode

Eric Pouech eric.pouech at wanadoo.fr
Fri Feb 24 15:15:39 CST 2006


- rewrote auto mode as a specific set of commands to be run in
  regular parser, instead of hard coding those commands
- added a new function to store a set of commands to be executed
  in a file
- added 'echo' command to the parser functions
- got rid of dbg_action_mode
- added support of '--' on command line

A+
---

 programs/winedbg/dbg.y        |   33 ++++++++++++++++-
 programs/winedbg/debug.l      |    1 +
 programs/winedbg/debugger.h   |    2 +
 programs/winedbg/tgt_active.c |   34 ++++++-----------
 programs/winedbg/winedbg.c    |   80 +++++++++++++++++++----------------------
 5 files changed, 83 insertions(+), 67 deletions(-)

diff --git a/programs/winedbg/dbg.y b/programs/winedbg/dbg.y
index 8207d84..890af74 100644
--- a/programs/winedbg/dbg.y
+++ b/programs/winedbg/dbg.y
@@ -51,7 +51,7 @@ int yyerror(const char*);
 
 %token tCONT tPASS tSTEP tLIST tNEXT tQUIT tHELP tBACKTRACE tALL tINFO tUP tDOWN
 %token tENABLE tDISABLE tBREAK tHBREAK tWATCH tDELETE tSET tMODE tPRINT tEXAM
-%token tABORT tVM86
+%token tABORT tVM86 tECHO
 %token tCLASS tMAPS tSTACK tSEGMENTS tSYMBOL tREGS tWND tQUEUE tLOCAL tEXCEPTION
 %token tPROCESS tTHREAD tMODREF tEOL tEOF
 %token tFRAME tSHARE tCOND tDISPLAY tUNDISPLAY tDISASSEMBLE
@@ -141,6 +141,7 @@ command:
     | tATTACH tNUM     		{ dbg_attach_debuggee($2, FALSE, TRUE); }
     | tDETACH                   { dbg_detach_debuggee(); }
     | tMINIDUMP pathname        { minidump_write($2, (dbg_curr_thread && dbg_curr_thread->in_exception) ? &dbg_curr_thread->excpt_record : NULL);}
+    | tECHO tSTRING             { dbg_printf("%s\n", $2); }
     | run_command
     | list_command
     | disassemble_command
@@ -554,3 +555,33 @@ int yyerror(const char* s)
     dbg_printf("%s\n", s);
     return 0;
 }
+
+HANDLE parser_generate_command_file(const char* pmt, ...)
+{
+    HANDLE      hFile;
+    char        path[MAX_PATH], file[MAX_PATH];
+    DWORD       w;
+    const char* p;
+
+    GetTempPath(sizeof(path), path);
+    GetTempFileName(path, "WD", 0, file);
+    hFile = CreateFileA(file, GENERIC_READ|GENERIC_WRITE|DELETE, FILE_SHARE_DELETE, 
+                        NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, 0);
+    if (hFile != INVALID_HANDLE_VALUE)
+    {
+        va_list ap;
+
+        WriteFile(hFile, pmt, strlen(pmt), &w, 0);
+        va_start(ap, pmt);
+        while ((p = va_arg(ap, const char*)) != NULL)
+        {
+            WriteFile(hFile, "\n", 1, &w, 0);
+            WriteFile(hFile, p, strlen(p), &w, 0);
+        }
+        va_end(ap);
+        WriteFile(hFile, "\nquit\n", 6, &w, 0);
+        SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
+    }
+    return hFile;
+}
+
diff --git a/programs/winedbg/debug.l b/programs/winedbg/debug.l
index ad1a986..71d10f0 100644
--- a/programs/winedbg/debug.l
+++ b/programs/winedbg/debug.l
@@ -180,6 +180,7 @@ STRING     \"[^\n"]+\"
 <INITIAL>detach|detac|deta|det   	{ BEGIN(NOCMD); return tDETACH; }
 <INITIAL>maintenance|maint              { BEGIN(MAINT_CMD); return tMAINTENANCE; }
 <INITIAL>minidump|mdmp                  { BEGIN(PATH_EXPECTED); return tMINIDUMP; }
+<INITIAL>echo				{ BEGIN(ASTRING_EXPECTED); return tECHO; }
 <NOPROCESS>attach|attac|atta|att 	{ BEGIN(NOCMD); return tATTACH; }
 <INFO_CMD>share|shar|sha		{ return tSHARE; }
 <INFO_CMD>locals|local|loca|loc		{ return tLOCAL; }
diff --git a/programs/winedbg/debugger.h b/programs/winedbg/debugger.h
index 38ad642..ecfe2f4 100644
--- a/programs/winedbg/debugger.h
+++ b/programs/winedbg/debugger.h
@@ -282,6 +282,7 @@ extern void             parser(const cha
 extern void             parser_handle(HANDLE);
 extern int              input_read_line(const char* pfx, char* buffer, int size);
 extern int              input_fetch_entire_line(const char* pfx, char** line);
+extern HANDLE           parser_generate_command_file(const char*, ...);
 
   /* debug.l */
 extern void             lexeme_flush(void);
@@ -374,7 +375,6 @@ extern enum dbg_start   dbg_active_attac
 extern enum dbg_start   dbg_active_launch(int argc, char* argv[]);
 extern enum dbg_start   dbg_active_auto(int argc, char* argv[]);
   /* temporary for tgt_active.c */
-extern enum dbg_action_mode {winedbg_mode, automatic_mode} dbg_action_mode;
 extern unsigned         dbg_main_loop(HANDLE);
 
   /* tgt_minidump.c */
diff --git a/programs/winedbg/tgt_active.c b/programs/winedbg/tgt_active.c
index 226227c..53d3a4d 100644
--- a/programs/winedbg/tgt_active.c
+++ b/programs/winedbg/tgt_active.c
@@ -33,7 +33,6 @@
 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
 
 static char*            dbg_last_cmd_line;
-/*static*/ enum dbg_action_mode dbg_action_mode;
 
 struct be_process_io be_process_active_io =
 {
@@ -404,13 +403,6 @@ static DWORD dbg_handle_exception(const 
         dbg_printf( ", invalid program stack" );
     }
 
-    if (dbg_action_mode == automatic_mode)
-    {
-        dbg_exception_prolog(is_debug, rec);
-        dbg_exception_epilog();
-        return 0;  /* terminate execution */
-    }
-
     if (dbg_exception_prolog(is_debug, rec))
     {
 	dbg_interactiveP = TRUE;
@@ -693,19 +685,8 @@ void dbg_wait_next_exception(DWORD cont,
     {
         if (dbg_handle_debug_event(&de)) break;
     }
-    switch (dbg_action_mode)
-    {
-    case automatic_mode:
-        /* print some extra information */
-        dbg_printf("Modules:\n");
-        info_win32_module(0); /* print all modules */
-        dbg_printf("Threads:\n");
-        info_win32_threads();
-        break;
-    default:
-        dbg_interactiveP = TRUE;
-        parser_handle(hFile);
-    }
+    dbg_interactiveP = TRUE;
+    parser_handle(hFile);
     dbg_printf("WineDbg terminated on pid 0x%lx\n", dbg_curr_pid);
 
     return 0;
@@ -880,7 +861,14 @@ enum dbg_start    dbg_active_launch(int 
  */
 enum dbg_start dbg_active_auto(int argc, char* argv[])
 {
+    HANDLE      hFile;
+
     argc--; argv++;
-    dbg_action_mode = automatic_mode;
-    return dbg_active_attach(argc, argv);
+    hFile = parser_generate_command_file("echo Modules:", "info share",
+                                         "echo Threads:", "info threads",
+					 NULL);
+    if (hFile == INVALID_HANDLE_VALUE || !dbg_active_attach(argc, argv))
+        return FALSE;
+    dbg_main_loop(hFile);
+    return TRUE;
 }
diff --git a/programs/winedbg/winedbg.c b/programs/winedbg/winedbg.c
index e8678c4..40eee6c 100644
--- a/programs/winedbg/winedbg.c
+++ b/programs/winedbg/winedbg.c
@@ -491,7 +491,6 @@ int main(int argc, char** argv)
         return retv;
     }
     dbg_init_console();
-    dbg_action_mode = winedbg_mode;
 
     SymSetOptions((SymGetOptions() & ~(SYMOPT_UNDNAME)) |
                   SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS | SYMOPT_AUTO_PUBLICS);
@@ -501,63 +500,60 @@ int main(int argc, char** argv)
         /* force some internal variables */
         DBG_IVAR(BreakOnDllLoad) = 0;
         dbg_houtput = GetStdHandle(STD_ERROR_HANDLE);
-        ds = dbg_active_auto(argc, argv);
+        switch (dbg_active_auto(argc, argv))
+        {
+        case start_ok:          return 0;
+        case start_error_parse: return dbg_winedbg_usage();
+        case start_error_init:  return -1;
+        }
     }
-    else
+    /* parse options */
+    while (argc > 0 && argv[0][0] == '-')
     {
-        /* parse options */
-        while (argc > 0 && argv[0][0] == '-')
+        if (!strcmp(argv[0], "--command"))
         {
-            if (!strcmp(argv[0], "--command"))
+            argc--; argv++;
+            hFile = parser_generate_command_file(argv[0], NULL);
+            if (hFile == INVALID_HANDLE_VALUE)
             {
-                char        path[MAX_PATH], file[MAX_PATH];
-                DWORD       w;
-
-                GetTempPath(sizeof(path), path);
-                GetTempFileName(path, "WD", 0, file);
-                argc--; argv++;
-                hFile = CreateFileA(file, GENERIC_READ|GENERIC_WRITE|DELETE, FILE_SHARE_DELETE, 
-                                    NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, 0);
-                if (hFile == INVALID_HANDLE_VALUE)
-                {
-                    dbg_printf("Couldn't open temp file %s (%lu)\n", file, GetLastError());
-                    return 1;
-                }
-                WriteFile(hFile, argv[0], strlen(argv[0]), &w, 0);
-                WriteFile(hFile, "\nquit\n", 6, &w, 0);
-                SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
-
-                argc--; argv++;
-                continue;
+                dbg_printf("Couldn't open temp file (%lu)\n", GetLastError());
+                return 1;
             }
-            if (!strcmp(argv[0], "--file"))
+            argc--; argv++;
+            continue;
+        }
+        if (!strcmp(argv[0], "--file"))
+        {
+            argc--; argv++;
+            hFile = CreateFileA(argv[0], GENERIC_READ|DELETE, 0, 
+                                NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+            if (hFile == INVALID_HANDLE_VALUE)
             {
-                argc--; argv++;
-                hFile = CreateFileA(argv[0], GENERIC_READ|DELETE, 0, 
-                                    NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
-                if (hFile == INVALID_HANDLE_VALUE)
-                {
-                    dbg_printf("Couldn't open file %s (%lu)\n", argv[0], GetLastError());
-                    return 1;
-                }
-                argc--; argv++;
-                continue;
+                dbg_printf("Couldn't open file %s (%lu)\n", argv[0], GetLastError());
+                return 1;
             }
-            return dbg_winedbg_usage();
+            argc--; argv++;
+            continue;
+        }
+        if (!strcmp(argv[0], "--"))
+        {
+            argc--; argv++;
+            break;
         }
-        if (!argc) ds = start_ok;
-        else if ((ds = dbg_active_attach(argc, argv)) == start_error_parse)
-            ds = dbg_active_launch(argc, argv);
+        return dbg_winedbg_usage();
     }
+    if (!argc) ds = start_ok;
+    else if ((ds = dbg_active_attach(argc, argv)) == start_error_parse)
+        ds = dbg_active_launch(argc, argv);
     switch (ds)
     {
     case start_ok:              break;
     case start_error_parse:     return dbg_winedbg_usage();
     case start_error_init:      return -1;
     }
+
     retv = dbg_main_loop(hFile);
-    /* don't save modified variables in auto mode */
-    if (dbg_action_mode != automatic_mode) dbg_save_internal_vars();
+    dbg_save_internal_vars();
 
     return retv;
 }





More information about the wine-patches mailing list