import gdb-1999-08-16 snapshot
[binutils.git] / gdb / kod-cisco.c
1 /* Kernel Object Display facility for Cisco
2    Copyright 1999 Free Software Foundation, Inc.
3    
4    Written by Tom Tromey <tromey@cygnus.com>.
5    
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "gdb_string.h"
24
25 #ifdef HAVE_STDLIB_H
26 #include <stdlib.h>
27 #endif
28
29 /* Define this to turn off communication with target.  */
30 /* #define FAKE_PACKET */
31
32 /* Size of buffer used for remote communication.  */
33 #define PBUFSIZ 400
34
35 /* Pointers to gdb callbacks.  */
36 static void (*gdb_kod_display) (char *);
37 static void (*gdb_kod_query) (char *, char *, int *);
38
39 \f
40
41 /* Initialize and return library name and version.
42    The gdb side of KOD, kod.c, passes us two functions: one for
43    displaying output (presumably to the user) and the other for
44    querying the target.  */
45 char *
46 cisco_kod_open (void (*display_func) (char *),
47                 void (*query_func) (char *, char *, int *))
48 {
49   char buffer[PBUFSIZ];
50   int bufsiz = PBUFSIZ;
51   int i, count;
52
53   gdb_kod_display = display_func;
54   gdb_kod_query = query_func;
55
56   /* Get the OS info, and check the version field.  This is the stub
57      version, which we use to see whether we will understand what
58      comes back.  This is lame, but the `qKoL' request doesn't
59      actually provide enough configurability.
60      
61      Right now the only defined version number is `0.0.0'.
62      This stub supports qKoI and the `a' (any) object requests qKaL
63      and qKaI.  Each `a' object is returned as a 4-byte integer ID.
64      An info request on an object returns a pair of 4-byte integers;
65      the first is the object pointer and the second is the thread ID.  */
66
67 #ifndef FAKE_PACKET
68   (*gdb_kod_query) ("oI;", buffer, &bufsiz);
69 #else
70   strcpy (buffer, "Cisco IOS/Classic/13.4 0.0.0");
71 #endif
72
73   count = 2;
74   for (i = 0; count && buffer[i] != '\0'; ++i)
75     {
76       if (buffer[i] == ' ')
77         --count;
78     }
79
80   if (buffer[i] == '\0')
81     error ("Remote returned malformed packet\n");
82   if (strcmp (&buffer[i], "0.0.0"))
83     error ("Remote returned unknown stub version: %s\n", &buffer[i]);
84
85   /* Return name, version, and description.  I hope we have enough
86      space.  */
87   return (strdup ("gdbkodcisco v0.0.0 - Cisco Kernel Object Display"));
88 }
89
90 /* Close the connection.  */
91 void
92 cisco_kod_close ()
93 {
94 }
95
96 /* Print a "bad packet" message.  */
97 static void
98 bad_packet ()
99 {
100   (*gdb_kod_display) ("Remote target returned malformed packet.\n");
101 }
102
103 /* Print information about currently known kernel objects.
104    We currently ignore the argument.  There is only one mode of
105    querying the Cisco kernel: we ask for a dump of everything, and
106    it returns it.  */
107 void
108 cisco_kod_request (char *arg, int from_tty)
109 {
110   char buffer[PBUFSIZ], command[PBUFSIZ];
111   int done = 0, i;
112   int fail = 0;
113
114   char **sync_ids;
115   int sync_len = 0;
116   int sync_next = 0;
117   char *prev_id = NULL;
118
119   if (! arg || strcmp (arg, "any"))
120     {
121       /* "Top-level" command.  This is really silly, but it also seems
122          to be how KOD is defined.  */
123       /* Even sillier is the fact that this first line must start
124          with the word "List".  See kod.tcl.  */
125       (*gdb_kod_display) ("List of Cisco Kernel Objects\n");
126       (*gdb_kod_display) ("Object\tDescription\n");
127       (*gdb_kod_display) ("any\tAny and all objects\n");
128       return;
129     }
130
131   while (! done)
132     {
133       int off = 0;              /* Where we are in the string.  */
134       long count;               /* Number of objects in this packet.  */
135       int bufsiz = PBUFSIZ;
136       char *s_end;
137
138       strcpy (command, "aL");
139       if (prev_id)
140         {
141           strcat (command, ",");
142           strcat (command, prev_id);
143         }
144       strcat (command, ";");
145
146 #ifndef FAKE_PACKET
147       /* We talk to the target by calling through the query function
148          passed to us when we were initialized.  */
149       (*gdb_kod_query) (command, buffer, &bufsiz);
150 #else
151       /* Fake up a multi-part packet.  */
152       if (! strncmp (&command[3], "a500005a", 8))
153         strcpy (buffer, "KAL,01,1,f500005f;f500005f;");
154       else
155         strcpy (buffer, "KAL,02,0,a500005a;a500005a;de02869f;");
156 #endif
157
158       /* Empty response is an error.  */
159       if (strlen (buffer) == 0)
160         {
161           (*gdb_kod_display) ("Remote target did not recognize kernel object query command.\n");
162           fail = 1;
163           break;
164         }
165
166       /* If we don't get a `K' response then the buffer holds the
167          target's error message.  */
168       if (buffer[0] != 'K')
169         {
170           (*gdb_kod_display) (buffer);
171           fail = 1;
172           break;
173         }
174
175       /* Make sure we get the response we expect.  */
176       if (strncmp (buffer, "KAL,", 4))
177         {
178           bad_packet ();
179           fail = 1;
180           break;
181         }
182       off += 4;
183
184       /* Parse out the count.  We expect to convert exactly two
185          characters followed by a comma.  */
186       count = strtol (&buffer[off], &s_end, 16);
187       if (s_end - &buffer[off] != 2 || buffer[off + 2] != ',')
188         {
189           bad_packet ();
190           fail = 1;
191           break;
192         }
193       off += 3;
194
195       /* Parse out the `done' flag.  */
196       if ((buffer[off] != '0' && buffer[off] != '1')
197           || buffer[off + 1] != ',')
198         {
199           bad_packet ();
200           fail = 1;
201           break;
202         }
203       done = buffer[off] == '1';
204       off += 2;
205
206       /* Id of the last item; we might this to construct the next
207          request.  */
208       prev_id = &buffer[off];
209       if (strlen (prev_id) < 8 || buffer[off + 8] != ';')
210         {
211           bad_packet ();
212           fail = 1;
213           break;
214         }
215       buffer[off + 8] = '\0';
216       off += 9;
217
218       if (sync_len == 0)
219         sync_ids = (char **) xmalloc (count * sizeof (char *));
220       else
221         sync_ids = (char **) xrealloc (sync_ids,
222                                        (sync_len + count) * sizeof (char *));
223       sync_len += count;
224
225       for (i = 0; i < count; ++i)
226         {
227           if (strlen (&buffer[off]) < 8 || buffer[off + 8] != ';')
228             {
229               bad_packet ();
230               fail = 1;
231               break;
232             }
233           buffer[off + 8] = '\0';
234           sync_ids[sync_next++] = xstrdup (&buffer[off]);
235           off += 9;
236         }
237
238       if (buffer[off] != '\0')
239         {
240           bad_packet ();
241           fail = 1;
242           break;
243         }
244     }
245
246   /* We've collected all the sync object IDs.  Now query to get the
247      specific information, and arrange to print this info.  */
248   if (! fail)
249     {
250       (*gdb_kod_display) ("Object ID\tObject Pointer\tThread ID\n");
251
252       for (i = 0; i < sync_next; ++i)
253         {
254           int off = 0;
255           int bufsiz = PBUFSIZ;
256
257           /* For now assume a query can be accomplished in a single
258              transaction.  This is implied in the protocol document.
259              See comments above, and the KOD protocol document, to
260              understand the parsing of the return value.  */
261           strcpy (command, "aI,");
262           strcat (command, sync_ids[i]);
263           strcat (command, ";");
264
265 #ifndef FAKE_PACKET
266           (*gdb_kod_query) (command, buffer, &bufsiz);
267 #else
268           strcpy (buffer, "KAI,");
269           strcat (buffer, sync_ids[i]);
270           strcat (buffer, ",ffef00a0,cd00123d;");
271 #endif
272
273           if (strlen (buffer) == 0)
274             {
275               (*gdb_kod_display) ("Remote target did not recognize KOD command.\n");
276               break;
277             }
278
279           if (strncmp (buffer, "KAI,", 4))
280             {
281               bad_packet ();
282               break;
283             }
284           off += 4;
285
286           if (strncmp (&buffer[off], sync_ids[i], 8)
287               || buffer[off + 8] != ',')
288             {
289               bad_packet ();
290               break;
291             }
292           off += 9;
293
294           /* Extract thread id and sync object pointer.  */
295           if (strlen (&buffer[off]) != 2 * 8 + 2
296               || buffer[off + 8] != ','
297               || buffer[off + 17] != ';')
298             {
299               bad_packet ();
300               break;
301             }
302
303           buffer[off + 8] = '\0';
304           buffer[off + 17] = '\0';
305
306           /* Display the result.  */
307           (*gdb_kod_display) (sync_ids[i]);
308           (*gdb_kod_display) ("\t");
309           (*gdb_kod_display) (&buffer[off]);
310           (*gdb_kod_display) ("\t");
311           (*gdb_kod_display) (&buffer[off + 9]);
312           (*gdb_kod_display) ("\n");
313         }
314     }
315
316   /* Free memory.  */
317   for (i = 0; i < sync_next; ++i)
318     free (sync_ids[i]);
319   free (sync_ids);
320 }
This page took 0.042804 seconds and 4 git commands to generate.