Skip to content

Commit 7910379

Browse files
committed
Better Windows compatibility and bugfix for $(process_command_line)
commit_hash:a0bd2f6efc7bfaef236e172b5735c00b524da09b
1 parent 9365fe3 commit 7910379

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

yt/yt/core/logging/file_log_writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ TString FormatFileName(const TString& fileNamePattern)
5151
formatter
5252
.SetProperty("process_id", ToString(GetCurrentProcessId()))
5353
.SetProperty("process_name", SanitizeFileName(GetCurrentProcessName()))
54-
.SetProperty("process_command_line", SanitizeFileName(GetCurrentProcessName()));
54+
.SetProperty("process_command_line", SanitizeFileName(GetCurrentProcessCommandLine()));
5555
return formatter.Format(fileNamePattern);
5656
}
5757

yt/yt/core/misc/proc.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include "proc.h"
2-
#include "common.h"
3-
#include "string.h"
42

53
#include <yt/yt/core/logging/log.h>
64

@@ -22,9 +20,12 @@
2220
#include <util/string/strip.h>
2321
#include <util/string/vector.h>
2422

23+
#include <util/system/getpid.h>
2524
#include <util/system/info.h>
2625
#include <util/system/fs.h>
2726
#include <util/system/fstat.h>
27+
#include <util/system/thread.h>
28+
2829
#include <util/folder/iterator.h>
2930
#include <util/folder/filelist.h>
3031

@@ -327,24 +328,12 @@ std::vector<int> GetPidsUnderParent(int targetPid)
327328

328329
size_t GetCurrentProcessId()
329330
{
330-
#if defined(_linux_)
331-
return getpid();
332-
#else
333-
YT_ABORT();
334-
#endif
331+
return GetPID();
335332
}
336333

337334
size_t GetCurrentThreadId()
338335
{
339-
#if defined(_linux_)
340-
return static_cast<size_t>(::syscall(SYS_gettid));
341-
#elif defined(_darwin_)
342-
uint64_t tid;
343-
YT_VERIFY(pthread_threadid_np(nullptr, &tid) == 0);
344-
return static_cast<size_t>(tid);
345-
#else
346-
return ::GetCurrentThreadId();
347-
#endif
336+
return TThread::CurrentThreadNumericId();
348337
}
349338

350339
std::vector<size_t> GetCurrentProcessThreadIds()
@@ -371,7 +360,7 @@ std::vector<size_t> GetCurrentProcessThreadIds()
371360

372361
bool IsUserspaceThread(size_t tid)
373362
{
374-
#ifdef __linux__
363+
#if defined(__linux__)
375364
TFileInput file(Format("/proc/%v/stat", tid));
376365
auto statFields = SplitString(file.ReadLine(), " ");
377366
constexpr int StartStackIndex = 27;
@@ -383,23 +372,34 @@ bool IsUserspaceThread(size_t tid)
383372
return startStack != 0;
384373
#else
385374
Y_UNUSED(tid);
386-
return false;
375+
return true;
387376
#endif
388377
}
389378

390379
std::string GetCurrentProcessName()
391380
{
392-
#ifdef __linux__
381+
#if defined(__linux__)
393382
return std::string(Trim(TUnbufferedFileInput("/proc/self/comm").ReadAll(), "\n"));
383+
#elif defined(_win_)
384+
char path[MAX_PATH];
385+
DWORD length = ::GetModuleFileNameA(nullptr, path, MAX_PATH);
386+
if (length == 0) {
387+
return "(unknown)";
388+
}
389+
std::string fullPath(path, length);
390+
auto pos = fullPath.find_last_of("\\/");
391+
return pos == std::string::npos ? fullPath : fullPath.substr(pos + 1);
394392
#else
395393
return "(unknown)";
396394
#endif
397395
}
398396

399397
std::string GetCurrentProcessCommandLine()
400398
{
401-
#ifdef __linux__
399+
#if defined(__linux__)
402400
return std::string(Trim(TUnbufferedFileInput("/proc/self/cmdline").ReadAll(), "\n"));
401+
#elif defined(_win_)
402+
return ::GetCommandLineA();
403403
#else
404404
return "(unknown)";
405405
#endif

0 commit comments

Comments
 (0)