Fixed a bug in Cachegrind -- if the profiled program changes working directory,
the output file wouldn't get written.  No longer creating the file at startup
and then writing it at the end;  just writing it at the end.  Also recording
the start directory at the start so that the output ends up in it even if the
program does change directory.

Had to add VG_(getcwd)() to vg_mylibc.c for this.

Added a regression test for it too.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1576 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c
index 115949e..b81e36c 100644
--- a/coregrind/vg_mylibc.c
+++ b/coregrind/vg_mylibc.c
@@ -1131,6 +1131,7 @@
    panic(VG_(details).name, VG_(details).bug_reports_to, str);
 }
 
+
 /* ---------------------------------------------------------------------
    Primitive support for reading files.
    ------------------------------------------------------------------ */
@@ -1149,9 +1150,7 @@
       ok: */
    fd = vg_do_syscall3(__NR_open, (UInt)pathname, flags, mode);
    /* VG_(printf)("result = %d\n", fd); */
-   if (VG_(is_kerror)(fd)) {
-      fd = -1;
-   } 
+   if (VG_(is_kerror)(fd)) fd = -1;
    return fd;
 }
 
@@ -1200,7 +1199,20 @@
    return VG_(is_kerror)(res) ? (-1) : 0;
 }
 
-/* Misc functions looking for a proper home. */
+/* Nb: we do not allow the Linux extension which malloc()s memory for the
+   buffer if buf==NULL, because we don't want Linux calling malloc() */
+Char* VG_(getcwd) ( Char* buf, Int size )
+{
+   Int res;
+   vg_assert(buf != NULL);
+   res = vg_do_syscall2(__NR_getcwd, (UInt)buf, (UInt)size);
+   return VG_(is_kerror)(res) ? ((Char*)NULL) : (Char*)res;
+}
+
+
+/* ---------------------------------------------------------------------
+   Misc functions looking for a proper home.
+   ------------------------------------------------------------------ */
 
 /* We do getenv without libc's help by snooping around in
    VG_(client_envp) as determined at startup time. */