2006-03-04 18:16:49 +00:00
|
|
|
#ifndef HEADER_Process
|
|
|
|
#define HEADER_Process
|
|
|
|
/*
|
|
|
|
htop - Process.h
|
2015-03-21 19:52:54 +00:00
|
|
|
(C) 2004-2015 Hisham H. Muhammad
|
2020-08-19 23:35:24 +00:00
|
|
|
(C) 2020 Red Hat, Inc. All Rights Reserved.
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2006-03-04 18:16:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <stdbool.h>
|
2020-10-09 13:13:06 +00:00
|
|
|
#include <stdint.h>
|
2020-11-01 00:36:53 +00:00
|
|
|
#include <sys/types.h>
|
2020-09-19 11:55:23 +00:00
|
|
|
|
|
|
|
#include "Object.h"
|
2020-12-15 18:44:48 +00:00
|
|
|
#include "ProcessField.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "RichString.h"
|
|
|
|
|
2015-05-20 02:30:11 +00:00
|
|
|
|
2021-05-25 17:02:12 +00:00
|
|
|
#define PROCESS_FLAG_IO 0x00000001
|
|
|
|
#define PROCESS_FLAG_CWD 0x00000002
|
|
|
|
|
2020-10-31 01:56:16 +00:00
|
|
|
#define DEFAULT_HIGHLIGHT_SECS 5
|
2013-05-24 22:46:01 +00:00
|
|
|
|
2020-12-15 18:44:36 +00:00
|
|
|
typedef enum ProcessField_ {
|
2016-05-30 15:22:07 +00:00
|
|
|
NULL_PROCESSFIELD = 0,
|
2015-03-16 04:43:04 +00:00
|
|
|
PID = 1,
|
|
|
|
COMM = 2,
|
|
|
|
STATE = 3,
|
|
|
|
PPID = 4,
|
|
|
|
PGRP = 5,
|
|
|
|
SESSION = 6,
|
2021-03-21 18:40:56 +00:00
|
|
|
TTY = 7,
|
2015-03-16 04:43:04 +00:00
|
|
|
TPGID = 8,
|
|
|
|
MINFLT = 10,
|
|
|
|
MAJFLT = 12,
|
|
|
|
PRIORITY = 18,
|
|
|
|
NICE = 19,
|
|
|
|
STARTTIME = 21,
|
2015-03-17 02:01:48 +00:00
|
|
|
PROCESSOR = 38,
|
2020-11-20 16:09:34 +00:00
|
|
|
M_VIRT = 39,
|
2015-03-16 04:43:04 +00:00
|
|
|
M_RESIDENT = 40,
|
|
|
|
ST_UID = 46,
|
|
|
|
PERCENT_CPU = 47,
|
|
|
|
PERCENT_MEM = 48,
|
|
|
|
USER = 49,
|
|
|
|
TIME = 50,
|
|
|
|
NLWP = 51,
|
|
|
|
TGID = 52,
|
2020-10-30 16:02:20 +00:00
|
|
|
PERCENT_NORM_CPU = 53,
|
2021-05-02 11:29:39 +00:00
|
|
|
ELAPSED = 54,
|
2021-04-18 17:25:56 +00:00
|
|
|
PROC_COMM = 124,
|
|
|
|
PROC_EXE = 125,
|
2021-05-25 17:02:12 +00:00
|
|
|
CWD = 126,
|
2020-12-15 18:44:48 +00:00
|
|
|
|
|
|
|
/* Platform specific fields, defined in ${platform}/ProcessField.h */
|
|
|
|
PLATFORM_PROCESS_FIELDS
|
|
|
|
|
|
|
|
LAST_PROCESSFIELD
|
2015-03-16 04:43:04 +00:00
|
|
|
} ProcessField;
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-10-19 10:05:06 +00:00
|
|
|
struct Settings_;
|
|
|
|
|
2021-04-10 09:10:50 +00:00
|
|
|
/* Holds information about regions of the cmdline that should be
|
|
|
|
* highlighted (e.g. program basename, delimiter, comm). */
|
|
|
|
typedef struct ProcessCmdlineHighlight_ {
|
|
|
|
size_t offset; /* first character to highlight */
|
|
|
|
size_t length; /* How many characters to highlight, zero if unused */
|
|
|
|
int attr; /* The attributes used to highlight */
|
|
|
|
int flags; /* Special flags used for selective highlighting, zero for always */
|
|
|
|
} ProcessCmdlineHighlight;
|
|
|
|
|
|
|
|
/* ProcessMergedCommand is populated by Process_makeCommandStr: It
|
|
|
|
* contains the merged Command string, and the information needed by
|
|
|
|
* Process_writeCommand to color the string. str will be NULL for kernel
|
|
|
|
* threads and zombies */
|
|
|
|
typedef struct ProcessMergedCommand_ {
|
2021-07-14 17:24:18 +00:00
|
|
|
char* str; /* merged Command string */
|
2021-04-10 09:10:50 +00:00
|
|
|
size_t highlightCount; /* how many portions of cmdline to highlight */
|
|
|
|
ProcessCmdlineHighlight highlights[8]; /* which portions of cmdline to highlight */
|
|
|
|
bool separateComm : 1; /* whether comm is a separate field */
|
|
|
|
bool unmatchedExe : 1; /* whether exe matched with cmdline */
|
|
|
|
bool cmdlineChanged : 1; /* whether cmdline changed */
|
|
|
|
bool exeChanged : 1; /* whether exe changed */
|
|
|
|
bool commChanged : 1; /* whether comm changed */
|
|
|
|
bool prevMergeSet : 1; /* whether showMergedCommand was set */
|
|
|
|
bool prevPathSet : 1; /* whether showProgramPath was set */
|
|
|
|
bool prevCommSet : 1; /* whether findCommInCmdline was set */
|
|
|
|
bool prevCmdlineSet : 1; /* whether stripExeFromCmdline was set */
|
2021-07-17 18:59:50 +00:00
|
|
|
bool prevShowThreadNames : 1; /* whether showThreadNames was set */
|
2021-04-10 09:10:50 +00:00
|
|
|
} ProcessMergedCommand;
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
typedef struct Process_ {
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Super object for emulated OOP */
|
2006-03-04 18:16:49 +00:00
|
|
|
Object super;
|
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Pointer to quasi-global data structures */
|
2020-10-31 01:56:16 +00:00
|
|
|
const struct ProcessList_* processList;
|
2020-10-21 19:26:05 +00:00
|
|
|
const struct Settings_* settings;
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Process identifier */
|
2010-02-25 01:43:18 +00:00
|
|
|
pid_t pid;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Parent process identifier */
|
2015-03-16 04:43:04 +00:00
|
|
|
pid_t ppid;
|
2015-03-15 23:29:13 +00:00
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Thread group identifier */
|
|
|
|
pid_t tgid;
|
2015-03-15 23:29:13 +00:00
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Process group identifier */
|
2021-01-27 14:12:15 +00:00
|
|
|
int pgrp;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Session identifier */
|
2021-01-27 14:12:15 +00:00
|
|
|
int session;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Foreground group identifier of the controlling terminal */
|
2007-11-08 23:23:01 +00:00
|
|
|
int tpgid;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
2021-04-10 12:08:26 +00:00
|
|
|
/* This is a kernel (helper) task */
|
|
|
|
bool isKernelThread;
|
|
|
|
|
|
|
|
/* This is a userland thread / LWP */
|
|
|
|
bool isUserlandThread;
|
|
|
|
|
2021-03-21 18:40:56 +00:00
|
|
|
/* Controlling terminal identifier of the process */
|
|
|
|
unsigned long int tty_nr;
|
|
|
|
|
|
|
|
/* Controlling terminal name of the process */
|
|
|
|
char* tty_name;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* User identifier */
|
2015-03-16 04:43:04 +00:00
|
|
|
uid_t st_uid;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* User name */
|
|
|
|
const char* user;
|
|
|
|
|
2021-01-30 12:39:19 +00:00
|
|
|
/* Process runtime (in hundredth of a second) */
|
|
|
|
unsigned long long int time;
|
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/*
|
|
|
|
* Process name including arguments.
|
|
|
|
* Use Process_getCommand() for Command actually displayed.
|
|
|
|
*/
|
2020-12-19 15:21:08 +00:00
|
|
|
char* cmdline;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
2021-04-10 09:46:57 +00:00
|
|
|
/* End Offset in cmdline of the process basename */
|
|
|
|
int cmdlineBasenameEnd;
|
2021-02-01 21:24:42 +00:00
|
|
|
|
2021-04-10 09:18:08 +00:00
|
|
|
/* Start Offset in cmdline of the process basename */
|
|
|
|
int cmdlineBasenameStart;
|
|
|
|
|
2021-01-30 14:31:59 +00:00
|
|
|
/* The process' "command" name */
|
2021-07-14 17:24:18 +00:00
|
|
|
char* procComm;
|
2021-01-30 14:31:59 +00:00
|
|
|
|
|
|
|
/* The main process executable */
|
2021-07-14 17:24:18 +00:00
|
|
|
char* procExe;
|
2021-01-30 14:31:59 +00:00
|
|
|
|
2021-05-25 17:02:12 +00:00
|
|
|
/* The process/thread working directory */
|
2021-07-14 17:24:18 +00:00
|
|
|
char* procCwd;
|
2021-05-25 17:02:12 +00:00
|
|
|
|
2021-02-01 22:23:10 +00:00
|
|
|
/* Offset in procExe of the process basename */
|
|
|
|
int procExeBasenameOffset;
|
|
|
|
|
2021-02-01 21:37:41 +00:00
|
|
|
/* Tells if the executable has been replaced in the filesystem since start */
|
|
|
|
bool procExeDeleted;
|
|
|
|
|
2021-04-04 16:07:26 +00:00
|
|
|
/* Tells if the process uses replaced shared libraries since start */
|
|
|
|
bool usesDeletedLib;
|
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* CPU number last executed on */
|
2015-03-17 02:01:48 +00:00
|
|
|
int processor;
|
2014-02-27 19:35:22 +00:00
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* CPU usage during last cycle (in percent) */
|
2014-02-27 19:35:22 +00:00
|
|
|
float percent_cpu;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Memory usage during last cycle (in percent) */
|
2014-02-27 19:35:22 +00:00
|
|
|
float percent_mem;
|
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Scheduling priority */
|
2006-03-04 18:16:49 +00:00
|
|
|
long int priority;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Nice value */
|
2006-03-04 18:16:49 +00:00
|
|
|
long int nice;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Number of threads in this process */
|
2007-05-21 19:10:53 +00:00
|
|
|
long int nlwp;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Process start time (in seconds elapsed since the Epoch) */
|
2010-03-29 18:36:11 +00:00
|
|
|
time_t starttime_ctime;
|
2014-02-27 19:35:22 +00:00
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Process start time (cached formatted string) */
|
|
|
|
char starttime_show[8];
|
|
|
|
|
|
|
|
/* Total program size (in kilobytes) */
|
2020-11-20 16:09:34 +00:00
|
|
|
long m_virt;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Resident set size (in kilobytes) */
|
2014-04-10 01:32:54 +00:00
|
|
|
long m_resident;
|
2014-02-27 19:35:22 +00:00
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Number of minor faults the process has made which have not required loading a memory page from disk */
|
|
|
|
unsigned long int minflt;
|
|
|
|
|
|
|
|
/* Number of major faults the process has made which have required loading a memory page from disk */
|
|
|
|
unsigned long int majflt;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process state (platform dependent):
|
|
|
|
* D - Waiting
|
|
|
|
* I - Idle
|
|
|
|
* L - Acquiring lock
|
|
|
|
* R - Running
|
|
|
|
* S - Sleeping
|
|
|
|
* T - Stopped (on a signal)
|
|
|
|
* X - Dead
|
|
|
|
* Z - Zombie
|
|
|
|
* t - Tracing stop
|
|
|
|
* ? - Unknown
|
|
|
|
*/
|
|
|
|
char state;
|
|
|
|
|
|
|
|
/* Whether the process was updated during the current scan */
|
|
|
|
bool updated;
|
|
|
|
|
|
|
|
/* Whether the process was tagged by the user */
|
|
|
|
bool tag;
|
|
|
|
|
|
|
|
/* Whether to display this process */
|
|
|
|
bool show;
|
|
|
|
|
|
|
|
/* Whether this process was shown last cycle */
|
|
|
|
bool wasShown;
|
2014-02-27 19:35:22 +00:00
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Whether to show children of this process in tree-mode */
|
|
|
|
bool showChildren;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal time counts for showing new and exited processes.
|
|
|
|
*/
|
2021-03-30 04:55:48 +00:00
|
|
|
uint64_t seenStampMs;
|
|
|
|
uint64_t tombStampMs;
|
2020-10-31 01:56:16 +00:00
|
|
|
|
2021-01-27 14:11:37 +00:00
|
|
|
/*
|
|
|
|
* Internal state for tree-mode.
|
|
|
|
*/
|
|
|
|
int indent;
|
2020-11-18 11:19:42 +00:00
|
|
|
unsigned int tree_left;
|
|
|
|
unsigned int tree_right;
|
|
|
|
unsigned int tree_depth;
|
|
|
|
unsigned int tree_index;
|
2021-04-10 09:10:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal state for merged Command display
|
|
|
|
*/
|
|
|
|
ProcessMergedCommand mergedCommand;
|
2006-03-04 18:16:49 +00:00
|
|
|
} Process;
|
|
|
|
|
2015-01-22 01:27:31 +00:00
|
|
|
typedef struct ProcessFieldData_ {
|
2021-01-27 14:11:37 +00:00
|
|
|
/* Name (displayed in setup menu) */
|
2015-01-22 01:27:31 +00:00
|
|
|
const char* name;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Title (display in main screen); must have same width as the printed values */
|
2015-01-22 01:27:31 +00:00
|
|
|
const char* title;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Description (displayed in setup menu) */
|
2015-01-22 01:27:31 +00:00
|
|
|
const char* description;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Scan flag to enable scan-method otherwise not run */
|
2020-10-09 13:13:06 +00:00
|
|
|
uint32_t flags;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Whether the values are process identifies; adjusts the width of title and values if true */
|
2020-12-15 18:44:52 +00:00
|
|
|
bool pidColumn;
|
2021-01-27 14:11:37 +00:00
|
|
|
|
|
|
|
/* Whether the column should be sorted in descending order by default */
|
2021-01-21 13:27:23 +00:00
|
|
|
bool defaultSortDesc;
|
2015-01-22 01:27:31 +00:00
|
|
|
} ProcessFieldData;
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2015-03-16 04:43:04 +00:00
|
|
|
// Implemented in platform-specific code:
|
2020-10-06 10:28:11 +00:00
|
|
|
void Process_writeField(const Process* this, RichString* str, ProcessField field);
|
2020-12-23 12:02:32 +00:00
|
|
|
int Process_compare(const void* v1, const void* v2);
|
2015-03-16 04:43:04 +00:00
|
|
|
void Process_delete(Object* cast);
|
2020-12-15 18:44:52 +00:00
|
|
|
extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD];
|
|
|
|
#define PROCESS_MAX_PID_DIGITS 19
|
|
|
|
extern int Process_pidDigits;
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2021-07-14 17:24:18 +00:00
|
|
|
typedef Process* (*Process_New)(const struct Settings_*);
|
2020-10-06 10:28:11 +00:00
|
|
|
typedef void (*Process_WriteField)(const Process*, RichString*, ProcessField);
|
2020-12-23 12:02:32 +00:00
|
|
|
typedef int (*Process_CompareByKey)(const Process*, const Process*, ProcessField);
|
2020-10-17 10:54:45 +00:00
|
|
|
typedef const char* (*Process_GetCommandStr)(const Process*);
|
2015-04-01 02:23:10 +00:00
|
|
|
|
|
|
|
typedef struct ProcessClass_ {
|
|
|
|
const ObjectClass super;
|
|
|
|
const Process_WriteField writeField;
|
2020-12-17 23:09:55 +00:00
|
|
|
const Process_CompareByKey compareByKey;
|
2020-10-17 10:54:45 +00:00
|
|
|
const Process_GetCommandStr getCommandStr;
|
2015-04-01 02:23:10 +00:00
|
|
|
} ProcessClass;
|
|
|
|
|
2020-12-17 23:09:55 +00:00
|
|
|
#define As_Process(this_) ((const ProcessClass*)((this_)->super.klass))
|
2015-03-17 02:01:48 +00:00
|
|
|
|
2021-04-24 10:06:49 +00:00
|
|
|
#define Process_getCommand(this_) (As_Process(this_)->getCommandStr ? As_Process(this_)->getCommandStr((const Process*)(this_)) : Process_getCommandStr((const Process*)(this_)))
|
2020-12-18 21:12:26 +00:00
|
|
|
#define Process_compareByKey(p1_, p2_, key_) (As_Process(p1_)->compareByKey ? (As_Process(p1_)->compareByKey(p1_, p2_, key_)) : Process_compareByKey_Base(p1_, p2_, key_))
|
2020-10-17 10:54:45 +00:00
|
|
|
|
2020-10-27 20:26:45 +00:00
|
|
|
static inline pid_t Process_getParentPid(const Process* this) {
|
|
|
|
return this->tgid == this->pid ? this->ppid : this->tgid;
|
|
|
|
}
|
2018-04-05 22:31:18 +00:00
|
|
|
|
2020-10-27 20:26:45 +00:00
|
|
|
static inline bool Process_isChildOf(const Process* this, pid_t pid) {
|
|
|
|
return pid == Process_getParentPid(this);
|
|
|
|
}
|
2017-09-14 20:10:39 +00:00
|
|
|
|
2021-07-14 17:24:18 +00:00
|
|
|
static inline bool Process_isKernelThread(const Process* this) {
|
2021-04-10 12:08:26 +00:00
|
|
|
return this->isKernelThread;
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:24:18 +00:00
|
|
|
static inline bool Process_isUserlandThread(const Process* this) {
|
2021-04-10 12:08:26 +00:00
|
|
|
return this->isUserlandThread;
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:24:18 +00:00
|
|
|
static inline bool Process_isThread(const Process* this) {
|
2021-04-10 12:08:26 +00:00
|
|
|
return Process_isUserlandThread(this) || Process_isKernelThread(this);
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:21:22 +00:00
|
|
|
#define CMDLINE_HIGHLIGHT_FLAG_SEPARATOR 0x00000001
|
|
|
|
#define CMDLINE_HIGHLIGHT_FLAG_BASENAME 0x00000002
|
|
|
|
#define CMDLINE_HIGHLIGHT_FLAG_COMM 0x00000004
|
|
|
|
#define CMDLINE_HIGHLIGHT_FLAG_DELETED 0x00000008
|
|
|
|
|
Process.{h,c}: Use integer types that are more portable
When building on a 32-bit system, the compiler warned that the
following line uses a constant whose value is the overflow result
of a compile-time computation:
Process.c (line 109): } else if (number < 10000 * ONE_M) {
Namely, this constant expression:
10000 * ONE_M
was intended to produce the following value:
10485760000
However, the result overflowed to produce:
1895825408
The reason for this overflow is as follows:
o The macros are expanded:
10000 * (ONE_K * ONE_K)
10000 * (1024L * 1024L)
o The untyped constant expression "10000" is typed:
10000U * (1024L * 1024L)
o The parenthesized expression is evaluated:
10000U * (1048576L)
o The left operand ("10000U") is converted:
10000L * (1048576L)
Unbound by integer sizes, that last multiplication
would produce the following value:
10485760000
However, on a 32-bit machine, where a long is 32 bits
(really 31 bits when talking about positive numbers),
the maximum value that can be computed is 2**31-1:
2147483647
Consequently, the computation overflows.
o The compiler produces a long int value that is the
the result of overflow (10485760000 % 2**31):
1895825408L
Actually, I think this overflow is implementation-defined,
so it's not even a portable description of what happens.
The solution is to use a long long int (or, even better,
an unsigned long long int) type for the constant expression;
the C standard mandates a sufficiently large maximum value
for such types.
Hence, the following change is made to the bad line:
- } else if (number < 10000 * ONE_M) {
+ } else if (number < 10000ULL * ONE_M) {
However, the whole line is now patently silly, because the
variable "number" is typed "unsigned long", and so it will
always be less than the constant expression (the compiler
will warn about this, too).
Hence, "number" must be typed "unsigned long long"; however,
this necessitates changing all of the string formats from
something like "%lu" to something like "%llu".
Et voila! This commit is born.
Then, for the sake of completeness, the declared types of the
constant-expression macros are updated:
o ONE_K is made unsigned (a "UL" instead of "L")
o ONE_T is computed by introducing "1ULL *"
o Similar changes are made for ONE_DECIMAL_{K,T}
Also, a non-portable overflow-conversion to a signed value
has been replaced with a portable comparison:
- if ((long long) number == -1LL) {
+ if (number == ULLONG_MAX) {
It might be worth reviewing the rest of the code for other
cases where overflows are not handled correctly; even at
runtime, it's often necessary to check for overflow unless
such behavior is expected (especially for signed integer
values, for which overflow has implementation-defined
behavior).
2020-09-29 14:04:22 +00:00
|
|
|
#define ONE_K 1024UL
|
2008-03-09 08:58:38 +00:00
|
|
|
#define ONE_M (ONE_K * ONE_K)
|
|
|
|
#define ONE_G (ONE_M * ONE_K)
|
Process.{h,c}: Use integer types that are more portable
When building on a 32-bit system, the compiler warned that the
following line uses a constant whose value is the overflow result
of a compile-time computation:
Process.c (line 109): } else if (number < 10000 * ONE_M) {
Namely, this constant expression:
10000 * ONE_M
was intended to produce the following value:
10485760000
However, the result overflowed to produce:
1895825408
The reason for this overflow is as follows:
o The macros are expanded:
10000 * (ONE_K * ONE_K)
10000 * (1024L * 1024L)
o The untyped constant expression "10000" is typed:
10000U * (1024L * 1024L)
o The parenthesized expression is evaluated:
10000U * (1048576L)
o The left operand ("10000U") is converted:
10000L * (1048576L)
Unbound by integer sizes, that last multiplication
would produce the following value:
10485760000
However, on a 32-bit machine, where a long is 32 bits
(really 31 bits when talking about positive numbers),
the maximum value that can be computed is 2**31-1:
2147483647
Consequently, the computation overflows.
o The compiler produces a long int value that is the
the result of overflow (10485760000 % 2**31):
1895825408L
Actually, I think this overflow is implementation-defined,
so it's not even a portable description of what happens.
The solution is to use a long long int (or, even better,
an unsigned long long int) type for the constant expression;
the C standard mandates a sufficiently large maximum value
for such types.
Hence, the following change is made to the bad line:
- } else if (number < 10000 * ONE_M) {
+ } else if (number < 10000ULL * ONE_M) {
However, the whole line is now patently silly, because the
variable "number" is typed "unsigned long", and so it will
always be less than the constant expression (the compiler
will warn about this, too).
Hence, "number" must be typed "unsigned long long"; however,
this necessitates changing all of the string formats from
something like "%lu" to something like "%llu".
Et voila! This commit is born.
Then, for the sake of completeness, the declared types of the
constant-expression macros are updated:
o ONE_K is made unsigned (a "UL" instead of "L")
o ONE_T is computed by introducing "1ULL *"
o Similar changes are made for ONE_DECIMAL_{K,T}
Also, a non-portable overflow-conversion to a signed value
has been replaced with a portable comparison:
- if ((long long) number == -1LL) {
+ if (number == ULLONG_MAX) {
It might be worth reviewing the rest of the code for other
cases where overflows are not handled correctly; even at
runtime, it's often necessary to check for overflow unless
such behavior is expected (especially for signed integer
values, for which overflow has implementation-defined
behavior).
2020-09-29 14:04:22 +00:00
|
|
|
#define ONE_T (1ULL * ONE_G * ONE_K)
|
2021-04-14 18:16:16 +00:00
|
|
|
#define ONE_P (1ULL * ONE_T * ONE_K)
|
2006-03-04 18:16:49 +00:00
|
|
|
|
Process.{h,c}: Use integer types that are more portable
When building on a 32-bit system, the compiler warned that the
following line uses a constant whose value is the overflow result
of a compile-time computation:
Process.c (line 109): } else if (number < 10000 * ONE_M) {
Namely, this constant expression:
10000 * ONE_M
was intended to produce the following value:
10485760000
However, the result overflowed to produce:
1895825408
The reason for this overflow is as follows:
o The macros are expanded:
10000 * (ONE_K * ONE_K)
10000 * (1024L * 1024L)
o The untyped constant expression "10000" is typed:
10000U * (1024L * 1024L)
o The parenthesized expression is evaluated:
10000U * (1048576L)
o The left operand ("10000U") is converted:
10000L * (1048576L)
Unbound by integer sizes, that last multiplication
would produce the following value:
10485760000
However, on a 32-bit machine, where a long is 32 bits
(really 31 bits when talking about positive numbers),
the maximum value that can be computed is 2**31-1:
2147483647
Consequently, the computation overflows.
o The compiler produces a long int value that is the
the result of overflow (10485760000 % 2**31):
1895825408L
Actually, I think this overflow is implementation-defined,
so it's not even a portable description of what happens.
The solution is to use a long long int (or, even better,
an unsigned long long int) type for the constant expression;
the C standard mandates a sufficiently large maximum value
for such types.
Hence, the following change is made to the bad line:
- } else if (number < 10000 * ONE_M) {
+ } else if (number < 10000ULL * ONE_M) {
However, the whole line is now patently silly, because the
variable "number" is typed "unsigned long", and so it will
always be less than the constant expression (the compiler
will warn about this, too).
Hence, "number" must be typed "unsigned long long"; however,
this necessitates changing all of the string formats from
something like "%lu" to something like "%llu".
Et voila! This commit is born.
Then, for the sake of completeness, the declared types of the
constant-expression macros are updated:
o ONE_K is made unsigned (a "UL" instead of "L")
o ONE_T is computed by introducing "1ULL *"
o Similar changes are made for ONE_DECIMAL_{K,T}
Also, a non-portable overflow-conversion to a signed value
has been replaced with a portable comparison:
- if ((long long) number == -1LL) {
+ if (number == ULLONG_MAX) {
It might be worth reviewing the rest of the code for other
cases where overflows are not handled correctly; even at
runtime, it's often necessary to check for overflow unless
such behavior is expected (especially for signed integer
values, for which overflow has implementation-defined
behavior).
2020-09-29 14:04:22 +00:00
|
|
|
#define ONE_DECIMAL_K 1000UL
|
2014-02-27 19:35:22 +00:00
|
|
|
#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
|
2014-04-10 01:32:54 +00:00
|
|
|
#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
|
Process.{h,c}: Use integer types that are more portable
When building on a 32-bit system, the compiler warned that the
following line uses a constant whose value is the overflow result
of a compile-time computation:
Process.c (line 109): } else if (number < 10000 * ONE_M) {
Namely, this constant expression:
10000 * ONE_M
was intended to produce the following value:
10485760000
However, the result overflowed to produce:
1895825408
The reason for this overflow is as follows:
o The macros are expanded:
10000 * (ONE_K * ONE_K)
10000 * (1024L * 1024L)
o The untyped constant expression "10000" is typed:
10000U * (1024L * 1024L)
o The parenthesized expression is evaluated:
10000U * (1048576L)
o The left operand ("10000U") is converted:
10000L * (1048576L)
Unbound by integer sizes, that last multiplication
would produce the following value:
10485760000
However, on a 32-bit machine, where a long is 32 bits
(really 31 bits when talking about positive numbers),
the maximum value that can be computed is 2**31-1:
2147483647
Consequently, the computation overflows.
o The compiler produces a long int value that is the
the result of overflow (10485760000 % 2**31):
1895825408L
Actually, I think this overflow is implementation-defined,
so it's not even a portable description of what happens.
The solution is to use a long long int (or, even better,
an unsigned long long int) type for the constant expression;
the C standard mandates a sufficiently large maximum value
for such types.
Hence, the following change is made to the bad line:
- } else if (number < 10000 * ONE_M) {
+ } else if (number < 10000ULL * ONE_M) {
However, the whole line is now patently silly, because the
variable "number" is typed "unsigned long", and so it will
always be less than the constant expression (the compiler
will warn about this, too).
Hence, "number" must be typed "unsigned long long"; however,
this necessitates changing all of the string formats from
something like "%lu" to something like "%llu".
Et voila! This commit is born.
Then, for the sake of completeness, the declared types of the
constant-expression macros are updated:
o ONE_K is made unsigned (a "UL" instead of "L")
o ONE_T is computed by introducing "1ULL *"
o Similar changes are made for ONE_DECIMAL_{K,T}
Also, a non-portable overflow-conversion to a signed value
has been replaced with a portable comparison:
- if ((long long) number == -1LL) {
+ if (number == ULLONG_MAX) {
It might be worth reviewing the rest of the code for other
cases where overflows are not handled correctly; even at
runtime, it's often necessary to check for overflow unless
such behavior is expected (especially for signed integer
values, for which overflow has implementation-defined
behavior).
2020-09-29 14:04:22 +00:00
|
|
|
#define ONE_DECIMAL_T (1ULL * ONE_DECIMAL_G * ONE_DECIMAL_K)
|
2021-04-14 18:16:16 +00:00
|
|
|
#define ONE_DECIMAL_P (1ULL * ONE_DECIMAL_T * ONE_DECIMAL_K)
|
2014-02-27 19:35:22 +00:00
|
|
|
|
2020-09-08 12:28:34 +00:00
|
|
|
void Process_setupColumnWidths(void);
|
2015-08-20 03:32:47 +00:00
|
|
|
|
2021-04-14 18:16:16 +00:00
|
|
|
/* Takes number in bytes (base 1024). Prints 6 columns. */
|
|
|
|
void Process_printBytes(RichString* str, unsigned long long number, bool coloring);
|
2015-03-16 04:43:04 +00:00
|
|
|
|
2021-04-14 18:16:16 +00:00
|
|
|
/* Takes number in kilo bytes (base 1024). Prints 6 columns. */
|
|
|
|
void Process_printKBytes(RichString* str, unsigned long long number, bool coloring);
|
2015-03-16 04:43:04 +00:00
|
|
|
|
2021-04-14 18:16:16 +00:00
|
|
|
/* Takes number as count (base 1000). Prints 12 columns. */
|
|
|
|
void Process_printCount(RichString* str, unsigned long long number, bool coloring);
|
2015-03-16 04:43:04 +00:00
|
|
|
|
2021-04-14 18:16:16 +00:00
|
|
|
/* Takes time in hundredths of a seconds. Prints 9 columns. */
|
|
|
|
void Process_printTime(RichString* str, unsigned long long totalHundredths, bool coloring);
|
|
|
|
|
|
|
|
/* Takes rate in bare unit (base 1024) per second. Prints 12 columns. */
|
|
|
|
void Process_printRate(RichString* str, double rate, bool coloring);
|
2020-10-13 12:26:40 +00:00
|
|
|
|
2021-04-14 18:16:16 +00:00
|
|
|
void Process_fillStarttimeBuffer(Process* this);
|
2015-03-16 04:43:04 +00:00
|
|
|
|
2021-01-10 14:57:46 +00:00
|
|
|
void Process_printLeftAlignedField(RichString* str, int attr, const char* content, unsigned int width);
|
|
|
|
|
2020-10-06 10:28:11 +00:00
|
|
|
void Process_display(const Object* cast, RichString* out);
|
2015-01-22 01:27:31 +00:00
|
|
|
|
2020-09-02 07:38:44 +00:00
|
|
|
void Process_done(Process* this);
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-10-05 11:19:50 +00:00
|
|
|
extern const ProcessClass Process_class;
|
2012-12-05 15:12:20 +00:00
|
|
|
|
2020-10-21 19:26:05 +00:00
|
|
|
void Process_init(Process* this, const struct Settings_* settings);
|
2008-03-09 08:58:38 +00:00
|
|
|
|
2020-09-02 07:38:44 +00:00
|
|
|
void Process_toggleTag(Process* this);
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-10-31 01:56:16 +00:00
|
|
|
bool Process_isNew(const Process* this);
|
|
|
|
|
|
|
|
bool Process_isTomb(const Process* this);
|
|
|
|
|
2020-09-02 07:38:44 +00:00
|
|
|
bool Process_setPriority(Process* this, int priority);
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-09-02 07:38:44 +00:00
|
|
|
bool Process_changePriorityBy(Process* this, Arg delta);
|
2012-10-04 23:59:45 +00:00
|
|
|
|
2020-09-02 07:38:44 +00:00
|
|
|
bool Process_sendSignal(Process* this, Arg sgn);
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2020-12-23 12:02:32 +00:00
|
|
|
int Process_pidCompare(const void* v1, const void* v2);
|
2006-07-11 06:13:32 +00:00
|
|
|
|
2020-12-23 12:02:32 +00:00
|
|
|
int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key);
|
2020-12-18 21:12:26 +00:00
|
|
|
|
2021-04-24 10:06:49 +00:00
|
|
|
// Avoid direct calls, use Process_getCommand instead
|
2021-07-14 17:24:18 +00:00
|
|
|
const char* Process_getCommandStr(const Process* this);
|
2021-04-24 10:06:49 +00:00
|
|
|
|
2021-05-18 20:29:25 +00:00
|
|
|
void Process_updateComm(Process* this, const char* comm);
|
|
|
|
void Process_updateCmdline(Process* this, const char* cmdline, int basenameStart, int basenameEnd);
|
|
|
|
void Process_updateExe(Process* this, const char* exe);
|
|
|
|
|
2021-04-10 11:31:39 +00:00
|
|
|
/* This function constructs the string that is displayed by
|
|
|
|
* Process_writeCommand and also returned by Process_getCommandStr */
|
2021-07-14 17:24:18 +00:00
|
|
|
void Process_makeCommandStr(Process* this);
|
2021-04-10 11:31:39 +00:00
|
|
|
|
2021-07-14 17:24:18 +00:00
|
|
|
void Process_writeCommand(const Process* this, int attr, int baseAttr, RichString* str);
|
2021-04-10 11:31:39 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#endif
|