That sounds like a scenario that shouldn't need any modification of V8
itself, since JavaScript gives you a lot of control to override how
properties behave. Here's a (not very well-tested, and probably with bugs)
function to enable logging of a particular property:
function enableLogging(obj, name) {
var descriptor = Object.getOwnPropertyDescriptor(obj, name);
if (!descriptor) {
// Prop isn't on that object, maybe a prototype?
obj = Object.getPrototypeOf(obj);
if (!obj) {
throw new Error("can't find property");
}
return enableLogging(obj, name);
}
if (!descriptor.configurable) {
throw new Error("can't reconfigure property");
}
if (descriptor.get) {
var originalGet = descriptor.get;
descriptor.get = function () {
var result = originalGet.apply(this, arguments);
console.log("got " + name, result);
return result;
};
}
if (descriptor.set) {
var originalSet = descriptor.set;
descriptor.set = function (v) {
console.log("setting " + name, v);
return originalSet.apply(this, arguments);
};
}
if (descriptor.value) {
var value = descriptor.value;
delete descriptor.value;
descriptor.get = function () {
console.log("got " + name, value);
return value;
};
if (descriptor.writable) {
delete descriptor.writable;
descriptor.set = function (v) {
console.log("setting " + name, v);
value = v;
};
}
}
Object.defineProperty(obj, name, descriptor);
}
Then you just call it like enableLogging(document, "cookie") for whichever
properties are of interest, and it will print to the developer console. I
guess there might be a further step if you need this to automatically write
to a file or something; maybe then you'd need to replace console.log with
some custom function.
On Wednesday, April 15, 2020 at 10:17:57 AM UTC-7, [email protected] wrote:
>
> Hi Jakob,
>
> Thanks for your answer.
>
> Regarding logging properties:
> What if capturing a few targeted functions and properties only? I have a
> list of them (~30 built-in JS APIs or properties), including window.name,
> document.cookie, localStorage.setItem(), etc.
> Will this make life easier? If yes, should I still follow the same
> direction as you suggested?
>
> Regarding logging functions:
> I've been trying to use the following code (get from this link
> <https://stackoverflow.com/questions/25666539/v8-cant-get-calling-function-name-in-a-functioncallback>
>
> at StackOverflow
> <https://stackoverflow.com/questions/25666539/v8-cant-get-calling-function-name-in-a-functioncallback>
> ):
> global->Set(v8::String::NewFromUtf8(isolate, "eval"),
> v8::FunctionTemplate::New(isolate, *MY_LOGGING_FUNCTION*));
> Unfortunately, I didn't make this work. By any chance do you know the
> right place to put this code in order to trigger *MY_LOGGING_FUNCTION.*
>
> BTW, while searching before I post this topic, I notice you answered lots
> of questions in the group. Just want to say thanks again.
>
>
>
>
>
>
> On Wednesday, April 15, 2020 at 4:53:46 AM UTC-7, Jakob Kummerow wrote:
>>
>> The first part of this is easy: the existing --trace flag traces all
>> function calls. If printing to stdout is not what you need, you can at
>> least grep for FLAG_trace in the code to see where you'd have to add
>> your own code.
>>
>> The second part, logging all accessed properties, seems considerably
>> harder -- I'm not sure how to do it. As a start, you can turn off ICs, and
>> add code to the RUNTIME_FUNCTIONs you see in ic.cc. But that leaves a
>> bunch of implicit property loads inside built-in functions, and those don't
>> go through any common bottlenecks, so you'd have to find them all by hand.
>> It's going to be a lot of work.
>>
>> On Wed, Apr 15, 2020 at 9:20 AM <[email protected]> wrote:
>>
>>> Hello,
>>>
>>> For research reason, I need to log all the function names and properties
>>> accessed from javascript. Given the below example code from a webpage:
>>>
>>> <script>
>>> console.log('test");
>>> window.name="new name";
>>> </script>
>>>
>>> I'd like to capture that 'console.log()' and 'window.name' have been
>>> accessed.
>>>
>>>
>>> I'm quite new to v8 development. I've been struggling with the v8 source
>>> code in the chromium for a while.
>>> Could anyone save my life by giving me some hints or code snippets to
>>> get started?
>>>
>>>
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/v8-dev/cddfe2a9-91e1-4cb4-a060-c08cd7b4e1d3%40googlegroups.com.