Skip site navigation (1)Skip section navigation (2)
Date:      09 Oct 2002 18:51:29 +0400
From:      "Vladimir B. " Grebenschikov <vova@sw.ru>
To:        Maxime Henrion <mux@freebsd.org>
Cc:        Bruce Evans <bde@zeta.org.au>, freebsd-current@freebsd.org
Subject:   Re: DDB sysctl function
Message-ID:  <1034175089.1028.6.camel@vbook.express.ru>
In-Reply-To: <20021009122319.GS57622@elvis.mu.org>
References:  <20021009094616.GP57622@elvis.mu.org> <20021009222321.I4967-100000@gamplex.bde.org>  <20021009122319.GS57622@elvis.mu.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--=-MkHvBySDGQaJAZybSXW7
Content-Type: text/plain; charset=KOI8-R
Content-Transfer-Encoding: quoted-printable

=F7 Wed, 09.10.2002, =D7 16:23, Maxime Henrion =CE=C1=D0=C9=D3=C1=CC:=20
> Bruce Evans wrote:
> > On Wed, 9 Oct 2002, Maxime Henrion wrote:
> >=20
> > > What I meant in my previous mail is that you could malloc() these
> > > objects instead of putting them on the stack.  Also, you don't need
> > > buffers that big since the size you need is bounded at max(sizeof(int=
),
> > > TOK_STRING_SIZE), which is likely to be TOK_STRING_SIZE :-).
> >=20
> > ddb can't call malloc() or any other general memory allocation function=
,
> > since it may be (and often is) invoked in the middle of such functions.
>=20
> I guess using a buffer of TOK_STRING_SIZE bytes for the tIDENT case and
> int for the tNUMBER case would be best then.  We could use the same
> buffer if we don't plan to support platforms with ints bigger than 120
> bytes :-).

As final (I hope) solution:

- all buffers now are static

- for sysctlw now it is not need escape dots in passed strings
     sysctlw 0.3 hw.model

- it is possible not to pass array of intergers to sysctlw
     sysctlw 1.2.3.4 12323,345465,657,67,68787

=20
> Maxime
--=20
Vladimir B. Grebenschikov
vova@sw.ru, SWsoft, Inc.

--=-MkHvBySDGQaJAZybSXW7
Content-Disposition: attachment; filename=ddb_sysctl.diff
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-patch; name=ddb_sysctl.diff; charset=KOI8-R

Index: ddb/db_command.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /ext/ncvs/src/sys/ddb/db_command.c,v
retrieving revision 1.46
diff -u -r1.46 db_command.c
--- ddb/db_command.c	1 Oct 2002 21:59:46 -0000	1.46
+++ ddb/db_command.c	8 Oct 2002 19:53:05 -0000
@@ -422,6 +422,8 @@
 	{ "gdb",	db_gdb,			0,	0 },
 	{ "reset",	db_reset,		0,	0 },
 	{ "kill",	db_kill,		CS_OWN,	0 },
+	{ "sysctl",     db_sysctl_cmd,          CS_OWN, 0 },
+	{ "sysctlw",    db_sysctlw_cmd,         CS_OWN, 0 },
 	{ (char *)0, }
 };
=20
Index: ddb/db_examine.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /ext/ncvs/src/sys/ddb/db_examine.c,v
retrieving revision 1.29
diff -u -r1.29 db_examine.c
--- ddb/db_examine.c	25 Jun 2002 15:59:24 -0000	1.29
+++ ddb/db_examine.c	8 Oct 2002 20:10:10 -0000
@@ -44,7 +44,6 @@
=20
 static char	db_examine_format[TOK_STRING_SIZE] =3D "x";
=20
-static void	db_examine(db_addr_t, char *, int);
 static void	db_search(db_addr_t, int, db_expr_t, db_expr_t, u_int);
=20
 /*
@@ -67,7 +66,7 @@
 	db_examine((db_addr_t) addr, db_examine_format, count);
 }
=20
-static void
+void
 db_examine(addr, fmt, count)
 	register
 	db_addr_t	addr;
Index: ddb/db_sysctl_cmd.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: ddb/db_sysctl_cmd.c
diff -N ddb/db_sysctl_cmd.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ddb/db_sysctl_cmd.c	9 Oct 2002 14:41:26 -0000
@@ -0,0 +1,191 @@
+/*-
+ * Copyright (c) 2002  Potatin Mike
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP=
OSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENT=
IAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STR=
ICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY W=
AY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: $
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+
+#include <ddb/ddb.h>
+
+#include <ddb/db_lex.h>
+#include <ddb/db_output.h>
+#include <ddb/db_command.h>
+#include <ddb/db_sym.h>
+#include <ddb/db_access.h>
+
+#include <sys/sysctl.h>
+#include <sys/proc.h>
+
+#define SYSCTL_DATA_BUFSIZE	256
+#define SYSCTL_MAX_MIB		64
+
+static int  sysctl_mib [SYSCTL_MAX_MIB];
+static char sysctl_wbuf[SYSCTL_DATA_BUFSIZE];
+static char sysctl_buf [SYSCTL_DATA_BUFSIZE];
+
+/* fill sysctl mib */
+static int
+db_sysctl_getmib(int *miblen)
+{
+	int t;
+=09
+	*miblen =3D 0;
+	while((t =3D db_read_token()) =3D=3D tNUMBER) {
+		if (*miblen >=3D SYSCTL_MAX_MIB)
+			return(tEOF);
+
+		sysctl_mib[*miblen] =3D db_tok_number;
+		(*miblen)++;
+	=09
+		if((t =3D db_read_token()) !=3D tDOT)
+			break;
+	}
+	return(t);
+}
+
+/*=20
+ *  write into sysctl
+ *  SYNTAX:
+ *     sysctlw 0.3 <int>[,<int>,<int>...]
+ *   or=20
+ *     sysctlw 0.3 <string>
+ *   where:
+ *     0.3      - mib of sysctl
+ *     <int>    - hexdecimal number
+ *     <string> - some alphanumeric string
+ *                all not-alnums should be escaped (except dot)
+ */
+void
+db_sysctlw_cmd(d1, d2, d3, d4)
+	db_expr_t	d1;
+	boolean_t	d2;
+	db_expr_t	d3;
+	char *		d4;
+{
+	int t;
+	size_t ret =3D 0;
+	size_t readcount =3D SYSCTL_DATA_BUFSIZE;
+	size_t wsize;
+	int miblen;
+	int *wp;
+	int err;
+
+	t =3D db_sysctl_getmib(&miblen);
+       	switch (t) {
+		case tIDENT:
+			strncpy(sysctl_wbuf, db_tok_string, sizeof(sysctl_wbuf)-1);
+			wsize =3D strlen(sysctl_wbuf);
+			while(db_read_token() =3D=3D tDOT) {
+				int l;
+				if (db_read_token() !=3D tIDENT)
+					break;
+				l =3D strlen(db_tok_string) + 1;
+				if (wsize + l < sizeof(sysctl_wbuf)-1) {
+					strcat(sysctl_wbuf, ".");
+					strcat(sysctl_wbuf, db_tok_string);
+					wsize +=3D l;
+				} else
+					break;
+			}
+			sysctl_wbuf[sizeof(sysctl_wbuf)-1] =3D '\0';=09
+			break;
+		case tNUMBER:
+			wp =3D (int *) sysctl_wbuf;
+			*wp++ =3D db_tok_number;
+			wsize =3D sizeof(int);
+			while(db_read_token() =3D=3D tCOMMA) {
+				if (db_read_token() !=3D tIDENT)=20
+					break;
+				=09
+				if (wsize + sizeof(int) < sizeof(sysctl_wbuf)) {
+					*wp++ =3D db_tok_number;
+					wsize +=3D sizeof(int);
+				} else
+					break;
+			}
+			break;
+		default:
+			db_printf("no ident\n");
+			db_flush_lex();
+			return;
+	}
+
+	db_skip_to_eol();
+
+	if(((err =3D kernel_sysctl(
+		     FIRST_THREAD_IN_PROC(initproc),=20
+		     sysctl_mib, miblen, sysctl_buf, &readcount,=20
+		     sysctl_wbuf, wsize, &ret))) =3D=3D 0) {
+		db_examine((db_addr_t) sysctl_buf, "x", ret/sizeof(int));
+	} else {
+		db_printf("sysctl error %d %d\n", err, ret);
+	}
+}
+
+/*
+ * get sysctl value
+ * SYNTAX:
+ *   sysctl 0.3=20
+ * or=20
+ *   sysctl 0.3 /s
+ * where
+ *   0.3 - mib of examined sysctl=20
+ *   /s  - standart ddb examine modifyer (can be used without slash)
+ */
+void
+db_sysctl_cmd(d1, d2, d3, d4)
+	db_expr_t	d1;
+	boolean_t	d2;
+	db_expr_t	d3;
+	char *		d4;
+{
+	int t;
+	int miblen;
+	size_t ret =3D 0;
+	size_t readcount =3D SYSCTL_DATA_BUFSIZE;
+	int err;
+	char modif[16]=3D"x";
+
+	t =3D db_sysctl_getmib(&miblen);
+
+	if(t =3D=3D tSLASH)=20
+		t =3D db_read_token();
+
+	if(t =3D=3D tIDENT)
+		strncpy(modif, db_tok_string, 15);
+=09
+	db_skip_to_eol();
+
+	if(((err =3D kernel_sysctl(
+		     FIRST_THREAD_IN_PROC(initproc),=20
+		     sysctl_mib, miblen, sysctl_buf,=20
+		     &readcount, NULL, 0, &ret))) =3D=3D 0) {
+		db_examine((db_addr_t) sysctl_buf, modif, ret/sizeof(int));
+	} else {
+		db_printf("sysctl error %d %d\n", err, ret);
+	}
+}
Index: ddb/ddb.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /ext/ncvs/src/sys/ddb/ddb.h,v
retrieving revision 1.30
diff -u -r1.30 ddb.h
--- ddb/ddb.h	21 Sep 2002 17:29:36 -0000	1.30
+++ ddb/ddb.h	8 Oct 2002 20:09:28 -0000
@@ -84,6 +84,7 @@
 db_addr_t	db_disasm(db_addr_t loc, boolean_t altfmt);
 				/* instruction disassembler */
 void		db_error(const char *s);
+void		db_examine(db_addr_t, char *, int);
 int		db_expression(db_expr_t *valuep);
 int		db_get_variable(db_expr_t *valuep);
 void		db_iprintf(const char *,...) __printflike(1, 2);
@@ -125,6 +126,8 @@
 db_cmdfcn_t	db_trace_until_matching_cmd;
 db_cmdfcn_t	db_watchpoint_cmd;
 db_cmdfcn_t	db_write_cmd;
+db_cmdfcn_t	db_sysctl_cmd;
+db_cmdfcn_t	db_sysctlw_cmd;
=20
 #if 0
 db_cmdfcn_t	db_help_cmd;
Index: conf/files
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /ext/ncvs/src/sys/conf/files,v
retrieving revision 1.713
diff -u -r1.713 files
--- conf/files	5 Oct 2002 16:35:26 -0000	1.713
+++ conf/files	8 Oct 2002 19:53:05 -0000
@@ -218,6 +218,7 @@
 ddb/db_variables.c	optional ddb
 ddb/db_watch.c		optional ddb
 ddb/db_write_cmd.c	optional ddb
+ddb/db_sysctl_cmd.c	optional ddb
 dev/aac/aac.c		optional aac
 dev/aac/aac_debug.c	optional aac
 dev/aac/aac_disk.c	optional aac

--=-MkHvBySDGQaJAZybSXW7--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1034175089.1028.6.camel>