Skip to content

Commit bee8772

Browse files
committed
[Backtracing][Linux] Reorganise modules for _Backtracing.
Using `SwiftShims` is undesirable - it creates all kinds of build issues, and means shipping the `_SwiftBacktracing.h` header in the SDK, which is not necessary. While we're doing this, add the necessary definitions for reading ELF and DWARF information. rdar://110261712
1 parent 1fb15b9 commit bee8772

File tree

19 files changed

+2402
-455
lines changed

19 files changed

+2402
-455
lines changed

stdlib/public/SwiftShims/swift/shims/CrashInfo.h renamed to include/swift/Runtime/CrashInfo.h

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,52 @@
1818
#ifndef SWIFT_CRASHINFO_H
1919
#define SWIFT_CRASHINFO_H
2020

21-
#include "SwiftStdint.h"
21+
#include <inttypes.h>
2222

2323
#ifdef __cplusplus
2424
namespace swift {
25+
namespace runtime {
26+
namespace backtrace {
2527
extern "C" {
2628
#endif
2729

2830
struct CrashInfo {
29-
__swift_uint64_t crashing_thread;
30-
__swift_uint64_t signal;
31-
__swift_uint64_t fault_address;
32-
__swift_uint64_t mctx;
31+
uint64_t crashing_thread;
32+
uint64_t signal;
33+
uint64_t fault_address;
34+
35+
#ifdef __APPLE__
36+
uint64_t mctx;
37+
#elif defined(__linux__)
38+
uint64_t thread_list;
39+
#endif
40+
};
41+
42+
#ifdef __linux__
43+
44+
struct memserver_req {
45+
uint64_t addr;
46+
uint64_t len;
3347
};
3448

49+
struct memserver_resp {
50+
uint64_t addr;
51+
int64_t len;
52+
/* Then len bytes of data */
53+
};
54+
55+
struct thread {
56+
uint64_t next;
57+
int64_t tid;
58+
uint64_t uctx;
59+
};
60+
61+
#endif
62+
3563
#ifdef __cplusplus
3664
} // extern "C"
65+
} // namespace backtrace
66+
} // namespace runtime
3767
} // namespace swift
3868
#endif
3969

stdlib/public/Backtracing/CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,26 @@ set(BACKTRACING_SOURCES
3030
Registers.swift
3131
SymbolicatedBacktrace.swift
3232
Utils.swift
33-
Shims.cpp
33+
Win32Extras.cpp
3434

3535
get-cpu-context.${SWIFT_ASM_EXT}
3636
)
3737

38-
set(BACKTRACING_COMPILE_FLAGS)
38+
set(BACKTRACING_COMPILE_FLAGS
39+
"-Xfrontend;-experimental-spi-only-imports"
40+
"-Xcc;-I${SWIFT_SOURCE_DIR}/include"
41+
"-Xcc;-I${CMAKE_BINARY_DIR}/include"
42+
"-Xcc;-fno-implicit-module-maps"
43+
"-Xcc;-fbuiltin-module-map"
44+
"-Xcc;-fmodule-map-file=${SWIFT_STDLIB_SOURCE_DIR}/public/SwiftShims/swift/shims/module.modulemap"
45+
"-Xcc;-fmodule-map-file=${SWIFT_STDLIB_SOURCE_DIR}/public/Backtracing/modules/module.modulemap")
46+
47+
###TODO: Add these when we add static linking support
48+
#
49+
#list(APPEND BACKTRACING_COMPILE_FLAGS
50+
# "-Xcc;-I${SWIFT_PATH_TO_ZLIB_SOURCE}"
51+
# "-Xcc;-I${SWIFT_PATH_TO_ZSTD_SOURCE}/lib"
52+
# "-Xcc;-I${SWIFT_PATH_TO_LIBLZMA_SOURCE}/src/liblzma/api")
3953

4054
if(SWIFT_ASM_AVAILABLE)
4155
list(APPEND BACKTRACING_SOURCES get-cpu-context.${SWIFT_ASM_EXT})
@@ -52,7 +66,7 @@ set(LLVM_OPTIONAL_SOURCES
5266
add_swift_target_library(swift_Backtracing ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
5367
${BACKTRACING_SOURCES}
5468

55-
SWIFT_MODULE_DEPENDS ${concurrency}
69+
SWIFT_MODULE_DEPENDS ${concurrency} _StringProcessing
5670

5771
LINK_LIBRARIES ${swift_backtracing_link_libraries}
5872

stdlib/public/Backtracing/Shims.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
//===--- Compression.h - C decls for compression libraries ------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Includes and definitions to allow us to use the compression libraries
14+
// (zlib, zstd and liblzma) in the backtracing module.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_BACKTRACING_COMPRESSION_H
19+
#define SWIFT_BACKTRACING_COMPRESSION_H
20+
21+
#include <stdint.h>
22+
#include <stdlib.h>
23+
24+
// Right now, we're soft linking to zlib/zstd/liblzma, so that users don't
25+
// need it installed (but if they try to do something that requires it,
26+
// they'll see an error message).
27+
//
28+
// As a result, we've grabbed copies of the relevant definitions here so
29+
// that we don't need to install the -dev packages in order to build Swift.
30+
31+
#if SWIFT_BACKTRACE_STATIC_ZLIB
32+
#include "zlib.h"
33+
#else
34+
// This is the version we took the z_stream structure from
35+
#define ZLIB_VERSION "1.2.11"
36+
37+
#define Z_OK 0
38+
#define Z_STREAM_END 1
39+
40+
#define Z_NO_FLUSH 0
41+
42+
typedef struct z_stream_s {
43+
uint8_t *next_in;
44+
unsigned avail_in;
45+
unsigned long total_in;
46+
47+
uint8_t *next_out;
48+
unsigned avail_out;
49+
unsigned long total_out;
50+
51+
const char *msg;
52+
struct internal_state *state;
53+
54+
void (*zalloc)(void *, unsigned, unsigned);
55+
void (*zfree)(void *, void *);
56+
void *opaque;
57+
58+
int data_type;
59+
60+
unsigned long adler;
61+
unsigned long reserved;
62+
} z_stream;
63+
64+
typedef z_stream *z_streamp;
65+
#endif
66+
67+
#if SWIFT_BACKTRACE_STATIC_ZSTD
68+
#include "zstd.h"
69+
#else
70+
typedef struct ZSTD_inBuffer_s {
71+
const void *src;
72+
size_t size;
73+
size_t pos;
74+
} ZSTD_inBuffer;
75+
76+
typedef struct ZSTD_outBuffer_s {
77+
void *dst;
78+
size_t size;
79+
size_t pos;
80+
} ZSTD_outBuffer;
81+
#endif
82+
83+
#if SWIFT_BACKTRACE_STATIC_LIBLZMA
84+
#include "lzma.h"
85+
#else
86+
typedef enum {
87+
LZMA_OK = 0,
88+
LZMA_STREAM_END = 1,
89+
LZMA_NO_CHECK = 2,
90+
LZMA_UNSUPPORTED_CHECK = 3,
91+
LZMA_GET_CHECK = 4,
92+
LZMA_MEM_ERROR = 5,
93+
LZMA_MEMLIMIT_ERROR = 6,
94+
LZMA_FORMAT_ERROR = 7,
95+
LZMA_OPTIONS_ERROR = 8,
96+
LZMA_DATA_ERROR = 9,
97+
LZMA_BUF_ERROR = 10,
98+
LZMA_PROG_ERROR = 11,
99+
} lzma_ret;
100+
101+
typedef enum {
102+
LZMA_RUN = 0,
103+
LZMA_SYNC_FLUSH = 1,
104+
LZMA_FULL_FLUSH = 2,
105+
LZMA_FULL_BARRIER = 4,
106+
LZMA_FINISH = 3
107+
} lzma_action;
108+
109+
typedef enum {
110+
LZMA_RESERVED_ENUM = 0,
111+
} lzma_reserved_enum;
112+
113+
typedef struct {
114+
void *(*alloc)(void *, size_t, size_t);
115+
void (*free)(void *, void *);
116+
void *opaque;
117+
} lzma_allocator;
118+
119+
typedef struct lzma_internal_s lzma_internal;
120+
121+
typedef struct {
122+
const uint8_t *next_in;
123+
size_t avail_in;
124+
uint64_t total_in;
125+
126+
uint8_t *next_out;
127+
size_t avail_out;
128+
uint64_t total_out;
129+
130+
const lzma_allocator *allocator;
131+
132+
lzma_internal *internal;
133+
134+
void *reserved_ptr1;
135+
void *reserved_ptr2;
136+
void *reserved_ptr3;
137+
void *reserved_ptr4;
138+
uint64_t reserved_int1;
139+
uint64_t reserved_int2;
140+
size_t reserved_int3;
141+
size_t reserved_int4;
142+
lzma_reserved_enum reserved_enum1;
143+
lzma_reserved_enum reserved_enum2;
144+
} lzma_stream;
145+
146+
#define LZMA_STREAM_INIT {0}
147+
#endif
148+
149+
#ifdef __cplusplus
150+
namespace swift {
151+
extern "C" {
152+
#endif
153+
154+
// The Swift importer can't cope with complex macros; it will do inline
155+
// functions, however.
156+
static inline lzma_stream lzma_stream_init() {
157+
return (lzma_stream)LZMA_STREAM_INIT;
158+
}
159+
static inline z_stream zlib_stream_init() {
160+
return (z_stream){ 0 };
161+
}
162+
163+
#ifdef __cplusplus
164+
} // extern "C"
165+
} // namespace swift
166+
#endif
167+
168+
#endif // SWIFT_BACKTRACING_COMPRESSION_H
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===--- FixedLayout.h - Types whose layout must be fixed -------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Defines types whose in-memory layout must be fixed, which therefore have
14+
// to be defined using C code.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_BACKTRACING_FIXED_LAYOUT_H
19+
#define SWIFT_BACKTRACING_FIXED_LAYOUT_H
20+
21+
#ifdef __cplusplus
22+
namespace swift {
23+
extern "C" {
24+
#endif
25+
26+
#include <stdint.h>
27+
28+
struct x86_64_gprs {
29+
uint64_t _r[16];
30+
uint64_t rflags;
31+
uint16_t cs, fs, gs, _pad0;
32+
uint64_t rip;
33+
uint64_t valid;
34+
};
35+
36+
struct i386_gprs {
37+
uint32_t _r[8];
38+
uint32_t eflags;
39+
uint16_t segreg[6];
40+
uint32_t eip;
41+
uint32_t valid;
42+
};
43+
44+
struct arm64_gprs {
45+
uint64_t _x[32];
46+
uint64_t pc;
47+
uint64_t valid;
48+
};
49+
50+
struct arm_gprs {
51+
uint32_t _r[16];
52+
uint32_t valid;
53+
};
54+
55+
#ifdef __cpluspus
56+
} // extern "C"
57+
} // namespace swift
58+
#endif
59+
60+
#endif // SWIFT_BACKTRACING_FIXED_LAYOUT_H

0 commit comments

Comments
 (0)