Saturday, 17 December 2016

decompiling DEX into Java sourcecode, dex to jar

How can one decompile Android DEX (VM bytecode) files into corresponding Java sourcecode?

Thursday, 8 December 2016

Lightweight memory leak debugging on linux

I looked for existing answers first and saw that Valgrind is everyone’s favorite tool for memory leak debugging on linux. Unfortunately Valgrind does not seem to work for my purposes. I will try to explain why.
Constraints:
  • The leak reproduces only in customer’s environment. Due to certain legal restrictions we have to work with existing binary. No rebuilds.
  • In regular environment our application consumes ~10% CPU. Say, we can tolerate up to 10x CPU usage increase. Valgrind with default memcheck settings does much worse making our application unresponsive for long periods of time.
What I need is an equivalent of Microsoft’s UMDH: turn on stack tracing for each heap allocation, then at certain point of time dump all allocations grouped by stacks and ordered by allocation count in descending order. Our app ships on both Windows and Linux platforms, so I know that performance on Windows under UMDH is still tolerable.
Here are the tools/methods I considered
  • Valgrind's -memcheck and –massif tools They do much more than needed (like scanning whole process memory for every allocation pointer), they are too slow, and they still don’t do exactly what I
    need (dump callstacks sorted by counts), so I will have to write some scripts parsing the output
  • dmalloc library (dmalloc.com) requires new binary
  • LeakTracer (http://www.andreasen.org/LeakTracer/) Works only with C++ new/delete (I need malloc/free as well), does not have group-by-stack and sort functionality
  • Implementing the tool myself as .so library using LD_PRELOAD mechanism (Overriding 'malloc' using the LD_PRELOAD mechanism) That will take at least a week given my coding-for-Linux skills and it feels like inventing a bicycle.
Did I miss anything? Are there any lightweight Valgrind options or existing LD_PRELOAD tool?
--------------------------------------------------------------------------------------------------------------------------


Best Answer;



Surprisingly, I was unable to find anything like Microsoft's UMDH in open-source domain or available for immediate download. (I also looked at Google Heap Leak Checker but it is more like Valgrind rather than to UMDH). So I ended up writing the tool myself using malloc instrumentation project as a reference point:
The tool has number of limitations, but it worked just fine for my purposes.




Missing odd indexes from txt file - parsing whitespace-separated fields

Im trying to write a script which works as follows:
My input is a text file with 8 rows and 8 columns, filled with values 0 or 1, with a single space character each separating the columns.
I need to check the 4th number in each row, and output false, if it is 0, and true, if it is 1.
My code at the moment looks like this:
param($fname)
$rows = (Get-Content $fname)
for ($i=0;$i -lt $rows.Length;$i++)
{ 
 if ($rows[$i][6] -eq 1)
  {
   Write-Host "true"
  }
 if ($rows[$i][6] -ne 1)
  {
    Write-Host "false"
  }
}
So I use [$i][6], because I get that that's the 4th number, accounting for the number of spaces acting as separators.
I checked and thought it was perfect, but somehow it says false for every line, but when I Write-Host $rows[0][6] it is 1.
-------------------------------------------------------------------------------------------------------------------------

Best Answer;



tl;dr
# Create sample input file, with values of interest in 4th field
# (0-based field index 3).
@'
0 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 
'@ > file

foreach ($line in (Get-Content file)) {
    $fields = -split $line
    if ($fields[3] -eq '1') { 
        "true"
    } else {
        "false"
    }
}
yields:
false
true

There are many subtleties to consider in your original code, but the above code:
  • offers a more awk-like approach by splitting each input line into whitespace-separated fields, whatever the length of the fields, courtesy of the unary -split operator.
  • subscripts (indices) can then be based on field indices rather than character positions.
  • All fields returned by -split ... are strings, hence the comparison with string literal '1', but, generally, PowerShell performs a lot of behind-the-scenes conversion magic for you: with the code above - unlike with your own code - using 1 would have worked too.

As for why your approach failed:
  • Indexing into (using a subscript with) a string value in PowerShell is a special case: it implicitly treats the string as a character array, and, with a single index such as 6, returns a [char]instance.
  • It is the LHS (left-hand side) of an expression involving a binary operator such as -eq that determines what type the RHS (right-hand side) will be coerced to, if necessary, before applying the operator:
    • ([char] '1') -eq 1 # !! $false
      • Coercing the (implied) [int] type of RHS 1 to the LHS type [char] yields Unicode codepoint U+0001, i.e., a control character rather than the "ASCII" digit '1', which is why the comparison fails.
      • @PetSerAl's helpful, but cryptic suggestion (in a comment on the question) to use '1'[0] rather than 1 as the RHS solves the problem in this particular case, because '1'[0] returns 1 as a [char] instance, but the solution doesn't generalize to multi-character field values.
    • '1' -eq 1 # $true; same as: ([string] 1) -eq 1 or ([string] 1) -eq '1'
      • Converting integer 1 to a string indeed is the same as '1'.








What is the best way to parse large XML (size of 1GB) in C#?

I have a 1GB XML file and want to parse it. If I use XML Textreader or XMLDocument, the result is very slow and some times it hangs...

-------------------------------------------------------------------------------------------------------------------------

Answer

I would just like to back up everyone who promotes XmlReader with a performance comparison that I found:

Wednesday, 7 December 2016

Sign up How to pass argument upstairs in Angular2

I have to pass an argument to a function.
<select class="chooseWatchlist" (change)="updateWatchlistTable(item.name)">
   <option *ngFor="let item of _items">{{ item.name }}</option>
</select>
It's really important for me, because I have to pass that item.name argument to a http request. But unfortunately angular doesn't see that variable, because it's kinda downstairs... The function has to take item.name of actual chosen option in the select box. The function will be executed with every value change in that box.
Is there any solution to fix it? Or maybe there is another way to solve it?
Finally, the http request:
private updateWatchlistTable(name) {
  this.http.get('http://localhost:8080/api/points/getValue/' + name)
   .subscribe(res => this._watchlistElements = res.json());
  console.log(name);
}
console.log(name) returns undefined.
As you can see I need that argument item.name to make this http request work. Any help appreciated. :)
--------------------------------------------------------------------------------------------------------------------------

Best Answer;




With ngValue you can bind a value and
with (ngModelChange)="$event.name" you have access to the name.
With [(ngModel)]="selectedItem" you can get and set which item is selected.
<select class="chooseWatchlist" [(ngModel)]="selectedItem" (ngModelChange)="updateWatchlistTable($event.name)">
   <option *ngFor="let item of _items" [ngValue]="item">{{ item.name }}</option>
</select>

How to work with Generic type members in extension method






I started this extension method for fun, but now I find myself stuck into it,
I started this extension method for fun, but now I find myself stuck into it,
Actually I want to create an extension method to extend LINQ, the method will look like this:
books.In(b => b.Id, 1,2,3,4,5);
It will return books with Id range in the second params[] ,
It also could be done like this:
books.In(b => b.Title, "t1","t2","t3","t4","t5");
What I come up with is this method (I just knew about expression tree today!):
 public static List<TEntity> In<TEntity, TMember>(this List<TEntity> list,
            Expression<Func<TEntity, TMember>> identifier, params TMember[] keys)
        {
            Type t = typeof(TEntity); //How to use reflection here?

            List<TEntity> myList = list.Where(m => keys.Contains(m.));
            return myList;
        }
my problem is how to access the generic class members: i.e. m.Id or m.Title from the identifier expression? Can I use reflection to make this happen?


--------------------------------------------------------------------------------------------------------------------------

Best Answer;



This should do:
public static IEnumerable<TEnitity> In<TEnitity, TMember>(this IEnumerable<TEnitity> source, Func<TEnitity, TMember> projector, IEnumerable<TMember> validCases) =>
        source.Where(s => validCases.Contains(projector(s)));
Or, to comply exaclty with the signature you are proposing:
public static IEnumerable<TEntity> In<TEntity, TMember>(this IEnumerable<TEntity> source, Func<TEntity, TMember> projector, params TMember[] validCases) =>
        source.Where(s => validCases.Contains(projector(s)))
On why I'm returning IEnumerable<T> instead of List<T> the reasoning is cheap generalization; if the consumer wants a list returned, then he simply needs to call ToList, but why make him pay the expense of enumerating greedily if maybe he only wants to iterate once through the enumeration? Remeber, LINQ is lazy, leverage that and make it work as little as possible.
On why I'm accepting IEnumerable<T> as an argument, again cheap generalization; why accept only a list? Why not an array, a queue, a stack, a collection, an iterator block, a hashset, etc?