- Timestamp:
- Sep 15, 2020 7:46:48 PM (4 years ago)
- Location:
- trunk/src/kash
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/shfile.c
r3458 r3465 156 156 #ifdef SHFILE_IN_USE 157 157 158 # ifdef DEBUG 159 # if K_OS == K_OS_WINDOWS 160 static KU64 shfile_nano_ts(void) 161 { 162 static KBOOL volatile s_has_factor = K_FALSE; 163 static double volatile s_factor; 164 double factor; 165 LARGE_INTEGER now; 166 if (s_has_factor) 167 factor = s_factor; 168 else 169 { 170 QueryPerformanceFrequency(&now); 171 s_factor = factor = (double)1000000000.0 / now.QuadPart; 172 s_has_factor = K_TRUE; 173 } 174 QueryPerformanceCounter(&now); 175 return (KU64)(now.QuadPart * factor); 176 } 177 # endif /* K_OS_WINDOWS */ 178 # endif /* DEBUG */ 179 158 180 /** 159 181 * Close the specified native handle. 160 182 * 161 183 * @param native The native file handle. 162 * @param f lags The flags in case they might come in handy later.163 */ 164 static void shfile_native_close(intptr_t native, unsigned flags)184 * @param file The file table entry if available. 185 */ 186 static void shfile_native_close(intptr_t native, shfile *file) 165 187 { 166 188 # if K_OS == K_OS_WINDOWS 189 # ifdef DEBUG 190 KU64 ns = shfile_nano_ts(); 191 BOOL fRc2 = FlushFileBuffers((HANDLE)native); 192 KU64 ns2 = shfile_nano_ts(); 167 193 BOOL fRc = CloseHandle((HANDLE)native); 168 assert(fRc); (void)fRc; 194 assert(fRc); K_NOREF(fRc); 195 ns2 -= ns; 196 ns = shfile_nano_ts() - ns; 197 if (ns > 1000000) 198 TRACE2((NULL, "shfile_native_close: %u ns (%u) %p oflags=%#x %s\n", 199 ns, ns2, native, file ? file->oflags : 0, file ? file->dbgname : NULL)); 200 # else 201 BOOL fRc = CloseHandle((HANDLE)native); 202 assert(fRc); K_NOREF(fRc); 203 # endif 169 204 # else 170 205 int s = errno; … … 172 207 errno = s; 173 208 # endif 174 (void)flags;209 K_NOREF(file); 175 210 } 176 211 … … 202 237 new_tab[i].shflags = 0; 203 238 new_tab[i].native = -1; 239 # ifdef DEBUG 240 new_tab[i].dbgname = NULL; 241 # endif 204 242 } 205 243 … … 228 266 * @param fdMin The minimum file descriptor number, pass -1 if any is ok. 229 267 * @param who Who we're doing this for (for logging purposes). 230 */ 231 static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned oflags, unsigned shflags, int fdMin, const char *who) 268 * @param dbgname The filename, if applicable/available. 269 */ 270 static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned oflags, unsigned shflags, int fdMin, 271 const char *who, const char *dbgname) 232 272 { 233 273 shmtxtmp tmp; … … 240 280 if (fdMin >= SHFILE_MAX) 241 281 { 242 TRACE2((NULL, "shfile_insert: fdMin=%d is out of bounds; native=%p \n", fdMin, native));243 shfile_native_close(native, oflags);282 TRACE2((NULL, "shfile_insert: fdMin=%d is out of bounds; native=%p %s\n", fdMin, native, dbgname)); 283 shfile_native_close(native, NULL); 244 284 errno = EMFILE; 245 285 return -1; … … 249 289 { 250 290 int e = errno; 251 TRACE2((NULL, "shfile_insert: F_SETFD failed %d; native=%p \n", e, native));291 TRACE2((NULL, "shfile_insert: F_SETFD failed %d; native=%p %s\n", e, native, dbgname)); 252 292 close((int)native); 253 293 errno = e; … … 280 320 pfdtab->tab[fd].shflags = shflags; 281 321 pfdtab->tab[fd].native = native; 282 TRACE2((NULL, "shfile_insert: #%d: native=%p oflags=%#x shflags=%#x\n", fd, native, oflags, shflags)); 322 #ifdef DEBUG 323 pfdtab->tab[fd].dbgname = dbgname ? sh_strdup(NULL, dbgname) : NULL; 324 #endif 325 TRACE2((NULL, "shfile_insert: #%d: native=%p oflags=%#x shflags=%#x %s\n", fd, native, oflags, shflags, dbgname)); 283 326 } 284 327 else 285 shfile_native_close(native, oflags);328 shfile_native_close(native, NULL); 286 329 287 330 shmtx_leave(&pfdtab->mtx, &tmp); … … 308 351 * @param fdMin The minimum file descriptor number, pass -1 if any is ok. 309 352 * @param who Who we're doing this for (for logging purposes). 310 */ 311 static int shfile_copy_insert_and_close(shfdtab *pfdtab, int *pnative, unsigned oflags, unsigned shflags, int fdMin, const char *who) 353 * @param dbgname The filename, if applicable/available. 354 */ 355 static int shfile_copy_insert_and_close(shfdtab *pfdtab, int *pnative, unsigned oflags, unsigned shflags, int fdMin, 356 const char *who, const char *dbgname) 312 357 { 313 358 int fd = -1; … … 319 364 320 365 if (native_copy != -1) 321 fd = shfile_insert(pfdtab, native_copy, oflags, shflags, fdMin, who );366 fd = shfile_insert(pfdtab, native_copy, oflags, shflags, fdMin, who, dbgname); 322 367 return fd; 323 368 } … … 743 788 fFlags2 = 0; 744 789 745 fd2 = shfile_insert(pfdtab, (intptr_t)h, fFlags, fFlags2, i, "shtab_init" );790 fd2 = shfile_insert(pfdtab, (intptr_t)h, fFlags, fFlags2, i, "shtab_init", NULL); 746 791 assert(fd2 == i); (void)fd2; 747 792 if (fd2 != i) … … 769 814 else 770 815 fFlags2 = SHFILE_FLAGS_FILE; 771 fd2 = shfile_insert(pfdtab, (intptr_t)hFile, fFlags, fFlags2, i, "shtab_init" );816 fd2 = shfile_insert(pfdtab, (intptr_t)hFile, fFlags, fFlags2, i, "shtab_init", NULL); 772 817 assert(fd2 == i); (void)fd2; 773 818 if (fd2 != i) … … 810 855 if (native == -1) 811 856 native = fd; 812 fd2 = shfile_insert(pfdtab, native, oflags, fFlags2, fd, "shtab_init" );857 fd2 = shfile_insert(pfdtab, native, oflags, fFlags2, fd, "shtab_init", NULL); 813 858 assert(fd2 == fd); (void)fd2; 814 859 if (fd2 != fd) … … 859 904 # endif 860 905 *dst = *src; 906 # ifdef DEBUG 907 if (src->dbgname) 908 dst->dbgname = sh_strdup(NULL, src->dbgname); 909 # endif 861 910 # if K_OS == K_OS_WINDOWS 862 911 if (DuplicateHandle(GetCurrentProcess(), … … 1193 1242 && !(file->shflags & SHFILE_FLAGS_TRACE)) 1194 1243 { 1195 shfile_native_close(file->native, file ->oflags);1244 shfile_native_close(file->native, file); 1196 1245 1197 1246 file->fd = -1; … … 1199 1248 file->shflags = 0; 1200 1249 file->native = -1; 1250 # ifdef DEBUG 1251 sh_free(NULL, file->dbgname); 1252 file->dbgname = NULL; 1253 # endif 1201 1254 } 1202 1255 } … … 1315 1368 if (!fd) 1316 1369 { 1370 # ifdef DEBUG 1371 KU64 ns = shfile_nano_ts(); 1372 # endif 1317 1373 SetLastError(0); 1318 1374 hFile = CreateFileA(absname, … … 1323 1379 dwFlagsAndAttributes, 1324 1380 NULL /* hTemplateFile */); 1381 # ifdef DEBUG 1382 ns = shfile_nano_ts() - ns; 1383 if (ns > 1000000) 1384 TRACE2((NULL, "shfile_open: %s ns hFile=%p (%d) %s\n", ns, hFile, GetLastError(), absname)); 1385 # endif 1325 1386 if (hFile != INVALID_HANDLE_VALUE) 1326 fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, 0, -1, "shfile_open" );1387 fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, 0, -1, "shfile_open", absname); 1327 1388 else 1328 1389 fd = shfile_dos2errno(GetLastError()); … … 1335 1396 fd = open(absname, flags, mode); 1336 1397 if (fd != -1) 1337 fd = shfile_copy_insert_and_close(pfdtab, &fd, flags, 0, -1, "shfile_open" );1398 fd = shfile_copy_insert_and_close(pfdtab, &fd, flags, 0, -1, "shfile_open", absname); 1338 1399 } 1339 1400 … … 1364 1425 if (CreatePipe(&hRead, &hWrite, &SecurityAttributes, 4096)) 1365 1426 { 1366 fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe" );1427 fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe", "pipe-rd"); 1367 1428 if (fds[0] != -1) 1368 1429 { 1369 fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe" );1430 fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe", "pipe-wr"); 1370 1431 if (fds[1] != -1) 1371 1432 rc = 0; … … 1378 1439 if (!pipe(native_fds)) 1379 1440 { 1380 fds[0] = shfile_copy_insert_and_close(pfdtab, &native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe" );1441 fds[0] = shfile_copy_insert_and_close(pfdtab, &native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe", "pipe-rd"); 1381 1442 if (fds[0] != -1) 1382 1443 { 1383 fds[1] = shfile_copy_insert_and_close(pfdtab, &native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe" );1444 fds[1] = shfile_copy_insert_and_close(pfdtab, &native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe", "pipe-wr"); 1384 1445 if (fds[1] != -1) 1385 1446 rc = 0; … … 1459 1520 { 1460 1521 if (pfdtab->tab[fdto].fd != -1) 1461 shfile_native_close(pfdtab->tab[fdto].native, pfdtab->tab[fdto].oflags);1522 shfile_native_close(pfdtab->tab[fdto].native, &pfdtab->tab[fdto]); 1462 1523 1463 1524 /* setup the target. */ … … 1466 1527 pfdtab->tab[fdto].shflags = file->shflags; 1467 1528 pfdtab->tab[fdto].native = file->native; 1529 # ifdef DEBUG 1530 pfdtab->tab[fdto].dbgname = file->dbgname; 1531 # endif 1468 1532 1469 1533 /* close the source. */ … … 1472 1536 file->shflags = 0; 1473 1537 file->native = -1; 1538 # ifdef DEBUG 1539 file->dbgname = NULL; 1540 # endif 1474 1541 1475 1542 rc = fdto; … … 1529 1596 pfdtab->tab[fdto].shflags = file->shflags; 1530 1597 pfdtab->tab[fdto].native = file->native; 1598 # ifdef DEBUG 1599 pfdtab->tab[fdto].dbgname = file->dbgname; 1600 # endif 1531 1601 1532 1602 /* close the source. */ … … 1535 1605 file->shflags = 0; 1536 1606 file->native = -1; 1607 # ifdef DEBUG 1608 file->dbgname = NULL; 1609 # endif 1537 1610 } 1538 1611 else … … 1567 1640 if (file) 1568 1641 { 1569 shfile_native_close(file->native, file ->oflags);1642 shfile_native_close(file->native, file); 1570 1643 1571 1644 file->fd = -1; … … 1573 1646 file->shflags = 0; 1574 1647 file->native = -1; 1648 # ifdef DEBUG 1649 sh_free(NULL, file->dbgname); 1650 file->dbgname = NULL; 1651 # endif 1575 1652 1576 1653 shfile_put(pfdtab, file, &tmp); … … 1757 1834 FALSE /* bInheritHandle */, 1758 1835 DUPLICATE_SAME_ACCESS)) 1759 rc = shfile_insert(pfdtab, (intptr_t)hNew, file->oflags, file->shflags, arg, "shfile_fcntl"); 1836 rc = shfile_insert(pfdtab, (intptr_t)hNew, file->oflags, file->shflags, arg, 1837 "shfile_fcntl", SHFILE_DBGNAME(file->dbgname)); 1760 1838 else 1761 1839 rc = shfile_dos2errno(GetLastError()); … … 1763 1841 int nativeNew = fcntl(file->native, F_DUPFD, SHFILE_UNIX_MIN_FD); 1764 1842 if (nativeNew != -1) 1765 rc = shfile_insert(pfdtab, nativeNew, file->oflags, file->shflags, arg, "shfile_fcntl"); 1843 rc = shfile_insert(pfdtab, nativeNew, file->oflags, file->shflags, arg, 1844 "shfile_fcntl", SHFILE_DBGNAME(file->dbgname)); 1766 1845 else 1767 1846 rc = -1; -
trunk/src/kash/shfile.h
r3458 r3465 106 106 unsigned shflags; /**< The shell file descriptor flags. */ 107 107 intptr_t native; /**< The native file descriptor number. */ 108 #ifdef DEBUG 109 char *dbgname; /**< The name of the file, if applicable, debug builds only. */ 110 # define SHFILE_DBGNAME(a) a 111 # else 112 # define SHFILE_DBGNAME(a) NULL 113 #endif 108 114 } shfile; 109 115
Note:
See TracChangeset
for help on using the changeset viewer.