This is the mail archive of the
gdb-patches@sources.redhat.com
mailing list for the GDB project.
[patch] Add argc variable to cli script
- From: "Th Klein" <th dot r dot klein at web dot de>
- To: gdb-patches at sources dot redhat dot com
- Date: Wed, 06 Apr 2005 20:17:56 +0200
- Subject: [patch] Add argc variable to cli script
- Organization: http://freemail.web.de/
Hello,
This patch adds the readonly variable $argc into the cli.
So the following example script file data:
###################################################################
# example file script.tst
#
define hexarg_echo
if ($argc <= 0)
printf "no args given\n"
else
if ($argc == 1)
printf "0x%8.8x\n", $arg0
else
if ($argc >= 12)
printf "(%d) 0x%8.8x .. 0x%8.8x\n", $argc, $arg0, $arg11
print $arg0
print $arg9
print $arg10
else
printf "0x%8.8x 0x%8.8x\n", $arg0 ,$arg1
end
end
end
end
document hexarg_echo
simple $argc example
end
#
##################################################################
can be use like this:
(gdb) source script.tst
(gdb) hexarg_echo
no args given
(gdb) hexarg_echo 0
0x00000000
(gdb) hexarg_echo 0 1 2 3 4 5 6 7 8 9 10
0x00000000 0x00000001
(gdb) hexarg_echo 0 1 2 3 4 5 6 7 8 9 10 11
(12) 0x00000000 .. 0x0000000b
$1 = 0
$2 = 9
$3 = 10
(gdb) hexarg_echo 0 1 2 3 4 5 6 7 8 9 10 11 12
(13) 0x00000000 .. 0x0000000b
$4 = 0
$5 = 9
$6 = 10
Regards,
Thomas
2005-04-06 Thomas Klein <Th.R.Klein@web.de>
* cli: Add "$argc", to return the number of user function parameters
* cli: Modify maximum number of user agruments from ten to fivteen
Index: cli-cmds.c
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
retrieving revision 1.58
diff -u -r1.58 cli-cmds.c
--- cli-cmds.c 16 Mar 2005 15:58:41 -0000 1.58
+++ cli-cmds.c 5 Apr 2005 17:48:54 -0000
@@ -1161,7 +1161,10 @@
Definition appears on following lines, one command per line.\n\
End with a line of just \"end\".\n\
Use the \"document\" command to give documentation for the new command.\n\
-Commands defined in this way may have up to ten arguments."));
+Commands defined in this way may have up to " MAXUSERARGS_STRING " arguments.\n\
+Within the user defined function all arguments are accessable \n\
+as readonly variables $arg0, $arg1 ...\n\
+The total number of arguments is held in the readonly variable $argc ."));
c = add_cmd ("source", class_support, source_command, _("\
Read commands from a file named FILE.\n\
Index: cli-script.c
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-script.c,v
retrieving revision 1.28
diff -u -r1.28 cli-script.c
--- cli-script.c 11 Feb 2005 18:13:55 -0000 1.28
+++ cli-script.c 5 Apr 2005 17:48:54 -0000
@@ -49,7 +49,6 @@
static int control_level;
/* Structure for arguments to user defined functions. */
-#define MAXUSERARGS 10
struct user_args
{
struct user_args *next;
@@ -572,7 +571,7 @@
{
while ((p = strchr (p, '$')))
{
- if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
+ if (strncmp (p, "$arg", 4) == 0 && (isdigit (p[4]) || p[4] == 'c' || p[4] == 'C' ))
return p;
p++;
}
@@ -586,7 +585,8 @@
insert_args (char *line)
{
char *p, *save_line, *new_line;
- unsigned len, i;
+ unsigned len;
+ int i;
/* First we need to know how much memory to allocate for the new line. */
save_line = line;
@@ -594,15 +594,33 @@
while ((p = locate_arg (line)))
{
len += p - line;
- i = p[4] - '0';
+ if( p[4] != 'c' && p[4] != 'C' )
+ i = (int)strtol(&p[4], NULL, 10);
+ else
+ i = -1;
+
if (i >= user_args->count)
{
error (_("Missing argument %d in user function."), i);
return NULL;
}
- len += user_args->a[i].len;
- line = p + 5;
+ if(i>=0)
+ len += user_args->a[i].len;
+ else if ( user_args->count < 10 )
+ len += 1;
+ else
+ len += 2;
+ if(i<10)
+ line = p + 5;
+ else if (i < 100)
+ line = p + 6;
+ else
+ {
+ error ("user defined function may not have more than 99 arguments even if MAXUSERARGS is %d.\n",
+ MAXUSERARGS);
+ return NULL;
+ }
}
/* Don't forget the tail. */
@@ -625,15 +643,46 @@
memcpy (new_line, line, p - line);
new_line += p - line;
- i = p[4] - '0';
+
+ if( p[4] != 'c' && p[4] != 'C' )
+ {
+ i = (int)strtol(&p[4], NULL, 10);
+
+ len = user_args->a[i].len;
+ if (len)
+ {
+ memcpy (new_line, user_args->a[i].arg, len);
+ new_line += len;
+ }
+ if(i<10)
+ line = p + 5;
+ else //if (i < 100)
+ line = p + 6;
+
+ }
+ else
+ {
+ i = user_args->count;
+ if( i >= 10 && i < 100)
+ {
+ int n = i / 10;
+
+ i -= n*10;
+ *new_line++ = n + '0';
+ }
+ if(i >= 0 && i < 10)
+ {
+ *new_line++ = i + '0';
+ }
+ else
+ {
+ error("wrong value in $argc\n");
+ xfree(save_line);
+ return NULL;
+ }
+ line = p + 5;
+ }
- len = user_args->a[i].len;
- if (len)
- {
- memcpy (new_line, user_args->a[i].arg, len);
- new_line += len;
- }
- line = p + 5;
}
/* Don't forget the tail. */
strcpy (new_line, line);
Index: cli-script.h
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-script.h,v
retrieving revision 1.6
diff -u -r1.6 cli-script.h
--- cli-script.h 22 Dec 2003 03:43:19 -0000 1.6
+++ cli-script.h 5 Apr 2005 17:48:54 -0000
@@ -18,6 +18,8 @@
#if !defined (CLI_SCRIPT_H)
#define CLI_SCRIPT_H 1
+#define MAXUSERARGS 15
+#define MAXUSERARGS_STRING "15"
struct ui_file;
struct command_line;
______________________________________________________________
Verschicken Sie romantische, coole und witzige Bilder per SMS!
Jetzt bei WEB.DE FreeMail: http://f.web.de/?mc=021193