---
name: "unw_getcontext"
section: "3libunwind"
platform: "linux"
package: "libunwind-dev"
version: "1.8.1-0.1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/unw_getcontext/3libunwind"
---

# unw\_getcontext(3libunwind)

## NAME

unw\_getcontext -- get initial machine-state

## SYNOPSIS

\#include \<libunwind.h\>\

int unw\_getcontext(unw\_context\_t \*ucp);\

## DESCRIPTION

The unw\_getcontext() routine initializes the context structure  pointed to by ucp with the machine state of the call site. The  exact set of registers stored by unw\_getcontext() is  platform-specific, but, in general, at least all preserved  (\`\`callee-saved'') and all frame-related registers, such as the  stack pointer, will be stored.

This routine is normally implemented as a macro and applications  should not attempt to take its address.

## PLATFORM-SPECIFIC NOTES

On IA-64, unw\_context\_t has a layout that is compatible with  that of ucontext\_t and such structures can be initialized with  getcontext() instead of unw\_getcontext(). However, the  reverse is *not* true and it is *not* safe to use structures  initialized by unw\_getcontext() in places where a structure  initialized by getcontext() is expected. The reason for this  asymmetry is that unw\_getcontext() is optimized for maximum  performance and does not, for example, save the signal mask.

## RETURN VALUE

On successful completion, unw\_getcontext() returns 0.  Otherwise, a value of -1 is returned.

## THREAD AND SIGNAL SAFETY

unw\_getcontext() is thread safe as well as safe to use  from a signal handler.

## SEE ALSO

libunwind(3libunwind), unw\_init\_local(3libunwind)

## AUTHOR

David Mosberger-Tang\
 Email: **dmosberger@gmail.com**\
 WWW: **http://www.nongnu.org/libunwind/**.
