Skip to content

Commit ebfcd51

Browse files
committed
added mono_unity_image_set_mempool_chunk_foreach in order to report memory blocks from the VM heap
added mono_unity_domain_mempool_chunk_foreach in order to report memory blocks inside the current domain's memory pool added mono_unity_root_domain_mempool_chunk_foreach in order to report memory blocks inside the root domain's memory pool added mono_unity_assembly_mempool_chunk_foreach in order to report memory blocks inside an assembly's image's memory pool added mono_unity_class_get_data_size in order to return static field data size for a MonoClass added mono_unity_class_get_static_field_data in order to access static fields memory for a given MonoClass added mono_unity_class_field_is_literal in order to find out if the attributes added mono_unity_start/stop_gc_world() in order to control the GC world state added mono_unity_gc_heap_foreach in order to report each allocated GC heap section added mono_unity_gchandles_foreach_get_target in order to report all gc handle targets tracked by the garbage collector added mono_unity_object_header_size in order to report mono object header size added mono_unity_array_object_header_size in order to report mono array object header size added mono_unity_offset_of_array_length/bounds_in_array_object_header in order to report the offset of the length/bounds of the array within the header added mono_unity_allocation_granularity in order to report the minimum allocation granulariy inside the vm added mono_unity_get_name_full_chunked in order to extract a types name in chunks added mono_unity_type_is_static in order to report if the given type is static added mono_unity_type_is_pointer_type in order to report if the given type is a pointer type
1 parent 90fe44f commit ebfcd51

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed

mono/metadata/unity-utils.c

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,3 +1379,238 @@ mono_unity_get_enable_handler_block_guards (void)
13791379
{
13801380
return enable_handler_block_guards;
13811381
}
1382+
1383+
//helper structures for VM information extraction functions
1384+
typedef struct {
1385+
GFunc callback;
1386+
gpointer user_data;
1387+
} execution_ctx;
1388+
1389+
typedef struct
1390+
{
1391+
gpointer start;
1392+
size_t size;
1393+
} mono_heap_chunk;
1394+
1395+
// class metadata memory
1396+
static void
1397+
handle_mem_pool_chunk(gpointer chunkStart, gpointer chunkEnd, gpointer userData)
1398+
{
1399+
mono_heap_chunk chunk;
1400+
chunk.start = chunkStart;
1401+
chunk.size = (uint8_t *)chunkEnd - (uint8_t *)chunkStart;
1402+
1403+
execution_ctx *ctx = (execution_ctx *)userData;
1404+
ctx->callback(&chunk, ctx->user_data);
1405+
}
1406+
1407+
static void
1408+
handle_image_set_mem_pool(MonoImageSet *imageSet, gpointer user_data)
1409+
{
1410+
mono_mempool_foreach_block(imageSet->mempool, handle_mem_pool_chunk, user_data);
1411+
}
1412+
1413+
MONO_API void
1414+
mono_unity_image_set_mempool_chunk_foreach(GFunc callback, gpointer user_data)
1415+
{
1416+
execution_ctx ctx;
1417+
ctx.callback = callback;
1418+
ctx.user_data = user_data;
1419+
1420+
mono_metadata_image_set_foreach(handle_image_set_mem_pool, &ctx);
1421+
}
1422+
1423+
MONO_API void
1424+
mono_unity_domain_mempool_chunk_foreach(MonoDomain *domain, GFunc callback, gpointer user_data)
1425+
{
1426+
mono_domain_lock(domain);
1427+
1428+
execution_ctx ctx;
1429+
ctx.callback = callback;
1430+
ctx.user_data = user_data;
1431+
mono_mempool_foreach_block(domain->mp, handle_mem_pool_chunk, &ctx);
1432+
1433+
mono_domain_unlock(domain);
1434+
}
1435+
1436+
MONO_API void
1437+
mono_unity_root_domain_mempool_chunk_foreach(GFunc callback, gpointer user_data)
1438+
{
1439+
MonoDomain *domain = mono_get_root_domain();
1440+
mono_domain_lock(domain);
1441+
1442+
execution_ctx ctx;
1443+
ctx.callback = callback;
1444+
ctx.user_data = user_data;
1445+
mono_mempool_foreach_block(domain->mp, handle_mem_pool_chunk, &ctx);
1446+
1447+
mono_domain_unlock(domain);
1448+
}
1449+
1450+
MONO_API void
1451+
mono_unity_assembly_mempool_chunk_foreach(MonoAssembly *assembly, GFunc callback, gpointer user_data)
1452+
{
1453+
MonoImage *image = assembly->image;
1454+
mono_image_lock(image);
1455+
1456+
execution_ctx ctx;
1457+
ctx.callback = callback;
1458+
ctx.user_data = user_data;
1459+
mono_mempool_foreach_block(image->mempool, handle_mem_pool_chunk, &ctx);
1460+
1461+
if (image->module_count > 0) {
1462+
guint32 i;
1463+
1464+
for (i = 0; i < image->module_count; ++i) {
1465+
MonoImage *moduleImage = image->modules[i];
1466+
1467+
if (moduleImage) {
1468+
mono_mempool_foreach_block(moduleImage->mempool, handle_mem_pool_chunk, &ctx);
1469+
}
1470+
}
1471+
}
1472+
mono_image_unlock(image);
1473+
}
1474+
1475+
// class metadata
1476+
1477+
MONO_API void
1478+
mono_unity_type_get_name_full_chunked(MonoType *type, GFunc chunkReportFunc, gpointer userData)
1479+
{
1480+
mono_type_get_name_chunked(type, MONO_TYPE_NAME_FORMAT_IL, chunkReportFunc, userData);
1481+
}
1482+
1483+
MONO_API gboolean
1484+
mono_unity_type_is_pointer_type(MonoType *type)
1485+
{
1486+
return type->type == MONO_TYPE_PTR;
1487+
}
1488+
1489+
MONO_API gboolean
1490+
mono_unity_type_is_static(MonoType *type)
1491+
{
1492+
return (type->attrs & FIELD_ATTRIBUTE_STATIC) != 0;
1493+
}
1494+
1495+
MONO_API MonoVTable *
1496+
mono_unity_class_try_get_vtable(MonoDomain *domain, MonoClass *klass)
1497+
{
1498+
return mono_class_try_get_vtable(domain, klass);
1499+
}
1500+
1501+
MONO_API uint32_t
1502+
mono_unity_class_get_data_size(MonoClass *klass)
1503+
{
1504+
return mono_class_data_size(klass);
1505+
}
1506+
1507+
MONO_API void *
1508+
mono_unity_vtable_get_static_field_data(MonoVTable *vTable)
1509+
{
1510+
return mono_vtable_get_static_field_data(vTable);
1511+
}
1512+
1513+
MONO_API gboolean
1514+
mono_unity_class_field_is_literal(MonoClassField *field)
1515+
{
1516+
return (field->type->attrs & FIELD_ATTRIBUTE_LITERAL) != 0;
1517+
}
1518+
1519+
// GC world control
1520+
MONO_API void
1521+
mono_unity_stop_gc_world()
1522+
{
1523+
#if HAVE_BDWGC_GC
1524+
GC_stop_world_external();
1525+
#else
1526+
g_assert_not_reached();
1527+
#endif
1528+
}
1529+
1530+
MONO_API void
1531+
mono_unity_start_gc_world()
1532+
{
1533+
#if HAVE_BDWGC_GC
1534+
GC_start_world_external();
1535+
#else
1536+
g_assert_not_reached();
1537+
#endif
1538+
}
1539+
1540+
1541+
//GC memory
1542+
static void
1543+
handle_gc_heap_chunk(void *userdata, gpointer chunk_start, gpointer chunk_end)
1544+
{
1545+
execution_ctx *ctx = (execution_ctx *)userdata;
1546+
mono_heap_chunk chunk;
1547+
chunk.start = chunk_start;
1548+
chunk.size = (uint8_t *)chunk_end - (uint8_t *)chunk_start;
1549+
ctx->callback(&chunk, ctx->user_data);
1550+
}
1551+
1552+
MONO_API void
1553+
mono_unity_gc_heap_foreach(GFunc callback, gpointer user_data)
1554+
{
1555+
#if HAVE_BDWGC_GC
1556+
execution_ctx ctx;
1557+
ctx.callback = callback;
1558+
ctx.user_data = user_data;
1559+
1560+
GC_foreach_heap_section(&ctx, handle_gc_heap_chunk);
1561+
#else
1562+
g_assert_not_reached();
1563+
#endif
1564+
}
1565+
1566+
//GC handles
1567+
static void
1568+
handle_gc_handle(gpointer handle_target, gpointer handle_report_callback)
1569+
{
1570+
execution_ctx *ctx = (execution_ctx *)handle_report_callback;
1571+
ctx->callback(handle_target, ctx->user_data);
1572+
}
1573+
1574+
MONO_API void
1575+
mono_unity_gc_handles_foreach_get_target(GFunc callback, gpointer user_data)
1576+
{
1577+
#if HAVE_BDWGC_GC
1578+
execution_ctx ctx;
1579+
ctx.callback = callback;
1580+
ctx.user_data = user_data;
1581+
mono_gc_strong_handle_foreach(handle_gc_handle, &ctx);
1582+
#else
1583+
g_assert_not_reached();
1584+
#endif
1585+
}
1586+
1587+
// VM runtime info
1588+
MONO_API uint32_t
1589+
mono_unity_object_header_size()
1590+
{
1591+
return (uint32_t)(sizeof(MonoObject));
1592+
}
1593+
1594+
MONO_API uint32_t
1595+
mono_unity_array_object_header_size()
1596+
{
1597+
return offsetof(MonoArray, vector);
1598+
}
1599+
1600+
MONO_API uint32_t
1601+
mono_unity_offset_of_array_length_in_array_object_header()
1602+
{
1603+
return offsetof(MonoArray, max_length);
1604+
}
1605+
1606+
MONO_API uint32_t
1607+
mono_unity_offset_of_array_bounds_in_array_object_header()
1608+
{
1609+
return offsetof(MonoArray, bounds);
1610+
}
1611+
1612+
MONO_API uint32_t
1613+
mono_unity_allocation_granularity()
1614+
{
1615+
return (uint32_t)(2 * sizeof(void *));
1616+
}

0 commit comments

Comments
 (0)