From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25327 invoked by alias); 20 May 2008 18:01:23 -0000 Received: (qmail 25301 invoked by uid 367); 20 May 2008 18:01:23 -0000 Date: Tue, 20 May 2008 18:01:00 -0000 Message-ID: <20080520180123.25285.qmail@sourceware.org> From: cagney@sourceware.org To: frysk-cvs@sourceware.org Subject: [SCM] master: Don't copy FileBytes. X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Oldrev: 73d257dda14d543954b156e4b5ecbb20312cc0b0 X-Git-Newrev: e3e3774f9865d9c6e7a94fc53b60ab5c0a5a96aa Mailing-List: contact frysk-cvs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: frysk-cvs-owner@sourceware.org Reply-To: frysk@sourceware.org X-SW-Source: 2008-q2/txt/msg00255.txt.bz2 The branch, master has been updated via e3e3774f9865d9c6e7a94fc53b60ab5c0a5a96aa (commit) from 73d257dda14d543954b156e4b5ecbb20312cc0b0 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email. - Log ----------------------------------------------------------------- commit e3e3774f9865d9c6e7a94fc53b60ab5c0a5a96aa Author: Andrew Cagney Date: Tue May 20 13:59:50 2008 -0400 Don't copy FileBytes. frysk-sys/frysk/sys/proc/ChangeLog 2008-05-20 Andrew Cagney * jni/Stat.cxx: Delete stray print statements. frysk-sys/jnixx/ChangeLog 2008-05-20 Andrew Cagney * elements.hxx (class FileBytes): Add pid/tid method. * elements.cxx (FileBytes::FileBytes): Implement. (slurp): Refactor out of FileBytes constructor; include NUL in length. ----------------------------------------------------------------------- Summary of changes: frysk-sys/frysk/sys/proc/ChangeLog | 2 + frysk-sys/frysk/sys/proc/jni/Stat.cxx | 2 - frysk-sys/jnixx/ChangeLog | 5 ++ frysk-sys/jnixx/elements.cxx | 68 ++++++++++++++++++++++----------- frysk-sys/jnixx/elements.hxx | 1 + 5 files changed, 53 insertions(+), 25 deletions(-) First 500 lines of diff: diff --git a/frysk-sys/frysk/sys/proc/ChangeLog b/frysk-sys/frysk/sys/proc/ChangeLog index dc6accf..a2a3c3a 100644 --- a/frysk-sys/frysk/sys/proc/ChangeLog +++ b/frysk-sys/frysk/sys/proc/ChangeLog @@ -1,5 +1,7 @@ 2008-05-20 Andrew Cagney + * jni/Stat.cxx: Delete stray print statements. + * jni/Stat.cxx: Implement. * jni/MapsBuilder.cxx: Implement. diff --git a/frysk-sys/frysk/sys/proc/jni/Stat.cxx b/frysk-sys/frysk/sys/proc/jni/Stat.cxx index d4135bb..df0e85a 100644 --- a/frysk-sys/frysk/sys/proc/jni/Stat.cxx +++ b/frysk-sys/frysk/sys/proc/jni/Stat.cxx @@ -130,9 +130,7 @@ frysk::sys::proc::Stat::scan(jnixx::env env, jint procPid, jint threadTid) { frysk::sys::proc::Stat frysk::sys::proc::Stat::scan(jnixx::env env, jint procPid) { - fprintf(stderr, "opening %d\n", procPid); FileBytes bytes = FileBytes(env, procPid, "stat"); - fprintf(stderr, "elements %s\n", bytes.elements); if (bytes.elements == NULL) return Stat(env, NULL); ::scan(env, (const char*) bytes.elements, *this, GetFine(env)); diff --git a/frysk-sys/jnixx/ChangeLog b/frysk-sys/jnixx/ChangeLog index ed3049d..fb7e090 100644 --- a/frysk-sys/jnixx/ChangeLog +++ b/frysk-sys/jnixx/ChangeLog @@ -1,5 +1,10 @@ 2008-05-20 Andrew Cagney + * elements.hxx (class FileBytes): Add pid/tid method. + * elements.cxx (FileBytes::FileBytes): Implement. + (slurp): Refactor out of FileBytes constructor; include NUL in + length. + * scan.hxx: New. * scan.cxx: New. diff --git a/frysk-sys/jnixx/elements.cxx b/frysk-sys/jnixx/elements.cxx index 937609c..65b3315 100644 --- a/frysk-sys/jnixx/elements.cxx +++ b/frysk-sys/jnixx/elements.cxx @@ -101,16 +101,8 @@ chars2strings(::jnixx::env env, char** argv) { return strings; } -FileBytes::FileBytes(jnixx::env env, const char* fmt, ...) { - // Convert the string into a file. - char file[FILENAME_MAX]; - va_list ap; - va_start(ap, fmt); - if (::vsnprintf(file, sizeof file, fmt, ap) >= FILENAME_MAX) { - errnoException(env, errno, "snprintf"); - } - va_end(ap); - +void +slurp(jnixx::env env, FileBytes& bytes, const char* file) { // Attempt to open the file. int fd = ::open(file, O_RDONLY); if (fd < 0) { @@ -124,49 +116,79 @@ FileBytes::FileBytes(jnixx::env env, const char* fmt, ...) { // reads are needed to confirm EOF. Allocating 2&BUFSIZE ensures // that there's always space for at least two reads. Ref SW #3370 jsize allocated = BUFSIZ * 2 + 1; - elements = (jbyte*) ::malloc(allocated); - if (elements == NULL) { + bytes.elements = (jbyte*) ::malloc(allocated); + if (bytes.elements == NULL) { errnoException(env, errno, "malloc"); } - length = 0; + bytes.length = 0; while (true) { // Attempt to fill the remaining buffer; less space for a // terminating NUL character. - int size = ::read(fd, elements + length, allocated - length - 1); + int size = ::read(fd, bytes.elements + bytes.length, + allocated - bytes.length - 1); if (size < 0) { ::close(fd); - release(); + bytes.release(); + // Abandon the read with elements == NULL. return; } else if (size == 0) { break; } - length += size; + bytes.length += size; - if (length + BUFSIZ >= allocated) { + if (bytes.length + BUFSIZ >= allocated) { // Not enough space for the next ~BUFSIZ'd read; expand the // buffer. Don't trust realloc with the pointer; will need to // free the old buffer if something goes wrong. allocated += BUFSIZ; - jbyte *tmp = (jbyte*)::realloc(elements, allocated); + jbyte *tmp = (jbyte*)::realloc(bytes.elements, allocated); if (tmp == NULL) { int err = errno; ::close(fd); - release(); + bytes.release(); errnoException(env, err, "realloc"); } - elements = tmp; + bytes.elements = tmp; } } ::close(fd); // Null terminate the buffer. - elements[length] = '\0'; + bytes.elements[bytes.length] = '\0'; + bytes.length++; // count the trailing NUL } -FileBytes::FileBytes(jnixx::env env, jint pid, const char* name) { - FileBytes(env, "/proc/%d/%s", (int) pid, name); +FileBytes::FileBytes(jnixx::env env, const char* fmt, ...) { + // Convert the string into a file. + char file[FILENAME_MAX]; + va_list ap; + va_start(ap, fmt); + if (::vsnprintf(file, sizeof file, fmt, ap) >= FILENAME_MAX) { + errnoException(env, errno, "vsnprintf"); + } + va_end(ap); + slurp(env, *this, file); +} + +FileBytes::FileBytes(jnixx::env env, int pid, const char* name) { + // Convert the string into a file. + char file[FILENAME_MAX]; + if (::snprintf(file, sizeof file, "/proc/%d/%s", pid, name) >= FILENAME_MAX) { + errnoException(env, errno, "snprintf"); + } + slurp(env, *this, file); +} + +FileBytes::FileBytes(jnixx::env env, int pid, int tid, const char* name) { + // Convert the string into a file. + char file[FILENAME_MAX]; + if (::snprintf(file, sizeof file, "/proc/%d/task/%d/%s", pid, tid, name) + >= FILENAME_MAX) { + errnoException(env, errno, "snprintf"); + } + slurp(env, *this, file); } void diff --git a/frysk-sys/jnixx/elements.hxx b/frysk-sys/jnixx/elements.hxx index ebaad34..28b41f8 100644 --- a/frysk-sys/jnixx/elements.hxx +++ b/frysk-sys/jnixx/elements.hxx @@ -100,6 +100,7 @@ public: FileBytes(jnixx::env, const char* fmt, ...) __attribute__((format(printf, 3, 4))); FileBytes(jnixx::env, int pid, const char* name); + FileBytes(jnixx::env, int pid, int tid, const char* name); void release(); ~FileBytes() { release(); hooks/post-receive -- frysk system monitor/debugger