Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 1 | /* |
WHR | b347b25 | 2022-05-13 22:17:58 +0800 | [diff] [blame] | 2 | htop - freebsd/FreeBSDProcess.c |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 3 | (C) 2015 Hisham H. Muhammad |
| 4 | Released under the GNU GPL, see the COPYING file |
| 5 | in the source distribution for its full text. |
| 6 | */ |
| 7 | |
| 8 | #include "Process.h" |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 9 | #include "FreeBSDProcess.h" |
WHR | c75954f | 2022-05-27 15:21:28 +0800 | [diff] [blame] | 10 | #include "bsd/BSDProcess.h" |
| 11 | #include "ProcessList.h" |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 12 | #include "Platform.h" |
| 13 | #include "CRT.h" |
WHR | bf91db8 | 2022-05-31 11:38:24 +0800 | [diff] [blame] | 14 | #include <sys/param.h> |
WHR | b789618 | 2022-05-29 19:49:41 +0800 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <sys/sysctl.h> |
| 17 | #include <sys/user.h> |
Martin "eto" Misuth | 8c00fa4 | 2015-09-30 22:04:26 +0200 | [diff] [blame] | 18 | #include <string.h> |
WHR | b789618 | 2022-05-29 19:49:41 +0800 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <errno.h> |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 21 | |
| 22 | /*{ |
WHR | c75954f | 2022-05-27 15:21:28 +0800 | [diff] [blame] | 23 | #include "Settings.h" |
| 24 | #include <stdbool.h> |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 25 | |
WHR | b78bdf7 | 2022-06-01 15:30:46 +0800 | [diff] [blame] | 26 | #define PROCESS_FLAG_JAIL 0x100 |
| 27 | #define PROCESS_FLAG_EMULATION 0x200 |
| 28 | |
WHR | 75303f8 | 2022-05-14 16:42:56 +0800 | [diff] [blame] | 29 | typedef enum { |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 30 | // Add platform-specific fields here, with ids >= 100 |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 31 | HTOP_JID_FIELD = 100, |
| 32 | HTOP_JAIL_FIELD, |
| 33 | HTOP_EMULATION_FIELD, |
| 34 | HTOP_LAST_PROCESSFIELD |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 35 | } FreeBSDProcessField; |
Martin "eto" Misuth | 8c00fa4 | 2015-09-30 22:04:26 +0200 | [diff] [blame] | 36 | |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 37 | typedef struct FreeBSDProcess_ { |
| 38 | Process super; |
WHR | a6edd8c | 2022-05-03 22:20:39 +0800 | [diff] [blame] | 39 | bool kernel; |
Martin "eto" Misuth | 8c00fa4 | 2015-09-30 22:04:26 +0200 | [diff] [blame] | 40 | int jid; |
| 41 | char* jname; |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 42 | char *emulation; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 43 | } FreeBSDProcess; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 44 | }*/ |
| 45 | |
Hisham Muhammad | b291fba | 2015-04-09 15:40:27 -0300 | [diff] [blame] | 46 | ProcessClass FreeBSDProcess_class = { |
| 47 | .super = { |
| 48 | .extends = Class(Process), |
| 49 | .display = Process_display, |
| 50 | .delete = Process_delete, |
| 51 | .compare = FreeBSDProcess_compare |
| 52 | }, |
WHR | 923e459 | 2022-05-14 10:43:08 +0800 | [diff] [blame] | 53 | .writeField = FreeBSDProcess_writeField, |
Hisham Muhammad | b291fba | 2015-04-09 15:40:27 -0300 | [diff] [blame] | 54 | }; |
| 55 | |
WHR | 0fcf47e | 2022-06-14 16:52:14 +0800 | [diff] [blame] | 56 | FieldData Process_fields[] = { |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 57 | [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 58 | [HTOP_PID_FIELD] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, |
| 59 | [HTOP_NAME_FIELD] = { .name = "NAME", .title = "NAME ", .description = "Process (executable) name", .flags = 0, }, |
| 60 | [HTOP_COMM_FIELD] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, }, |
WHR | 04cc76a | 2022-05-29 21:41:00 +0800 | [diff] [blame] | 61 | [HTOP_STATE_FIELD] = { .name = "STATE", .title = "S ", .description = "Process state (R running, I idle, S sleeping, D uninterruptible sleeping, T stoppd, Z zombie, W idle kernel process, L waiting lock)", .flags = 0 }, |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 62 | [HTOP_PPID_FIELD] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, }, |
| 63 | [HTOP_PGRP_FIELD] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, |
| 64 | [HTOP_SESSION_FIELD] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, |
WHR | c75954f | 2022-05-27 15:21:28 +0800 | [diff] [blame] | 65 | [HTOP_TTY_FIELD] = { .name = "TTY", .title = "TTY ", .description = "Controlling terminal", .flags = 0, }, |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 66 | [HTOP_TPGID_FIELD] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, }, |
| 67 | [HTOP_MINFLT_FIELD] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, |
| 68 | [HTOP_MAJFLT_FIELD] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, |
| 69 | [HTOP_PRIORITY_FIELD] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, }, |
| 70 | [HTOP_NICE_FIELD] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, }, |
| 71 | [HTOP_STARTTIME_FIELD] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, }, |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 72 | |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 73 | [HTOP_PROCESSOR_FIELD] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, }, |
| 74 | [HTOP_M_SIZE_FIELD] = { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, }, |
| 75 | [HTOP_M_RESIDENT_FIELD] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, }, |
| 76 | [HTOP_REAL_UID_FIELD] = { .name = "REAL_UID", .title = " RUID ", .description = "Real user ID", .flags = 0, }, |
| 77 | [HTOP_EFFECTIVE_UID_FIELD] = { .name = "EFFECTIVE_UID", .title = " EUID ", .description = "Effective user ID", .flags = 0, }, |
| 78 | [HTOP_PERCENT_CPU_FIELD] = { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, }, |
| 79 | [HTOP_PERCENT_MEM_FIELD] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, }, |
| 80 | [HTOP_REAL_USER_FIELD] = { .name = "REAL_USER", .title = "REAL_USER ", .description = "Real user (or numeric user ID if name cannot be determined)", .flags = 0, }, |
| 81 | [HTOP_EFFECTIVE_USER_FIELD] = { .name = "EFFECTIVE_USER", .title = "EFFE_USER ", .description = "Effective user (or numeric user ID if name cannot be determined)", .flags = 0, }, |
| 82 | [HTOP_TIME_FIELD] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, }, |
| 83 | [HTOP_NLWP_FIELD] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, }, |
| 84 | [HTOP_TGID_FIELD] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, }, |
| 85 | [HTOP_JID_FIELD] = { .name = "JID", .title = " JID ", .description = "Jail prison ID", .flags = 0, }, |
WHR | b78bdf7 | 2022-06-01 15:30:46 +0800 | [diff] [blame] | 86 | [HTOP_JAIL_FIELD] = { .name = "JAIL", .title = "JAIL ", .description = "Jail prison name", .flags = PROCESS_FLAG_JAIL }, |
| 87 | [HTOP_EMULATION_FIELD] = { .name = "EMULATION", .title = "EMULATION ", .description = "Binary format emulation type", .flags = PROCESS_FLAG_EMULATION }, |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 88 | [HTOP_LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 89 | }; |
| 90 | |
Hisham Muhammad | 9428010 | 2015-08-20 00:32:47 -0300 | [diff] [blame] | 91 | ProcessPidColumn Process_pidColumns[] = { |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 92 | { .id = HTOP_JID_FIELD, .label = "JID" }, |
| 93 | { .id = HTOP_PID_FIELD, .label = "PID" }, |
| 94 | { .id = HTOP_PPID_FIELD, .label = "PPID" }, |
| 95 | { .id = HTOP_TPGID_FIELD, .label = "TPGID" }, |
| 96 | { .id = HTOP_TGID_FIELD, .label = "TGID" }, |
| 97 | { .id = HTOP_PGRP_FIELD, .label = "PGRP" }, |
| 98 | { .id = HTOP_SESSION_FIELD, .label = "SID" }, |
Hisham Muhammad | 9428010 | 2015-08-20 00:32:47 -0300 | [diff] [blame] | 99 | { .id = 0, .label = NULL }, |
| 100 | }; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 101 | |
| 102 | FreeBSDProcess* FreeBSDProcess_new(Settings* settings) { |
Hisham | b54d2dd | 2016-02-02 15:53:02 +0100 | [diff] [blame] | 103 | FreeBSDProcess* this = xCalloc(1, sizeof(FreeBSDProcess)); |
Hisham Muhammad | b291fba | 2015-04-09 15:40:27 -0300 | [diff] [blame] | 104 | Object_setClass(this, Class(FreeBSDProcess)); |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 105 | Process_init(&this->super, settings); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | void Process_delete(Object* cast) { |
| 110 | FreeBSDProcess* this = (FreeBSDProcess*) cast; |
| 111 | Process_done((Process*)cast); |
Martin "eto" Misuth | 8c00fa4 | 2015-09-30 22:04:26 +0200 | [diff] [blame] | 112 | free(this->jname); |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 113 | free(this->emulation); |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 114 | free(this); |
| 115 | } |
| 116 | |
WHR | e6316e0 | 2022-06-09 22:17:22 +0800 | [diff] [blame] | 117 | void FreeBSDProcess_writeField(const Process *super, RichString* str, ProcessField field) { |
| 118 | const FreeBSDProcess *this = (const FreeBSDProcess *)super; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 119 | char buffer[256]; buffer[255] = '\0'; |
WHR | a754650 | 2022-05-14 16:02:04 +0800 | [diff] [blame] | 120 | int attr = CRT_colors[HTOP_DEFAULT_COLOR]; |
WHR | 42dac52 | 2022-05-13 21:00:08 +0800 | [diff] [blame] | 121 | int n = sizeof buffer; |
Hung-Yi Chen | cc4267c | 2016-02-13 02:09:50 +0800 | [diff] [blame] | 122 | switch ((int) field) { |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 123 | // add FreeBSD-specific fields here |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 124 | case HTOP_JID_FIELD: |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 125 | xSnprintf(buffer, n, Process_pidFormat, this->jid); |
| 126 | break; |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 127 | case HTOP_JAIL_FIELD: |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 128 | xSnprintf(buffer, n, "%-11s ", this->jname); |
| 129 | if (buffer[12]) { |
| 130 | buffer[11] = ' '; |
| 131 | buffer[12] = '\0'; |
| 132 | } |
| 133 | break; |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 134 | case HTOP_EMULATION_FIELD: |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 135 | xSnprintf(buffer, n, "%-16s ", this->emulation); |
| 136 | if(buffer[17]) { |
| 137 | buffer[16] = ' '; |
| 138 | buffer[17] = 0; |
| 139 | } |
| 140 | break; |
| 141 | default: |
WHR | c75954f | 2022-05-27 15:21:28 +0800 | [diff] [blame] | 142 | BSDProcess_writeField(super, str, field); |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 143 | return; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 144 | } |
| 145 | RichString_append(str, attr, buffer); |
| 146 | } |
| 147 | |
Hisham Muhammad | b291fba | 2015-04-09 15:40:27 -0300 | [diff] [blame] | 148 | long FreeBSDProcess_compare(const void* v1, const void* v2) { |
WHR | 923e459 | 2022-05-14 10:43:08 +0800 | [diff] [blame] | 149 | const FreeBSDProcess *p1, *p2; |
| 150 | const Settings *settings = ((const Process *)v1)->settings; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 151 | if (settings->direction == 1) { |
WHR | 923e459 | 2022-05-14 10:43:08 +0800 | [diff] [blame] | 152 | p1 = v1; |
| 153 | p2 = v2; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 154 | } else { |
WHR | 923e459 | 2022-05-14 10:43:08 +0800 | [diff] [blame] | 155 | p2 = v1; |
| 156 | p1 = v2; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 157 | } |
Hung-Yi Chen | cc4267c | 2016-02-13 02:09:50 +0800 | [diff] [blame] | 158 | switch ((int) settings->sortKey) { |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 159 | // add FreeBSD-specific fields here |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 160 | case HTOP_JID_FIELD: |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 161 | return (p1->jid - p2->jid); |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 162 | case HTOP_JAIL_FIELD: |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 163 | if(!p1->jname && !p2->jname) return p1->jid - p2->jid; |
| 164 | return settings->sort_strcmp(p1->jname ? p1->jname : "", p2->jname ? p2->jname : ""); |
WHR | 69b3046 | 2022-05-27 11:42:26 +0800 | [diff] [blame] | 165 | case HTOP_EMULATION_FIELD: |
WHR | b8eb088 | 2022-05-26 18:42:24 +0800 | [diff] [blame] | 166 | return settings->sort_strcmp(p1->emulation, p2->emulation); |
| 167 | default: |
| 168 | return Process_compare(v1, v2); |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
WHR | 49675a0 | 2022-05-13 14:14:52 +0800 | [diff] [blame] | 172 | bool Process_isKernelProcess(const Process *this) { |
| 173 | return ((const FreeBSDProcess *)this)->kernel; |
WHR | a6edd8c | 2022-05-03 22:20:39 +0800 | [diff] [blame] | 174 | } |
| 175 | |
WHR | 49675a0 | 2022-05-13 14:14:52 +0800 | [diff] [blame] | 176 | bool Process_isExtraThreadProcess(const Process* this) { |
WHR | ac01eec | 2022-04-28 13:57:10 +0800 | [diff] [blame] | 177 | return false; |
Hisham Muhammad | adbfe3c | 2015-03-16 03:13:42 -0300 | [diff] [blame] | 178 | } |
WHR | b789618 | 2022-05-29 19:49:41 +0800 | [diff] [blame] | 179 | |
| 180 | char **Process_getKernelStackTrace(const Process *this) { |
| 181 | #ifdef KERN_PROC_KSTACK |
| 182 | char **v = xMalloc(2 * sizeof(char *)); |
| 183 | unsigned int i = 0; |
| 184 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_KSTACK, this->pid }; |
| 185 | size_t len; |
| 186 | if(sysctl(mib, 4, NULL, &len, NULL, 0) < 0) { |
| 187 | ret_err_msg: |
| 188 | v[0] = strdup(strerror(errno)); |
| 189 | if(v[0]) { |
| 190 | v[1] = NULL; |
| 191 | } else { |
| 192 | free(v); |
| 193 | v = NULL; |
| 194 | } |
| 195 | return v; |
| 196 | } |
| 197 | if(len < sizeof(struct kinfo_kstack)) { |
| 198 | v[0] = xStrdup("No stack available"); |
| 199 | v[1] = NULL; |
| 200 | return v; |
| 201 | } |
| 202 | struct kinfo_kstack *kiks_buffer = malloc(len); |
| 203 | if(!kiks_buffer) goto ret_err_msg; |
| 204 | if(sysctl(mib, 4, kiks_buffer, &len, NULL, 0) < 0) { |
| 205 | free(kiks_buffer); |
| 206 | goto ret_err_msg; |
| 207 | } |
| 208 | len /= sizeof(struct kinfo_kstack); |
| 209 | for(size_t j = 0; j < len; j++) { |
| 210 | struct kinfo_kstack *kiks = kiks_buffer + j; |
| 211 | v = xRealloc(v, (i + 3) * sizeof(char *)); |
| 212 | v[i] = xMalloc(32); |
| 213 | snprintf(v[i++], 32, "Thread %d:", kiks->kkst_tid); |
| 214 | switch(kiks->kkst_state) { |
| 215 | char *p, *end_p; |
| 216 | case KKST_STATE_STACKOK: |
| 217 | p = kiks->kkst_trace; |
| 218 | do { |
| 219 | end_p = strchr(p, '\n'); |
| 220 | int prefix_len = *p == '#' ? 0 : 3; |
| 221 | size_t frame_len = prefix_len + |
| 222 | (end_p ? (size_t)(end_p - p) : strlen(p)); |
| 223 | v[i] = xMalloc(frame_len + 1); |
| 224 | if(*p != '#') memcpy(v[i], "#? ", 3); |
| 225 | memcpy(v[i] + prefix_len, p, frame_len - prefix_len); |
| 226 | v[i][frame_len] = 0; |
| 227 | v = xRealloc(v, (++i + 1) * sizeof(char *)); |
| 228 | } while(end_p && *(p = end_p + 1)); |
| 229 | break; |
| 230 | case KKST_STATE_SWAPPED: |
| 231 | v[i++] = xStrdup("swapped"); |
| 232 | break; |
| 233 | case KKST_STATE_RUNNING: |
| 234 | v[i++] = xStrdup("running"); |
| 235 | break; |
| 236 | default: |
| 237 | v[i++] = xStrdup("unknown state"); |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | free(kiks_buffer); |
| 242 | v[i] = NULL; |
| 243 | return v; |
| 244 | #else |
| 245 | return NULL; |
| 246 | #endif |
| 247 | } |