Get the actual structure size before alignment - windbg

So it turns out that both dt struct and ?? sizeof struct return the total size the struct occupies in memory after alignment.
Is there a way to get the actual size of the struct before alignment?
I need this functionality for a function that returns the actual size of a field within a struct. For example:
__declspec(align(64)) struct ALIGNED_STRUCT {
char field;
}
running ?? sizeof(ALIGNED_STRUCT) one should get 0x40 which makes it hard to deduce the actual size of the internal field.
edit:
command outputs:
2:001> dt -v ALIGNED_STRUCT
test!ALIGNED_STRUCT
struct ALIGNED_STRUCT, 1 elements, 0x40 bytes
+0x000 field : Char
3:001> ?? sizeof(ALIGNED_STRUCT)
0x40

No -- there isn't a way to return the structure size "before alignment". That's not really meaningful in any case. The compiler is always using the aligned size. The symbols have the aligned size. That's the size of the type.
If you are looking for things like the "size of an internal field", there are numerous ways to accomplish this. As mentioned in comments, you can do the quick dirty EE sizeof thing:
dx sizeof(((ALIGNED_STRUCT *)0)->field)
You can also get full access to the underlying type system via the data model APIs (in either a C/C++ extension or in JavaScript) which will allow you to find out pretty much whatever you want about the types: their fields, sizes, offsets, function parameter types, template arguments, etc...
From C/C++, you can:
QI for IDebugHostSymbols (https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/dbgmodel/nn-dbgmodel-idebughostsymbols)
Get an IDebugHostModule (https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/dbgmodel/nn-dbgmodel-idebughostmodule) for the module containing your structure by calling the FindModuleByName method.
Get an IDebugHostType (https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/dbgmodel/nn-dbgmodel-idebughosttype) for the type you want to inquire about (e.g.: ALIGNED_STRUCT) by calling FindTypeByName
Enumerate its fields with EnumerateChildren, getting an IDebugHostField (https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/dbgmodel/nn-dbgmodel-idebughostfield) for each
Get the offset of such field by calling GetOffset
Get the type of each field by calling GetType, getting you back to another IDebugHostType
Get the size of the field by calling GetSize
That might look something like this:
ComPtr<IDebugHostSymbols> spHostSymbols; /* QI this off IDebugClient, etc... */
ComPtr<IDebugHostModule> spMyModule;
if (FAILED(spHostSymbols->FindModuleByName(USE_CURRENT_HOST_CONTEXT, L"myModule", &spMyModule)))
{
return ...;
}
ComPtr<IDebugHostType> spType;
if (FAILED(spMyModule->FindTypeByName(L"ALIGNED_STRUCT", &spType)))
{
return ...;
}
ComPtr<IDebugHostType> spType; /* get the type of an object */
//
// Enumerate every field of this type. Note thiat this *WILL NOT* enumerate
// fields of base classes!
//
ComPtr<IDebugHostSymbolEnumerator> spEnum;
if (SUCCEEDED(spType->EnumerateChildren(SymbolField, nullptr, &spEnum)))
{
ComPtr<IDebugHostSymbol> spFieldSymbol;
HRESULT hr = S_OK;
while (SUCCEEDED(hr))
{
hr = spEnum->GetNext(&spFieldSymbol);
if (SUCCEEDED(hr))
{
ComPtr<IDebugHostField> spField;
if (SUCCEEDED(spFieldSymbol.As(&spField))) /* should always succeed */
{
// spField is each field of the type in turn
}
ULONG64 fieldOffset;
if (SUCCEEDED(spField->GetOffset(&fieldOffset)) /* won't succeed on static fields */
{
// fieldOffset is the offset of the field within the type
}
ComPtr<IDebugHostType> spFieldType;
if (SUCCEEDED(spField->GetType(&spFieldType))
{
ULONG64 fieldSize;
if (SUCCEEDED(spFieldType->GetSize(&fieldSize)))
{
// fieldSize contains the size (aligned) of the field's type
}
}
}
}
// hr == E_BOUNDS : we hit the end of the enumerator
// hr == E_ABORT : user requested interruption, propagate upwards immediately
}
For C++, this can be made significantly easier by using the C++17 helper library on GitHub (https://github.com/microsoft/WinDbg-Libraries/blob/master/DbgModelCppLib/DbgModelClientEx.h)
That might look something like:
Module myModule(HostContext::DeferredCurrent(), L"myModule");
Type alignedStruct(myModule, L"ALIGNED_STRUCT");
//
// The below will *NOT* enumerate fields of base classes. You must explicitly
// recurse if you want such.
//
for(Field f : alignedStruct.Fields())
{
//
// Get the offset and size of each field.
//
ULONG64 fieldOffset = f.GetOffset();
ULONG64 fieldSize = f.Type().Size();
}
In JavaScript (see https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/native-objects-in-javascript-extensions-type-objects), this would look like:
Call host.getModuleType to get a type object for the structure (e.g.: ALIGNED_STRUCT)
Get access to the field in question by accessing the named property of fields on the type (e.g.: myType.fields.field_name)
Get the offset of of such field by accessing the offset property
Get the type of each field by accessing the type property
Get the size of the field by accessing the size property of its type
That might look something like:
var myType = host.getModuleType("myModule", "ALIGNED_STRUCT");
var fields = myType.fields;
//
// In JavaScript, fields has properties named according to each field. If
// you want to enumerate, get the property names and access those keys.
//
var fieldNames = Object.getOwnPropertyNames(fields);
for (var fieldName of fieldNames)
{
var field = fields[fieldName];
//
// Get the offset and size of each field...
//
var fieldOffset = field.offset;
var fieldSize = field.type.size;
}
In either of these cases, you would need to manually recurse base classes if you're looking at C++ objects where fields are contained in base classes.
Hope that helps...

William has provided a comprehensive answer this answer is just a practical example of that
0:000> dx #$stru
#$stru : Pcb _KPROCESS (+ 0x0)
name : Pcb
type : _KPROCESS
locationKind : member
offset : 0x0
0:000> dx #$stru.type.fields.Header.type.size
#$stru.type.fields.Header.type.size : 0x18
0:000>

Related

Dart: Storing arguments in a variable before passing

For purposes of testing, it's useful to be able to "prepare" arguments to a function before executing such that the arguments can be checked against any result.
In JavaScript I can do this:
function testFunc ({id, count}) {
/* perform some operation */
return {id, count}
}
const args = {
id: 'someId',
count: Math.round(Math.random() * 10),
}
const res = testFunc({...args})
/* check that count is correct etc */
How do I achieve the same flexibility in Dart?
Map<String, dynamic> testFunc({String id, int count}) {
/* perform some operation */
return {
'id': id,
'count': count,
};
}
final args = /* erm? */
testFunc(args); /* hmmm? */
Am I trying to push the limits of a strongly typed language?
Dart is a statically checked language.
Using a value with an unknown run-time structure, like a map, as parameters makes it impossible to statically check the validity of the arguments.
Take the example:
var data = {#name: "hello", #age: 18};
Person(...data)
(Using symbols for referring to source names, same as Function.apply and noSuchMethod).
Here it looks easy to see that the map actually has a #name and an #age entry, but that's because the map is written as a literal right next to the application. That's the one situation where you don't actually need spread arguments, because you could just write the arguments directly.
In all actually useful cases, it's not possible to see statically which entries the map has, and which types the values are for each key. The type of the map, Map<Symbol, dynamic> is not strong enough to allow checking the call. You should just use Function.apply(Person, [], data) ... except that we (still) do not allow constructors as functions (#216).
If Dart had typed structs/named tuples, it might be possible to do:
// Static type is the named tuple type `(String name, int age)`
var data = (name: "name", age: 18);
var person = Person(...data);
At this point, the static type of data is specific enough to allow the call to be checked statically.
also this is issue in GitHub which I take the answer from

Convert list of strings to an enum list (One,Two,etc)

I've searched a lot but can't find a clear answer anywhere for string list -> enum.
I've got a list of strings that I want to turn into an enum that I can select from in Unity inspector.
Specifically, I'm trying to make an enum list of all the currently set-up Input buttons from project settings. I've got all the names, just don't know how to make it an enum or similar. Ideally showing up like a KeyCode variable in inspector.
Currently trying (and failing) with:
foreach (string s in names)
{
if (Enum.TryParse(s, true, out list))
Debug.Log(list);
else Debug.Log("FAILED");
}
"names" = static List<string> names;
"list" = static MyList list;
"MyList" = enum MyList { Null }
Returns "FAILED" 58 times for only 29 Input axis.
I want a simple solution, so if its not possible or relatively simple, I'll work out something else.
Code for getting the "names" list of strings (Works correctly):
var inputManager = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];
SerializedObject obj = new SerializedObject(inputManager);
SerializedProperty axisArray = obj.FindProperty("m_Axes");
if (axisArray.arraySize == 0)
Debug.Log("No Axes");
for (int i = 0; i < axisArray.arraySize; ++i)
{
var axis = axisArray.GetArrayElementAtIndex(i);
var name = axis.FindPropertyRelative("m_Name").stringValue;
names.Add(name);
}
Normally you can do
foreach (string colorName in Enum.GetNames(typeof(Colors))) which will iterate the names of the enums.
In your code above you havent shown what list is, nor where names has come from. However.
enum Things
{
Item1 = 0,
Item2 = 1
}
You can get the name from string name = Enum.GetName(typeof(Things), (int)Things.Item2) and you can get values from names with int value = (int)Enum.Parse(typeof(Things), nameOfThing)
So depending on what you actually want in a list and what you start with, iterate through and pick the relevant one
I ended up making my own solution, since (and please correct me if I'm wrong) the TryParse() and Parse() methods appear to need the enum to already contain entries with the same name (or index int) as the string to parse. This defeats the purpose for me, since I am doing this because I don't have the names in there already.
My solution ended up being to switch to having a single string input variable instead of an enum, then use Odin Inspector's ValidateInput attribute to check (for spelling errors, and) if the input variable matches any of the string entries in my dynamic list of InputManager input names (which I update manually using Odin Inspector's Button attribute and the code in the original post).
It's slightly less clean than I wanted, but does the job, so I'm satisfied.

When does Chapel pass by reference and when by constant?

I am looking for examples of Chapel passing by reference. This example works but it seems like bad form since I am "returning" the input. Does this waste memory? Is there an explicit way to operate on a class?
class PowerPuffGirl {
var secretIngredients: [1..0] string;
}
var bubbles = new PowerPuffGirl();
bubbles.secretIngredients.push_back("sugar");
bubbles.secretIngredients.push_back("spice");
bubbles.secretIngredients.push_back("everything nice");
writeln(bubbles.secretIngredients);
proc kickAss(b: PowerPuffGirl) {
b.secretIngredients.push_back("Chemical X");
return b;
}
bubbles = kickAss(bubbles);
writeln(bubbles.secretIngredients);
And it produces the output
sugar spice everything nice
sugar spice everything nice Chemical X
What is the most efficient way to use a function to modify Bubbles?
Whether Chapel passes an argument by reference or not can be controlled by the argument intent. For example, integers normally pass by value but we can pass one by reference:
proc increment(ref x:int) { // 'ref' here is an argument intent
x += 1;
}
var x:int = 5;
increment(x);
writeln(x); // outputs 6
The way that a type passes when you don't specify an argument is known as the default intent. Chapel passes records, domains, and arrays by reference by default; but of these only arrays are modifiable inside the function. ( Records and domains pass by const ref - meaning they are passed by reference but that the function they are passed to cannot modify them. Arrays pass by ref or const ref depending upon what the function does with them - see array default intent ).
Now, to your question specifically, class instances pass by "value" by default, but Chapel considers the "value" of a class instance to be a pointer. That means that instead of allowing a field (say) to be mutated, passing a class instance by ref just means that it could be replaced with a different class instance. There isn't currently a way to say that a class instance's fields should not be modifiable in the function (other than making them to be explicitly immutable data types).
Given all of that, I don't see any inefficiencies with the code sample you provided in the question. In particular, here:
proc kickAss(b: PowerPuffGirl) {
b.secretIngredients.push_back("Chemical X");
return b;
}
the argument accepting b will receive a copy of the pointer to the instance and the return b will return a copy of that pointer. The contents of the instance (in particular the secretIngredients array) will remain stored where it was and won't be copied in the process.
One more thing:
This example works but it seems like bad form since I am "returning" the input.
As I said, this isn't really a problem for class instances or integers. What about an array?
proc identity(A) {
return A;
}
var A:[1..100] int;
writeln(identity(A));
In this example, the return A in identity() actually does cause a copy of the array to be made. That copy wasn't created when passing the array in to identity(), since the array was passed by with a const ref intent. But, since the function returns something "by value" that was a reference, it's necessary to copy it as part of returning. See also arrays return by value by default in the language evolution document.
In any case, if one wants to return an array by reference, it's possible to do so with the ref or const ref return intent, e.g.:
proc refIdentity(ref arg) ref {
return arg;
}
var B:[1..10] int;
writeln(refIdentity(B));
Now there is no copy of the array and everything is just referring to the same B.
Note though that it's currently possible to write programs that return a reference to a variable that no longer exists. The compiler includes some checking in that area but it's not complete. Hopefully improvements in that area are coming soon.

mongodb mongocxx C++11 - find_on(): error:

I wrote something like:
bsoncxx::document::value filter_document = document{}
<< "_id" << bsoncxx::oid { strJobID }
<< finalize;
auto retVal = collTasks.find_one(
filter_document, mongocxx::options::find {});
Result:
../src/CMongo.cpp:371:73:
error: cannot bind
‘bsoncxx::v_noabi::document::value’ lvalue to
‘bsoncxx::v_noabi::document::value&&’
auto retVal = collTasks.find_one(filter_document, MyFindOptions);
It's looking pretty much like the example. and the type of the argument is viwe_or_value...
There are owning and not owning type to represent the data.
value is the owning type
view the non owning type
If a owning type leves its scope {...} than it is destroying the underlying databuffer. A view generatrated from a value can't destroy the databuffer. But it depents on it. The view is referencing on the databuffer of the value. so if the view is valid, but value not, this leads somitimes into a SIGSEG a segmentation fault. Because you're trieng to acces data which are not existing anymore.
You don't want your fint_one() function to distroy your generated ID. This why this function accepts not a value.
Btw: Those && are moving-Operators. A Object is not copied, it is cut and paste. The source doesn't have this afterwards.
So try following, it should work ;).
docJobReturned = collTasks.find_one(
filter_document.view(), mongocxx::options::find {});

using macros to constrain lists

I am trying to constrain my list items to be equal to certain values under certain conditions.
For that I have devised a define as computed macro that
define <num_prob_constraints'struct_member> "CHECK_and_SET_CONSTRAINTS <lst'exp>" as computed {
//var cur : list of uint = <lst'exp>.as_a(list of uint);
var t : uint = <lst'exp>.as_a(list of uint).size();
print t;
for i from 1 to 4 {
result = append(result,"keep ",<lst'exp>,"[",i,"]==",i,"=> ",<lst'exp>,"[",i,"]==389; \n");
};
};
and in my code I use this macro like this:
struct schedule{
n : uint;
sched_w : list of list of int;
CHECK_and_SET_CONSTRAINTS sched_w;
};
But this does not work. First, it prints some random size (From the macro) instead of the list’s real size.
Secondly, I get errors of this sort:
*** Error: '1' is of type 'int', while expecting type 'list of int'.
in code generated by macro defined at line 3 in
sports_sched_macro.e
keep sched_w[1]==1=> sched_w[1]==389;
expanded at line 8 in sports_sched.e
CHECK_and_SET_CONSTRAINTS sched_w;
Any ideas on what is wrong here?
Macros are simply code substituts. Their function is simply to replace some string with another (calculated or not) during the parsing phase.
This means that the macro will be deployed where you used it in the parsing phase which precedes the generation phase. So, in fact, the list does not exist yet, and you cannot access it’s size and items.
More specifically, your macro is deployed this way:
struct schedule {
n : uint;
sched_w : list of list of int;
keep sched_w[1]==2=> sched_w[1]==389;
keep sched_w[2]==2=> sched_w[2]==389;
...
...
};
The error message you received tell you that you cannot access specific list items explicitly (since the list size and item’s values are yet undetermined).
If you want to keep your list with size 4, and if the value is 2 you want to replace it with 389, you may need to use the post_generate() method, as you are trying to access values that are already assigned to the list items:
keep sched_w.size()==4;
post_generate() is also{
for each in sched_w {
if (it==2) {it=389};
};
};
Are you sure you want to constrain a 2-dimensional list? This looks a bit different. E.g. for an array schedule[4][1]:
schedule: list of list of int;
keep schedule.size() == 4;
keep for each (sublist) in schedule {
sublist.size() == 1;
for each (elem) in sublist {
...
};
};