Run through all command line arguments on Darwin.

Also fixes the basename offset for highlighting the basename.
Closes #379.
This commit is contained in:
Hisham Muhammad 2016-02-18 23:45:17 -02:00
parent 5ee6715843
commit 42c4459375
2 changed files with 16 additions and 57 deletions

View File

@ -71,7 +71,7 @@ void DarwinProcess_setStartTime(Process *proc, struct extern_proc *ep, time_t no
strftime(proc->starttime_show, 7, ((proc->starttime_ctime > now - 86400) ? "%R " : "%b%d "), &date); strftime(proc->starttime_show, 7, ((proc->starttime_ctime > now - 86400) ? "%R " : "%b%d "), &date);
} }
char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) { char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset) {
/* This function is from the old Mac version of htop. Originally from ps? */ /* This function is from the old Mac version of htop. Originally from ps? */
int mib[3], argmax, nargs, c = 0; int mib[3], argmax, nargs, c = 0;
size_t size; size_t size;
@ -169,13 +169,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
/* Save where the argv[0] string starts. */ /* Save where the argv[0] string starts. */
sp = cp; sp = cp;
/* *basenameOffset = 0;
* Iterate through the '\0'-terminated strings and convert '\0' to ' '
* until a string is found that has a '=' character in it (or there are
* no more strings in procargs). There is no way to deterministically
* know where the command arguments end and the environment strings
* start, which is why the '=' character is searched for as a heuristic.
*/
for ( np = NULL; c < nargs && cp < &procargs[size]; cp++ ) { for ( np = NULL; c < nargs && cp < &procargs[size]; cp++ ) {
if ( *cp == '\0' ) { if ( *cp == '\0' ) {
c++; c++;
@ -185,49 +179,11 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
} }
/* Note location of current '\0'. */ /* Note location of current '\0'. */
np = cp; np = cp;
if (*basenameOffset == 0) {
if ( !show_args ) { *basenameOffset = cp - sp;
/*
* Don't convert '\0' characters to ' '.
* However, we needed to know that the
* command name was terminated, which we
* now know.
*/
break;
} }
} }
}
#if 0
/*
* If eflg is non-zero, continue converting '\0' characters to ' '
* characters until no more strings that look like environment settings
* follow.
*/
if ( ( eflg != 0 )
&& ( ( getuid( ) == 0 )
|| ( k->kp_eproc.e_pcred.p_ruid == getuid( ) ) ) ) {
for ( ; cp < &procargs[size]; cp++ ) {
if ( *cp == '\0' ) {
if ( np != NULL ) {
if ( &np[1] == cp ) {
/*
* Two '\0' characters in a row.
* This should normally only
* happen after all the strings
* have been seen, but in any
* case, stop parsing.
*/
break;
}
/* Convert previous '\0'. */
*np = ' ';
}
/* Note location of current '\0'. */
np = cp;
}
}
} }
#endif
/* /*
* sp points to the beginning of the arguments/environment string, and * sp points to the beginning of the arguments/environment string, and
@ -237,6 +193,9 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
/* Empty or unterminated string. */ /* Empty or unterminated string. */
goto ERROR_B; goto ERROR_B;
} }
if (*basenameOffset == 0) {
*basenameOffset = np - sp;
}
/* Make a copy of the string. */ /* Make a copy of the string. */
retval = xStrdup(sp); retval = xStrdup(sp);
@ -250,6 +209,7 @@ ERROR_B:
free( procargs ); free( procargs );
ERROR_A: ERROR_A:
retval = xStrdup(k->kp_proc.p_comm); retval = xStrdup(k->kp_proc.p_comm);
*basenameOffset = strlen(retval);
return retval; return retval;
} }
@ -285,13 +245,7 @@ void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t
proc->tty_nr = ps->kp_eproc.e_tdev & 0xff; /* TODO tty_nr is unsigned */ proc->tty_nr = ps->kp_eproc.e_tdev & 0xff; /* TODO tty_nr is unsigned */
DarwinProcess_setStartTime(proc, ep, now); DarwinProcess_setStartTime(proc, ep, now);
proc->comm = DarwinProcess_getCmdLine(ps, &(proc->basenameOffset));
/* The command is from the old Mac htop */
char *slash;
proc->comm = DarwinProcess_getCmdLine(ps, false);
slash = strrchr(proc->comm, '/');
proc->basenameOffset = (NULL != slash) ? (slash - proc->comm) : 0;
} }
/* Mutable information */ /* Mutable information */

View File

@ -32,12 +32,17 @@ bool Process_isThread(Process* this);
void DarwinProcess_setStartTime(Process *proc, struct extern_proc *ep, time_t now); void DarwinProcess_setStartTime(Process *proc, struct extern_proc *ep, time_t now);
char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ); char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset);
void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t now, bool exists); void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t now, bool exists);
void DarwinProcess_setFromLibprocPidinfo(DarwinProcess *proc, DarwinProcessList *dpl); void DarwinProcess_setFromLibprocPidinfo(DarwinProcess *proc, DarwinProcessList *dpl);
/*
* Scan threads for process state information.
* Based on: http://stackoverflow.com/questions/6788274/ios-mac-cpu-usage-for-thread
* and https://github.com/max-horvath/htop-osx/blob/e86692e869e30b0bc7264b3675d2a4014866ef46/ProcessList.c
*/
void DarwinProcess_scanThreads(DarwinProcess *dp); void DarwinProcess_scanThreads(DarwinProcess *dp);
#endif #endif