|
From: <sv...@va...> - 2005-08-10 00:07:54
|
Author: sewardj
Date: 2005-08-09 12:37:09 +0100 (Tue, 09 Aug 2005)
New Revision: 4352
Log:
Remove from m_main.c all glibc dependencies except malloc and free.
This required implementing VG_(access) and VG_(snprintf).
Modified:
branches/ASPACEM/coregrind/m_libcfile.c
branches/ASPACEM/coregrind/m_libcprint.c
branches/ASPACEM/coregrind/m_main.c
branches/ASPACEM/coregrind/pub_core_libcfile.h
branches/ASPACEM/include/pub_tool_libcprint.h
Modified: branches/ASPACEM/coregrind/m_libcfile.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
--- branches/ASPACEM/coregrind/m_libcfile.c 2005-08-09 00:45:22 UTC (rev =
4351)
+++ branches/ASPACEM/coregrind/m_libcfile.c 2005-08-09 11:37:09 UTC (rev =
4352)
@@ -182,6 +182,26 @@
return res.isError ? -1 : res.val;
}
=20
+/* Check accessibility of a file. Returns zero for access granted,
+ nonzero otherwise. */
+Int VG_(access) ( HChar* path, Bool irusr, Bool iwusr, Bool ixusr )
+{
+#if defined(VGO_linux)
+ /* Very annoyingly, I cannot find any definition for R_OK et al in
+ the kernel interfaces. Therefore I reluctantly resort to
+ hardwiring in these magic numbers that I determined by
+ experimentation. */
+ UWord w =3D (irusr ? 4/*R_OK*/ : 0)
+ | (iwusr ? 2/*W_OK*/ : 0)
+ | (ixusr ? 1/*X_OK*/ : 0);
+ SysRes res =3D VG_(do_syscall2)(__NR_access, (UWord)path, w);
+ return res.isError ? 1 : res.val;
+#else
+# error "Don't know how to do VG_(access) on this OS"
+#endif
+}
+
+
/* ---------------------------------------------------------------------
Socket-related stuff. This is very Linux-kernel specific.
------------------------------------------------------------------ */
Modified: branches/ASPACEM/coregrind/m_libcprint.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
--- branches/ASPACEM/coregrind/m_libcprint.c 2005-08-09 00:45:22 UTC (rev=
4351)
+++ branches/ASPACEM/coregrind/m_libcprint.c 2005-08-09 11:37:09 UTC (rev=
4352)
@@ -70,10 +70,12 @@
printf() and friends
------------------------------------------------------------------ */
=20
-typedef struct {
- char buf[100];
- int n;
-} printf_buf;
+typedef=20
+ struct {
+ HChar buf[100];
+ Int n;
+ }=20
+ printf_buf;
=20
// Adds a single char to the buffer. When the buffer gets sufficiently
// full, we write its contents to the logging sink.
@@ -151,6 +153,53 @@
return ret;
}
=20
+
+/* A replacement for snprintf. */
+typedef=20
+ struct {
+ HChar* buf;
+ Int buf_size;
+ Int buf_used;
+ }=20
+ snprintf_buf;
+
+static void add_to_vg_snprintf_buf ( HChar c, void* p )
+{
+ snprintf_buf* b =3D p;
+ if (b->buf_size > 0 && b->buf_used < b->buf_size) {
+ b->buf[b->buf_used++] =3D c;
+ if (b->buf_used < b->buf_size)
+ b->buf[b->buf_used] =3D 0;
+ }=20
+}
+
+UInt VG_(vsnprintf) ( Char* buf, Int size, const HChar *format, va_list =
vargs )
+{
+ Int ret;
+ snprintf_buf b;
+ b.buf =3D buf;
+ b.buf_size =3D size < 0 ? 0 : size;
+ b.buf_used =3D 0;
+
+ ret =3D VG_(debugLog_vprintf)=20
+ ( add_to_vg_snprintf_buf, &b, format, vargs );
+
+ return b.buf_used;
+}
+
+UInt VG_(snprintf) ( Char* buf, Int size, const HChar *format, ... )
+{
+ UInt ret;
+ va_list vargs;
+
+ va_start(vargs,format);
+ ret =3D VG_(vsnprintf)(buf, size, format, vargs);
+ va_end(vargs);
+
+ return ret;
+}
+
+
/* ---------------------------------------------------------------------
percentify()
------------------------------------------------------------------ */
Modified: branches/ASPACEM/coregrind/m_main.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
--- branches/ASPACEM/coregrind/m_main.c 2005-08-09 00:45:22 UTC (rev 4351=
)
+++ branches/ASPACEM/coregrind/m_main.c 2005-08-09 11:37:09 UTC (rev 4352=
)
@@ -61,9 +61,7 @@
#include "pub_core_transtab.h"
#include "pub_core_ume.h"
=20
-#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
=20
#include "memcheck/memcheck.h"
=20
@@ -279,7 +277,7 @@
VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS|VKI_MAP_FIXED|VKI_MA=
P_NORESERVE,
-1, 0);
if (res.isError) {
- fprintf(stderr,=20
+ VG_(printf)(
"valgrind: Could not allocate address space (%p bytes)\n"
"valgrind: for shadow memory\n"
"valgrind: Possible causes:\n"
@@ -291,7 +289,7 @@
"valgrind: too-small (eg. 2GB) user address space.\n"
, (void*)shadow_size
);=20
- exit(1);
+ VG_(exit)(1);
}
}
}
@@ -304,14 +302,14 @@
// at call site.
static char* get_file_clo(char* dir)
{
-# define FLEN 512
Int n;
SysRes fd;
struct vki_stat s1;
Char* f_clo =3D NULL;
- Char filename[FLEN];
+ Char filename[VKI_PATH_MAX];
=20
- snprintf(filename, FLEN, "%s/.valgrindrc", ( NULL =3D=3D dir ? "" : d=
ir ) );
+ VG_(snprintf)(filename, VKI_PATH_MAX, "%s/.valgrindrc",=20
+ ( NULL =3D=3D dir ? "" : dir ) );
fd =3D VG_(open)(filename, 0, VKI_S_IRUSR);
if ( !fd.isError ) {
if ( 0 =3D=3D VG_(fstat)(fd.val, &s1) ) {
@@ -324,7 +322,6 @@
VG_(close)(fd.val);
}
return f_clo;
-# undef FLEN
}
=20
static Int count_args(char* s)
@@ -375,8 +372,8 @@
=20
// get_file_clo() allocates the return value with malloc(). We do no=
t
// free f1_clo and f2_clo as they get put into vg_argv[] which must p=
ersist.
- char* env_clo =3D getenv(VALGRINDOPTS);
- char* f1_clo =3D get_file_clo( getenv("HOME") );
+ char* env_clo =3D VG_(getenv)(VALGRINDOPTS);
+ char* f1_clo =3D get_file_clo( VG_(getenv)("HOME") );
char* f2_clo =3D get_file_clo(".");
=20
/* copy any extra args from file or environment, if present */
@@ -393,8 +390,8 @@
f2_arg_count =3D count_args(f2_clo);
=20
if (0)
- printf("extra-argc=3D%d %d %d\n",
- env_arg_count, f1_arg_count, f2_arg_count);
+ VG_(printf)("extra-argc=3D%d %d %d\n",
+ env_arg_count, f1_arg_count, f2_arg_count);
=20
/* +2: +1 for null-termination, +1 for added '--' */
from =3D vg_argv0;
@@ -447,7 +444,7 @@
int vg_argc0;
char** vg_argv0;
char** cl_argv;
- char* env_clo =3D getenv(VALGRINDCLO);
+ char* env_clo =3D VG_(getenv)(VALGRINDCLO);
=20
if (env_clo !=3D NULL && *env_clo !=3D '\0') {
char *cp;
@@ -509,7 +506,7 @@
if (0) {
Int i;
for (i =3D 0; i < vg_argc0; i++)
- printf("vg_argv0[%d]=3D\"%s\"\n", i, vg_argv0[i]);
+ VG_(printf)("vg_argv0[%d]=3D\"%s\"\n", i, vg_argv0[i]);
}
=20
*vg_argc_out =3D vg_argc0;
@@ -595,11 +592,11 @@
vg_assert(preload_core_path);
=20
if (preload)
- snprintf(preload_core_path, preload_core_path_len, "%s/%s:%s",=20
- VG_(libdir), preload_core_so, preload);
+ VG_(snprintf)(preload_core_path, preload_core_path_len, "%s/%s:%s"=
,=20
+ VG_(libdir), preload_core_so, preload);
else
- snprintf(preload_core_path, preload_core_path_len, "%s/%s",=20
- VG_(libdir), preload_core_so);
+ VG_(snprintf)(preload_core_path, preload_core_path_len, "%s/%s",=20
+ VG_(libdir), preload_core_so);
=20
/* Count the original size of the env */
envc =3D 0; /* trailing NULL */
@@ -624,8 +621,8 @@
HChar *cp =3D malloc(len);
vg_assert(cp);
=20
- snprintf(cp, len, "%s%s:%s",
- ld_preload, preload_core_path, (*cpp)+ld_preload_len);
+ VG_(snprintf)(cp, len, "%s%s:%s",
+ ld_preload, preload_core_path, (*cpp)+ld_preload_=
len);
=20
*cpp =3D cp;
=20
@@ -641,7 +638,7 @@
HChar *cp =3D malloc(len);
vg_assert(cp);
=20
- snprintf(cp, len, "%s%s", ld_preload, preload_core_path);
+ VG_(snprintf)(cp, len, "%s%s", ld_preload, preload_core_path);
=20
ret[envc++] =3D cp;
}
@@ -665,7 +662,7 @@
*cp++ =3D '\0';
=20
if (0)
- printf("copied %p \"%s\" len %lld\n", orig, orig, (Long)(cp-orig))=
;
+ VG_(printf)("copied %p \"%s\" len %lld\n", orig, orig, (Long)(cp-o=
rig));
=20
*tab =3D cp;
=20
@@ -794,11 +791,11 @@
VG_(clstk_base) =3D VG_PGROUNDDN(cl_esp);
=20
if (0)
- printf("stringsize=3D%d auxsize=3D%d stacksize=3D%d\n"
- "clstk_base %p\n"
- "clstk_end %p\n",
- stringsize, auxsize, stacksize,
- (void*)VG_(clstk_base), (void*)VG_(clstk_end));
+ VG_(printf)("stringsize=3D%d auxsize=3D%d stacksize=3D%d\n"
+ "clstk_base %p\n"
+ "clstk_end %p\n",
+ stringsize, auxsize, stacksize,
+ (void*)VG_(clstk_base), (void*)VG_(clstk_end));
=20
/* =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D alloc=
ate space =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D */
=20
@@ -927,7 +924,7 @@
default:
/* stomp out anything we don't know about */
if (0)
- printf("stomping auxv entry %lld\n", (ULong)auxv->a_type);
+ VG_(printf)("stomping auxv entry %lld\n", (ULong)auxv->a_type);
auxv->a_type =3D AT_IGNORE;
break;
=20
@@ -960,8 +957,8 @@
if (*entry =3D=3D '\0')
entry =3D ".";
=20
- snprintf(buf, sizeof(buf), "%s/%s", entry, executable_name);
- if (access(buf, R_OK|X_OK) =3D=3D 0) {
+ VG_(snprintf)(buf, sizeof(buf), "%s/%s", entry, executable_name);
+ if (VG_(access)(buf, True/*r*/, False/*w*/, True/*x*/) =3D=3D 0) {
VG_(strncpy)( executable_name, buf, VKI_PATH_MAX-1 );
executable_name[VKI_PATH_MAX-1] =3D 0;
return True;
@@ -977,7 +974,7 @@
=20
if (VG_(strchr)(executable_name, '/') =3D=3D NULL) {
/* no '/' - we need to search the path */
- char *path =3D getenv("PATH");
+ char *path =3D VG_(getenv)("PATH");
scan_colsep(path, match_executable);
}
return executable_name;
@@ -1005,15 +1002,16 @@
Int len =3D VG_(strlen)(VG_(libdir)) + VG_(strlen)(toolname) + 16;
HChar buf[len];
=20
- snprintf(buf, len, "%s/vgpreload_%s.so", VG_(libdir), toolname);
- if (access(buf, R_OK) =3D=3D 0 || len >=3D VKI_PATH_MAX-1) {
+ VG_(snprintf)(buf, len, "%s/vgpreload_%s.so", VG_(libdir), toolname);
+ if (VG_(access)(buf, True/*r*/, False/*w*/, False/*x*/) =3D=3D 0=20
+ && len < VKI_PATH_MAX-1) {
VG_(strncpy)( load_tool__preloadpath, buf, VKI_PATH_MAX-1 );
load_tool__preloadpath[VKI_PATH_MAX-1] =3D 0;
*preloadpath_out =3D load_tool__preloadpath;
} else {
- fprintf(stderr, "valgrind: couldn't find preload file for tool %s\=
n",=20
- toolname);
- exit(127);
+ VG_(printf)("valgrind: couldn't find preload file for tool %s\n",=20
+ toolname);
+ VG_(exit)(127);
}
}
=20
@@ -1086,9 +1084,9 @@
.val;
ret =3D VG_(do_exec)(exec, info);
if (ret !=3D 0) {
- fprintf(stderr, "valgrind: do_exec(%s) failed: %s\n",
- exec, VG_(strerror)(ret));
- exit(127);
+ VG_(printf)("valgrind: do_exec(%s) failed: %s\n",
+ exec, VG_(strerror)(ret));
+ VG_(exit)(127);
}
}
=20
@@ -1241,8 +1239,8 @@
for (i =3D 1; i < vg_argc; i++) {
=20
if (local_strcmp(vg_argv[i], "--version") =3D=3D 0) {
- printf("valgrind-" VERSION "\n");
- exit(0);
+ VG_(printf)("valgrind-" VERSION "\n");
+ VG_(exit)(0);
=20
} else if (VG_CLO_STREQ(vg_argv[i], "--help") ||
VG_CLO_STREQ(vg_argv[i], "-h")) {
@@ -2266,25 +2264,35 @@
=20
Int main(Int argc, HChar **argv, HChar **envp)
{
- HChar **cl_argv;
- const HChar *tool =3D "memcheck"; // default to Memcheck
- const HChar *exec =3D NULL;
- HChar *preload; /* tool-specific LD_PRELOAD .so */
- HChar **env;
- Int need_help =3D 0; // 0 =3D no, 1 =3D --help, 2 =3D --help-deb=
ug
- struct exeinfo info;
- ToolInfo *toolinfo =3D NULL;
- Addr client_eip;
- Addr sp_at_startup; /* client's SP at the point we gained control=
. */
- UInt * client_auxv;
- struct vki_rlimit zero =3D { 0, 0 };
- Int loglevel, i;
+ HChar** cl_argv;
+ const HChar* tool =3D "memcheck"; // default to Memcheck
+ const HChar* exec =3D NULL;
+ HChar* preload; /* tool-specific LD_PRELOAD .so */
+ HChar** env;
+ Int need_help =3D 0; // 0 =3D no, 1 =3D --help, 2 =3D =
--help-debug
+ struct exeinfo info;
+ ToolInfo* toolinfo =3D NULL;
+ Addr client_eip;
+ Addr sp_at_startup; /* client's SP at the point we
+ gained control. */
+ UInt* client_auxv;
+ Int loglevel, i;
+ struct vki_rlimit zero =3D { 0, 0 };
=20
//=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
// Nb: startup is complex. Prerequisites are shown at every step.
//
// *** Be very careful when messing with the order ***
+ //
+ // TODO (JRS 9 Aug 05):=20
+ // - there's circular dependencies with VG_(getenv).
+ // TODO: review and clarify all issues to do with
+ // environment variables.
+ // =20
//=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
+ =20
+ /* This is needed to make VG_(getenv) usable early. */
+ VG_(client_envp) =3D (Char**)envp;
=20
//--------------------------------------------------------------
// Start up the logging mechanism
@@ -2340,7 +2348,7 @@
// Look for alternative libdir =20
// p: none
//--------------------------------------------------------------
- { HChar *cp =3D getenv(VALGRINDLIB);
+ if (0) { HChar *cp =3D VG_(getenv)(VALGRINDLIB);
if (cp !=3D NULL)
VG_(libdir) =3D cp;
}
Modified: branches/ASPACEM/coregrind/pub_core_libcfile.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
--- branches/ASPACEM/coregrind/pub_core_libcfile.h 2005-08-09 00:45:22 UT=
C (rev 4351)
+++ branches/ASPACEM/coregrind/pub_core_libcfile.h 2005-08-09 11:37:09 UT=
C (rev 4352)
@@ -60,6 +60,8 @@
extern Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optv=
al,
Int *optlen );
=20
+extern Int VG_(access) ( HChar* path, Bool irusr, Bool iwusr, Bool ixusr=
);
+
#endif // __PUB_CORE_LIBCFILE_H
=20
/*--------------------------------------------------------------------*/
Modified: branches/ASPACEM/include/pub_tool_libcprint.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
--- branches/ASPACEM/include/pub_tool_libcprint.h 2005-08-09 00:45:22 UTC=
(rev 4351)
+++ branches/ASPACEM/include/pub_tool_libcprint.h 2005-08-09 11:37:09 UTC=
(rev 4352)
@@ -39,12 +39,18 @@
* --log-fd/--log-file/--log-socket argument, which defaults to 2 (stder=
r).
* Hence no need for VG_(fprintf)().
*/
-extern UInt VG_(printf) ( const HChar *format, ... );
-extern UInt VG_(vprintf) ( const HChar *format, va_list vargs );
+extern UInt VG_(printf) ( const HChar *format, ... );
+extern UInt VG_(vprintf) ( const HChar *format, va_list vargs );
/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
-extern UInt VG_(sprintf) ( Char* buf, const HChar* format, ... );
-extern UInt VG_(vsprintf)( Char* buf, const HChar* format, va_list vargs=
);
=20
+extern UInt VG_(sprintf) ( Char* buf, const HChar* format, ... );
+extern UInt VG_(vsprintf) ( Char* buf, const HChar* format, va_list varg=
s );
+
+extern UInt VG_(snprintf) ( Char* buf, Int size,=20
+ const HChar *format, ... );
+extern UInt VG_(vsnprintf)( Char* buf, Int size,=20
+ const HChar *format, va_list varg=
s );
+
// Percentify n/m with d decimal places. Includes the '%' symbol at the=
end.
extern void VG_(percentify)(UInt n, UInt m, UInt d, Int n_buf, char buf[=
]);
=20
|