// // Purpose: vprintf with where // void SB_Trace::trace_where_vprintf(void *pp_ra, const char *pp_where, const char *pp_format, va_list pv_ap) { int lv_err; fpos64_t lv_pos; sigset_t lv_set_old; if (ip_trace_file == NULL) { // don't bother, if not initialized if (iv_trace_assert_no_trace) SB_util_assert_pne(ip_trace_file, NULL); } else { if (iv_trace_sig_hdlr) gv_trace_sig.lock(&lv_set_old); if (iv_trace_lock) gv_trace_mutex.lock(); if (iv_trace_max_size && (iv_trace_max_size != INT_MAX)) { lv_err = fgetpos64(ip_trace_file, &lv_pos); if (!lv_err && (lv_pos.__pos > iv_trace_max_size)) abort(); // can't use SB_util_abort } trace_print_time_id(); trace_print_ra(pp_ra, pp_where); trace_print_format(pp_format, pv_ap); if (iv_trace_lock) gv_trace_mutex.unlock(); if (iv_trace_sig_hdlr) gv_trace_sig.unlock(&lv_set_old); } }
int64_t CoreIOReader::Position() { CoreAssert(this != NULL); CoreAssert(IsOpen()); #ifdef HAS_FPOS64 fpos64_t lastpos; #if defined(TARGET_COMPILER_VC) || defined(TARGET_COMPILER_BORLAND) || \ (defined(TARGET_COMPILER_ICC) && defined(TARGET_OS_WINDOWS)) lastpos = _ftelli64(m_fileInputPointer); return lastpos; #elif defined (TARGET_OS_MACOSX) || defined (TARGET_OS_NETBSD) || \ defined (TARGET_OS_FREEBSD) || defined (TARGET_OS_OPENBSD) || \ defined (TARGET_COMPILER_MINGW) fgetpos(m_fileInputPointer, &lastpos); return lastpos; #else fgetpos64(m_fileInputPointer, &lastpos); return lastpos.__pos; #endif #else fpos_t lastpos; lastpos = ftell(m_fileInputPointer); return lastpos; #endif }
TEST(STDIO_TEST, funopen_seek) { #if defined(__BIONIC__) auto read_fn = [](void*, char*, int) { return -1; }; auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; }; auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; }; FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr); ASSERT_TRUE(fp != nullptr); fpos_t pos; #if defined(__LP64__) EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno); EXPECT_EQ(0xfedcba12LL, pos); #else EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno); EXPECT_EQ(EOVERFLOW, errno); #endif FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr); ASSERT_TRUE(fp64 != nullptr); fpos64_t pos64; EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno); EXPECT_EQ(0xfedcba12345678, pos64); #else GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; #endif }
int64_t CoreIOReader::Length() { CoreAssert(this != NULL); CoreAssert(IsOpen()); #ifndef __GNUC__ MutexHolder mh(&m_ioMutex); #endif #ifdef HAS_FPOS64 fpos64_t lastpos, endpos; #if defined(TARGET_COMPILER_VC) || defined(TARGET_COMPILER_BORLAND) || \ (defined(TARGET_COMPILER_ICC) && defined(TARGET_OS_WINDOWS)) lastpos = _ftelli64(m_fileInputPointer); _fseeki64(m_fileInputPointer, 0, SEEK_END); endpos = _ftelli64(m_fileInputPointer); _fseeki64(m_fileInputPointer, lastpos, SEEK_SET); #elif defined (TARGET_OS_MACOSX) || defined (TARGET_OS_NETBSD) || \ defined (TARGET_OS_FREEBSD) || defined (TARGET_OS_OPENBSD) || \ defined (TARGET_COMPILER_MINGW) fgetpos(m_fileInputPointer, &lastpos); fseek(m_fileInputPointer, 0, SEEK_END); fgetpos(m_fileInputPointer, &endpos); fsetpos(m_fileInputPointer, &lastpos); #else fgetpos64(m_fileInputPointer, &lastpos); fseeko64(m_fileInputPointer, 0, SEEK_END); fgetpos64(m_fileInputPointer, &endpos); fsetpos64(m_fileInputPointer, &lastpos); #endif #else fpos_t lastpos, endpos; lastpos = ftell(m_fileInputPointer); fseek(m_fileInputPointer, 0, SEEK_END); endpos = ftell(m_fileInputPointer); fseek(m_fileInputPointer, lastpos, SEEK_SET); #endif #if defined (TARGET_OS_WINDOWS) || defined (TARGET_OS_MACOSX) || defined (TARGET_OS_FREEBSD) || \ defined (TARGET_OS_NETBSD) || defined (TARGET_OS_OPENBSD) || defined (TARGET_COMPILER_CYGWIN) || \ defined (TARGET_OS_NDSFIRMWARE) return endpos; #elif defined (TARGET_OS_LINUX) return endpos.__pos; #endif }
int PerlIO_getpos(PerlIO *f, Fpos_t *pos) { #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64) return fgetpos64(f, pos); #else return fgetpos(f, pos); #endif }
static void AssertFileOffsetAt(FILE* fp, off64_t offset) { EXPECT_EQ(offset, ftell(fp)); EXPECT_EQ(offset, ftello(fp)); EXPECT_EQ(offset, ftello64(fp)); fpos_t pos; fpos64_t pos64; EXPECT_EQ(0, fgetpos(fp, &pos)); EXPECT_EQ(0, fgetpos64(fp, &pos64)); #if defined(__BIONIC__) EXPECT_EQ(offset, static_cast<off64_t>(pos)); EXPECT_EQ(offset, static_cast<off64_t>(pos64)); #else GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n"; #endif }
TEST(STDIO_TEST, seek_tell_family_smoke) { TemporaryFile tf; FILE* fp = fdopen(tf.fd, "w+"); // Initially we should be at 0. AssertFileOffsetAt(fp, 0); // Seek to offset 8192. ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET)); AssertFileOffsetAt(fp, 8192); fpos_t eight_k_pos; ASSERT_EQ(0, fgetpos(fp, &eight_k_pos)); // Seek forward another 8192... ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR)); AssertFileOffsetAt(fp, 8192 + 8192); fpos64_t sixteen_k_pos64; ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64)); // Seek back 8192... ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR)); AssertFileOffsetAt(fp, 8192); // Since we haven't written anything, the end is also at 0. ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); AssertFileOffsetAt(fp, 0); // Check that our fpos64_t from 16KiB works... ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64)); AssertFileOffsetAt(fp, 8192 + 8192); // ...as does our fpos_t from 8192. ASSERT_EQ(0, fsetpos(fp, &eight_k_pos)); AssertFileOffsetAt(fp, 8192); // Do fseeko and fseeko64 work too? ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET)); AssertFileOffsetAt(fp, 1234); ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET)); AssertFileOffsetAt(fp, 5678); fclose(fp); }
GSList* mono_w32process_get_modules (pid_t pid) { #if defined(_AIX) /* due to procfs, this won't work on i */ GSList *ret = NULL; FILE *fp; MonoW32ProcessModule *mod; struct prmap module; int i; fpos64_t curpos; char pidpath[32]; /* "/proc/<uint64_t max>/map" plus null, rounded */ char libpath[MAXPATHLEN + 1]; char membername[MAXPATHLEN + 1]; char combinedname[(MAXPATHLEN * 2) + 3]; /* lib, member, (), and nul */ sprintf (pidpath, "/proc/%d/map", pid); if ((fp = fopen(pidpath, "r"))) { while (fread (&module, sizeof (module), 1, fp) == 1 /* proc(4) declares such a struct to be the array terminator */ && (module.pr_size != 0 && module.pr_mflags != 0) && (module.pr_mflags & MA_READ)) { fgetpos64 (fp, &curpos); /* save our position */ fseeko (fp, module.pr_pathoff, SEEK_SET); while ((libpath[i++] = fgetc (fp))); i = 0; while ((membername[i++] = fgetc (fp))); i = 0; fsetpos64 (fp, &curpos); /* back to normal */ mod = g_new0 (MonoW32ProcessModule, 1); mod->address_start = module.pr_vaddr; mod->address_end = module.pr_vaddr + module.pr_size; mod->address_offset = module.pr_off; mod->perms = g_strdup ("r--p"); /* XXX? */ /* AIX has what appears to be device, channel and inode information, * but it's in a string. Try parsing it. * * XXX: I believe it's fstype.devno.chano.inode, but I'm uncertain * as to how that maps out, so I only fill in the inode (like BSD) */ sscanf (module.pr_mapname, "%*[^.].%*lu.%*u.%lu", &(mod->inode)); if (membername[0]) { snprintf(combinedname, MAXPATHLEN, "%s(%s)", libpath, membername); mod->filename = g_strdup (combinedname); } else { mod->filename = g_strdup (libpath); } if (g_slist_find_custom (ret, mod, mono_w32process_module_equals) == NULL) { ret = g_slist_prepend (ret, mod); } else { mono_w32process_module_free (mod); } } } else { mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't open process map file for pid %d", __func__, pid); return NULL; } if (ret) ret = g_slist_reverse (ret); fclose (fp); return(ret); #else GSList *ret = NULL; FILE *fp; MonoW32ProcessModule *mod; gchar buf[MAXPATHLEN + 1], *p, *endp; gchar *start_start, *end_start, *prot_start, *offset_start; gchar *maj_dev_start, *min_dev_start, *inode_start, prot_buf[5]; gpointer address_start, address_end, address_offset; guint32 maj_dev, min_dev; guint64 inode; guint64 device; fp = open_process_map (pid, "r"); if (!fp) { mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't open process map file for pid %d", __func__, pid); return NULL; } while (fgets (buf, sizeof(buf), fp)) { p = buf; while (g_ascii_isspace (*p)) ++p; start_start = p; if (!g_ascii_isxdigit (*start_start)) { continue; } address_start = (gpointer)strtoul (start_start, &endp, 16); p = endp; if (*p != '-') { continue; } ++p; end_start = p; if (!g_ascii_isxdigit (*end_start)) { continue; } address_end = (gpointer)strtoul (end_start, &endp, 16); p = endp; if (!g_ascii_isspace (*p)) { continue; } while (g_ascii_isspace (*p)) ++p; prot_start = p; if (*prot_start != 'r' && *prot_start != '-') { continue; } memcpy (prot_buf, prot_start, 4); prot_buf[4] = '\0'; while (!g_ascii_isspace (*p)) ++p; while (g_ascii_isspace (*p)) ++p; offset_start = p; if (!g_ascii_isxdigit (*offset_start)) { continue; } address_offset = (gpointer)strtoul (offset_start, &endp, 16); p = endp; if (!g_ascii_isspace (*p)) { continue; } while(g_ascii_isspace (*p)) ++p; maj_dev_start = p; if (!g_ascii_isxdigit (*maj_dev_start)) { continue; } maj_dev = strtoul (maj_dev_start, &endp, 16); p = endp; if (*p != ':') { continue; } ++p; min_dev_start = p; if (!g_ascii_isxdigit (*min_dev_start)) { continue; } min_dev = strtoul (min_dev_start, &endp, 16); p = endp; if (!g_ascii_isspace (*p)) { continue; } while (g_ascii_isspace (*p)) ++p; inode_start = p; if (!g_ascii_isxdigit (*inode_start)) { continue; } inode = (guint64)strtol (inode_start, &endp, 10); p = endp; if (!g_ascii_isspace (*p)) { continue; } #if defined(MAJOR_IN_MKDEV) || defined(MAJOR_IN_SYSMACROS) device = makedev ((int)maj_dev, (int)min_dev); #else device = 0; #endif if ((device == 0) && (inode == 0)) { continue; } while(g_ascii_isspace (*p)) ++p; /* p now points to the filename */ mod = g_new0 (MonoW32ProcessModule, 1); mod->address_start = address_start; mod->address_end = address_end; mod->perms = g_strdup (prot_buf); mod->address_offset = address_offset; mod->device = device; mod->inode = inode; mod->filename = g_strdup (g_strstrip (p)); if (g_slist_find_custom (ret, mod, mono_w32process_module_equals) == NULL) { ret = g_slist_prepend (ret, mod); } else { mono_w32process_module_free (mod); } } ret = g_slist_reverse (ret); fclose (fp); return(ret); #endif }