Skip to content

Commit

Permalink
Try to handle EINTR when writing data
Browse files Browse the repository at this point in the history
Hopefully fixes issues such as this one:

heaptrack debug(2) [8859:8859]@5830 dlopen_notify_callback: /tmp/KDevelop-5.2.1-x86_64/usr/lib/libxcb-dri2.so.0 7fd27a93d000
heaptrack debug(1) [8885:8885]@5830 child_fork()
heaptrack debug(1) [8859:8859]@5831 write error 4/Interrupted system call
KDE#1  0x00007fd298297f1e sp=0x00007ffe33957bd0 _ZN12_GLOBAL__N_19HeapTrack10writeErrorEv + 0x4c
KDE#2  0x00007fd298297cff sp=0x00007ffe33957c00 _ZN12_GLOBAL__N_19HeapTrack24dl_iterate_phdr_callbackEP12dl_phdr_infomPv + 0x13c
KDE#3  0x00007fd29123fc81 sp=0x00007ffe33957c50 dl_iterate_phdr + 0x171
KDE#4  0x00007fd298297ec1 sp=0x00007ffe33957d00 _ZN12_GLOBAL__N_19HeapTrack17updateModuleCacheEv + 0xa5
KDE#5  0x00007fd298297ae0 sp=0x00007ffe33957d20 _ZN12_GLOBAL__N_19HeapTrack12handleMallocEPvmRK5Trace + 0x54
KDE#6  0x00007fd298298805 sp=0x00007ffe33957d60 heaptrack_malloc + 0xef
KDE#7  0x00007fd298295f9b sp=0x00007ffe33957fb0 malloc + 0x5f
KDE#8  0x00007fd291ab6089 sp=0x00007ffe33957fe0 _Znwm + 0x19
KDE#9  0x00007fd29205cd42 sp=0x00007ffe33957ff0 _ZN7QObjectC1EPS_ + 0x22
KDE#10 0x00007fd292023374 sp=0x00007ffe33958010 _ZN8QLibraryC2ERK7QStringP7QObject + 0x14
KDE#11 0x00007fd292ef53d0 sp=0x00007ffe33958030 _ZN13KPluginLoader4loadEv + 0x60
KDE#12 0x00007fd292ef54aa sp=0x00007ffe33958090 _ZN13KPluginLoader8instanceEv + 0xa
KDE#13 0x00007fd292ef54ec sp=0x00007ffe339580a0 _ZN13KPluginLoader7factoryEv + 0x1c
KDE#14 0x00007fd297d4eece sp=0x00007ffe33958140 _ZN8KDevelop16PluginController18loadPluginInternalERK7QString + 0x58e
KDE#15 0x00007fd297d50396 sp=0x00007ffe339582a0 _ZN8KDevelop16PluginController10initializeEv + 0x8d6
KDE#16 0x00007fd297d5ab85 sp=0x00007ffe339583f0 _ZN8KDevelop11CorePrivate10initializeENS_4Core5SetupERK7QString + 0xb95
KDE#17 0x00007fd297d5c03d sp=0x00007ffe33958490 _ZN8KDevelop4Core10initializeENS0_5SetupERK7QString + 0x5d
KDE#18 0x000000000040b472 sp=0x00007ffe339584c0 main + 0x5182
KDE#19 0x00007fd29112ff4a sp=0x00007ffe339587d0 __libc_start_main + 0xea
KDE#20 0x000000000040caed sp=0x00007ffe33958890 _start + 0x29
heaptrack debug(1) [8859:8859]@5858 shutdown()
heaptrack debug(1) [8859:8859]@5858 destroying LockedData
heaptrack debug(1) [8859:8859]@5858 done destroying LockedData
heaptrack debug(1) [8859:8859]@5885 shutdown() done

CCBUG: 393387
  • Loading branch information
milianw committed Apr 23, 2018
1 parent d16bcef commit 77f1e06
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions src/track/libheaptrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,7 @@ class HeapTrack

debugLog<VeryVerboseOutput>("writeTimestamp(%" PRIx64 ")", elapsed.count());

if (fprintf(s_data->out, "c %" PRIx64 "\n", elapsed.count()) < 0) {
writeError();
return;
}
writeLine("c %" PRIx64 "\n", elapsed.count());
}

void writeRSS()
Expand All @@ -406,10 +403,7 @@ class HeapTrack
// TODO: use custom allocators with known page sizes to prevent tainting
// the RSS numbers with heaptrack-internal data

if (fprintf(s_data->out, "R %zx\n", rss) < 0) {
writeError();
return;
}
writeLine("R %zx\n", rss);
}

void handleMalloc(void* ptr, size_t size, const Trace& trace)
Expand All @@ -419,24 +413,16 @@ class HeapTrack
}
updateModuleCache();

const auto index = s_data->traceTree.index(trace, [this](uintptr_t ip, uint32_t index) {
if (fprintf(s_data->out, "t %" PRIxPTR " %x\n", ip, index) < 0) {
writeError();
return false;
}
return true;
});
const auto index = s_data->traceTree.index(
trace, [this](uintptr_t ip, uint32_t index) { return writeLine("t %" PRIxPTR " %x\n", ip, index); });

#ifdef DEBUG_MALLOC_PTRS
auto it = s_data->known.find(ptr);
assert(it == s_data->known.end());
s_data->known.insert(ptr);
#endif

if (fprintf(s_data->out, "+ %zx %x %" PRIxPTR "\n", size, index, reinterpret_cast<uintptr_t>(ptr)) < 0) {
writeError();
return;
}
writeLine("+ %zx %x %" PRIxPTR "\n", size, index, reinterpret_cast<uintptr_t>(ptr));
}

void handleFree(void* ptr)
Expand All @@ -451,10 +437,7 @@ class HeapTrack
s_data->known.erase(it);
#endif

if (fprintf(s_data->out, "- %" PRIxPTR "\n", reinterpret_cast<uintptr_t>(ptr)) < 0) {
writeError();
return;
}
writeLine("- %" PRIxPTR "\n", reinterpret_cast<uintptr_t>(ptr));
}

private:
Expand All @@ -468,23 +451,20 @@ class HeapTrack

debugLog<VerboseOutput>("dlopen_notify_callback: %s %zx", fileName, info->dlpi_addr);

if (fprintf(heaptrack->s_data->out, "m %s %zx", fileName, info->dlpi_addr) < 0) {
heaptrack->writeError();
if (!heaptrack->writeLine("m %s %zx", fileName, info->dlpi_addr)) {
return 1;
}

for (int i = 0; i < info->dlpi_phnum; i++) {
const auto& phdr = info->dlpi_phdr[i];
if (phdr.p_type == PT_LOAD) {
if (fprintf(heaptrack->s_data->out, " %zx %zx", phdr.p_vaddr, phdr.p_memsz) < 0) {
heaptrack->writeError();
if (!heaptrack->writeLine(" %zx %zx", phdr.p_vaddr, phdr.p_memsz)) {
return 1;
}
}
}

if (fputc('\n', heaptrack->s_data->out) == EOF) {
heaptrack->writeError();
if (!heaptrack->writeLine("\n")) {
return 1;
}

Expand Down Expand Up @@ -520,14 +500,42 @@ class HeapTrack
return;
}
debugLog<MinimalOutput>("%s", "updateModuleCache()");
if (fputs("m -\n", s_data->out) == EOF) {
writeError();
if (!writeLine("m -\n")) {
return;
}
dl_iterate_phdr(&dl_iterate_phdr_callback, this);
s_data->moduleCacheDirty = false;
}

template <typename... T>
inline bool writeLine(const char* fmt, T... args)
{
int ret = 0;
do {
ret = fprintf(s_data->out, fmt, args...);
} while (ret < 0 && errno == EINTR);

if (ret < 0) {
writeError();
}

return ret >= 0;
}

inline bool writeLine(const char* line)
{
int ret = 0;
do {
ret = fputs(line, s_data->out);
} while (ret < 0 && errno == EINTR);

if (ret < 0) {
writeError();
}

return ret >= 0;
}

void writeError()
{
debugLog<MinimalOutput>("write error %d/%s", errno, strerror(errno));
Expand Down

0 comments on commit 77f1e06

Please sign in to comment.