Skip to content

Commit 8c5d4c7

Browse files
authored
Add Mono EventPipe rundown support. (#47339)
* Add Mono EventPipe rundown support. Add support into Mono VM emitting rundown events into EventPipe stream. All rundown events emitted by CoreClr during EventPipe rundown phase are now also emitted by Mono, making sure enough meta information is available for tooling to correctly resolve callstacks (not yet emitted) included in EventPipe events emitted by Mono VM.
1 parent 9f37c52 commit 8c5d4c7

File tree

15 files changed

+1641
-112
lines changed

15 files changed

+1641
-112
lines changed

src/mono/mono/eglib/eglib-remap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define g_clear_error monoeg_g_clear_error
3030
#define g_convert monoeg_g_convert
3131
#define g_convert_error_quark monoeg_g_convert_error_quark
32+
#define g_fixed_buffer_custom_allocator monoeg_g_fixed_buffer_custom_allocator
3233
#define g_dir_close monoeg_g_dir_close
3334
#define g_dir_open monoeg_g_dir_open
3435
#define g_dir_read_name monoeg_g_dir_read_name
@@ -261,6 +262,7 @@
261262
#define g_usleep monoeg_g_usleep
262263
#define g_utf16_to_ucs4 monoeg_g_utf16_to_ucs4
263264
#define g_utf16_to_utf8 monoeg_g_utf16_to_utf8
265+
#define g_utf16_to_utf8_custom_alloc monoeg_g_utf16_to_utf8_custom_alloc
264266
#define g_utf16_ascii_equal monoeg_g_utf16_ascii_equal
265267
#define g_utf16_asciiz_equal monoeg_g_utf16_asciiz_equal
266268
#define g_utf8_jump_table monoeg_g_utf8_jump_table
@@ -272,6 +274,7 @@
272274
#define g_utf8_strup monoeg_g_utf8_strup
273275
#define g_utf8_to_ucs4_fast monoeg_g_utf8_to_ucs4_fast
274276
#define g_utf8_to_utf16 monoeg_g_utf8_to_utf16
277+
#define g_utf8_to_utf16_custom_alloc monoeg_g_utf8_to_utf16_custom_alloc
275278
#define g_utf8_validate monoeg_g_utf8_validate
276279
#define g_unichar_to_utf8 monoeg_g_unichar_to_utf8
277280
#define g_unichar_is_space monoeg_g_unichar_is_space

src/mono/mono/eglib/giconv.c

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written)
831831
}
832832

833833
static gunichar2 *
834-
eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong *items_written, gboolean include_nuls, gboolean replace_invalid_codepoints, GError **err)
834+
eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong *items_written, gboolean include_nuls, gboolean replace_invalid_codepoints, GCustomAllocator custom_alloc_func, gpointer custom_alloc_data, GError **err)
835835
{
836836
gunichar2 *outbuf, *outptr;
837837
size_t outlen = 0;
@@ -881,7 +881,16 @@ eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong
881881
if (items_written)
882882
*items_written = outlen;
883883

884-
outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
884+
if (G_LIKELY (!custom_alloc_func))
885+
outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
886+
else
887+
outptr = outbuf = (gunichar2 *)custom_alloc_func ((outlen + 1) * sizeof (gunichar2), custom_alloc_data);
888+
889+
if (G_UNLIKELY (custom_alloc_func && !outbuf)) {
890+
mono_set_errno (ENOMEM);
891+
goto error;
892+
}
893+
885894
inptr = (char *) str;
886895
inleft = len;
887896

@@ -908,8 +917,11 @@ eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong
908917

909918
return outbuf;
910919

911-
error:
912-
if (errno == EILSEQ) {
920+
error:
921+
if (errno == ENOMEM) {
922+
g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_MEMORY,
923+
"Allocation failed.");
924+
} else if (errno == EILSEQ) {
913925
g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
914926
"Illegal byte sequence encounted in the input.");
915927
} else if (items_read) {
@@ -931,19 +943,25 @@ eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong
931943
gunichar2 *
932944
g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
933945
{
934-
return eg_utf8_to_utf16_general (str, len, items_read, items_written, FALSE, FALSE, err);
946+
return eg_utf8_to_utf16_general (str, len, items_read, items_written, FALSE, FALSE, NULL, NULL, err);
947+
}
948+
949+
gunichar2 *
950+
g_utf8_to_utf16_custom_alloc (const gchar *str, glong len, glong *items_read, glong *items_written, GCustomAllocator custom_alloc_func, gpointer custom_alloc_data, GError **err)
951+
{
952+
return eg_utf8_to_utf16_general (str, len, items_read, items_written, FALSE, FALSE, custom_alloc_func, custom_alloc_data, err);
935953
}
936954

937955
gunichar2 *
938956
eg_utf8_to_utf16_with_nuls (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
939957
{
940-
return eg_utf8_to_utf16_general (str, len, items_read, items_written, TRUE, FALSE, err);
958+
return eg_utf8_to_utf16_general (str, len, items_read, items_written, TRUE, FALSE, NULL, NULL, err);
941959
}
942960

943961
gunichar2 *
944962
eg_wtf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
945963
{
946-
return eg_utf8_to_utf16_general (str, len, items_read, items_written, TRUE, TRUE, err);
964+
return eg_utf8_to_utf16_general (str, len, items_read, items_written, TRUE, TRUE, NULL, NULL, err);
947965
}
948966

949967
gunichar *
@@ -1018,8 +1036,9 @@ g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_wri
10181036
return outbuf;
10191037
}
10201038

1039+
static
10211040
gchar *
1022-
g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1041+
eg_utf16_to_utf8_general (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GCustomAllocator custom_alloc_func, gpointer custom_alloc_data, GError **err)
10231042
{
10241043
char *inptr, *outbuf, *outptr;
10251044
size_t outlen = 0;
@@ -1077,8 +1096,19 @@ g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *item
10771096

10781097
if (items_written)
10791098
*items_written = outlen;
1099+
1100+
if (G_LIKELY (!custom_alloc_func))
1101+
outptr = outbuf = g_malloc (outlen + 1);
1102+
else
1103+
outptr = outbuf = (char *)custom_alloc_func (outlen + 1, custom_alloc_data);
1104+
1105+
if (G_UNLIKELY (custom_alloc_func && !outbuf)) {
1106+
g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_MEMORY, "Allocation failed.");
1107+
if (items_written)
1108+
*items_written = 0;
1109+
return NULL;
1110+
}
10801111

1081-
outptr = outbuf = g_malloc (outlen + 1);
10821112
inptr = (char *) str;
10831113
inleft = len * 2;
10841114

@@ -1098,6 +1128,18 @@ g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *item
10981128
return outbuf;
10991129
}
11001130

1131+
gchar *
1132+
g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1133+
{
1134+
return eg_utf16_to_utf8_general (str, len, items_read, items_written, NULL, NULL, err);
1135+
}
1136+
1137+
gchar *
1138+
g_utf16_to_utf8_custom_alloc (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GCustomAllocator custom_alloc_func, gpointer custom_alloc_data, GError **err)
1139+
{
1140+
return eg_utf16_to_utf8_general (str, len, items_read, items_written, custom_alloc_func, custom_alloc_data, err);
1141+
}
1142+
11011143
gunichar *
11021144
g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
11031145
{
@@ -1302,3 +1344,17 @@ g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items
13021344

13031345
return outbuf;
13041346
}
1347+
1348+
gpointer
1349+
g_fixed_buffer_custom_allocator (gsize req_size, gpointer custom_alloc_data)
1350+
{
1351+
GFixedBufferCustomAllocatorData *fixed_buffer_custom_alloc_data = (GFixedBufferCustomAllocatorData *)custom_alloc_data;
1352+
if (!fixed_buffer_custom_alloc_data)
1353+
return NULL;
1354+
1355+
fixed_buffer_custom_alloc_data->req_buffer_size = req_size;
1356+
if (req_size > fixed_buffer_custom_alloc_data->buffer_size)
1357+
return NULL;
1358+
1359+
return fixed_buffer_custom_alloc_data->buffer;
1360+
}

src/mono/mono/eglib/glib.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,8 @@ typedef enum {
10041004
G_CONVERT_ERROR_FAILED,
10051005
G_CONVERT_ERROR_PARTIAL_INPUT,
10061006
G_CONVERT_ERROR_BAD_URI,
1007-
G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
1007+
G_CONVERT_ERROR_NOT_ABSOLUTE_PATH,
1008+
G_CONVERT_ERROR_NO_MEMORY
10081009
} GConvertError;
10091010

10101011
gchar *g_utf8_strup (const gchar *str, gssize len);
@@ -1031,6 +1032,20 @@ size_t g_utf16_len (const gunichar2 *);
10311032
#define u16to8(str) g_utf16_to_utf8(str, (glong)strlen(str), NULL, NULL, NULL)
10321033
#endif
10331034

1035+
typedef gpointer (*GCustomAllocator) (gsize req_size, gpointer custom_alloc_data);
1036+
1037+
typedef struct {
1038+
gpointer buffer;
1039+
gsize buffer_size;
1040+
gsize req_buffer_size;
1041+
} GFixedBufferCustomAllocatorData;
1042+
1043+
gpointer
1044+
g_fixed_buffer_custom_allocator (gsize req_size, gpointer custom_alloc_data);
1045+
1046+
gunichar2 *g_utf8_to_utf16_custom_alloc (const gchar *str, glong len, glong *items_read, glong *items_written, GCustomAllocator custom_alloc_func, gpointer custom_alloc_data, GError **err);
1047+
gchar *g_utf16_to_utf8_custom_alloc (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GCustomAllocator custom_alloc_func, gpointer custom_alloc_data, GError **err);
1048+
10341049
/*
10351050
* Path
10361051
*/

src/mono/mono/eglib/gmodule-win32.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,21 @@ g_module_address (void *addr, char *file_name, size_t file_name_len,
163163
* this being an exception.
164164
*/
165165
BOOL ret = GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)addr, &module);
166-
if (ret)
166+
if (!ret)
167167
return FALSE;
168168

169169
if (file_name != NULL && file_name_len >= 1) {
170170
/* sigh, non-const. AIX for POSIX is the same way. */
171171
WCHAR fname [MAX_PATH];
172172
DWORD bytes = GetModuleFileNameW (module, fname, G_N_ELEMENTS (fname));
173-
/* XXX: check for ERROR_INSUFFICIENT_BUFFER? */
174173
if (bytes) {
175174
/* Convert back to UTF-8 from wide for runtime */
176-
*file_name = '\0'; /* XXX */
175+
GFixedBufferCustomAllocatorData custom_alloc_data;
176+
custom_alloc_data.buffer = file_name;
177+
custom_alloc_data.buffer_size = file_name_len;
178+
custom_alloc_data.req_buffer_size = 0;
179+
if (!g_utf16_to_utf8_custom_alloc (fname, -1, NULL, NULL, g_fixed_buffer_custom_allocator, &custom_alloc_data, NULL))
180+
*file_name = '\0';
177181
} else {
178182
*file_name = '\0';
179183
}

0 commit comments

Comments
 (0)