Repository: dotnet/runtime
File: src/coreclr/pal/src/include/pal/process.h (C/C++)

Code before:
Parameters:
  signal - POSIX signal number
  siginfo - POSIX signal info or nullptr
  context - signal context or nullptr
  serialize - allow only one thread to generate core dump

(no return value)
--*/
VOID PROCCreateCrashDumpIfEnabled(int signal, siginfo_t* siginfo, void* context, bool serialize);

/*++
Function:
  PROCCreateCrashReportAndDumpIfEnabled

  Creates crash report and dump of the process if enabled.
  Should be called from crash handling code paths.

Parameters:
  signal - POSIX signal number
  siginfo - POSIX signal info or nullptr
  context - signal context or nullptr
  serialize - allow only one thread to generate core dump

(no return value)
--*/
VOID PROCCreateCrashReportAndDumpIfEnabled(int signal, siginfo_t* siginfo, void* context, bool serialize);

#ifdef __cplusplus
}
#endif // __cplusplus

#endif //PAL_PROCESS_H_



Review comment (bug):
Not sure about combining these into one. Could an option be doing something along these lines:

* Keep PROCCreateCrashDumpIfEnabled as is.
* Add a method that can be used when logging callstack for fatal errors since it should only be done once, like LogCallstackForFatalErrorOnce:
    
```
    // check if already called and return early.
    Thread *pThread = GetThreadNULLOk();
    if (pThread != NULL)
    {
        LogCallstackForLogWorker(pThread, NULL);
    }
    // set already called.
```

* Update call sites that currently output callstacks in response to fatal errors to call that method.
* Call sites that does some other callstack logging could just flip the bool controlling the LogCallstackForFatalErrorOnce.
* Rename MarkCrashReportAlreadyLogged to something more inline with callstack already logged, like CallstackForFatalErrorLogged.
* Since signal.cpp will need to call the LogCallstackForFatalError as well, you will need to have that symbol available to PAL, instead of using weak symbols use concept of callback functions following PAL_Set*Callback API's, look at PAL_SetShutdownCallback. That way you could set the callback to call CallstackForFatalErrorLogged from PAL without need for weak symbols and is inline with existing patterns.
* With these changes you can probably keep s_crashReportAlreadyLogged inside VM since it will not be directly accessed by the PAL anymore.

Code after:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

/*++



Module Name:

    include/pal/process.h

Abstract:

    Miscellaneous process related functions.

Revision History:



--*/

#ifndef _PAL_PROCESS_H_
#define _PAL_PROCESS_H_

#include "pal/palinternal.h"
#include "pal/stackstring.hpp"

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

/* thread ID of thread that has initiated an ExitProcess (or TerminateProcess).
   this is to make sure only one thread cleans up the PAL, and also to prevent
   calls to CreateThread from succeeding once shutdown has started
   [defined in process.c]
*/
extern Volatile<LONG> terminator;

// The process and session ID of this process, so we can avoid excessive calls to getpid() and getsid().
extern DWORD gPID;
extern DWORD gSID;

extern LPWSTR pAppDir;

// The Mac sandbox application group ID (if exists) and container (shared) path
#ifdef __APPLE__
extern LPCSTR gApplicationGroupId;
extern int gApplicationGroupIdLength;
#endif // __APPLE__
extern PathCharString *gSharedFilesPath;

/*++
Function:
  PROCGetProcessIDFromHandle

Abstract
  Return the process ID from a process handle
--*/
DWORD PROCGetProcessIDFromHandle(HANDLE hProcess);

/*++
Function:
  PROCCreateInitialProcess

Abstract
  Initialize all the structures for the initial process.

Parameter
  lpwstrCmdLine:   Command line.
  lpwstrFullPath : Full path to executable

Return
  TRUE: if successful
  FALSE: otherwise

Notes :
    This function takes ownership of lpwstrCmdLine, but not of lpwstrFullPath
--*/
BOOL PROCCreateInitialProcess(LPWSTR lpwstrCmdLine, LPWSTR lpwstrFullPath);

/*++
Function:
  PROCCleanupInitialProcess

Abstract
  Cleanup all the structures for the initial process.

Parameter
  VOID

Return
  VOID

--*/
VOID PROCCleanupInitialProcess(VOID);

/*++
Function
  PROCAbortInitialize()

Abstract
  Initialize the process abort crash dump program file path and
  name. Doing all of this ahead of time so nothing is allocated
  or copied in PROCAbort/signal handler.

Return
  TRUE - succeeds, FALSE - fails

--*/
BOOL PROCAbortInitialize();

/*++
Function:
  PROCAbort()

  Aborts the process after calling the shutdown cleanup handler. This function
  should be called instead of calling abort() directly.

Parameters:
  signal - POSIX signal number
  siginfo - POSIX signal info
  context - signal context or nullptr

  Does not return
--*/
#if !defined(HOST_ARM)  // PAL_NORETURN produces broken unwinding information for this method
                        // making crash dumps impossible to analyze
PAL_NORETURN
#endif
VOID PROCAbort(int signal = SIGABRT, siginfo_t* siginfo = nullptr, void* context = nullptr);

/*++
Function:
  PROCNotifyProcessShutdown

  Calls the abort handler to do any shutdown cleanup. Call be
  called from the unhandled native exception handler.

(no return value)
--*/
VOID PROCNotifyProcessShutdown(bool isExecutingOnAltStack = false);

/*++
Function:
  PROCCreateCrashDumpIfEnabled

  Creates crash dump of the process (if enabled). Can be
  called from the unhandled native exception handler.

Parameters:
  signal - POSIX signal number
  siginfo - POSIX signal info or nullptr
  context - signal context or nullptr
  serialize - allow only one thread to generate core dump

(no return value)
--*/
VOID PROCCreateCrashDumpIfEnabled(int signal, siginfo_t* siginfo, void* context, bool serialize);

/*++
Function:
  PROCLogManagedCallstackForSignal

  Invokes the registered callback to log the managed callstack for a signal.
  Used by Android since CreateDump is not supported there.

Parameters:
  signal - POSIX signal number

(no return value)
--*/
VOID PROCLogManagedCallstackForSignal(int signal);

#ifdef __cplusplus
}
#endif // __cplusplus

#endif //PAL_PROCESS_H_