|
|
1.1 root 1: head 1.1;
2: branch ;
3: access ;
4: symbols ;
5: locks bin:1.1; strict;
6: comment @@;
7:
8:
9: 1.1
10: date 93.03.11.07.46.36; author bin; state Exp;
11: branches ;
12: next ;
13:
14:
15: desc
16: @@
17:
18:
19:
20: 1.1
21: log
22: @Initial revision
23: @
24: text
25: @// File: dbh.C
26: //
27: // Purpose: main file for C++ debugger
28:
29:
30: // ----------------------------------------------------------------------
31: // Includes.
32:
33: #include <coff.h>
34: #include <fcntl.h>
35: #include <stdio.h>
36: #include <stdlib.h>
37: #include <stream.h>
38: #include <string.h>
39: #include "sys.h"
40:
41: // ----------------------------------------------------------------------
42: // Definitions.
43: // Constants.
44: // Macros with argument lists.
45: // Typedefs.
46: // Enums.
47:
48: const int STR_LEN = 256; // general-purpose buffer length
49: const int MAX_ARGS = 10; // limit on number of args, minus switches
50:
51: // command syntax in effect (command line parser selects one of these)
52: const int DBH_NULL = 0; // no valid syntax
53: const int DBH_COFF = 1; // dbh <coff file>
54:
55: // Class for keeping track of the main coff file under investigation.
56: class CoffObj {
57: public: // public fields
58: char * fname; // file name
59: public: // constructors/destructors
60: CoffObj(void): fname(NULL) {}
61: };
62:
63: // Class for command line parser.
64: class DbhCmd {
65: public: // public fields
66: int cmdSyntax;
67: const char * args[MAX_ARGS];
68:
69: public: // constructors/destructors
70: DbhCmd(void): cmdSyntax(DBH_NULL) { args[0] = NULL; }
71: };
72:
73: // ----------------------------------------------------------------------
74: // Functions.
75: // Import Functions.
76: // Export Functions.
77: // Local Functions.
78:
79: void coffLoad(const char *);
80: void cmdLine(int argc, char * argv[], DbhCmd &);
81: void dbhCoff(DbhCmd &);
82: void doCmd(const char *);
83: void doCmdStr(const char *);
84: void fatal(const char *);
85: void usage(void);
86:
87: // ----------------------------------------------------------------------
88: // Global Data.
89: // Import Variables.
90: // Export Variables.
91: // Local Variables.
92:
93: const char * cmd; // alias for argv[0]
94: char errstr[STR_LEN]; // error message buffer
95:
96: // ----------------------------------------------------------------------
97: // Code.
98:
99: // command line forms:
100: // dbh <coff file>
101:
102: int main(int argc, char * argv[]) {
103:
104: DbhCmd dbhCmd;
105:
106: // process command line
107: cmdLine(argc, argv, dbhCmd);
108:
109: // do whatever command syntax dictates
110: switch(dbhCmd.cmdSyntax) {
111: case DBH_COFF: // dbh <coff file>
112: dbhCoff(dbhCmd);
113: break;
114: }
115: }
116:
117: // Print a string to stderr and abort the debug session.
118: void
119: fatal(const char * msg) {
120: cerr << cmd << " error: " << msg << endl;
121: exit(1);
122: }
123:
124: // Given raw command arguments, process switches and parse
125: // the line to determine the type of db session. Return
126: // parse info in "dbhCmd".
127: void
128: cmdLine(int argc, char * argv[], DbhCmd & dbhCmd) {
129:
130: // copy command name to "cmd", mainly for error messages
131: cmd = argv[0];
132:
133: // check number of arguments
134: if (argc != 2) {
135: usage();
136: }
137:
138: // get command syntax and set args for subcommand
139: dbhCmd.cmdSyntax = DBH_COFF;
140: dbhCmd.args[0] = argv[1];
141: }
142:
143: // Display usage message and abort the debug session.
144: void
145: usage(void) {
146: cerr << "Usage:\t" << cmd << " <coff program>" << endl;
147: exit(1);
148: }
149:
150: // Run a dbh session for command syntax
151: // dbh <coff file>
152: void
153: dbhCoff(DbhCmd & dbhCmd) {
154: // get needed info from coff file
155: coffLoad(dbhCmd.args[0]);
156:
157: // enter command loop
158: doCmdStr(":x");
159: }
160:
161: // Load info from COFF binary.
162: void
163: coffLoad(const char * fname) {
164: FILEHDR coffh;
165: int coff_fd;
166:
167: if ((coff_fd = open(fname, O_RDONLY)) == -1) {
168: sprintf(errstr, "Can't open object file %s", fname);
169: fatal(errstr);
170: }
171:
172: if (read(coff_fd, &coffh, sizeof(coffh)) != sizeof(coffh)) {
173: sprintf(errstr, "Can't read object file %s", fname);
174: fatal(errstr);
175: }
176:
177: if (coffh.f_magic != C_386_MAGIC) {
178: sprintf(errstr, "Object file %s is not COFF", fname);
179: fatal(errstr);
180: }
181:
182: getCoffSyms(coff_fd);
183:
184: getCoffSegs(coff_fd);
185:
186: close(coff_fd);
187: }
188:
189: // Perform a db command string.
190: // String consists of zero or more commands separated by newlines.
191: void
192: doCmdStr(const char * cmdStr) {
193: char cmd1[STR_LEN];
194:
195: // copy each line, minus trailing newline, into "cmd1"
196: // and invoke single command processor
197: for (const char * cp = cmdStr; cp < cmdStr + strlen(cmdStr);) {
198: const char * delim = strchr(cp, '\n');
199: if (delim) {
200: int copyLen = STR_LEN;
201: if (delim - cp < copyLen)
202: copyLen = delim - cp;
203: strncpy(cmd1, cp, copyLen);
204: doCmd(cmd1);
205: cp = delim + 1;
206: } else {
207: strncpy(cmd1, cp, STR_LEN);
208: doCmd(cmd1);
209: break;
210: }
211: }
212: }
213:
214: // Do a single db command.
215: void
216: doCmd(const char * cmdStr) {
217: cout << cmdStr << endl;
218: }
219: @
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.